summaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-02-17 07:53:57 -0600
committertom barrett <spalf0@gmail.com>2019-02-17 07:53:57 -0600
commit892088d723fd3dc0aae969273331c2765f322e6f (patch)
tree1774b6c74e03c501d7b318406b05d53608d2f20d /src/modules
parentca26d538085704d3278063715a14308eeed70127 (diff)
gave engines a max acceleration
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/engines.rs53
1 files changed, 25 insertions, 28 deletions
diff --git a/src/modules/engines.rs b/src/modules/engines.rs
index ab6335a..007787c 100644
--- a/src/modules/engines.rs
+++ b/src/modules/engines.rs
@@ -2,18 +2,6 @@ use crate::constants;
use crate::mass::Mass;
use crate::math::Vector;
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub enum EnginesStatus {
- None,
- ApproachingTargetVelocity,
-}
-
-impl Default for EnginesStatus {
- fn default() -> Self {
- EnginesStatus::None
- }
-}
-
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Engines {
acceleration: Vector,
@@ -30,6 +18,15 @@ impl Engines {
}
}
+ 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 give_recv(
&mut self,
recv: String,
@@ -39,27 +36,24 @@ impl Engines {
) {
let mut acceleration = Vector::default();
match recv.as_str() {
- "5" => acceleration.x += 0.1,
- "0" => acceleration.x -= 0.1,
- "8" => acceleration.y += 0.1,
- "2" => acceleration.y -= 0.1,
- "4" => acceleration.z += 0.1,
- "6" => acceleration.z -= 0.1,
- "+" => acceleration = velocity * 0.05,
- "-" => {
- acceleration = velocity * -1.05;
- }
- "s" => {
- acceleration = velocity * -1.0;
- }
+ "5" => acceleration.x += constants::SHIP_ENGINES_ACCELERATION,
+ "0" => acceleration.x -= constants::SHIP_ENGINES_ACCELERATION,
+ "8" => acceleration.y += constants::SHIP_ENGINES_ACCELERATION,
+ "2" => acceleration.y -= constants::SHIP_ENGINES_ACCELERATION,
+ "4" => acceleration.z += constants::SHIP_ENGINES_ACCELERATION,
+ "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 {
- acceleration = target.velocity.clone() - velocity;
+ self.target_velocity = Some(target.velocity.clone());
}
}
"t" => {
if let Some(target) = target {
- acceleration = (target.position.clone() - position) * 0.01;
+ acceleration = (target.position.clone() - position).unitize()
+ * constants::SHIP_ENGINES_ACCELERATION;
}
}
_ => (),
@@ -68,9 +62,12 @@ impl Engines {
}
pub fn take_acceleration(&mut self) -> Vector {
- let acceleration = self.acceleration.clone();
+ let mut acceleration = self.acceleration.clone();
self.acceleration = Vector::default();
+ if acceleration.magnitude() >= constants::SHIP_ENGINES_ACCELERATION {
+ acceleration = acceleration.unitize() * constants::SHIP_ENGINES_ACCELERATION;
+ }
if self.fuel - acceleration.magnitude() >= 0.0 {
self.fuel -= acceleration.magnitude();
acceleration