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 | |
parent | 46fa862e04bc43311ba79ef3db70abf9014b9104 (diff) |
added vector math and tractorbeam module
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/engines.rs | 52 | ||||
-rw-r--r-- | src/modules/mod.rs | 1 | ||||
-rw-r--r-- | src/modules/navigation.rs | 10 | ||||
-rw-r--r-- | src/modules/tractorbeam.rs | 90 | ||||
-rw-r--r-- | src/modules/types.rs | 1 |
5 files changed, 113 insertions, 41 deletions
diff --git a/src/modules/engines.rs b/src/modules/engines.rs index a930847..f319ee5 100644 --- a/src/modules/engines.rs +++ b/src/modules/engines.rs @@ -1,64 +1,48 @@ use crate::mass::Mass; +use crate::math::Vector; #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Engines { - acceleration: (f64, f64, f64), + acceleration: Vector, } impl Engines { pub fn new() -> Engines { Engines { - acceleration: (0.0, 0.0, 0.0), + acceleration: Vector::default(), } } - pub fn recv_acceleration(&mut self) -> (f64, f64, f64) { - let acceleration = self.acceleration; - self.acceleration = (0.0, 0.0, 0.0); + pub fn recv_acceleration(&mut self) -> Vector { + let acceleration = self.acceleration.clone(); + self.acceleration = Vector::default(); acceleration } pub fn give_client_data(&mut self, ship: &Mass, target: Option<&Mass>, data: String) { - let mut acceleration = (0.0, 0.0, 0.0); + let mut acceleration = Vector::default(); match data.as_bytes() { - b"5\n" => acceleration.0 += 0.1, - b"0\n" => acceleration.0 -= 0.1, - b"8\n" => acceleration.1 += 0.1, - b"2\n" => acceleration.1 -= 0.1, - b"4\n" => acceleration.2 += 0.1, - b"6\n" => acceleration.2 -= 0.1, - b"+\n" => { - let m_v = ship.velocity; - acceleration = (m_v.0 * 0.05, m_v.1 * 0.05, m_v.2 * 0.05); - } + b"5\n" => acceleration.a += 0.1, + b"0\n" => acceleration.a -= 0.1, + b"8\n" => acceleration.b += 0.1, + b"2\n" => acceleration.b -= 0.1, + b"4\n" => acceleration.c += 0.1, + b"6\n" => acceleration.c -= 0.1, + b"+\n" => acceleration = ship.velocity.clone() * 0.05, b"-\n" => { - let m_v = ship.velocity; - acceleration = ( - -1.0 * m_v.0 * 0.05, - -1.0 * m_v.1 * 0.05, - -1.0 * m_v.2 * 0.05, - ); + acceleration = ship.velocity.clone() * -1.05; } b"s\n" => { - let m_v = ship.velocity; - acceleration = (-1.0 * m_v.0, -1.0 * m_v.1, -1.0 * m_v.2); + acceleration = ship.velocity.clone() * -1.0; } b"c\n" => { if let Some(target) = target { - let d_v = target.velocity; - let m_v = ship.velocity; - acceleration = (d_v.0 - m_v.0, d_v.1 - m_v.1, d_v.2 - m_v.2); + acceleration = target.velocity.clone() - ship.velocity.clone(); } } b"t\n" => { if let Some(target) = target { - let d_p = target.position; - let m_p = ship.position; - acceleration = ( - (d_p.0 - m_p.0) * 0.01, - (d_p.1 - m_p.1) * 0.01, - (d_p.2 - m_p.2) * 0.01, - ); + acceleration = (target.position.clone() - ship.position.clone()) * 0.01; } } _ => (), diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 3929e24..15c161b 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -4,4 +4,5 @@ pub mod engines; pub mod mining; pub mod navigation; pub mod refinery; +pub mod tractorbeam; pub mod types; diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs index 11f3e94..03a9ca5 100644 --- a/src/modules/navigation.rs +++ b/src/modules/navigation.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::time::SystemTime; use crate::mass::Mass; -use crate::math::distance; +use crate::math::Vector; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub enum NavigationStatus { @@ -52,14 +52,10 @@ impl Navigation { self.target_name = Some(target_name); } - pub fn verify_target( - &mut self, - ship_position: (f64, f64, f64), - masses: &HashMap<String, Mass>, - ) { + pub fn verify_target(&mut self, ship_position: Vector, masses: &HashMap<String, Mass>) { if let Some(name) = self.target_name.clone() { let target = masses.get(&name).unwrap(); - if distance(target.position, ship_position) > self.range { + if target.position.distance_from(ship_position) > self.range { self.target_name = None; self.status = NavigationStatus::None; } 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(), + } + } +} diff --git a/src/modules/types.rs b/src/modules/types.rs index 17e7c6c..91e41dc 100644 --- a/src/modules/types.rs +++ b/src/modules/types.rs @@ -5,5 +5,6 @@ pub enum ModuleType { Refinery, Dashboard, Navigation, + Tractorbeam, Construction, } |