diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-10 03:55:05 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-10 03:55:05 -0600 |
commit | f617213b4a48d73acd245580f8551a7c37ce9ad8 (patch) | |
tree | 46ab5a696d91d6405ff2d2875a57c403b94edfbd /src/modules/tractorbeam.rs | |
parent | 46fa862e04bc43311ba79ef3db70abf9014b9104 (diff) |
added vector math and tractorbeam module
Diffstat (limited to 'src/modules/tractorbeam.rs')
-rw-r--r-- | src/modules/tractorbeam.rs | 90 |
1 files changed, 90 insertions, 0 deletions
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<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 { + 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(), + } + } +} |