diff options
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/engines.rs | 49 | ||||
-rw-r--r-- | src/modules/navigation.rs | 8 |
2 files changed, 41 insertions, 16 deletions
diff --git a/src/modules/engines.rs b/src/modules/engines.rs index 55b9af9..fa16bf9 100644 --- a/src/modules/engines.rs +++ b/src/modules/engines.rs @@ -1,22 +1,45 @@ +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, + target_velocity: Option<Vector>, + pub fuel: f64, } impl Engines { pub fn new() -> Engines { Engines { acceleration: Vector::default(), + target_velocity: None, + fuel: constants::SHIP_ENGINES_FUEL_START, } } pub fn recv_acceleration(&mut self) -> Vector { let acceleration = self.acceleration.clone(); self.acceleration = Vector::default(); - acceleration + + if self.fuel - acceleration.magnitude() >= 0.0 { + self.fuel -= acceleration.magnitude(); + acceleration + } else { + Vector::default() + } } pub fn give_client_data( @@ -27,26 +50,26 @@ impl Engines { data: String, ) { let mut acceleration = Vector::default(); - match data.as_bytes() { - b"5\n" => acceleration.a += 0.1, - b"0\n" => acceleration.a -= 0.1, - b"8\n" => acceleration.b += 0.1, - b"2\n" => acceleration.b -= 0.1, - b"4\n" => acceleration.c += 0.1, - b"6\n" => acceleration.c -= 0.1, - b"+\n" => acceleration = velocity * 0.05, - b"-\n" => { + match data.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; } - b"s\n" => { + "s" => { acceleration = velocity * -1.0; } - b"c\n" => { + "c" => { if let Some(target) = target { acceleration = target.velocity.clone() - velocity; } } - b"t\n" => { + "t" => { if let Some(target) = target { acceleration = (target.position.clone() - position) * 0.01; } diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs index 0e855f8..0e010cb 100644 --- a/src/modules/navigation.rs +++ b/src/modules/navigation.rs @@ -48,9 +48,11 @@ impl Navigation { } pub fn give_target(&mut self, target_name: String) { - self.start = Some(SystemTime::now()); - self.status = NavigationStatus::Targeting; - self.target_name = Some(target_name); + if !target_name.is_empty() { + self.start = Some(SystemTime::now()); + self.status = NavigationStatus::Targeting; + self.target_name = Some(target_name); + } } pub fn verify_target(&mut self, ship_position: Vector, masses: &HashMap<String, Mass>) { |