From f617213b4a48d73acd245580f8551a7c37ce9ad8 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 10 Feb 2019 03:55:05 -0600 Subject: added vector math and tractorbeam module --- src/mass.rs | 87 +++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 34 deletions(-) (limited to 'src/mass.rs') diff --git a/src/mass.rs b/src/mass.rs index db97d49..14b0c83 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -4,20 +4,46 @@ use self::rand::distributions::Uniform; use self::rand::Rng; use crate::item::Item; +use crate::math::Vector; use crate::modules::construction::Construction; use crate::modules::dashboard::Dashboard; use crate::modules::engines::Engines; use crate::modules::mining::Mining; use crate::modules::navigation::Navigation; use crate::modules::refinery::Refinery; +use crate::modules::tractorbeam::Tractorbeam; use crate::modules::types::ModuleType; use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Mass { pub mass_type: MassType, - pub position: (f64, f64, f64), - pub velocity: (f64, f64, f64), + pub position: Vector, + pub velocity: Vector, + pub effects: Effects, +} + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct Effects { + acceleration: Vector, +} + +impl Effects { + pub fn new() -> Effects { + Effects { + acceleration: Vector::default(), + } + } + + pub fn give_acceleration(&mut self, acceleration: Vector) { + self.acceleration += acceleration; + } + + pub fn take_acceleration(&mut self) -> Vector { + let acceleration = self.acceleration.clone(); + self.acceleration = Vector::default(); + acceleration + } } #[derive(Serialize, Deserialize, Debug, Clone)] @@ -29,6 +55,7 @@ pub enum MassType { refinery: Option, dashboard: Option, navigation: Option, + tractorbeam: Option, construction: Option, }, Astroid { @@ -47,18 +74,7 @@ impl Mass { let mut rng = rand::thread_rng(); let p_range = Uniform::new(-50.0, 50.0); - let position = ( - rng.sample(p_range), - rng.sample(p_range), - rng.sample(p_range), - ); - let v_range = Uniform::new(-0.5, 0.5); - let velocity = ( - rng.sample(v_range), - rng.sample(v_range), - rng.sample(v_range), - ); let mut resources = Vec::new(); for _ in 0..rng.gen_range(0, 20) { @@ -71,8 +87,17 @@ impl Mass { Mass { mass_type: astroid, - position, - velocity, + position: Vector::new(( + rng.sample(p_range), + rng.sample(p_range), + rng.sample(p_range), + )), + velocity: Vector::new(( + rng.sample(v_range), + rng.sample(v_range), + rng.sample(v_range), + )), + effects: Effects::new(), } } @@ -83,36 +108,36 @@ impl Mass { refinery: Some(Refinery::new()), dashboard: Some(Dashboard::new()), navigation: Some(Navigation::new()), + tractorbeam: Some(Tractorbeam::new()), construction: Some(Construction::new()), storage: Storage::new(Vec::new()), }; Mass { mass_type: ship, - position: (0.0, 0.0, 0.0), - velocity: (0.0, 0.0, 0.0), + position: Vector::default(), + velocity: Vector::default(), + effects: Effects::new(), } } - pub fn new_item(item: Item, position: (f64, f64, f64), velocity: (f64, f64, f64)) -> Mass { + pub fn new_item(item: Item, position: Vector, velocity: Vector) -> Mass { Mass { mass_type: MassType::Item { item }, position, velocity, + effects: Effects::new(), } } - pub fn new_station( - module_type: ModuleType, - position: (f64, f64, f64), - velocity: (f64, f64, f64), - ) -> Mass { + pub fn new_station(module_type: ModuleType, position: Vector, velocity: Vector) -> Mass { let mass_type = MassType::Station { module_type }; Mass { mass_type, position, velocity, + effects: Effects::new(), } } @@ -123,12 +148,12 @@ impl Mass { modules.push(ModuleType::Refinery); modules.push(ModuleType::Dashboard); modules.push(ModuleType::Navigation); + modules.push(ModuleType::Tractorbeam); modules.push(ModuleType::Construction); modules } pub fn process(&mut self) { - let mut acceleration = (0.0, 0.0, 0.0); if let MassType::Ship { ref mut navigation, ref mut engines, @@ -142,18 +167,12 @@ impl Mass { refinery.as_mut().unwrap().process(); navigation.as_mut().unwrap().process(); construction.as_mut().unwrap().process(); - acceleration = engines.as_mut().unwrap().recv_acceleration(); + self.effects + .give_acceleration(engines.as_mut().unwrap().recv_acceleration()) } - self.accelerate(acceleration); - self.position.0 += self.velocity.0; - self.position.1 += self.velocity.1; - self.position.2 += self.velocity.2; - } - pub fn accelerate(&mut self, acceleration: (f64, f64, f64)) { - self.velocity.0 += acceleration.0; - self.velocity.1 += acceleration.1; - self.velocity.2 += acceleration.2; + self.velocity += self.effects.take_acceleration(); + self.position += self.velocity.clone(); } pub fn has_minerals(&self) -> bool { -- cgit v1.2.3