summaryrefslogtreecommitdiff
path: root/dungeon/src/entity.rs
diff options
context:
space:
mode:
Diffstat (limited to 'dungeon/src/entity.rs')
-rw-r--r--dungeon/src/entity.rs206
1 files changed, 6 insertions, 200 deletions
diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs
index d6ca7e5..5b4ac38 100644
--- a/dungeon/src/entity.rs
+++ b/dungeon/src/entity.rs
@@ -1,26 +1,18 @@
//! The `entity` module contains structures of all entities including players and enimies.
-use std::{
- mem::take,
- time::{Duration, Instant},
-};
+use std::mem::take;
use rand::Rng;
use crate::{
- Dungeon, PlayerAction, astar, const_pos,
+ Dungeon, astar,
map::Floor,
+ player::Player,
player_input::PlayerInput,
pos::{Direction, FPos, Pos},
rng::DungeonRng,
};
-/// `PLAYER_INVENTORY_SIZE` is the maximum size of the inventory
-pub const PLAYER_INVENTORY_SIZE: u16 = 5;
-
-/// `PLAYER_INVENTORY_SIZE_USIZE` is the maximum size of the inventory
-pub const PLAYER_INVENTORY_SIZE_USIZE: usize = PLAYER_INVENTORY_SIZE as usize;
-
/// 'MIN_ROAM_DIST' and 'MAX_ROAM_DIST' are the enemy roam ranges
pub const MIN_ROAM_DIST: u16 = 1;
pub const MAX_ROAM_DIST: u16 = 4;
@@ -55,7 +47,7 @@ impl Item {
player.entity.heal(3);
}
Self::SpeedPotion => {
- player.potion_timer = Instant::now().checked_add(Duration::from_secs(5));
+ player.potion_timer.set_secs(5);
player.entity.speed = player.entity.speed.inc();
}
Self::Bomb => {
@@ -339,73 +331,8 @@ impl Entity {
}
}
-/// The current "weapon" level we have
-#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
-pub enum Weapon {
- RustyKnife,
- ShiningBlade,
- GodlyBlade,
-}
-impl Weapon {
- /// Returns the number of hit points of damage this weapon does
- #[must_use]
- pub const fn attack_dmg(self) -> u32 {
- match self {
- Self::RustyKnife => 2,
- Self::ShiningBlade => 3,
- Self::GodlyBlade => 5,
- }
- }
-}
-
-/// The `Player` type represents the main player entity
-#[derive(Clone, Debug, PartialEq)]
-pub struct Player {
- pub entity: Entity,
- pub inventory: Vec<Item>,
- pub weapon: Weapon,
- // How long until we reset potion effects?
- pub potion_timer: Option<Instant>,
- pub active_inv_slot: usize,
- pub last_drop_time: Instant,
-}
-impl Player {
- /// Instantiates the game player at a given `Pos`
- ///
- /// # Examples
- ///
- /// ```
- /// use dungeon::{pos::Pos, entity::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![];
- let weapon = Weapon::RustyKnife;
- let potion_timer = None;
- let active_inv_slot = 0;
- let last_drop_time = Instant::now();
- Self {
- entity,
- inventory,
- weapon,
- potion_timer,
- active_inv_slot,
- last_drop_time,
- }
- }
-}
-impl Default for Player {
- fn default() -> Self {
- let pos = const_pos!(1, 1);
- Self::new(pos)
- }
-}
-
struct Updater<'a> {
- floor: &'a mut Floor,
+ floor: &'a Floor,
rng: &'a mut DungeonRng,
player_pos: Pos,
input: PlayerInput,
@@ -540,104 +467,11 @@ impl Updater<'_> {
entity.dir = dir;
}
}
-
- /// Update player potion timer
- fn update_player_potion_effects(player: &mut Player) {
- if let Some(timer) = player.potion_timer
- && Instant::now() > timer
- {
- player.potion_timer = None;
- player.entity.speed = player.entity.kind.initial_speed();
- }
- }
-
- /// Handle player "Use Item"
- fn handle_player_use_item(&mut self, player: &mut Player, action: &mut PlayerAction) {
- if self.input.use_item && player.active_inv_slot < player.inventory.len() {
- let item = player.inventory.remove(player.active_inv_slot);
- action.potion = item.is_potion();
- action.bomb = item.is_bomb();
- item.consume(player, self.floor);
- }
- }
-
- /// Handle player "Drop Item"
- fn handle_player_drop_item(
- &self,
- player: &mut Player,
- action: &mut PlayerAction,
- entities: &mut Vec<Entity>,
- ) {
- if self.input.drop && player.active_inv_slot < player.inventory.len() {
- let item = player.inventory.remove(player.active_inv_slot);
- entities.push(Entity::new(
- player.entity.pos,
- Direction::East,
- EntityKind::Item(item),
- ));
- player.active_inv_slot = player
- .active_inv_slot
- .max(player.inventory.len().saturating_sub(1));
- action.drop_item = true;
- player.last_drop_time = Instant::now();
- }
- }
-
- /// Handle player "Attack"
- const fn handle_player_attack(&self, _player: &mut Player, action: &mut PlayerAction) {
- if self.input.attack {
- // TODO: attack
- action.attack = true;
- }
- }
-
- /// Handle player "Pickup Items"
- fn handle_player_pickup_items(
- player: &mut Player,
- action: &mut PlayerAction,
- entities: &mut Vec<Entity>,
- ) {
- if player.last_drop_time.elapsed() < Duration::from_secs(3) {
- // delay picking up dropped items
- return;
- }
-
- let mut idx = 0;
- loop {
- if idx >= entities.len() {
- break;
- }
- if entities[idx].fpos.abs_diff(player.entity.fpos).magnitude() >= 0.25 {
- idx += 1;
- continue;
- }
- let Some(item) = entities[idx].get_item() else {
- idx += 1;
- continue;
- };
- if player.inventory.len() < PLAYER_INVENTORY_SIZE_USIZE {
- entities.remove(idx);
- player.inventory.push(item);
- action.pickup_item = true;
- } else {
- idx += 1;
- }
- }
- }
-
- /// Handle player "Change INV Slot"
- const fn handle_player_inv_slot(&self, player: &mut Player) {
- if let Some(slot) = self.input.inv_slot
- && slot < PLAYER_INVENTORY_SIZE_USIZE
- {
- player.active_inv_slot = slot;
- }
- }
}
impl Dungeon {
pub(crate) fn update_entities(&mut self, input: PlayerInput, delta_time: f32) {
let mut updater = Updater {
- floor: &mut self.floor,
+ floor: &self.floor,
rng: &mut self.game_rng,
player_pos: self.player.entity.pos,
input,
@@ -654,32 +488,4 @@ impl Dungeon {
}
self.entities.retain(Entity::is_alive);
}
-
- pub(crate) fn update_player(
- &mut self,
- input: PlayerInput,
- delta_time: f32,
- ) -> PlayerAction {
- let mut updater = Updater {
- floor: &mut self.floor,
- rng: &mut self.game_rng,
- player_pos: self.player.entity.pos,
- input,
- delta_time,
- };
-
- let mut action = PlayerAction::default();
- Updater::update_player_potion_effects(&mut self.player);
- updater.handle_player_use_item(&mut self.player, &mut action);
- updater.handle_player_drop_item(&mut self.player, &mut action, &mut self.entities);
- Updater::handle_player_pickup_items(
- &mut self.player,
- &mut action,
- &mut self.entities,
- );
- updater.handle_player_attack(&mut self.player, &mut action);
- updater.handle_player_inv_slot(&mut self.player);
-
- action
- }
}