summaryrefslogtreecommitdiff
path: root/src/modules/tractorbeam.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/tractorbeam.rs')
-rw-r--r--src/modules/tractorbeam.rs101
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
}
}