summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-03-02 11:01:02 -0600
committertom barrett <spalf0@gmail.com>2019-03-02 11:01:02 -0600
commit0973ac1666a6ee3b606a537742abe506719fd156 (patch)
tree2611d628601e9bf69f8ab478bb209791c58a4a10
parentce9adc4cc1b4b30322dc30ee85c42ee1451ffbf9 (diff)
engines tests
-rw-r--r--src/modules/engines.rs13
-rw-r--r--tests/tests.rs42
2 files changed, 48 insertions, 7 deletions
diff --git a/src/modules/engines.rs b/src/modules/engines.rs
index ebf4e8b..9aa8297 100644
--- a/src/modules/engines.rs
+++ b/src/modules/engines.rs
@@ -40,14 +40,13 @@ impl Engines {
self.status = Status::None;
}
}
- match self.target_velocity.clone() {
- Some(target_velocity) => {
- self.acceleration += target_velocity - velocity;
- if self.acceleration == Vector::default() {
- self.target_velocity = None;
- }
+
+ if let Some(target_velocity) = self.target_velocity.clone() {
+ self.acceleration += target_velocity - velocity;
+ if self.acceleration == Vector::default() {
+ self.target_velocity = None;
+ self.status = Status::None;
}
- None => (),
}
}
diff --git a/tests/tests.rs b/tests/tests.rs
index 141cc41..ddbbaf7 100644
--- a/tests/tests.rs
+++ b/tests/tests.rs
@@ -173,4 +173,46 @@ mod test {
assert!(masses.len() == 2);
assert!(ship.item_count(ItemType::Iron) == 0);
}
+
+ #[test]
+ fn test_engines() {
+ let (mut ship, mut masses) = setup();
+ setup_ship_target(&mut ship, &mut masses);
+
+ let mut astroid = masses.remove("astroid").unwrap();
+ astroid.velocity = Vector::new((constants::SHIP_ENGINES_ACCELERATION * 2.0, 0.0, 0.0));
+ astroid.process(&mut masses);
+ masses.insert(String::from("astroid"), astroid);
+
+ ship.give_received_data(ModuleType::Engines, String::from("c"));
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION);
+
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION * 2.0);
+
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION * 2.0);
+
+ ship.give_received_data(ModuleType::Engines, String::from("s"));
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION);
+
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == 0.0);
+
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == 0.0);
+
+ ship.give_received_data(ModuleType::Engines, String::from("t"));
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION * -1.0);
+
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION * -1.0);
+
+ ship.give_received_data(ModuleType::Engines, String::from("t"));
+ ship.process(&mut masses);
+ assert!(ship.velocity.x == constants::SHIP_ENGINES_ACCELERATION * -2.0);
+ }
}