summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-02-23 07:40:05 -0600
committertom barrett <spalf0@gmail.com>2018-02-23 07:40:05 -0600
commit6634f73a6768f61f928d51941be5e970e6011f07 (patch)
tree1af5ef4a713061ad9bb10b0cb897362d59c430ec /src
parent53281aa16e262124631af4abd19e16a921de098d (diff)
-changed to floats and got vec type
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs8
-rw-r--r--src/bin/server.rs2
-rw-r--r--src/connection.rs22
-rw-r--r--src/mass.rs6
-rw-r--r--src/ship.rs8
5 files changed, 23 insertions, 23 deletions
diff --git a/src/astroid.rs b/src/astroid.rs
index 37460cf..0eed4ff 100644
--- a/src/astroid.rs
+++ b/src/astroid.rs
@@ -4,11 +4,11 @@ extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
pub struct Astroid {
name : String,
- location : (isize, isize, isize),
+ location : (f64, f64, f64),
}
impl Mass for Astroid {
- fn new(name : &str, location : (isize, isize, isize)) -> Astroid {
+ fn new(name : &str, location : (f64, f64, f64)) -> Astroid {
Astroid {
name : String::from(name),
location : location,
@@ -19,11 +19,11 @@ impl Mass for Astroid {
&self.name
}
- fn location(&self) -> (isize, isize, isize) {
+ fn location(&self) -> (f64, f64, f64) {
self.location
}
- fn set_location(&mut self, location : (isize, isize, isize)) {
+ fn set_location(&mut self, location : (f64, f64, f64)) {
self.location = location;
}
diff --git a/src/bin/server.rs b/src/bin/server.rs
index e042855..c327257 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -11,7 +11,7 @@ use space::connection::Connection;
fn populate() -> Vec<Box<Mass>> {
let mut masses : Vec<Box<Mass>> = Vec::new();
- masses.push(Box::new(Astroid::new("cZfAJ", (10, -5, 4))));
+ masses.push(Box::new(Astroid::new("cZfAJ", (10.0, -5.0, 4.0))));
masses
}
diff --git a/src/connection.rs b/src/connection.rs
index ac57176..59031c6 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)));
+ let ship = Box::new(Ship::new(name, (0.0,0.0,0.0)));
masses.push(ship);
masses.len() - 1
},
@@ -67,12 +67,12 @@ impl Connection {
let mut data = String::new();
match self.buff_r.read_line(&mut data) {
Ok(result) => match data.as_bytes() {
- b"5\n" => location.0 += 1,
- b"0\n" => location.0 -= 1,
- b"8\n" => location.1 += 1,
- b"2\n" => location.1 -= 1,
- b"4\n" => location.2 += 1,
- b"6\n" => location.2 -= 1,
+ b"5\n" => location.0 += 1.0,
+ b"0\n" => location.0 -= 1.0,
+ b"8\n" => location.1 += 1.0,
+ b"2\n" => location.1 -= 1.0,
+ b"4\n" => location.2 += 1.0,
+ b"6\n" => location.2 -= 1.0,
_ => {
if result == 0 {
self.open = false;
@@ -86,8 +86,8 @@ impl Connection {
Module::Navigation => {
let ship = &masses[self.index].downcast_ref::<Ship>().unwrap();
- let within_range : Vec<_> = masses.iter().filter(|mass|
- distance(ship.location(), mass.location()) < ship.range()).collect();
+ let within_range : Vec<&Box<Mass>> = masses.iter().filter(|mass|
+ distance(ship.location(), mass.location()) < ship.range()).collect();
let mut send = String::new();
for mass in within_range {
@@ -105,6 +105,6 @@ impl Connection {
}
-fn distance(l0 : (isize, isize, isize), l1 : (isize, isize, isize)) -> f64 {
- (((l1.0-l0.0).pow(2) + (l1.1-l0.1).pow(2) + (l1.2-l0.2).pow(2)) as f64).sqrt()
+fn distance(l0 : (f64, f64, f64), l1 : (f64, f64, f64)) -> f64 {
+ (((l1.0-l0.0).powf(2.0) + (l1.1-l0.1).powf(2.0) + (l1.2-l0.2).powf(2.0))).sqrt()
}
diff --git a/src/mass.rs b/src/mass.rs
index 8c50348..0a49dc6 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,10 +1,10 @@
use downcast::Any;
pub trait Mass : Any {
- fn new(name : &str, location : (isize, isize, isize)) -> Self where Self: Sized;
+ fn new(name : &str, location : (f64, f64, f64)) -> Self where Self: Sized;
fn name(&self) -> &String;
- fn location(&self) -> (isize, isize, isize);
- fn set_location(&mut self, location : (isize, isize, isize));
+ fn location(&self) -> (f64, f64, f64);
+ fn set_location(&mut self, location : (f64, f64, f64));
fn serialize(&self) -> String;
fn deserialize(&mut self, data : &str);
}
diff --git a/src/ship.rs b/src/ship.rs
index 49da84d..e5186c8 100644
--- a/src/ship.rs
+++ b/src/ship.rs
@@ -4,7 +4,7 @@ extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
pub struct Ship {
name : String,
- location : (isize, isize, isize),
+ location : (f64, f64, f64),
r : f64,
}
@@ -15,7 +15,7 @@ impl Ship {
}
impl Mass for Ship {
- fn new(name : &str, location : (isize, isize, isize)) -> Ship {
+ fn new(name : &str, location : (f64, f64, f64)) -> Ship {
Ship {
name : String::from(name),
location : location,
@@ -27,11 +27,11 @@ impl Mass for Ship {
&self.name
}
- fn location(&self) -> (isize, isize, isize) {
+ fn location(&self) -> (f64, f64, f64) {
self.location
}
- fn set_location(&mut self, location : (isize, isize, isize)) {
+ fn set_location(&mut self, location : (f64, f64, f64)) {
self.location = location;
}