diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/astroid.rs | 6 | ||||
-rw-r--r-- | src/connection.rs | 17 | ||||
-rw-r--r-- | src/navigation.rs | 15 | ||||
-rw-r--r-- | src/ship.rs | 6 |
4 files changed, 35 insertions, 9 deletions
diff --git a/src/astroid.rs b/src/astroid.rs index 077c5bf..ab00e6e 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -8,7 +8,7 @@ use astroid::rand::Rng; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, - t : Type, + mass_type : Type, position : (f64, f64, f64), velocity : (f64, f64, f64), } @@ -20,13 +20,13 @@ impl Astroid { .take(8) .collect(); let mut pr = Range::new(-50.0, 50.0); - let mut vr = Range::new(-5.0, 5.0); + let mut vr = Range::new(-1.0, 1.0); let mut rng = rand::thread_rng(); let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng)); let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng)); Astroid { name : name, - t : Type::Astroid, + mass_type : Type::Astroid, position : position, velocity : velocity, } diff --git a/src/connection.rs b/src/connection.rs index 8cf2a15..eacbb10 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -3,9 +3,13 @@ use std::io::BufReader; use std::net::TcpStream; extern crate serde_json; +extern crate erased_serde; + +use erased_serde::Deserializer; use ship::Ship; use mass::Mass; +use astroid::Astroid; use module::{Module, from_primitive}; use math::distance; @@ -101,6 +105,19 @@ impl Connection { Ok(_result) => (), Err(_error) => self.open = false, } + + let mut string_mass = String::new(); + self.buff_r.read_line(&mut string_mass).unwrap(); + if string_mass.len() > 0 { + let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); + let mut deserialized : Box<Deserializer> = Box::new(Deserializer::erase(json)); + if string_mass.contains("Ship") { + let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); + } + else { + let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); + } + } } } } diff --git a/src/navigation.rs b/src/navigation.rs index 4683c0c..978c154 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -50,7 +50,6 @@ pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader< } } - write!(stdout, "{}{}Targets:", termion::clear::All, termion::cursor::Goto(1,1)).unwrap(); @@ -72,12 +71,20 @@ pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader< let c = c.unwrap(); let mut send = String::new(); send.push(c as char); - println!("{}", send); if send.as_bytes() == b"q" { break; } - //send.push_str("\n"); - //stream.write(send.as_bytes()).unwrap(); + else { + let i = match send.parse::<usize>() { + Ok(num) => num, + Err(_err) => 100, + }; + if i < masses.len() { + send = masses[i].serialize(); + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); + } + } } None => () } diff --git a/src/ship.rs b/src/ship.rs index 0ea97e0..16a3d01 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -6,8 +6,9 @@ pub struct Ship { name : String, position : (f64, f64, f64), velocity : (f64, f64, f64), - t : Type, + mass_type : Type, r : f64, + target : Option<usize>, } impl Ship { @@ -16,8 +17,9 @@ impl Ship { name : String::from(name), position : position, velocity : (0.0, 0.0, 0.0), - t : Type::Ship, + mass_type : Type::Ship, r : 100.0, + target : None, } } |