summaryrefslogtreecommitdiff
path: root/dungeon/src/player.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon/src/player.rs')
-rw-r--r--dungeon/src/player.rs39
1 files changed, 30 insertions, 9 deletions
diff --git a/dungeon/src/player.rs b/dungeon/src/player.rs
index 34b0ab1..d0f5344 100644
--- a/dungeon/src/player.rs
+++ b/dungeon/src/player.rs
@@ -3,7 +3,8 @@ use std::time::Instant;
use crate::{
Dungeon, const_pos,
entity::{Entity, EntityKind, Item},
- map::Floor,
+ map::{Floor, Tile},
+ msg::Message,
player_input::PlayerInput,
pos::{Direction, Pos},
};
@@ -116,16 +117,16 @@ impl Inventory {
std::mem::take(&mut self.inner[self.active])
}
- /// Attemps to pickup an item from the ground, returning the
- /// item if the inventory is full
- pub fn pickup(&mut self, item: Item) -> Option<Item> {
+ /// Attemps to pickup an item from the ground.
+ /// Returns if there was space for the item
+ pub fn pickup(&mut self, item: Item) -> bool {
for slot in &mut self.inner {
if slot.is_none() {
*slot = Some(item);
- return None;
+ return true;
}
}
- Some(item)
+ false
}
/// Returns an iterator over each inv slot, and if the slot is active
@@ -191,6 +192,7 @@ struct Updater<'a> {
player: &'a mut Player,
floor: &'a mut Floor,
entities: &'a mut Vec<Entity>,
+ msg: &'a mut Message,
input: PlayerInput,
action: PlayerAction,
}
@@ -219,13 +221,13 @@ impl Updater<'_> {
EntityKind::Item(item),
));
self.action.drop_item = true;
- self.player.pickup_timer.set_secs(5);
+ self.player.pickup_timer.set_secs(2);
}
}
/// Handle "Pickup Item" action
fn handle_pickup_item(&mut self) {
- if !self.player.pickup_timer.finished() || self.player.pickup_timer.is_set() {
+ if !self.player.pickup_timer.finished() && self.player.pickup_timer.is_set() {
return;
}
self.player.pickup_timer.reset();
@@ -247,7 +249,7 @@ impl Updater<'_> {
idx += 1;
continue;
};
- if self.player.inventory.pickup(item).is_none() {
+ if self.player.inventory.pickup(item) {
self.entities.remove(idx);
self.action.pickup_item = true;
} else {
@@ -264,6 +266,23 @@ impl Updater<'_> {
}
}
+ /// Handle "Player Interact" action
+ fn handle_interact(&mut self) {
+ if !self.input.interact {
+ return;
+ }
+
+ // check for chests
+ if let Tile::Chest(opt) = self.floor.get_mut(self.player.entity.pos)
+ && let Some(item) = *opt
+ && self.player.inventory.pickup(item)
+ {
+ *opt = None;
+ self.msg
+ .set_message(&format!("Woah a chest! You found a '{item}'!"));
+ }
+ }
+
/// Handle "Inv Slot" action
const fn handle_inv_slot(&mut self) {
if let Some(slot) = self.input.inv_slot {
@@ -278,6 +297,7 @@ impl Dungeon {
player: &mut self.player,
floor: &mut self.floor,
entities: &mut self.entities,
+ msg: &mut self.msg,
input,
action: PlayerAction::default(),
};
@@ -287,6 +307,7 @@ impl Dungeon {
updater.handle_drop_item();
updater.handle_pickup_item();
updater.handle_attack();
+ updater.handle_interact();
updater.handle_inv_slot();
updater.action