From 22ec8b31c66475f29106f2fda46969e427f2e704 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 5 Apr 2018 02:04:11 -0500 Subject: -changed how data is passed (learned you can serialize a vec) -added targeting struct to ship and got timer prototype --- src/astroid.rs | 2 +- src/bin/client.rs | 13 +++++------ src/connection.rs | 2 +- src/lib.rs | 1 + src/ship.rs | 68 +++++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 57 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/astroid.rs b/src/astroid.rs index ffdeabf..5b2a3b3 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -44,7 +44,7 @@ impl Mass for Astroid { self.position } - fn serialize(&self) ->String { + fn serialize(&self) -> String { serde_json::to_string(self).unwrap() } diff --git a/src/bin/client.rs b/src/bin/client.rs index cf12e53..bb99485 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -30,21 +30,20 @@ fn main() { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); - let mut modules : Vec<&str> = data.split(";").collect(); - modules.pop(); + let modules : Vec = serde_json::from_str(&data.replace("\n","")).unwrap(); println!("Choose your module:"); for (i, module) in modules.iter().enumerate() { - println!("{}) {}", i, module.replace("\n", "")); + println!("{}) {:?}", i, module); } let mut choice = String::new(); io::stdin().read_line(&mut choice).expect("Failed"); - let mut module = modules[choice.replace("\n","").parse::().unwrap()].to_owned(); - module.push_str("\n"); - stream.write(module.as_bytes()).unwrap(); + let module = modules[choice.replace("\n", "").parse::().unwrap()].clone(); + + let send = serde_json::to_string(&module).unwrap() + "\n"; + stream.write(send.as_bytes()).unwrap(); - let module : Module = serde_json::from_str(&module.replace("\n","")).unwrap(); match module { Module::Dashboard => client_dashboard(buff_r), Module::Engines => client_engines(stream, buff_r), diff --git a/src/connection.rs b/src/connection.rs index a71bced..f0b3c25 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -28,7 +28,7 @@ impl Connection { let index = match result { Some(index) => index, None => { - let ship = Box::new(Ship::new(name, (0.0,0.0,0.0))); + let ship = Box::new(Ship::new(name, (0.0, 0.0, 0.0))); masses.push(ship); masses.len() - 1 }, diff --git a/src/lib.rs b/src/lib.rs index b7a3305..fee73fa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ extern crate serde_derive; extern crate downcast; extern crate termion; +extern crate time; pub mod mass; pub mod ship; diff --git a/src/ship.rs b/src/ship.rs index e226936..f56492e 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,7 +1,24 @@ +use std::time::{Duration, Instant, SystemTime}; + use module::Module; use mass::{Mass, Type}; extern crate serde_json; +#[derive(Serialize, Deserialize, Debug, Clone)] +enum TargetingStatus { + None, + Targeting, + Targeted, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +struct Targeting { + target : Option, + status : TargetingStatus, + time : u64, + start : Option, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Ship { name : String, @@ -9,8 +26,8 @@ pub struct Ship { velocity : (f64, f64, f64), mass_type : Type, r : f64, - target : Option, modules : Vec, + targeting : Targeting, } impl Ship { @@ -22,13 +39,18 @@ impl Ship { modules.push(Module::Dashboard); Ship { - name : String::from(name), - position : position, - velocity : (0.0, 0.0, 0.0), - mass_type : Type::Ship, - r : 100.0, - target : None, - modules : modules, + name : String::from(name), + position : position, + velocity : (0.0, 0.0, 0.0), + mass_type : Type::Ship, + r : 100.0, + targeting : Targeting { + target : None, + status : TargetingStatus::None, + time : 3, + start : None, + }, + modules : modules, } } @@ -59,7 +81,6 @@ impl Ship { self.velocity.0 *= 1.05; self.velocity.1 *= 1.05; self.velocity.2 *= 1.05; - } pub fn range(&self) -> f64 { @@ -67,23 +88,20 @@ impl Ship { } pub fn give_target(&mut self, target : Option) { - self.target = target; + self.targeting.target = target; + self.targeting.status = TargetingStatus::Targeted; + //self.targeting.start = Some(SystemTime::now()); } pub fn recv_target(&self) -> Option { - self.target + match self.targeting.status { + TargetingStatus::Targeted => self.targeting.target, + _ => None + } } pub fn get_modules(&self) -> String { - let mut data = String::new(); - - for module in self.modules.iter() { - data.push_str(&serde_json::to_string(&module).unwrap()); - data.push_str(";"); - } - data.push_str("\n"); - - data + serde_json::to_string(&self.modules).unwrap() + "\n" } } @@ -96,6 +114,16 @@ impl Mass for Ship { self.position.0 += self.velocity.0; self.position.1 += self.velocity.1; self.position.2 += self.velocity.2; + /* + match self.targeting.start { + Some(time) => { + if time.elapsed().unwrap().as_secs() > self.targeting.time { + self.targeting.status = TargetingStatus::Targeted; + } + } + None => (), + } + */ } fn position(&self) -> (f64, f64, f64) { -- cgit v1.2.3