summaryrefslogtreecommitdiff
path: root/dungeon
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon')
-rw-r--r--dungeon/src/entity.rs13
-rw-r--r--dungeon/src/map.rs5
-rw-r--r--dungeon/src/player.rs39
3 files changed, 46 insertions, 11 deletions
diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs
index 5b4ac38..22ac3e5 100644
--- a/dungeon/src/entity.rs
+++ b/dungeon/src/entity.rs
@@ -1,6 +1,6 @@
//! The `entity` module contains structures of all entities including players and enimies.
-use std::mem::take;
+use std::{fmt::Display, mem::take};
use rand::Rng;
@@ -71,6 +71,17 @@ impl Item {
matches!(self, Self::HealthPotion | Self::SpeedPotion)
}
}
+impl Display for Item {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ match self {
+ Self::HeartFragment => f.write_str("Heart Fragment"),
+ Self::HealthPotion => f.write_str("Health Potion"),
+ Self::SpeedPotion => f.write_str("Speed Potion"),
+ Self::Bomb => f.write_str("Bomb"),
+ Self::LargeBomb => f.write_str("Large Bomb"),
+ }
+ }
+}
/// Different speed entities can move
#[repr(u8)]
diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs
index ba3e143..f19157b 100644
--- a/dungeon/src/map.rs
+++ b/dungeon/src/map.rs
@@ -45,7 +45,10 @@ impl Tile {
/// Returns if the tile is walkable
#[must_use]
pub const fn is_walkable(self) -> bool {
- matches!(self, Self::Room | Self::Hallway | Self::Stairs)
+ matches!(
+ self,
+ Self::Room | Self::Hallway | Self::Stairs | Self::Chest(_)
+ )
}
/// Returns if the tile is blast resistant
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