diff options
author | tom barrett <spalf0@gmail.com> | 2018-04-06 02:43:03 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-04-06 02:43:03 -0500 |
commit | c5209135dd2032ba86a5ca3ad0647c33007970e1 (patch) | |
tree | 4f73c7bfa8c0e1c7b6c3cf208e3c8f585ec5c173 /src | |
parent | 9d05fa5a62e6447727c184722590587c24f6827c (diff) |
-made seperate targeting file
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/ship.rs | 53 | ||||
-rw-r--r-- | src/targeting.rs | 60 |
3 files changed, 68 insertions, 46 deletions
@@ -13,5 +13,6 @@ pub mod module; pub mod astroid; pub mod engines; pub mod dashboard; +pub mod targeting; pub mod connection; pub mod navigation; diff --git a/src/ship.rs b/src/ship.rs index 4f2a910..44f7d06 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,23 +1,8 @@ -use std::time::SystemTime; +extern crate serde_json; use module::Module; use mass::{Mass, Type}; -extern crate serde_json; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub enum TargetingStatus { - None, - Targeting, - Targeted, -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -struct Targeting { - target : Option<String>, - status : TargetingStatus, - time : u64, - start : Option<SystemTime>, -} +use targeting::{Targeting, TargetingStatus}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Ship { @@ -44,12 +29,7 @@ impl Ship { 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, - }, + targeting : Targeting::new(), modules : modules, } } @@ -88,25 +68,15 @@ impl Ship { } pub fn give_target(&mut self, target : Option<String>) { - self.targeting.target = target; - match self.targeting.target { - Some(_) => { - self.targeting.status = TargetingStatus::Targeting; - self.targeting.start = Some(SystemTime::now()); - }, - None => { - self.targeting.status = TargetingStatus::None; - self.targeting.start = None; - } - } + self.targeting.give_target(target); } pub fn recv_target(&self) -> Option<String> { - self.targeting.target.clone() + self.targeting.get_target() } pub fn recv_target_status(&self) -> TargetingStatus { - self.targeting.status.clone() + self.targeting.get_status() } pub fn get_modules(&self) -> String { @@ -123,16 +93,7 @@ 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; - self.targeting.start = None; - } - } - None => (), - } + self.targeting.process() } fn position(&self) -> (f64, f64, f64) { diff --git a/src/targeting.rs b/src/targeting.rs new file mode 100644 index 0000000..754e7a4 --- /dev/null +++ b/src/targeting.rs @@ -0,0 +1,60 @@ +use std::time::SystemTime; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum TargetingStatus { + None, + Targeting, + Targeted, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Targeting { + target : Option<String>, + status : TargetingStatus, + time : u64, + start : Option<SystemTime>, +} + +impl Targeting { + pub fn new() -> Targeting { + Targeting { + target : None, + status : TargetingStatus::None, + time : 3, + start : None, + } + } + + pub fn process(&mut self) { + match self.start { + Some(time) => { + if time.elapsed().unwrap().as_secs() > self.time { + self.status = TargetingStatus::Targeted; + self.start = None; + } + } + None => (), + } + } + pub fn give_target(&mut self, target : Option<String>) { + self.target = target; + match self.target { + Some(_) => { + self.status = TargetingStatus::Targeting; + self.start = Some(SystemTime::now()); + }, + None => { + self.status = TargetingStatus::None; + self.start = None; + } + } + } + + pub fn get_target(&self) -> Option<String> { + self.target.clone() + } + + pub fn get_status(&self) -> TargetingStatus { + self.status.clone() + } +} |