From f6c9fbea8f535febaf51dc08c353e985aa73988d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 5 Jun 2018 02:21:04 -0500 Subject: frail implementation of mining --- src/item.rs | 2 +- src/mass.rs | 18 ++++++++++++++++-- src/modules/mining.rs | 36 ++++++++++++++++++++++++++++++++++-- src/server/mining.rs | 24 +++++++++++++++++++++++- src/storage.rs | 11 +++++++++++ 5 files changed, 85 insertions(+), 6 deletions(-) diff --git a/src/item.rs b/src/item.rs index 63e7b01..90c9ed8 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,6 +1,6 @@ #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Item { - name : String, + pub name : String, size : usize, } diff --git a/src/mass.rs b/src/mass.rs index 85651d4..4bb44b0 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -48,7 +48,6 @@ impl Mass { resources.push(Item::new("Iron", 1)); } - let astroid = MassType::Astroid { resources : Storage::new(resources), }; @@ -88,7 +87,8 @@ impl Mass { pub fn process(&mut self) { let mut acceleration = (0.0, 0.0, 0.0); match self.mass_type { - MassType::Ship{ref mut navigation, ref mut engines, ..} => { + MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ..} => { + mining.as_mut().unwrap().process(); navigation.as_mut().unwrap().process(); acceleration = engines.as_mut().unwrap().recv_acceleration(); }, @@ -105,4 +105,18 @@ impl Mass { self.velocity.1 += acceleration.1; self.velocity.2 += acceleration.2; } + + pub fn take(&mut self, name : &str) -> Option { + match self.mass_type { + MassType::Ship{ref mut storage, ..} => storage.take(name), + MassType::Astroid{ref mut resources, ..} => resources.take(name), + } + } + + pub fn give(&mut self, item : Item) { + match self.mass_type { + MassType::Ship{ref mut storage, ..} => storage.give(item), + MassType::Astroid{ref mut resources, ..} => resources.give(item), + } + } } diff --git a/src/modules/mining.rs b/src/modules/mining.rs index e22918f..7f11f05 100644 --- a/src/modules/mining.rs +++ b/src/modules/mining.rs @@ -6,6 +6,7 @@ pub struct Mining { pub status : bool, time : u64, start : Option, + pub ready : bool, } @@ -14,10 +15,41 @@ impl Mining { Mining { range : 10.0, status : false, - time : 1, + time : 5, start : None, + ready : false, } } - pub fn toggle(&self) {} + pub fn toggle(&mut self) { + self.status = !self.status; + self.start = match self.status { + true => Some(SystemTime::now()), + false => None, + }; + } + + pub fn off(&mut self) { + self.status = false; + } + + pub fn take(&mut self) { + self.ready = false; + } + + pub fn process(&mut self) { + match self.start.clone() { + Some(timer) => { + if timer.elapsed().unwrap().as_secs() > self.time { + self.start = Some(SystemTime::now()); + self.ready = true; + } + } + _ => (), + } + if !self.status { + self.start = None; + self.ready = false; + } + } } diff --git a/src/server/mining.rs b/src/server/mining.rs index c64f03e..91210f8 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -14,8 +14,8 @@ use server::connection::ServerConnection; pub struct MiningData { pub has_astroid_target : bool, pub is_within_range : bool, - pub range : f64, pub status : bool, + pub range : f64, } impl ServerConnection { @@ -23,6 +23,7 @@ impl ServerConnection { let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); let mut connection_good = true; + let mut item = None; if let MassType::Ship{ref mut mining, ref navigation, ..} = ship.mass_type { let mut mining = mining.as_mut().unwrap(); @@ -51,6 +52,27 @@ impl ServerConnection { } Err(_error) => (), } + + if !mining_data.is_within_range { + mining.off(); + } + else { + if mining.status && mining.ready { + mining.take(); + match navigation.target_name.clone() { + Some(name) => { + let target = masses.get_mut(&name).unwrap(); + item = target.take("Iron"); + } + _ => (), + } + } + } + } + + match item { + Some(item) => ship.give(item), + None => (), } masses.insert(self.name.clone(), ship); diff --git a/src/storage.rs b/src/storage.rs index 00dca8b..3e900b2 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -13,4 +13,15 @@ impl Storage { capacity : 100, } } + + pub fn take(&mut self, name : &str) -> Option { + match self.items.iter().position(|item| item.name == name) { + Some(index) => Some(self.items.remove(index)), + None => None, + } + } + + pub fn give(&mut self, item : Item) { + self.items.push(item); + } } -- cgit v1.2.3