summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-05 02:04:11 -0500
committertom barrett <spalf0@gmail.com>2018-04-05 02:04:11 -0500
commit22ec8b31c66475f29106f2fda46969e427f2e704 (patch)
treef1f439b793ad93d49bee497e1e5675d177e2f65c /src
parentc297e871b7ce8935dd3bb1c65ecae37000d9a331 (diff)
-changed how data is passed (learned you can serialize a vec) -added targeting struct to ship and got timer prototype
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs2
-rw-r--r--src/bin/client.rs13
-rw-r--r--src/connection.rs2
-rw-r--r--src/lib.rs1
-rw-r--r--src/ship.rs68
5 files changed, 57 insertions, 29 deletions
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<Module> = 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::<usize>().unwrap()].to_owned();
- module.push_str("\n");
- stream.write(module.as_bytes()).unwrap();
+ let module = modules[choice.replace("\n", "").parse::<usize>().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,16 +1,33 @@
+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<usize>,
+ status : TargetingStatus,
+ time : u64,
+ start : Option<SystemTime>,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Ship {
name : String,
position : (f64, f64, f64),
velocity : (f64, f64, f64),
mass_type : Type,
r : f64,
- target : Option<usize>,
modules : Vec<Module>,
+ 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<usize>) {
- self.target = target;
+ self.targeting.target = target;
+ self.targeting.status = TargetingStatus::Targeted;
+ //self.targeting.start = Some(SystemTime::now());
}
pub fn recv_target(&self) -> Option<usize> {
- 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) {