diff options
| author | Ryan Symons <47405201+rsymons22@users.noreply.github.com> | 2025-11-05 16:08:47 -0500 |
|---|---|---|
| committer | Ryan Symons <47405201+rsymons22@users.noreply.github.com> | 2025-11-05 16:08:47 -0500 |
| commit | 5ebb24a3ce8ae5de125204490cef2eddace10897 (patch) | |
| tree | be67815fcd82e4c49e1bebed83a6cc632785a927 /dungeon/src | |
| parent | Added enemy code structure and test movement (diff) | |
| download | DungeonCrawl-5ebb24a3ce8ae5de125204490cef2eddace10897.tar.gz DungeonCrawl-5ebb24a3ce8ae5de125204490cef2eddace10897.tar.bz2 DungeonCrawl-5ebb24a3ce8ae5de125204490cef2eddace10897.zip | |
Moved most of the movement stuff to entity.
Diffstat (limited to 'dungeon/src')
| -rw-r--r-- | dungeon/src/enemy.rs | 58 | ||||
| -rw-r--r-- | dungeon/src/entity.rs | 37 |
2 files changed, 38 insertions, 57 deletions
diff --git a/dungeon/src/enemy.rs b/dungeon/src/enemy.rs index 8c818f5..4742bef 100644 --- a/dungeon/src/enemy.rs +++ b/dungeon/src/enemy.rs @@ -1,20 +1,8 @@ -use crate::{Direction, Entity, FPos, Pos}; +use crate::{Direction, Entity, EntityMoveSpeed, FPos, Pos}; pub const ZOMBIE_HEALTH: u32 = 50; #[derive(Clone, Debug, PartialEq)] -pub enum EnemyMoveSpeed { - Slow, - Medium, - Fast, -} - -/// Every (value) seconds, an enemy with a (speed) can move one tile -pub const SLOW_SPEED_SEC_PER_TILE: f32 = 1.; -pub const MEDIUM_SPEED_SEC_PER_TILE: f32 = 0.5; -pub const FAST_SPEED_SEC_PER_TILE: f32 = 0.25; - -#[derive(Clone, Debug, PartialEq)] pub enum EnemyAttackType { Melee, Ranged, @@ -29,7 +17,6 @@ pub enum EnemyType { pub struct Enemy { pub entity: Entity, pub enemy_type: EnemyType, - pub move_speed: EnemyMoveSpeed, pub attack_type: EnemyAttackType, time_acc: f32, } @@ -44,15 +31,14 @@ impl Enemy { fn _new( pos: Pos, health: u32, + move_speed: EntityMoveSpeed, enemy_type: EnemyType, - move_speed: EnemyMoveSpeed, attack_type: EnemyAttackType, ) -> Self { - let entity = Entity::enemy(pos, health); + let entity = Entity::enemy(pos, move_speed, health); Self { entity, enemy_type, - move_speed, attack_type, time_acc: 0., } @@ -62,45 +48,9 @@ impl Enemy { Self::_new( pos, ZOMBIE_HEALTH, + EntityMoveSpeed::Slow, EnemyType::Zombie, - EnemyMoveSpeed::Slow, EnemyAttackType::Melee, ) } - - fn is_there(&self) -> bool { - self.time_acc - >= match &self.move_speed { - EnemyMoveSpeed::Slow => SLOW_SPEED_SEC_PER_TILE, - EnemyMoveSpeed::Medium => MEDIUM_SPEED_SEC_PER_TILE, - EnemyMoveSpeed::Fast => FAST_SPEED_SEC_PER_TILE, - } - } - - /// - /// Rough concept for enemy "AI": - /// - /// State: Roam/Patrol - /// Moves around in a small area (2x2, 3x3, 4x4) randomly choosing direction and how much to move. - /// State: Attack - /// Sees player in facing direction (only in box/rectangle in dir) - /// (ex. facing north, if player is anywhere above monster and theres no walls in the way, attack) - /// Do A* pathing to player until enemy dead or player dead or floor change - /// - /// Current implementation below is just a test - /// - pub fn update(&mut self, player_pos: Pos, time_since_last_frame: f32) { - if self.entity.pos == player_pos { - return; - } - - self.time_acc += time_since_last_frame; - if self.is_there() { - let temp_pos = self.entity.pos.step(Direction::West).unwrap(); - self.entity.pos = temp_pos; - self.entity.fpos = FPos::from_pos(temp_pos); - - self.time_acc = 0.; - } - } } diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs index 5f0425f..685e519 100644 --- a/dungeon/src/entity.rs +++ b/dungeon/src/entity.rs @@ -26,6 +26,23 @@ pub enum EntityKind { Item(Item), } +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum EntityMoveSpeed { + Slow, + Medium, + Fast, +} +impl EntityMoveSpeed { + /// Returns value in tiles/second + pub fn value(&self) -> f32 { + match &self { + EntityMoveSpeed::Slow => 1., + EntityMoveSpeed::Medium => 2., + EntityMoveSpeed::Fast => 3., + } + } +} + /// The `Entity` kind represents the main player, or any other /// ai autonomous character that can move freely across the /// dungeon. @@ -39,6 +56,8 @@ pub struct Entity { pub dir: Direction, /// Which kind this entity is (along with entity kind specific data) pub kind: EntityKind, + /// Move speed of this entity + pub move_speed: EntityMoveSpeed, /// The amount of health this entity has (None if this Entity does not use health) pub health: Option<u32>, } @@ -61,6 +80,7 @@ impl Entity { pos: Pos, dir: Direction, kind: EntityKind, + move_speed: EntityMoveSpeed, health: Option<u32>, ) -> Self { let fpos = FPos::from_pos(pos); @@ -69,6 +89,7 @@ impl Entity { fpos, dir, kind, + move_speed, health, } } @@ -87,14 +108,24 @@ impl Entity { pub const fn player(pos: Pos) -> Self { let dir = Direction::East; let kind = EntityKind::Player; + let move_speed = EntityMoveSpeed::Medium; let health = Some(PLAYER_FULL_HEALTH); - Self::new(pos, dir, kind, health) + Self::new(pos, dir, kind, move_speed, health) } - pub const fn enemy(pos: Pos, health: u32) -> Self { + pub const fn enemy(pos: Pos, move_speed: EntityMoveSpeed, health: u32) -> Self { let dir = Direction::East; let kind = EntityKind::Enemy; - Self::new(pos, dir, kind, Some(health)) + Self::new(pos, dir, kind, move_speed, Some(health)) + } + + pub fn move_by_dir(&mut self, dir: Direction, delta_time: f32) { + if let Some(fp) = self + .fpos + .step_by(dir, delta_time * &self.move_speed.value()) + { + self.fpos = fp; + } } } |