From 39b16e379fb679aa56ded8ab5463569e5633a656 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 25 Jun 2018 08:36:57 -0500 Subject: added item mass, allow stopping of ship, if storage is full item goes into space --- src/storage.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/storage.rs') diff --git a/src/storage.rs b/src/storage.rs index 6aa8417..3c6e684 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -3,19 +3,25 @@ use item::Item; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Storage { items : Vec, + carrying : usize, capacity : usize, } impl Storage { pub fn new(items : Vec) -> Storage { + let mut carrying = 0; + for item in items.iter() { + carrying += item.size; + } Storage { items : items, - capacity : 100, + capacity : 10, + carrying : carrying, } } pub fn has_minerals(&self) -> bool { - match self.items.iter().position(|item| item.name == "Iron") { + match self.items.iter().position(|item| item.is_mineral()) { Some(_) => true, None => false, } @@ -28,7 +34,14 @@ impl Storage { } } - pub fn give(&mut self, item : Item) { - self.items.push(item); + pub fn give(&mut self, item : Item) -> bool { + match self.capacity >= self.carrying + item.size { + true => { + self.carrying += item.size; + self.items.push(item); + true + }, + false => false, + } } } -- cgit v1.2.3