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 --- Cargo.lock | 12 ++++++++++ Cargo.toml | 1 + src/astroid.rs | 2 +- src/bin/client.rs | 13 +++++------ src/connection.rs | 2 +- src/lib.rs | 1 + src/ship.rs | 68 +++++++++++++++++++++++++++++++++++++++---------------- 7 files changed, 70 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ef5b2dd..f75a247 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,6 +123,7 @@ dependencies = [ "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -153,6 +154,16 @@ dependencies = [ "redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "time" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-xid" version = "0.0.4" @@ -198,6 +209,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" +"checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" diff --git a/Cargo.toml b/Cargo.toml index baefb5f..2258e6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ serde_json = "1.0" termion = "1.5.1" downcast = "0.8" rand = "0.4" +time = "0.1" 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