diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-27 07:54:45 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-27 07:54:45 -0600 |
commit | bd39a5f5bf71e10a73dafb98c5f8ec88aa3bc7fa (patch) | |
tree | b358f70323884f528bc294a8b9034f199b39c9c4 | |
parent | b5896e2d7597e42818a47710da22098d178bf8f6 (diff) |
-added ability for ships to slow their velocity vector with one keypress
-rw-r--r-- | src/astroid.rs | 4 | ||||
-rw-r--r-- | src/connection.rs | 13 | ||||
-rw-r--r-- | src/mass.rs | 1 | ||||
-rw-r--r-- | src/ship.rs | 23 |
4 files changed, 35 insertions, 6 deletions
diff --git a/src/astroid.rs b/src/astroid.rs index fb14751..28009ca 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -44,6 +44,10 @@ impl Mass for Astroid { serde_json::to_string(self).unwrap() } + fn slow(&mut self) { + + } + fn process(&mut self) { self.position.0 += self.velocity.0; self.position.1 += self.velocity.1; diff --git a/src/connection.rs b/src/connection.rs index bcd4a30..8cf2a15 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -68,12 +68,13 @@ impl Connection { let mut data = String::new(); match self.buff_r.read_line(&mut data) { Ok(result) => match data.as_bytes() { - b"5\n" => acceleration.0 += 1.0, - b"0\n" => acceleration.0 -= 1.0, - b"8\n" => acceleration.1 += 1.0, - b"2\n" => acceleration.1 -= 1.0, - b"4\n" => acceleration.2 += 1.0, - b"6\n" => acceleration.2 -= 1.0, + 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" => masses[self.index].slow(), _ => { if result == 0 { self.open = false; diff --git a/src/mass.rs b/src/mass.rs index d91b292..30442ec 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -5,6 +5,7 @@ pub trait Mass : Any { fn position(&self) -> (f64, f64, f64); fn serialize(&self) -> String; fn process(&mut self); + fn slow(&mut self); fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); } diff --git a/src/ship.rs b/src/ship.rs index 30c68d9..0ea97e0 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -39,6 +39,29 @@ impl Mass for Ship { serde_json::to_string(self).unwrap() } + fn slow(&mut self) { + if self.velocity.0 > 0.01 { + self.velocity.0 += -1.0 * self.velocity.0 * 0.1; + } + else { + self.velocity.0 = 0.0; + } + + if self.velocity.1 > 0.01 { + self.velocity.1 += -1.0 * self.velocity.1 * 0.1; + } + else { + self.velocity.1 = 0.0; + } + + if self.velocity.2 > 0.01 { + self.velocity.2 += -1.0 * self.velocity.2 * 0.1; + } + else { + self.velocity.2 = 0.0; + } + } + fn process(&mut self) { self.position.0 += self.velocity.0; self.position.1 += self.velocity.1; |