summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/server.rs2
-rw-r--r--src/mass.rs33
-rw-r--r--src/modules/engines.rs70
3 files changed, 57 insertions, 48 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 62da029..e8fa735 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -50,7 +50,7 @@ fn main() {
connection.open = connection.stream.write(send.as_bytes()).is_ok();
let recv = connection.receive();
- ship.give_received_data(connection.module_type.clone(), recv, &masses);
+ ship.give_received_data(connection.module_type.clone(), recv);
masses.insert(connection.name.clone(), ship);
}
diff --git a/src/mass.rs b/src/mass.rs
index 7cc7d70..3d90573 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -186,6 +186,12 @@ impl Mass {
masses.insert(target_name.to_string(), target);
}
+ let target = match &navigation.target_name {
+ Some(target_name) => masses.get(target_name),
+ None => None,
+ };
+
+ engines.process(self.position.clone(), self.velocity.clone(), target);
refinery.process(storage);
navigation.process(self.position.clone(), masses);
construction.process(
@@ -194,7 +200,6 @@ impl Mass {
masses,
storage,
);
- engines.process(self.velocity.clone());
self.effects.give_acceleration(engines.take_acceleration());
}
@@ -236,12 +241,7 @@ impl Mass {
}
}
- pub fn give_received_data(
- &mut self,
- module_type: ModuleType,
- recv: String,
- masses: &HashMap<String, Mass>,
- ) {
+ pub fn give_received_data(&mut self, module_type: ModuleType, recv: String) {
if let MassType::Ship {
ref mut navigation,
ref mut engines,
@@ -252,18 +252,9 @@ impl Mass {
..
} = self.mass_type
{
- let target = match &navigation.target_name {
- Some(target_name) => masses.get(target_name),
- None => None,
- };
match module_type {
ModuleType::Navigation => navigation.give_received_data(recv),
- ModuleType::Engines => engines.give_received_data(
- recv,
- self.position.clone(),
- self.velocity.clone(),
- target,
- ),
+ ModuleType::Engines => engines.give_received_data(recv, self.velocity.clone()),
ModuleType::Mining => mining.give_received_data(recv),
ModuleType::Construction => construction.give_received_data(recv),
ModuleType::Refinery => refinery.give_received_data(recv),
@@ -299,12 +290,8 @@ impl Mass {
pub fn item_count(&self, item_type: ItemType) -> usize {
match &self.mass_type {
- MassType::Ship {
- storage, ..
- } => storage.item_count(item_type),
- MassType::Astroid {
- resources, ..
- } => resources.item_count(item_type),
+ MassType::Ship { storage, .. } => storage.item_count(item_type),
+ MassType::Astroid { resources, .. } => resources.item_count(item_type),
_ => 0,
}
}
diff --git a/src/modules/engines.rs b/src/modules/engines.rs
index 313346a..ebf4e8b 100644
--- a/src/modules/engines.rs
+++ b/src/modules/engines.rs
@@ -5,6 +5,7 @@ use crate::modules::navigation;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Engines {
+ pub status: Status,
acceleration: Vector,
target_velocity: Option<Vector>,
pub fuel: f64,
@@ -13,28 +14,44 @@ pub struct Engines {
impl Engines {
pub fn new() -> Engines {
Engines {
+ status: Status::None,
acceleration: Vector::default(),
target_velocity: None,
fuel: constants::SHIP_ENGINES_FUEL_START,
}
}
- pub fn process(&mut self, velocity: Vector) {
- if let Some(target_velocity) = self.target_velocity.clone() {
- self.acceleration += target_velocity - velocity;
- if self.acceleration == Vector::default() {
- self.target_velocity = None
+ pub fn process(&mut self, position: Vector, velocity: Vector, target: Option<&Mass>) {
+ if self.target_velocity.is_none() && self.status != Status::None {
+ if self.status == Status::Stopping {
+ self.target_velocity = Some(Vector::default());
+ }
+ if let Some(target) = target {
+ match self.status {
+ Status::TowardsTarget => {
+ self.acceleration = (target.position.clone() - position).unitize()
+ * constants::SHIP_ENGINES_ACCELERATION;
+ self.status = Status::None;
+ }
+ Status::FollowingTarget => self.target_velocity = Some(target.velocity.clone()),
+ _ => (),
+ }
+ } else {
+ self.status = Status::None;
}
}
+ match self.target_velocity.clone() {
+ Some(target_velocity) => {
+ self.acceleration += target_velocity - velocity;
+ if self.acceleration == Vector::default() {
+ self.target_velocity = None;
+ }
+ }
+ None => (),
+ }
}
- pub fn give_received_data(
- &mut self,
- recv: String,
- position: Vector,
- velocity: Vector,
- target: Option<&Mass>,
- ) {
+ pub fn give_received_data(&mut self, recv: String, velocity: Vector) {
let mut acceleration = Vector::default();
match recv.as_str() {
"5" => acceleration.x += constants::SHIP_ENGINES_ACCELERATION,
@@ -45,18 +62,9 @@ impl Engines {
"6" => acceleration.z -= constants::SHIP_ENGINES_ACCELERATION,
"+" => acceleration = velocity.unitize() * constants::SHIP_ENGINES_ACCELERATION,
"-" => acceleration = velocity.unitize() * -1.0 * constants::SHIP_ENGINES_ACCELERATION,
- "s" => self.target_velocity = Some(Vector::default()),
- "c" => {
- if let Some(target) = target {
- self.target_velocity = Some(target.velocity.clone());
- }
- }
- "t" => {
- if let Some(target) = target {
- acceleration = (target.position.clone() - position).unitize()
- * constants::SHIP_ENGINES_ACCELERATION;
- }
- }
+ "s" => self.status = Status::Stopping,
+ "c" => self.status = Status::FollowingTarget,
+ "t" => self.status = Status::TowardsTarget,
_ => (),
}
self.acceleration = acceleration;
@@ -91,3 +99,17 @@ pub struct ClientData {
pub has_target: bool,
pub fuel: f64,
}
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
+pub enum Status {
+ None,
+ Stopping,
+ FollowingTarget,
+ TowardsTarget,
+}
+
+impl Default for Status {
+ fn default() -> Self {
+ Status::None
+ }
+}