diff options
Diffstat (limited to 'dungeon/src/entity.rs')
| -rw-r--r-- | dungeon/src/entity.rs | 112 |
1 files changed, 112 insertions, 0 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) + } +} |