Skip to content

Commit

Permalink
Adjust Pulse and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur committed Oct 16, 2024
1 parent 9ed97d2 commit 2bc7977
Showing 1 changed file with 62 additions and 1 deletion.
63 changes: 62 additions & 1 deletion src/input_context/input_condition/pulse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl InputCondition for Pulse {
};

// If the repeat count limit has not been reached.
if self.held_timer.duration() > self.interval * trigger_count as f32 {
if self.held_timer.duration() >= self.interval * trigger_count as f32 {
// Trigger when held duration exceeds the interval threshold.
self.trigger_count += 1;
ActionState::Fired
Expand All @@ -106,3 +106,64 @@ impl InputCondition for Pulse {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn tap() {
let world = World::new();
let actions_data = ActionsData::default();

let mut condition = Pulse::new(1.0);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 1.0.into()),
ActionState::Fired,
);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.5, 1.0.into()),
ActionState::Ongoing,
);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.5, 1.0.into()),
ActionState::Fired,
);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 1.0.into()),
ActionState::Ongoing,
);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 0.0.into()),
ActionState::None,
);
}

#[test]
fn not_trigger_on_start() {
let world = World::new();
let actions_data = ActionsData::default();

let mut condition = Pulse::new(1.0).trigger_on_start(false);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 1.0.into()),
ActionState::Ongoing,
);
}

#[test]
fn trigger_limit() {
let world = World::new();
let actions_data = ActionsData::default();

let mut condition = Pulse::new(1.0).with_trigger_limit(1);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 1.0.into()),
ActionState::Fired,
);
assert_eq!(
condition.evaluate(&world, &actions_data, 0.0, 1.0.into()),
ActionState::None,
);
}
}

0 comments on commit 2bc7977

Please sign in to comment.