diff options
author | tom barrett <spalf0@gmail.com> | 2018-06-25 08:36:57 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-06-25 08:36:57 -0500 |
commit | 39b16e379fb679aa56ded8ab5463569e5633a656 (patch) | |
tree | 446c1f2e609836c61109aa27400ac4bf2155c7dc /src/storage.rs | |
parent | 7094849bf164ff6f853c8f6812a0e831a66762f3 (diff) |
added item mass, allow stopping of ship, if storage is full item goes into space
Diffstat (limited to 'src/storage.rs')
-rw-r--r-- | src/storage.rs | 21 |
1 files changed, 17 insertions, 4 deletions
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<Item>, + carrying : usize, capacity : usize, } impl Storage { pub fn new(items : Vec<Item>) -> 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, + } } } |