//! The `entity` module contains structures of all entities including players and enimies. use crate::{Direction, FPos, 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)] pub struct Entity { /// The fixed grid position of the entity pub pos: Pos, /// The floating (real) current position of the entity pub fpos: FPos, /// 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, } 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, ) -> Self { let fpos = FPos::from_pos(pos); Self { pos, fpos, 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)] pub struct Player { pub entity: Entity, pub inventory: Vec, } 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) } }