diff options
| -rw-r--r-- | assets/atlas.bmp | bin | 16522 -> 16522 bytes | |||
| -rw-r--r-- | dungeon/src/entity.rs | 13 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 5 | ||||
| -rw-r--r-- | dungeon/src/player.rs | 39 | ||||
| -rw-r--r-- | game/src/lib.rs | 7 | ||||
| -rw-r--r-- | graphics/src/render.rs | 7 |
6 files changed, 55 insertions, 16 deletions
diff --git a/assets/atlas.bmp b/assets/atlas.bmp Binary files differindex 3ec946b..3f7c4eb 100644 --- a/assets/atlas.bmp +++ b/assets/atlas.bmp 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 diff --git a/game/src/lib.rs b/game/src/lib.rs index 23cdcfa..3d9c8a7 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -1,6 +1,6 @@ use dungeon::{ - Dungeon, UpdateResult, player::PLAYER_INVENTORY_SIZE_USIZE, player_input::PlayerInput, - pos::Direction, + Dungeon, UpdateResult, entity::Item, map::Tile, player::PLAYER_INVENTORY_SIZE_USIZE, + player_input::PlayerInput, pos::Direction, }; use graphics::{Key, Window}; @@ -86,7 +86,8 @@ impl Game { self.window.toggle_debug(); } if self.window.is_key_pressed(Key::F4) { - self.dungeon.player.entity.health = 0; + *self.dungeon.floor.get_mut(self.dungeon.player.entity.pos) = + Tile::Chest(Some(Item::Bomb)); } // Update game state diff --git a/graphics/src/render.rs b/graphics/src/render.rs index 762827c..fe8ff92 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -102,6 +102,8 @@ const ATLAS_FLOOR: (u16, u16) = (1, 0); const ATLAS_STAIR: (u16, u16) = (2, 0); const ATLAS_WALL_TOP: (u16, u16) = (3, 0); const ATLAS_WALL_EDGE: (u16, u16) = (0, 1); +const ATLAS_CHEST_CLOSED: (u16, u16) = (0, 3); +const ATLAS_CHEST_OPEN: (u16, u16) = (1, 3); // UI atlas.bmp textures const ATLAS_INV_CONTAINER: (u16, u16) = (1, 1); @@ -115,7 +117,7 @@ const ATLAS_FLOOR_EXT3: (u16, u16) = (2, 2); const ATLAS_FLOOR_EXT4: (u16, u16) = (3, 2); // Misc atlas.bmp textures -const ATLAS_ERROR: (u16, u16) = (3, 3); +//const ATLAS_ERROR: (u16, u16) = (3, 3); /// Full source rec for any texture const FULL_SOURCE_REC: Rectangle = rect! { @@ -401,7 +403,8 @@ impl Renderer { Tile::Wall => ATLAS_WALL_SIDE, Tile::Room | Tile::Hallway => ATLAS_FLOOR, Tile::Stairs => ATLAS_STAIR, - Tile::Chest(_) => ATLAS_ERROR, + Tile::Chest(Some(_)) => ATLAS_CHEST_CLOSED, + Tile::Chest(None) => ATLAS_CHEST_OPEN, }; rt.draw_atlas( &self.textures.atlas, |