From f617213b4a48d73acd245580f8551a7c37ce9ad8 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 10 Feb 2019 03:55:05 -0600 Subject: added vector math and tractorbeam module --- src/modules/engines.rs | 52 +++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) (limited to 'src/modules/engines.rs') diff --git a/src/modules/engines.rs b/src/modules/engines.rs index a930847..f319ee5 100644 --- a/src/modules/engines.rs +++ b/src/modules/engines.rs @@ -1,64 +1,48 @@ use crate::mass::Mass; +use crate::math::Vector; #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Engines { - acceleration: (f64, f64, f64), + acceleration: Vector, } impl Engines { pub fn new() -> Engines { Engines { - acceleration: (0.0, 0.0, 0.0), + acceleration: Vector::default(), } } - pub fn recv_acceleration(&mut self) -> (f64, f64, f64) { - let acceleration = self.acceleration; - self.acceleration = (0.0, 0.0, 0.0); + pub fn recv_acceleration(&mut self) -> Vector { + let acceleration = self.acceleration.clone(); + self.acceleration = Vector::default(); acceleration } pub fn give_client_data(&mut self, ship: &Mass, target: Option<&Mass>, data: String) { - let mut acceleration = (0.0, 0.0, 0.0); + let mut acceleration = Vector::default(); match data.as_bytes() { - b"5\n" => acceleration.0 += 0.1, - b"0\n" => acceleration.0 -= 0.1, - b"8\n" => acceleration.1 += 0.1, - b"2\n" => acceleration.1 -= 0.1, - b"4\n" => acceleration.2 += 0.1, - b"6\n" => acceleration.2 -= 0.1, - b"+\n" => { - let m_v = ship.velocity; - acceleration = (m_v.0 * 0.05, m_v.1 * 0.05, m_v.2 * 0.05); - } + 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 = ship.velocity.clone() * 0.05, b"-\n" => { - let m_v = ship.velocity; - acceleration = ( - -1.0 * m_v.0 * 0.05, - -1.0 * m_v.1 * 0.05, - -1.0 * m_v.2 * 0.05, - ); + acceleration = ship.velocity.clone() * -1.05; } b"s\n" => { - let m_v = ship.velocity; - acceleration = (-1.0 * m_v.0, -1.0 * m_v.1, -1.0 * m_v.2); + acceleration = ship.velocity.clone() * -1.0; } b"c\n" => { if let Some(target) = target { - let d_v = target.velocity; - let m_v = ship.velocity; - acceleration = (d_v.0 - m_v.0, d_v.1 - m_v.1, d_v.2 - m_v.2); + acceleration = target.velocity.clone() - ship.velocity.clone(); } } b"t\n" => { if let Some(target) = target { - let d_p = target.position; - let m_p = ship.position; - acceleration = ( - (d_p.0 - m_p.0) * 0.01, - (d_p.1 - m_p.1) * 0.01, - (d_p.2 - m_p.2) * 0.01, - ); + acceleration = (target.position.clone() - ship.position.clone()) * 0.01; } } _ => (), -- cgit v1.2.3