From 892088d723fd3dc0aae969273331c2765f322e6f Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 17 Feb 2019 07:53:57 -0600 Subject: gave engines a max acceleration --- src/constants.rs | 1 + src/mass.rs | 1 + src/math.rs | 2 +- src/modules/engines.rs | 53 ++++++++++++++++++++++++-------------------------- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 85b0638..3259f2f 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -14,6 +14,7 @@ pub const SHIP_NAVIGATION_RANGE: f64 = 100.0; pub const SHIP_REFINERY_TIME: u64 = 5; pub const SHIP_TRACTORBEAM_STRENGTH: f64 = 0.1; pub const SHIP_ENGINES_FUEL_START: f64 = 100.0; +pub const SHIP_ENGINES_ACCELERATION: f64 = 0.1; pub const IRON_SIZE: usize = 1; pub const HYDROGEN_SIZE: usize = 1; diff --git a/src/mass.rs b/src/mass.rs index 33144ab..c556e0c 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -174,6 +174,7 @@ impl Mass { refinery.process(); navigation.process(); construction.process(); + engines.process(self.velocity.clone()); self.effects.give_acceleration(engines.take_acceleration()) } diff --git a/src/math.rs b/src/math.rs index 99839df..9a9f7b8 100644 --- a/src/math.rs +++ b/src/math.rs @@ -11,7 +11,7 @@ pub fn rand_name() -> String { .collect() } -#[derive(Serialize, Deserialize, Debug, Clone, Default)] +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)] pub struct Vector { pub x: f64, pub y: f64, diff --git a/src/modules/engines.rs b/src/modules/engines.rs index ab6335a..007787c 100644 --- a/src/modules/engines.rs +++ b/src/modules/engines.rs @@ -2,18 +2,6 @@ use crate::constants; use crate::mass::Mass; use crate::math::Vector; -#[derive(Serialize, Deserialize, Debug, Clone)] -pub enum EnginesStatus { - None, - ApproachingTargetVelocity, -} - -impl Default for EnginesStatus { - fn default() -> Self { - EnginesStatus::None - } -} - #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Engines { acceleration: Vector, @@ -30,6 +18,15 @@ impl Engines { } } + pub fn process(&mut self, velocity: Vector) { + if let Some(target_velocity) = self.target_velocity.clone() { + self.acceleration += target_velocity - velocity; + if self.acceleration == Vector::default() { + self.target_velocity = None + } + } + } + pub fn give_recv( &mut self, recv: String, @@ -39,27 +36,24 @@ impl Engines { ) { let mut acceleration = Vector::default(); match recv.as_str() { - "5" => acceleration.x += 0.1, - "0" => acceleration.x -= 0.1, - "8" => acceleration.y += 0.1, - "2" => acceleration.y -= 0.1, - "4" => acceleration.z += 0.1, - "6" => acceleration.z -= 0.1, - "+" => acceleration = velocity * 0.05, - "-" => { - acceleration = velocity * -1.05; - } - "s" => { - acceleration = velocity * -1.0; - } + "5" => acceleration.x += constants::SHIP_ENGINES_ACCELERATION, + "0" => acceleration.x -= constants::SHIP_ENGINES_ACCELERATION, + "8" => acceleration.y += constants::SHIP_ENGINES_ACCELERATION, + "2" => acceleration.y -= constants::SHIP_ENGINES_ACCELERATION, + "4" => acceleration.z += constants::SHIP_ENGINES_ACCELERATION, + "6" => acceleration.z -= constants::SHIP_ENGINES_ACCELERATION, + "+" => acceleration = velocity.unitize() * constants::SHIP_ENGINES_ACCELERATION, + "-" => acceleration = velocity.unitize() * -1.0 * constants::SHIP_ENGINES_ACCELERATION, + "s" => self.target_velocity = Some(Vector::default()), "c" => { if let Some(target) = target { - acceleration = target.velocity.clone() - velocity; + self.target_velocity = Some(target.velocity.clone()); } } "t" => { if let Some(target) = target { - acceleration = (target.position.clone() - position) * 0.01; + acceleration = (target.position.clone() - position).unitize() + * constants::SHIP_ENGINES_ACCELERATION; } } _ => (), @@ -68,9 +62,12 @@ impl Engines { } pub fn take_acceleration(&mut self) -> Vector { - let acceleration = self.acceleration.clone(); + let mut acceleration = self.acceleration.clone(); self.acceleration = Vector::default(); + if acceleration.magnitude() >= constants::SHIP_ENGINES_ACCELERATION { + acceleration = acceleration.unitize() * constants::SHIP_ENGINES_ACCELERATION; + } if self.fuel - acceleration.magnitude() >= 0.0 { self.fuel -= acceleration.magnitude(); acceleration -- cgit v1.2.3