From c5209135dd2032ba86a5ca3ad0647c33007970e1 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 6 Apr 2018 02:43:03 -0500 Subject: -made seperate targeting file --- src/lib.rs | 1 + src/ship.rs | 53 +++++++------------------------------------------ src/targeting.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 46 deletions(-) create mode 100644 src/targeting.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index fee73fa..a8d9717 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, - status : TargetingStatus, - time : u64, - start : Option, -} +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) { - 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 { - 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, + status : TargetingStatus, + time : u64, + start : Option, +} + +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) { + 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 { + self.target.clone() + } + + pub fn get_status(&self) -> TargetingStatus { + self.status.clone() + } +} -- cgit v1.2.3