summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-02-27 06:59:58 -0600
committertom barrett <spalf0@gmail.com>2018-02-27 06:59:58 -0600
commitb5896e2d7597e42818a47710da22098d178bf8f6 (patch)
treed48107628b0c10fc41493e35f8ba8f3039c3b9bc /src
parent87346ea9b91d441e92c476785c2cb979f9a23bf5 (diff)
-now use velocity and position vector, astroids are now randomly populated
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs47
-rw-r--r--src/bin/server.rs8
-rw-r--r--src/connection.rs18
-rw-r--r--src/mass.rs6
-rw-r--r--src/navigation.rs13
-rw-r--r--src/ship.rs40
6 files changed, 87 insertions, 45 deletions
diff --git a/src/astroid.rs b/src/astroid.rs
index 6aa1807..fb14751 100644
--- a/src/astroid.rs
+++ b/src/astroid.rs
@@ -1,35 +1,58 @@
use mass::{Mass, Type};
+extern crate rand;
+use self::rand::distributions::Range;
+use astroid::rand::distributions::Sample;
extern crate serde_json;
+use astroid::rand::Rng;
#[derive(Serialize, Deserialize, Debug)]
pub struct Astroid {
name : String,
t : Type,
- location : (f64, f64, f64),
+ position : (f64, f64, f64),
+ velocity : (f64, f64, f64),
}
-impl Mass for Astroid {
- fn new(name : &str, location : (f64, f64, f64)) -> Astroid {
+impl Astroid {
+ pub fn new() -> Astroid {
+ let name : String = rand::thread_rng()
+ .gen_ascii_chars()
+ .take(8)
+ .collect();
+ let mut r = Range::new(-50.0, 50.0);
+ let mut rng = rand::thread_rng();
+ let position = (r.sample(&mut rng), r.sample(&mut rng), r.sample(&mut rng));
Astroid {
- name : String::from(name),
- t : Type::Astroid,
- location : location,
+ name : name,
+ t : Type::Astroid,
+ position : position,
+ velocity : (0.0, 0.0, 0.0),
}
}
+}
+impl Mass for Astroid {
fn name(&self) -> &String {
&self.name
}
- fn location(&self) -> (f64, f64, f64) {
- self.location
- }
-
- fn set_location(&mut self, location : (f64, f64, f64)) {
- self.location = location;
+ fn position(&self) -> (f64, f64, f64) {
+ self.position
}
fn serialize(&self) ->String {
serde_json::to_string(self).unwrap()
}
+
+ fn process(&mut self) {
+ self.position.0 += self.velocity.0;
+ self.position.1 += self.velocity.1;
+ self.position.2 += self.velocity.2;
+ }
+
+ fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) {
+ self.velocity.0 += acceleration.0;
+ self.velocity.1 += acceleration.1;
+ self.velocity.2 += acceleration.2;
+ }
}
diff --git a/src/bin/server.rs b/src/bin/server.rs
index c327257..36840c1 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -11,7 +11,9 @@ 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.0, -5.0, 4.0))));
+ masses.push(Box::new(Astroid::new()));
+ masses.push(Box::new(Astroid::new()));
+ masses.push(Box::new(Astroid::new()));
masses
}
@@ -32,6 +34,10 @@ fn main() {
}
connections.retain(|connection| connection.open);
+ for mass in masses.iter_mut() {
+ mass.process();
+ }
+
sleep(Duration::from_millis(100));
}
}
diff --git a/src/connection.rs b/src/connection.rs
index db258e4..bcd4a30 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -64,16 +64,16 @@ impl Connection {
}
}
Module::Engines => {
- let mut location = masses[self.index].location();
+ let mut acceleration = (0.0, 0.0, 0.0);
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.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,
+ b"5\n" => acceleration.0 += 1.0,
+ b"0\n" => acceleration.0 -= 1.0,
+ b"8\n" => acceleration.1 += 1.0,
+ b"2\n" => acceleration.1 -= 1.0,
+ b"4\n" => acceleration.2 += 1.0,
+ b"6\n" => acceleration.2 -= 1.0,
_ => {
if result == 0 {
self.open = false;
@@ -82,13 +82,13 @@ impl Connection {
},
Err(_error) => (),
}
- masses[self.index].set_location(location);
+ masses[self.index].give_acceleration(acceleration);
}
Module::Navigation => {
let ship = &masses[self.index].downcast_ref::<Ship>().unwrap();
let within_range : Vec<&Box<Mass>> = masses.iter().filter(|mass|
- distance(ship.location(), mass.location()) < ship.range()).collect();
+ distance(ship.position(), mass.position()) < ship.range()).collect();
let mut send = String::new();
for mass in within_range {
diff --git a/src/mass.rs b/src/mass.rs
index 71723cd..d91b292 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,11 +1,11 @@
use downcast::Any;
pub trait Mass : Any {
- fn new(name : &str, location : (f64, f64, f64)) -> Self where Self: Sized;
fn name(&self) -> &String;
- fn location(&self) -> (f64, f64, f64);
- fn set_location(&mut self, location : (f64, f64, f64));
+ fn position(&self) -> (f64, f64, f64);
fn serialize(&self) -> String;
+ fn process(&mut self);
+ fn give_acceleration(&mut self, acceleration : (f64, f64, f64));
}
#[derive(Serialize, Deserialize, Debug)]
diff --git a/src/navigation.rs b/src/navigation.rs
index cc2221a..4683c0c 100644
--- a/src/navigation.rs
+++ b/src/navigation.rs
@@ -16,7 +16,7 @@ use ship::Ship;
use math::distance;
use astroid::Astroid;
-pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader<TcpStream>){
+pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode().unwrap();
let mut stdin = async_stdin().bytes();
@@ -55,14 +55,16 @@ pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader<T
termion::clear::All,
termion::cursor::Goto(1,1)).unwrap();
- let location = ship.expect("zz").location();
+ let position = ship.unwrap().position();
for (i, mass) in masses.iter().enumerate() {
- write!(stdout, "{}{}) {} {:?} Distance : {}",
+ write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2}",
termion::cursor::Goto(1, 2 + i as u16),
i,
mass.name(),
- mass.location(),
- distance(mass.location(), location)).unwrap();
+ mass.position().0,
+ mass.position().1,
+ mass.position().2,
+ distance(mass.position(), position)).unwrap();
}
match stdin.next() {
@@ -70,6 +72,7 @@ pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader<T
let c = c.unwrap();
let mut send = String::new();
send.push(c as char);
+ println!("{}", send);
if send.as_bytes() == b"q" {
break;
}
diff --git a/src/ship.rs b/src/ship.rs
index 078c2c1..30c68d9 100644
--- a/src/ship.rs
+++ b/src/ship.rs
@@ -4,40 +4,50 @@ extern crate serde_json;
#[derive(Serialize, Deserialize, Debug)]
pub struct Ship {
name : String,
- location : (f64, f64, f64),
+ position : (f64, f64, f64),
+ velocity : (f64, f64, f64),
t : Type,
r : f64,
}
impl Ship {
- pub fn range(&self) -> f64 {
- self.r
- }
-}
-
-impl Mass for Ship {
- fn new(name : &str, location : (f64, f64, f64)) -> Ship {
+ pub fn new(name : &str, position : (f64, f64, f64)) -> Ship {
Ship {
name : String::from(name),
- location : location,
+ position : position,
+ velocity : (0.0, 0.0, 0.0),
t : Type::Ship,
r : 100.0,
}
}
- fn name(&self) -> &String {
- &self.name
+ pub fn range(&self) -> f64 {
+ self.r
}
+}
- fn location(&self) -> (f64, f64, f64) {
- self.location
+impl Mass for Ship {
+ fn name(&self) -> &String {
+ &self.name
}
- fn set_location(&mut self, location : (f64, f64, f64)) {
- self.location = location;
+ fn position(&self) -> (f64, f64, f64) {
+ self.position
}
fn serialize(&self) -> String {
serde_json::to_string(self).unwrap()
}
+
+ fn process(&mut self) {
+ self.position.0 += self.velocity.0;
+ self.position.1 += self.velocity.1;
+ self.position.2 += self.velocity.2;
+ }
+
+ fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) {
+ self.velocity.0 += acceleration.0;
+ self.velocity.1 += acceleration.1;
+ self.velocity.2 += acceleration.2;
+ }
}