From f617213b4a48d73acd245580f8551a7c37ce9ad8 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 10 Feb 2019 03:55:05 -0600 Subject: added vector math and tractorbeam module --- src/modules/tractorbeam.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/modules/tractorbeam.rs (limited to 'src/modules/tractorbeam.rs') diff --git a/src/modules/tractorbeam.rs b/src/modules/tractorbeam.rs new file mode 100644 index 0000000..1ddc7d0 --- /dev/null +++ b/src/modules/tractorbeam.rs @@ -0,0 +1,90 @@ +use crate::mass::Mass; +use crate::math::Vector; + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct Tractorbeam { + pub status: TractorbeamStatus, + strength: f64, + desired_distance: Option, +} + +#[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 { + status: TractorbeamStatus::None, + strength: 0.1, + desired_distance: None, + } + } + + pub fn toggle_pull(&mut self) { + self.status = match self.status { + TractorbeamStatus::None => TractorbeamStatus::Pull, + TractorbeamStatus::Push => TractorbeamStatus::Pull, + TractorbeamStatus::Bring => TractorbeamStatus::Pull, + TractorbeamStatus::Pull => TractorbeamStatus::None, + } + } + + pub fn toggle_push(&mut self) { + self.status = match self.status { + TractorbeamStatus::None => TractorbeamStatus::Push, + TractorbeamStatus::Pull => TractorbeamStatus::Push, + TractorbeamStatus::Bring => TractorbeamStatus::Push, + TractorbeamStatus::Push => TractorbeamStatus::None, + } + } + + pub fn toggle_bring(&mut self, desired_distance: f64) { + self.desired_distance = Some(desired_distance); + self.status = match self.status { + TractorbeamStatus::None => TractorbeamStatus::Bring, + TractorbeamStatus::Pull => TractorbeamStatus::Bring, + TractorbeamStatus::Push => TractorbeamStatus::Bring, + TractorbeamStatus::Bring => TractorbeamStatus::None, + } + } + + pub fn off(&mut self) { + self.status = TractorbeamStatus::None; + } + + pub fn get_acceleration(&self, ship: Mass, target: Mass) -> 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(), + } + } +} -- cgit v1.2.3