summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-03-01 03:51:36 -0600
committertom barrett <spalf0@gmail.com>2018-03-01 03:51:36 -0600
commit233aa5d803e8015f78f663bc9af6bb33e56eb96e (patch)
tree797240efc2d00c02b5b4c3ea493595226f96a88d
parent3d0efb20ac17ae18c495ffbb1b7d28b22851cb28 (diff)
-added copying of mass vector, fixed buildmass bug, and allow giving of target
-rw-r--r--src/astroid.rs8
-rw-r--r--src/connection.rs6
-rw-r--r--src/engines.rs2
-rw-r--r--src/mass.rs11
-rw-r--r--src/navigation.rs19
-rw-r--r--src/ship.rs22
6 files changed, 42 insertions, 26 deletions
diff --git a/src/astroid.rs b/src/astroid.rs
index 1e7c99c..c789b87 100644
--- a/src/astroid.rs
+++ b/src/astroid.rs
@@ -7,7 +7,7 @@ extern crate serde_json;
use mass::{Mass, Type};
use astroid::rand::Rng;
-#[derive(Serialize, Deserialize, Debug)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Astroid {
name : String,
mass_type : Type,
@@ -48,8 +48,12 @@ impl Mass for Astroid {
serde_json::to_string(self).unwrap()
}
- fn slow(&mut self) {
+ fn slow(&mut self) {}
+ fn range(&self) -> f64 {0.0}
+ fn give_target(&mut self, target : Option<usize>) {}
+ fn box_clone(&self) -> Box<Mass> {
+ Box::new((*self).clone())
}
fn process(&mut self) {
diff --git a/src/connection.rs b/src/connection.rs
index 3ca3b08..0bae3b2 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -55,11 +55,11 @@ impl Connection {
}
}
- pub fn process(&mut self, masses : &mut Vec<Box<Mass>>) {
+ pub fn process(&mut self, mut masses : &mut Vec<Box<Mass>>) {
self.open = match self.module {
Module::Dashboard => server_dashboard(masses[self.index].serialize(), &self.stream),
- Module::Engines => server_engines(&mut self.buff_r, &mut masses[self.index]),
- Module::Navigation => server_navigation(masses, self.index, &self.stream, &mut self.buff_r),
+ Module::Engines => server_engines(&mut masses[self.index], &mut self.buff_r),
+ Module::Navigation => server_navigation(&mut masses.to_vec(), &mut masses[self.index], &self.stream, &mut self.buff_r),
};
}
}
diff --git a/src/engines.rs b/src/engines.rs
index 9d8841d..d320c9d 100644
--- a/src/engines.rs
+++ b/src/engines.rs
@@ -34,7 +34,7 @@ pub fn client_engines(mut stream : TcpStream) {
}
}
-pub fn server_engines(buff_r : &mut BufReader<TcpStream>, ship : &mut Box<Mass>) -> bool {
+pub fn server_engines(ship : &mut Box<Mass>, buff_r : &mut BufReader<TcpStream>) -> bool {
let mut acceleration = (0.0, 0.0, 0.0);
let mut data = String::new();
match buff_r.read_line(&mut data) {
diff --git a/src/mass.rs b/src/mass.rs
index 30442ec..d6feb6c 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -7,9 +7,18 @@ pub trait Mass : Any {
fn process(&mut self);
fn slow(&mut self);
fn give_acceleration(&mut self, acceleration : (f64, f64, f64));
+ fn box_clone(&self) -> Box<Mass>;
+ fn range(&self) -> f64;
+ fn give_target(&mut self, target : Option<usize>);
}
-#[derive(Serialize, Deserialize, Debug)]
+impl Clone for Box<Mass> {
+ fn clone(&self) -> Box<Mass> {
+ self.box_clone()
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Type {
Ship,
Astroid,
diff --git a/src/navigation.rs b/src/navigation.rs
index e55b65a..a9c38b2 100644
--- a/src/navigation.rs
+++ b/src/navigation.rs
@@ -30,13 +30,14 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
masses.push(build_mass(string_mass));
}
- let ship = masses.iter().find(|ship| ship.name() == &name);
+ let index = masses.iter().position(|ship| ship.name() == &name).unwrap();
+ let ship = masses.remove(index);
write!(stdout, "{}{}Targets:",
termion::clear::All,
termion::cursor::Goto(1,1)).unwrap();
- let position = ship.unwrap().position();
+ let position = ship.position();
for (i, mass) in masses.iter().enumerate() {
write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2}",
termion::cursor::Goto(1, 2 + i as u16),
@@ -75,9 +76,7 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
}
}
-pub fn server_navigation(masses : &mut Vec<Box<Mass>>, index : usize, mut stream : &TcpStream, buff_r : &mut BufReader<TcpStream>) -> bool {
- let ship = masses[index].downcast_ref::<Ship>().unwrap();
-
+pub fn server_navigation(masses : &mut Vec<Box<Mass>>, ship : &mut Box<Mass>, mut stream : &TcpStream, buff_r : &mut BufReader<TcpStream>) -> bool {
let within_range : Vec<&Box<Mass>> = masses.iter().filter(|mass|
distance(ship.position(), mass.position()) < ship.range()).collect();
@@ -93,23 +92,23 @@ pub fn server_navigation(masses : &mut Vec<Box<Mass>>, index : usize, mut stream
}
let mut string_mass = String::new();
- buff_r.read_line(&mut string_mass).unwrap();
+
if string_mass.len() > 0 {
let target = build_mass(&string_mass);
- //ship.give_target(masses.iter().position(|mass|
- // mass.name() == target.name()));
+ ship.give_target(masses.iter().position(|mass|
+ mass.name() == target.name()));
}
true
}
fn build_mass(string_mass : &str) -> Box<Mass> {
- let mut mass = Astroid::new();
if string_mass.contains("Ship") {
let mass : Ship = serde_json::from_str(&string_mass).unwrap();
+ return Box::new(mass)
}
else {
let mass : Astroid = serde_json::from_str(&string_mass).unwrap();
+ return Box::new(mass)
}
- Box::new(mass)
}
diff --git a/src/ship.rs b/src/ship.rs
index 08b4d93..2482db1 100644
--- a/src/ship.rs
+++ b/src/ship.rs
@@ -1,7 +1,7 @@
use mass::{Mass, Type};
extern crate serde_json;
-#[derive(Serialize, Deserialize, Debug)]
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Ship {
name : String,
position : (f64, f64, f64),
@@ -22,14 +22,6 @@ impl Ship {
target : None,
}
}
-
- pub fn give_target(&mut self, target : Option<usize>) {
- self.target = target;
- }
-
- pub fn range(&self) -> f64 {
- self.r
- }
}
impl Mass for Ship {
@@ -45,6 +37,14 @@ impl Mass for Ship {
serde_json::to_string(self).unwrap()
}
+ fn range(&self) -> f64 {
+ self.r
+ }
+
+ fn give_target(&mut self, target : Option<usize>) {
+ self.target = target;
+ }
+
fn slow(&mut self) {
if self.velocity.0 > 0.01 {
self.velocity.0 += -1.0 * self.velocity.0 * 0.1;
@@ -68,6 +68,10 @@ impl Mass for Ship {
}
}
+ fn box_clone(&self) -> Box<Mass> {
+ Box::new((*self).clone())
+ }
+
fn process(&mut self) {
self.position.0 += self.velocity.0;
self.position.1 += self.velocity.1;