summaryrefslogtreecommitdiff
path: root/src/mass.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mass.rs')
-rw-r--r--src/mass.rs104
1 files changed, 84 insertions, 20 deletions
diff --git a/src/mass.rs b/src/mass.rs
index 5742f83..f13e8ce 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,27 +1,91 @@
-extern crate serde;
-
-use downcast::Any;
-
-pub trait Mass : Any {
- fn recv_mass_type(&self) -> MassType;
- fn position(&self) -> (f64, f64, f64);
- fn serialize(&self) -> String;
- fn process(&mut self);
- fn give_acceleration(&mut self, acceleration : (f64, f64, f64));
- fn recv_velocity(&self) -> (f64, f64, f64);
- fn box_clone(&self) -> Box<Mass>;
-}
+extern crate rand;
-impl Clone for Box<Mass> {
- fn clone(&self) -> Box<Mass> {
- self.box_clone()
- }
+use self::rand::distributions::Range;
+use self::rand::distributions::Sample;
+
+use storage::Storage;
+use module::ModuleType;
+use targeting::Targeting;
+use mining::Mining;
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Mass {
+ pub mass_type : MassType,
+ pub position : (f64, f64, f64),
+ pub velocity : (f64, f64, f64),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
- Ship,
- Astroid,
+ Ship {
+ modules : Vec<ModuleType>,
+ mining : Mining,
+ targeting : Targeting,
+ storage : Storage,
+ },
+ Astroid{
+ resources : Storage,
+ },
}
-downcast!(Mass);
+impl Mass {
+ pub fn new_astroid() -> Mass {
+ let mut rng = rand::thread_rng();
+
+ let mut pr = Range::new(-50.0, 50.0);
+ let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));
+
+ let mut vr = Range::new(-0.5, 0.5);
+ let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));
+
+ let astroid = MassType::Astroid {
+ resources : Storage::new(Vec::new()),
+ };
+
+ Mass {
+ mass_type : astroid,
+ position : position,
+ velocity : velocity,
+ }
+ }
+
+ pub fn new_ship() -> Mass {
+ let mut modules = Vec::new();
+
+ modules.push(ModuleType::Navigation);
+ modules.push(ModuleType::Engines);
+ modules.push(ModuleType::Dashboard);
+ modules.push(ModuleType::Mining);
+
+ let ship = MassType::Ship {
+ modules : modules,
+ mining : Mining::new(),
+ targeting : Targeting::new(),
+ storage : Storage::new(Vec::new()),
+ };
+
+ Mass {
+ mass_type : ship,
+ position : (0.0,0.0,0.0),
+ velocity : (0.0,0.0,0.0),
+ }
+ }
+
+ pub fn process(&mut self) {
+ self.position.0 += self.velocity.0;
+ self.position.1 += self.velocity.1;
+ self.position.2 += self.velocity.2;
+ match self.mass_type {
+ MassType::Ship{ref mut targeting, ..} => {
+ targeting.process();
+ },
+ _ => (),
+ }
+ }
+
+ pub fn accelerate(&mut self, acceleration : (f64, f64, f64)) {
+ self.velocity.0 += acceleration.0;
+ self.velocity.1 += acceleration.1;
+ self.velocity.2 += acceleration.2;
+ }
+}