diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/astroid.rs | 75 | ||||
-rw-r--r-- | src/bin/client.rs | 2 | ||||
-rw-r--r-- | src/bin/server.rs | 7 | ||||
-rw-r--r-- | src/client/dashboard.rs | 4 | ||||
-rw-r--r-- | src/client/navigation.rs | 54 | ||||
-rw-r--r-- | src/connection.rs | 20 | ||||
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/mass.rs | 104 | ||||
-rw-r--r-- | src/mining.rs | 30 | ||||
-rw-r--r-- | src/module.rs | 14 | ||||
-rw-r--r-- | src/server/dashboard.rs | 4 | ||||
-rw-r--r-- | src/server/engines.rs | 42 | ||||
-rw-r--r-- | src/server/mining.rs | 40 | ||||
-rw-r--r-- | src/server/navigation.rs | 72 | ||||
-rw-r--r-- | src/ship.rs | 184 | ||||
-rw-r--r-- | src/targeting.rs | 10 |
16 files changed, 237 insertions, 430 deletions
diff --git a/src/astroid.rs b/src/astroid.rs deleted file mode 100644 index f95e362..0000000 --- a/src/astroid.rs +++ /dev/null @@ -1,75 +0,0 @@ -extern crate rand; -extern crate serde_json; - -use self::rand::distributions::Range; -use self::rand::distributions::Sample; - -use item::Item; -use storage::Storage; -use mass::{Mass, MassType}; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Astroid { - mass_type : MassType, - position : (f64, f64, f64), - velocity : (f64, f64, f64), - resouces : Storage, -} - -impl Astroid { - pub fn new() -> Astroid { - let mut rng = rand::thread_rng(); - - let mut pr = Range::new(-50.0, 50.0); - let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng)); - - let mut vr = Range::new(-0.5, 0.5); - let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng)); - - let mut rr = Range::new(0, 20); - let mut resouces = Vec::new(); - for _ in 0..rr.sample(&mut rng) { - resouces.push(Item::new("Iron", 1)) - } - Astroid { - mass_type : MassType::Astroid, - position : position, - velocity : velocity, - resouces : Storage::new(resouces), - } - } -} - -impl Mass for Astroid { - fn recv_mass_type(&self) -> MassType { - self.mass_type.clone() - } - - fn position(&self) -> (f64, f64, f64) { - self.position - } - - fn serialize(&self) -> String { - serde_json::to_string(self).unwrap() - } - - 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; - self.position.2 += self.velocity.2; - } - - fn recv_velocity(&self) -> (f64, f64, f64) { - self.velocity - } - - 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/client.rs b/src/bin/client.rs index 5460652..b690094 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -46,9 +46,9 @@ fn main() { stream.write(send.as_bytes()).unwrap(); match module { + ModuleType::Mining => client_mining(stream, buff_r), ModuleType::Dashboard => client_dashboard(buff_r), ModuleType::Engines => client_engines(stream, buff_r), ModuleType::Navigation => client_navigation(name, stream, buff_r), - ModuleType::Mining => client_mining(stream, buff_r), } } diff --git a/src/bin/server.rs b/src/bin/server.rs index c5954b0..68e091e 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -7,14 +7,13 @@ use std::collections::HashMap; use space::mass::Mass; use space::math::rand_name; -use space::astroid::Astroid; use space::connection::Connection; -fn populate() -> HashMap<String, Box<Mass>> { - let mut masses : HashMap<String, Box<Mass>> = HashMap::new(); +fn populate() -> HashMap<String, Mass> { + let mut masses : HashMap<String, Mass> = HashMap::new(); for _ in 0..10 { - masses.insert(rand_name(), Box::new(Astroid::new())); + masses.insert(rand_name(), Mass::new_astroid()); } masses diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs index 3776223..b9f0a08 100644 --- a/src/client/dashboard.rs +++ b/src/client/dashboard.rs @@ -4,13 +4,13 @@ use std::io::BufRead; use std::io::BufReader; use std::net::TcpStream; -use ship::Ship; +use mass::Mass; pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) { loop { let mut recv = String::new(); buff_r.read_line(&mut recv).unwrap(); - let ship : Ship = serde_json::from_str(&recv).unwrap(); + let ship : Mass = serde_json::from_str(&recv).unwrap(); println!("{:?}", ship); } } diff --git a/src/client/navigation.rs b/src/client/navigation.rs index e5a5069..171d050 100644 --- a/src/client/navigation.rs +++ b/src/client/navigation.rs @@ -1,19 +1,16 @@ extern crate termion; -extern crate itertools; +//extern crate itertools; extern crate serde_json; use std::net::TcpStream; use std::collections::BTreeMap; use self::termion::async_stdin; -use self::itertools::Itertools; use std::io::{BufRead, BufReader}; use std::io::{stdout, Read, Write}; use self::termion::raw::IntoRawMode; -use mass::Mass; -use ship::Ship; use math::distance; -use astroid::Astroid; +use mass::{Mass, MassType}; pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){ let stdout = stdout(); @@ -23,30 +20,24 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf loop { let mut recv = String::new(); buff_r.read_line(&mut recv).unwrap(); - - let string_hashmap = recv.split(";"); - let mut masses : BTreeMap<String, Box<Mass>> = BTreeMap::new(); - for string_element in string_hashmap { - if string_element.len() <= 1 { - break; - } - let (string_name, string_mass) = string_element.split("@").next_tuple().unwrap(); - masses.insert(string_name.to_string(), build_mass(string_mass)); - } - + let mut within_range : BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap(); write!(stdout, "{}{}Targets:", termion::clear::All, termion::cursor::Goto(1,1)).unwrap(); - let ship = masses.remove(&name).unwrap().downcast::<Ship>().unwrap(); + let ship = within_range.remove(&name).unwrap(); + let targeting = match ship.mass_type { + MassType::Ship{ref targeting, ..} => Some(targeting.clone()), + _ => None, + }.unwrap(); - for (i, (mass_name, mass)) in masses.iter().enumerate() { + for (i, (mass_name, mass)) in within_range.iter().enumerate() { - let target_data = match ship.recv_target() { + let target_data = match targeting.target.clone() { Some(target_name) => { if &target_name == mass_name { - serde_json::to_string(&ship.recv_targeting_status()).unwrap() + serde_json::to_string(&targeting.status).unwrap() } else { String::new() @@ -59,10 +50,10 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf termion::cursor::Goto(1, 2 + i as u16), i, mass_name, - mass.position().0, - mass.position().1, - mass.position().2, - distance(mass.position(), ship.position()), + mass.position.0, + mass.position.1, + mass.position.2, + distance(mass.position, ship.position), target_data ).unwrap(); } @@ -75,9 +66,9 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf } else { let i = c.to_digit(10).unwrap() as usize; - if i < masses.len() { + if i < within_range.len() { let mut send = String::new(); - send.push_str(masses.iter().nth(i).unwrap().0); + send.push_str(within_range.iter().nth(i).unwrap().0); send.push_str("\n"); stream.write(send.as_bytes()).unwrap(); } @@ -88,14 +79,3 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf stdout.flush().unwrap(); } } - -fn build_mass(string_mass : &str) -> Box<Mass> { - 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) - } -} diff --git a/src/connection.rs b/src/connection.rs index 91da04a..52d3352 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -5,9 +5,8 @@ use std::io::prelude::*; use std::net::TcpStream; use std::collections::HashMap; -use ship::Ship; -use mass::Mass; use module::ModuleType; +use mass::{Mass, MassType}; pub struct Connection { pub name : String, @@ -18,25 +17,26 @@ pub struct Connection { } impl Connection { - pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Box<Mass>>) -> Connection { + pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Mass>) -> Connection { let mut buff_r = BufReader::new(stream.try_clone().unwrap()); let mut recv = String::new(); buff_r.read_line(&mut recv).unwrap(); let name = &recv[..recv.find(":").unwrap()]; - let mass = masses.entry(name.to_string()).or_insert(Box::new(Ship::new((0.0, 0.0, 0.0)))); - let ship = mass.downcast_ref::<Ship>().unwrap(); + let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship()); - let modules = ship.recv_modules(); - stream.write(modules.as_bytes()).unwrap(); + let send = match ship.mass_type { + MassType::Ship{ref modules, ..} => serde_json::to_string(modules).unwrap() + "\n", + _ => String::new(), + }; + stream.write(send.as_bytes()).unwrap(); let mut recv = String::new(); buff_r.read_line(&mut recv).unwrap(); let module_type : ModuleType = serde_json::from_str(&recv.replace("\n","")).unwrap(); stream.set_nonblocking(true).unwrap(); - Connection { name : String::from(name), module_type : module_type, @@ -46,12 +46,12 @@ impl Connection { } } - pub fn process(&mut self, mut masses : &mut HashMap<String, Box<Mass>>) { + pub fn process(&mut self, mut masses : &mut HashMap<String, Mass>) { self.open = match self.module_type { + ModuleType::Mining => self.server_mining(&mut masses), ModuleType::Engines => self.server_engines(&mut masses), ModuleType::Dashboard => self.server_dashboard(&mut masses), ModuleType::Navigation => self.server_navigation(&mut masses), - ModuleType::Mining => self.server_mining(&mut masses), }; } } @@ -1,16 +1,13 @@ #[macro_use] extern crate serde_derive; -#[macro_use] -extern crate downcast; pub mod mass; pub mod item; -pub mod ship; pub mod math; pub mod module; +pub mod mining; pub mod client; pub mod server; pub mod storage; -pub mod astroid; pub mod targeting; pub mod connection; diff --git a/src/mass.rs b/src/mass.rs index 5742f83..f13e8ce 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -1,27 +1,91 @@ -extern crate serde; - -use downcast::Any; - -pub trait Mass : Any { - fn recv_mass_type(&self) -> MassType; - fn position(&self) -> (f64, f64, f64); - fn serialize(&self) -> String; - fn process(&mut self); - fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); - fn recv_velocity(&self) -> (f64, f64, f64); - fn box_clone(&self) -> Box<Mass>; -} +extern crate rand; -impl Clone for Box<Mass> { - fn clone(&self) -> Box<Mass> { - self.box_clone() - } +use self::rand::distributions::Range; +use self::rand::distributions::Sample; + +use storage::Storage; +use module::ModuleType; +use targeting::Targeting; +use mining::Mining; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Mass { + pub mass_type : MassType, + pub position : (f64, f64, f64), + pub velocity : (f64, f64, f64), } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum MassType { - Ship, - Astroid, + Ship { + modules : Vec<ModuleType>, + mining : Mining, + targeting : Targeting, + storage : Storage, + }, + Astroid{ + resources : Storage, + }, } -downcast!(Mass); +impl Mass { + pub fn new_astroid() -> Mass { + let mut rng = rand::thread_rng(); + + let mut pr = Range::new(-50.0, 50.0); + let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng)); + + let mut vr = Range::new(-0.5, 0.5); + let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng)); + + let astroid = MassType::Astroid { + resources : Storage::new(Vec::new()), + }; + + Mass { + mass_type : astroid, + position : position, + velocity : velocity, + } + } + + pub fn new_ship() -> Mass { + let mut modules = Vec::new(); + + modules.push(ModuleType::Navigation); + modules.push(ModuleType::Engines); + modules.push(ModuleType::Dashboard); + modules.push(ModuleType::Mining); + + let ship = MassType::Ship { + modules : modules, + mining : Mining::new(), + targeting : Targeting::new(), + storage : Storage::new(Vec::new()), + }; + + Mass { + mass_type : ship, + position : (0.0,0.0,0.0), + velocity : (0.0,0.0,0.0), + } + } + + pub fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + match self.mass_type { + MassType::Ship{ref mut targeting, ..} => { + targeting.process(); + }, + _ => (), + } + } + + pub fn accelerate(&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/mining.rs b/src/mining.rs new file mode 100644 index 0000000..ae4af49 --- /dev/null +++ b/src/mining.rs @@ -0,0 +1,30 @@ +extern crate serde_json; + +use std::time::SystemTime; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Mining { + pub range : f64, + pub status : bool, + time : u64, + start : Option<SystemTime>, +} + +impl Mining { + pub fn new() -> Mining { + Mining { + range : 10.0, + status : false, + time : 1, + start : None, + } + } + + pub fn start(&mut self) { + self.status = true; + } + + pub fn stop(&mut self) { + self.status = false; + } +} diff --git a/src/module.rs b/src/module.rs index 800f4d7..937c63c 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,15 +1,3 @@ -use downcast::Any; - -pub trait Module : Any { - fn box_clone(&self) -> Box<Module>; -} - -impl Clone for Box<Module> { - fn clone(&self) -> Box<Module> { - self.box_clone() - } -} - #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] pub enum ModuleType { Mining, @@ -17,5 +5,3 @@ pub enum ModuleType { Dashboard, Navigation, } - -downcast!(Module); diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs index d12ea35..82f73f9 100644 --- a/src/server/dashboard.rs +++ b/src/server/dashboard.rs @@ -7,9 +7,9 @@ use mass::Mass; use connection::Connection; impl Connection { - pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool { + pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let ship = masses.get(&self.name).unwrap(); - let send = ship.serialize() + "\n"; + let send = serde_json::to_string(&ship).unwrap(); match self.stream.write(send.as_bytes()) { Ok(_result) => true, Err(_error) => false, diff --git a/src/server/engines.rs b/src/server/engines.rs index ec55ee0..aa0e703 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -4,19 +4,21 @@ use std::io::Write; use std::io::BufRead; use std::collections::HashMap; -use ship::Ship; -use mass::Mass; +use mass::{Mass, MassType}; use connection::Connection; use targeting::TargetingStatus; impl Connection { - pub fn server_engines(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool { + pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let masses_clone = masses.clone(); - let mass = masses.get_mut(&self.name).unwrap(); - let ship = mass.downcast_mut::<Ship>().unwrap(); + let ship = masses.get_mut(&self.name).unwrap(); + let targeting = match ship.mass_type { + MassType::Ship{ref targeting, ..} => Some(targeting.clone()), + _ => None, + }.unwrap(); - let targeted = ship.recv_targeting_status() == TargetingStatus::Targeted; + let targeted = targeting.status == TargetingStatus::Targeted; let send = serde_json::to_string(&targeted).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { @@ -34,14 +36,24 @@ impl Connection { b"2\n" => acceleration.1 -= 0.1, b"4\n" => acceleration.2 += 0.1, b"6\n" => acceleration.2 -= 0.1, - b"+\n" => ship.speedup(), - b"-\n" => ship.slow(), + b"+\n" => { + let m_v = ship.velocity; + acceleration = (m_v.0 * 0.05, + m_v.1 * 0.05, + m_v.2 * 0.05); + }, + b"-\n" => { + let m_v = ship.velocity; + acceleration = (-1.0 * m_v.0 * 0.05, + -1.0 * m_v.1 * 0.05, + -1.0 * m_v.2 * 0.05); + }, b"c\n" => { - match ship.recv_target() { + match targeting.target { Some(name) => { let target = masses_clone.get(&name).unwrap(); - let d_v = target.recv_velocity(); - let m_v = ship.recv_velocity(); + let d_v = target.velocity; + let m_v = ship.velocity; acceleration = (d_v.0 - m_v.0, d_v.1 - m_v.1, d_v.2 - m_v.2); @@ -50,11 +62,11 @@ impl Connection { } }, b"t\n" => { - match ship.recv_target() { + match targeting.target { Some(name) => { let target = masses_clone.get(&name).unwrap(); - let d_p = target.position(); - let m_p = ship.position(); + let d_p = target.position; + let m_p = ship.position; acceleration = ((d_p.0 - m_p.0) * 0.01, (d_p.1 - m_p.1) * 0.01, (d_p.2 - m_p.2) * 0.01); @@ -71,7 +83,7 @@ impl Connection { Err(_error) => (), } - ship.give_acceleration(acceleration); + ship.accelerate(acceleration); true } diff --git a/src/server/mining.rs b/src/server/mining.rs index 9aa054f..ff26840 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -4,7 +4,6 @@ use std::io::BufRead; use std::io::Write; use std::collections::HashMap; -use ship::Ship; use math::distance; use mass::{Mass, MassType}; use connection::Connection; @@ -18,40 +17,38 @@ struct ServerData { } impl Connection { - pub fn server_mining(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool { + pub fn server_mining(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let masses_clone = masses.clone(); - let mass = masses.get_mut(&self.name).unwrap(); - let ship = mass.downcast_mut::<Ship>().unwrap(); + let ship = masses.get_mut(&self.name).unwrap(); - let target = match ship.recv_target() { - Some(name) => masses_clone.get(&name), - None => None, + let (mining, targeting) = match ship.mass_type { + MassType::Ship{ref targeting, ref mut mining, ..} => (Some(mining), Some(targeting.clone())), + _ => (None, None), }; + let mining = mining.unwrap(); + let target = masses_clone.get(&targeting.unwrap().target.unwrap()); let has_astroid_target = match target { - Some(target) => match target.recv_mass_type() { - MassType::Ship => false, - MassType::Astroid => true, + Some(target) => match target.mass_type { + MassType::Astroid{..} => true, + _ => false, }, None => false, }; let is_within_range = match has_astroid_target { true => match target { - Some(target) => match ship.recv_mining_range() > distance(ship.position(), target.position()) { - true => true, - false => false, - }, - None => false, + Some(target) => mining.range > distance(ship.position, target.position), + _ => false, } - false => false, + _ => false, }; let send = serde_json::to_string(&ServerData { has_astroid_target : has_astroid_target, is_within_range : is_within_range, - mining_range : ship.recv_mining_range(), - mining_status : ship.recv_mining_status(), + mining_range : mining.range, + mining_status : mining.status, }).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { @@ -64,9 +61,9 @@ impl Connection { Ok(result) => match recv.as_bytes() { b"F\n" => { if is_within_range { - match ship.recv_mining_status() { - true => ship.stop_mining(), - false => ship.start_mining(), + match mining.status { + true => mining.stop(), + false => mining.start(), } } }, @@ -78,7 +75,6 @@ impl Connection { } Err(_error) => (), } - true } } diff --git a/src/server/navigation.rs b/src/server/navigation.rs index f75c2b7..3813732 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -4,51 +4,51 @@ use std::io::Write; use std::io::BufRead; use std::collections::HashMap; -use mass::Mass; -use ship::Ship; +use mass::{Mass, MassType}; use math::distance; use connection::Connection; impl Connection { - pub fn server_navigation(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool { + pub fn server_navigation(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let masses_clone = masses.clone(); - let mass = masses.get_mut(&self.name).unwrap(); - let ship = mass.downcast_mut::<Ship>().unwrap(); - - match ship.recv_target() { - Some(name) => { - let target = masses_clone.get(&name).unwrap(); - if distance(target.position(), ship.position()) > ship.recv_range() { - ship.give_target(None); + let ship = masses.get_mut(&self.name).unwrap(); + let ship_position = ship.position; + + match ship.mass_type { + MassType::Ship{ref mut targeting, ..} => { + let target_name = targeting.clone().target; + match target_name { + Some(target_name) => { + let target = masses_clone.get(&target_name).unwrap(); + if distance(target.position, ship.position) > targeting.range { + targeting.target = None; + } + }, + _ => (), } - } - None => (), - } - let within_range : HashMap<&String, &Box<Mass>> = masses_clone.iter().filter(|&(_, mass)| - distance(ship.position(), mass.position()) < ship.recv_range()) - .collect(); - let mut send = String::new(); - for (name, mass) in within_range { - send.push_str(name); - send.push_str("@"); - send.push_str(&mass.serialize()); - send.push_str(";"); - } - send.push_str("\n"); - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } + let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)| + distance(ship_position, mass.position) < targeting.range) + .collect(); - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(_result) => (), - Err(_error) => (), - } - if recv.len() > 0 { - ship.give_target(Some(recv.replace("\n", ""))); + let send = serde_json::to_string(&within_range).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, + } + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(_result) => (), + Err(_error) => (), + } + if !recv.is_empty() { + targeting.give_target(Some(recv.replace("\n", ""))); + } + }, + _ => (), } + true } } diff --git a/src/ship.rs b/src/ship.rs deleted file mode 100644 index 403b4ed..0000000 --- a/src/ship.rs +++ /dev/null @@ -1,184 +0,0 @@ -extern crate serde_json; - -use std::time::SystemTime; -//use std::collections::HashMap; - -use storage::Storage; -use mass::{Mass, MassType}; -//use module::{Module, ModuleType}; -use module::ModuleType; -use targeting::{Targeting, TargetingStatus}; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Ship { - mass_type : MassType, - position : (f64, f64, f64), - velocity : (f64, f64, f64), - range : f64, - modules : Vec<ModuleType>, - mining : Mining, - targeting : Targeting, -// modules : HashMap<ModuleType, Box<Module>>, - storage : Storage, -} - -impl Ship { - pub fn new(position : (f64, f64, f64)) -> Ship { - let mut modules = Vec::new(); - - modules.push(ModuleType::Navigation); - modules.push(ModuleType::Engines); - modules.push(ModuleType::Dashboard); - modules.push(ModuleType::Mining); - -// let mut modules : HashMap<ModuleType, Box<Module>> = HashMap::new(); -// modules.insert(ModuleType::Navigation, Targeting::new()); -// modules.insert(ModuleType::Mining, Mining::new()); - - Ship { - mass_type : MassType::Ship, - position : position, - velocity : (0.0, 0.0, 0.0), - range : 100.0, - modules : modules, - mining : Mining::new(), - targeting : Targeting::new(), - storage : Storage::new(Vec::new()), - } - } - - pub fn slow(&mut self) { - if self.velocity.0 > 0.01 { - self.velocity.0 += -1.0 * self.velocity.0 * 0.1; - } - else { - self.velocity.0 = 0.0; - } - - if self.velocity.1 > 0.01 { - self.velocity.1 += -1.0 * self.velocity.1 * 0.1; - } - else { - self.velocity.1 = 0.0; - } - - if self.velocity.2 > 0.01 { - self.velocity.2 += -1.0 * self.velocity.2 * 0.1; - } - else { - self.velocity.2 = 0.0; - } - } - - pub fn speedup(&mut self) { - self.velocity.0 *= 1.05; - self.velocity.1 *= 1.05; - self.velocity.2 *= 1.05; - } - pub fn start_mining(&mut self) { - self.mining.start() - } - - pub fn stop_mining(&mut self) { - self.mining.stop() - } - - pub fn recv_range(&self) -> f64 { - self.range - } - - pub fn recv_mining_range(&self) -> f64 { - self.mining.recv_range() - } - - pub fn recv_mining_status(&self) -> bool { - self.mining.recv_status() - } - - pub fn give_target(&mut self, target : Option<String>) { - self.targeting.give_target(target); - } - - pub fn recv_target(&self) -> Option<String> { - self.targeting.recv_target() - } - - pub fn recv_targeting_status(&self) -> TargetingStatus { - self.targeting.recv_status() - } - - pub fn recv_modules(&self) -> String { - serde_json::to_string(&self.modules).unwrap() + "\n" - } -} - -impl Mass for Ship { - fn recv_mass_type(&self) -> MassType { - self.mass_type.clone() - } - - fn process(&mut self) { - self.position.0 += self.velocity.0; - self.position.1 += self.velocity.1; - self.position.2 += self.velocity.2; - self.targeting.process() - } - - fn position(&self) -> (f64, f64, f64) { - self.position - } - - fn serialize(&self) -> String { - serde_json::to_string(self).unwrap() - } - - fn box_clone(&self) -> Box<Mass> { - Box::new((*self).clone()) - } - - fn recv_velocity(&self) -> (f64, f64, f64) { - self.velocity - } - - fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { - self.velocity.0 += acceleration.0; - self.velocity.1 += acceleration.1; - self.velocity.2 += acceleration.2; - } -} - - -#[derive(Serialize, Deserialize, Debug, Clone)] -struct Mining { - range : f64, - status : bool, - time : u64, - start : Option<SystemTime>, -} - -impl Mining { - pub fn new() -> Mining { - Mining { - range : 10.0, - status : false, - time : 1, - start : None, - } - } - - pub fn start(&mut self) { - self.status = true; - } - - pub fn stop(&mut self) { - self.status = false; - } - - pub fn recv_range(&self) -> f64 { - self.range - } - - pub fn recv_status(&self) -> bool { - self.status - } -} diff --git a/src/targeting.rs b/src/targeting.rs index 49fdc54..3e566f4 100644 --- a/src/targeting.rs +++ b/src/targeting.rs @@ -9,10 +9,11 @@ pub enum TargetingStatus { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Targeting { - target : Option<String>, - status : TargetingStatus, - time : u64, - start : Option<SystemTime>, + pub target : Option<String>, + pub range : f64, + pub status : TargetingStatus, + time : u64, + start : Option<SystemTime>, } impl Targeting { @@ -20,6 +21,7 @@ impl Targeting { Targeting { target : None, status : TargetingStatus::None, + range : 100.0, time : 3, start : None, } |