From b5896e2d7597e42818a47710da22098d178bf8f6 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 27 Feb 2018 06:59:58 -0600 Subject: -now use velocity and position vector, astroids are now randomly populated --- src/astroid.rs | 47 +++++++++++++++++++++++++++++++++++------------ src/bin/server.rs | 8 +++++++- src/connection.rs | 18 +++++++++--------- src/mass.rs | 6 +++--- src/navigation.rs | 13 ++++++++----- src/ship.rs | 40 +++++++++++++++++++++++++--------------- 6 files changed, 87 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/astroid.rs b/src/astroid.rs index 6aa1807..fb14751 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -1,35 +1,58 @@ use mass::{Mass, Type}; +extern crate rand; +use self::rand::distributions::Range; +use astroid::rand::distributions::Sample; extern crate serde_json; +use astroid::rand::Rng; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, t : Type, - location : (f64, f64, f64), + position : (f64, f64, f64), + velocity : (f64, f64, f64), } -impl Mass for Astroid { - fn new(name : &str, location : (f64, f64, f64)) -> Astroid { +impl Astroid { + pub fn new() -> Astroid { + let name : String = rand::thread_rng() + .gen_ascii_chars() + .take(8) + .collect(); + let mut r = Range::new(-50.0, 50.0); + let mut rng = rand::thread_rng(); + let position = (r.sample(&mut rng), r.sample(&mut rng), r.sample(&mut rng)); Astroid { - name : String::from(name), - t : Type::Astroid, - location : location, + name : name, + t : Type::Astroid, + position : position, + velocity : (0.0, 0.0, 0.0), } } +} +impl Mass for Astroid { fn name(&self) -> &String { &self.name } - fn location(&self) -> (f64, f64, f64) { - self.location - } - - fn set_location(&mut self, location : (f64, f64, f64)) { - self.location = location; + fn position(&self) -> (f64, f64, f64) { + self.position } fn serialize(&self) ->String { serde_json::to_string(self).unwrap() } + + fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + } + + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { + self.velocity.0 += acceleration.0; + self.velocity.1 += acceleration.1; + self.velocity.2 += acceleration.2; + } } diff --git a/src/bin/server.rs b/src/bin/server.rs index c327257..36840c1 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -11,7 +11,9 @@ use space::connection::Connection; fn populate() -> Vec> { let mut masses : Vec> = Vec::new(); - masses.push(Box::new(Astroid::new("cZfAJ", (10.0, -5.0, 4.0)))); + masses.push(Box::new(Astroid::new())); + masses.push(Box::new(Astroid::new())); + masses.push(Box::new(Astroid::new())); masses } @@ -32,6 +34,10 @@ fn main() { } connections.retain(|connection| connection.open); + for mass in masses.iter_mut() { + mass.process(); + } + sleep(Duration::from_millis(100)); } } diff --git a/src/connection.rs b/src/connection.rs index db258e4..bcd4a30 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -64,16 +64,16 @@ impl Connection { } } Module::Engines => { - let mut location = masses[self.index].location(); + let mut acceleration = (0.0, 0.0, 0.0); let mut data = String::new(); match self.buff_r.read_line(&mut data) { Ok(result) => match data.as_bytes() { - b"5\n" => location.0 += 1.0, - b"0\n" => location.0 -= 1.0, - b"8\n" => location.1 += 1.0, - b"2\n" => location.1 -= 1.0, - b"4\n" => location.2 += 1.0, - b"6\n" => location.2 -= 1.0, + 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, _ => { if result == 0 { self.open = false; @@ -82,13 +82,13 @@ impl Connection { }, Err(_error) => (), } - masses[self.index].set_location(location); + masses[self.index].give_acceleration(acceleration); } Module::Navigation => { let ship = &masses[self.index].downcast_ref::().unwrap(); let within_range : Vec<&Box> = masses.iter().filter(|mass| - distance(ship.location(), mass.location()) < ship.range()).collect(); + distance(ship.position(), mass.position()) < ship.range()).collect(); let mut send = String::new(); for mass in within_range { diff --git a/src/mass.rs b/src/mass.rs index 71723cd..d91b292 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -1,11 +1,11 @@ use downcast::Any; pub trait Mass : Any { - fn new(name : &str, location : (f64, f64, f64)) -> Self where Self: Sized; fn name(&self) -> &String; - fn location(&self) -> (f64, f64, f64); - fn set_location(&mut self, location : (f64, f64, f64)); + fn position(&self) -> (f64, f64, f64); fn serialize(&self) -> String; + fn process(&mut self); + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); } #[derive(Serialize, Deserialize, Debug)] diff --git a/src/navigation.rs b/src/navigation.rs index cc2221a..4683c0c 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -16,7 +16,7 @@ use ship::Ship; use math::distance; use astroid::Astroid; -pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader){ +pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader){ let stdout = stdout(); let mut stdout = stdout.lock().into_raw_mode().unwrap(); let mut stdin = async_stdin().bytes(); @@ -55,14 +55,16 @@ pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader f64 { - self.r - } -} - -impl Mass for Ship { - fn new(name : &str, location : (f64, f64, f64)) -> Ship { + pub fn new(name : &str, position : (f64, f64, f64)) -> Ship { Ship { name : String::from(name), - location : location, + position : position, + velocity : (0.0, 0.0, 0.0), t : Type::Ship, r : 100.0, } } - fn name(&self) -> &String { - &self.name + pub fn range(&self) -> f64 { + self.r } +} - fn location(&self) -> (f64, f64, f64) { - self.location +impl Mass for Ship { + fn name(&self) -> &String { + &self.name } - fn set_location(&mut self, location : (f64, f64, f64)) { - self.location = location; + fn position(&self) -> (f64, f64, f64) { + self.position } fn serialize(&self) -> String { serde_json::to_string(self).unwrap() } + + fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + } + + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { + self.velocity.0 += acceleration.0; + self.velocity.1 += acceleration.1; + self.velocity.2 += acceleration.2; + } } -- cgit v1.2.3