diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-19 13:24:25 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-19 13:24:25 -0600 |
commit | a4efade392aa7127c373b0247d39274cb0decd10 (patch) | |
tree | 72ff44e69917873ca9933c4a56794a15b99fb90c /src/modules/tractorbeam.rs | |
parent | 892088d723fd3dc0aae969273331c2765f322e6f (diff) |
unified all server->client connection and brought logic to modules
Diffstat (limited to 'src/modules/tractorbeam.rs')
-rw-r--r-- | src/modules/tractorbeam.rs | 101 |
1 files changed, 57 insertions, 44 deletions
diff --git a/src/modules/tractorbeam.rs b/src/modules/tractorbeam.rs index c77ea1f..3bbb32a 100644 --- a/src/modules/tractorbeam.rs +++ b/src/modules/tractorbeam.rs @@ -9,20 +9,6 @@ pub struct Tractorbeam { desired_distance: Option<f64>, } -#[derive(Serialize, Deserialize, Debug, Clone)] -pub enum TractorbeamStatus { - None, - Push, - Pull, - Bring, -} - -impl Default for TractorbeamStatus { - fn default() -> Self { - TractorbeamStatus::None - } -} - impl Tractorbeam { pub fn new() -> Tractorbeam { Tractorbeam { @@ -32,7 +18,18 @@ impl Tractorbeam { } } - pub fn give_recv(&mut self, recv: String) { + pub fn process(&mut self) {} + + pub fn get_client_data(&self, target: Option<&Mass>) -> String { + let client_data = TractorbeamClientData { + has_target: target.is_some(), + status: self.status.clone(), + }; + + serde_json::to_string(&client_data).unwrap() + "\n" + } + + pub fn give_received_data(&mut self, recv: String) { match recv.as_str() { "o" => self.toggle_pull(), "p" => self.toggle_push(), @@ -41,7 +38,32 @@ impl Tractorbeam { } } - pub fn toggle_pull(&mut self) { + pub fn get_acceleration(&self, ship_position: Vector, target_position: Vector) -> Vector { + let acceleration = ship_position.clone() - target_position.clone(); + match self.status { + TractorbeamStatus::Push => acceleration.unitize() * -0.05, + TractorbeamStatus::Pull => acceleration.unitize() * 0.05, + TractorbeamStatus::Bring => match self.desired_distance { + Some(desired_distance) => { + if desired_distance > ship_position.distance_from(target_position) { + acceleration.unitize() * -0.05 + //some sort of velocity limiter + //if target.speed_torwards(ship) < 10.0 { + // acceleration.unitize() * -0.05 + //} else { + // Vector::default() + //} + } else { + acceleration.unitize() * 0.05 + } + } + None => Vector::default(), + }, + TractorbeamStatus::None => Vector::default(), + } + } + + fn toggle_pull(&mut self) { self.status = match self.status { TractorbeamStatus::None => TractorbeamStatus::Pull, TractorbeamStatus::Push => TractorbeamStatus::Pull, @@ -50,7 +72,7 @@ impl Tractorbeam { } } - pub fn toggle_push(&mut self) { + fn toggle_push(&mut self) { self.status = match self.status { TractorbeamStatus::None => TractorbeamStatus::Push, TractorbeamStatus::Pull => TractorbeamStatus::Push, @@ -59,7 +81,7 @@ impl Tractorbeam { } } - pub fn toggle_bring(&mut self, desired_distance: f64) { + fn toggle_bring(&mut self, desired_distance: f64) { self.desired_distance = Some(desired_distance); self.status = match self.status { TractorbeamStatus::None => TractorbeamStatus::Bring, @@ -68,33 +90,24 @@ impl Tractorbeam { TractorbeamStatus::Bring => TractorbeamStatus::None, } } +} - pub fn off(&mut self) { - self.status = TractorbeamStatus::None; - } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct TractorbeamClientData { + pub has_target: bool, + pub status: TractorbeamStatus, +} - pub fn get_acceleration(&self, position: Vector, target: Mass) -> Vector { - let acceleration = position.clone() - target.position.clone(); - match self.status { - TractorbeamStatus::Push => acceleration.unitize() * -0.05, - TractorbeamStatus::Pull => acceleration.unitize() * 0.05, - TractorbeamStatus::Bring => match self.desired_distance { - Some(desired_distance) => { - if desired_distance > position.distance_from(target.position) { - acceleration.unitize() * -0.05 - //some sort of velocity limiter - //if target.speed_torwards(ship) < 10.0 { - // acceleration.unitize() * -0.05 - //} else { - // Vector::default() - //} - } else { - acceleration.unitize() * 0.05 - } - } - None => Vector::default(), - }, - TractorbeamStatus::None => Vector::default(), - } +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum TractorbeamStatus { + None, + Push, + Pull, + Bring, +} + +impl Default for TractorbeamStatus { + fn default() -> Self { + TractorbeamStatus::None } } |