From 923e49d29ce2f51df608011db192cbb73e8f7345 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 23 Apr 2018 06:25:55 -0500 Subject: -split up module.rs --- src/modules/engines.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/modules/engines.rs (limited to 'src/modules/engines.rs') diff --git a/src/modules/engines.rs b/src/modules/engines.rs new file mode 100644 index 0000000..10e4c85 --- /dev/null +++ b/src/modules/engines.rs @@ -0,0 +1,70 @@ +use mass::Mass; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Engines { + acceleration : (f64, f64, f64), +} + +impl Engines { + pub fn new() -> Engines { + Engines { + acceleration : (0.0, 0.0, 0.0) + } + } + + pub fn recv_acceleration(&mut self) -> (f64, f64, f64) { + let acceleration = self.acceleration; + self.acceleration = (0.0, 0.0, 0.0); + acceleration + } + + pub fn give_client_data(&mut self, ship : &Mass, target : Option<&Mass>, data : String) { + let mut acceleration = (0.0, 0.0, 0.0); + 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"-\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); + }, + b"c\n" => { + match target { + Some(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); + }, + None => (), + } + }, + b"t\n" => { + match target { + Some(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); + }, + None => (), + } + }, + _ => (), + } + self.acceleration = acceleration; + } +} -- cgit v1.2.3