diff options
| author | Freya Murphy <freya@freyacat.org> | 2025-10-18 00:04:25 -0400 |
|---|---|---|
| committer | Freya Murphy <freya@freyacat.org> | 2025-10-18 00:04:48 -0400 |
| commit | 1be47bcbcbcfa3aa0e06342c6d1e9bb1540cbbd3 (patch) | |
| tree | 64e9b116d2b37139fd8e7dd4d598f2f254fba645 /dungeon | |
| parent | graphicsL add input functionality to (diff) | |
| download | DungeonCrawl-1be47bcbcbcfa3aa0e06342c6d1e9bb1540cbbd3.tar.gz DungeonCrawl-1be47bcbcbcfa3aa0e06342c6d1e9bb1540cbbd3.tar.bz2 DungeonCrawl-1be47bcbcbcfa3aa0e06342c6d1e9bb1540cbbd3.zip | |
dungeon: refactor into entity module, and add struct (better match proposal)
Diffstat (limited to 'dungeon')
| -rw-r--r-- | dungeon/src/entity.rs | 112 | ||||
| -rw-r--r-- | dungeon/src/lib.rs | 42 | ||||
| -rw-r--r-- | dungeon/src/map.rs | 88 |
3 files changed, 124 insertions, 118 deletions
diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs new file mode 100644 index 0000000..f997b99 --- /dev/null +++ b/dungeon/src/entity.rs @@ -0,0 +1,112 @@ +//! The `entity` module contains structures of all entities including players and enimies. + +use crate::{Direction, Pos, const_pos}; + +/// The `Item` type represents any item an entity may be using +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum Item { + Potion { heal: u32 }, + Weapon { atack: u32 }, + Armor { defense: u32 }, +} + +/// The `EntityKind` represents what kind of entity this is. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum EntityKind { + /// The main player + Player, + /// An item (not in an inventory) on the floor of the dungeon + Item(Item), +} + +/// The `Entity` kind represents the main player, or any other +/// ai autonomous character that can move freely across the +/// dungeon. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub struct Entity { + /// The fixed grid position of the entity + pub pos: Pos, + /// Which direction this entity is facing + pub dir: Direction, + /// Which kind this entity is (along with entity kind specific data) + pub kind: EntityKind, + /// The amount of health this entity has (None if this Entity does not use health) + pub health: Option<u32>, +} +impl Entity { + /// Creates a new `Entity` at a given `Pos`, `Direction`, and `EntityKind`. + /// + /// # Examples + /// + /// ``` + /// use dungeon::{Pos, Direction, Entity, EntityKind}; + /// + /// let pos = Pos::new(0, 0).unwrap(); + /// let dir = Direction::North; + /// let kind = EntityKind::Player; + /// let health = Some(100); + /// let entity = Entity::new(pos, dir, kind, health); + /// ``` + #[must_use] + pub const fn new( + pos: Pos, + dir: Direction, + kind: EntityKind, + health: Option<u32>, + ) -> Self { + Self { + pos, + dir, + kind, + health, + } + } + + /// Creates the Player version of the `Entity` + /// + /// # Examples + /// + /// ``` + /// use dungeon::{Pos, Entity}; + /// + /// let pos = Pos::new(0, 0).unwrap(); + /// let player = Entity::player(pos); + /// ``` + #[must_use] + pub const fn player(pos: Pos) -> Self { + let dir = Direction::East; + let kind = EntityKind::Player; + let health = Some(100); + Self::new(pos, dir, kind, health) + } +} + +/// The `Player` type represents the main player entity +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct Player { + pub entity: Entity, + pub inventory: Vec<Item>, +} +impl Player { + /// Instantiates the game player at a given `Pos` + /// + /// # Examples + /// + /// ``` + /// use dungeon::{Pos, Player}; + /// + /// let pos = Pos::new(1, 2).unwrap(); + /// let player = Player::new(pos); + /// ``` + pub fn new(pos: Pos) -> Self { + let entity = Entity::player(pos); + let inventory = vec![]; + Self { entity, inventory } + } +} +impl Default for Player { + fn default() -> Self { + let pos = const_pos!(1, 1); + Self::new(pos) + } +} diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index cc15455..0a04ca7 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -1,9 +1,11 @@ //! The `dungon` crate contains the core functionality for //! interacting with a `Dungeon` and its components. +mod entity; mod map; mod pos; +pub use entity::*; pub use map::*; pub use pos::*; @@ -11,8 +13,8 @@ pub use pos::*; /// dungeon crawler. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Dungeon { - player: Entity, - floor: Floor, + pub floor: Floor, + pub player: Player, } impl Dungeon { /// Creates a new `Dungeon`. @@ -27,9 +29,9 @@ impl Dungeon { #[must_use] pub fn new() -> Self { let floor = Floor::generate(); - let player = Entity::player(floor.player_start()); + let player = Player::new(floor.player_start()); - Self { player, floor } + Self { floor, player } } /// Creates a new `Dungeon` with a provided seed. @@ -45,41 +47,17 @@ impl Dungeon { #[must_use] pub fn new_seeded(seed: u64) -> Self { let floor = Floor::generate_seeded(seed); - let player = Entity::player(floor.player_start()); + let player = Player::new(floor.player_start()); - Self { player, floor } - } - - /// Returns a reference to the player - #[must_use] - pub fn player(&self) -> &Entity { - &self.player - } - - /// Returns a mutable reference to the player - #[must_use] - pub fn player_mut(&mut self) -> &mut Entity { - &mut self.player - } - - /// Returns a reference to the current floor - #[must_use] - pub fn floor(&self) -> &Floor { - &self.floor - } - - /// Returns a mutable reference to the current floor - #[must_use] - pub fn floor_mut(&mut self) -> &mut Floor { - &mut self.floor + Self { floor, player } } } impl Default for Dungeon { fn default() -> Self { let floor = Floor::default(); - let player = Entity::player(floor.player_start()); + let player = Player::default(); - Self { player, floor } + Self { floor, player } } } diff --git a/dungeon/src/map.rs b/dungeon/src/map.rs index 97bb36c..f962180 100644 --- a/dungeon/src/map.rs +++ b/dungeon/src/map.rs @@ -1,10 +1,7 @@ //! The `map` module contains structures of the dungeon game map -//! including the current `Floor`, map `Tile`, and `Entity`. +//! including the current `Floor`, and map `Tile`. -use crate::{ - const_pos, - pos::{Direction, Pos}, -}; +use crate::{const_pos, pos::Pos}; /// `MAP_SIZE` is the size of the size of the dungeon grid. pub const MAP_SIZE: u16 = 100; @@ -15,87 +12,6 @@ pub const MAP_SIZE_USIZE: usize = MAP_SIZE as usize; /// The number of tiles in the dungeon grid pub const TILE_COUNT: usize = MAP_SIZE_USIZE * MAP_SIZE_USIZE; -/// The `EntityKind` represents what kind of entity this is. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub enum EntityKind { - Player, -} - -/// The `Entity` kind represents the main player, or any other -/// ai autonomous character that can move freely across the -/// dungeon. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] -pub struct Entity { - pos: Pos, - dir: Direction, - kind: EntityKind, -} -impl Entity { - /// Creates a new `Entity` at a given `Pos`, `Direction`, and `EntityKind`. - /// - /// # Examples - /// - /// ``` - /// use dungeon::{Pos, Direction, Entity, EntityKind}; - /// - /// let pos = Pos::new(0, 0).unwrap(); - /// let dir = Direction::North; - /// let kind = EntityKind::Player; - /// let entity = Entity::new(pos, dir, kind); - /// ``` - #[must_use] - pub const fn new(pos: Pos, dir: Direction, kind: EntityKind) -> Self { - Self { pos, dir, kind } - } - - /// Creates the Player version of the `Entity` - /// - /// # Examples - /// - /// ``` - /// use dungeon::{Pos, Entity}; - /// - /// let pos = Pos::new(0, 0).unwrap(); - /// let player = Entity::player(pos); - /// ``` - #[must_use] - pub const fn player(pos: Pos) -> Self { - let dir = Direction::East; - let kind = EntityKind::Player; - Self::new(pos, dir, kind) - } - - /// Returns the `Pos` of the entity - #[must_use] - pub const fn pos(&self) -> Pos { - self.pos - } - - /// Returns a mutable referense to the `Pos` of the entity - #[must_use] - pub const fn pos_mut(&mut self) -> &mut Pos { - &mut self.pos - } - - /// Returns the `Direction` of the entity - #[must_use] - pub const fn dir(&self) -> Direction { - self.dir - } - - /// Returns a mutable referense to the `Direction` of the entity - #[must_use] - pub const fn dir_mut(&mut self) -> &mut Direction { - &mut self.dir - } - - /// Returns the `EntityKind` of this entity - #[must_use] - pub const fn kind(&self) -> EntityKind { - self.kind - } -} - /// The `Tile` enum represents what is (or is not) at /// any given spot in the dungeon grid. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |