diff options
| author | Yusuf Elsharawy <yusufse-2004@hotmail.com> | 2025-11-10 20:05:46 +0000 |
|---|---|---|
| committer | Yusuf Elsharawy <yusufse-2004@hotmail.com> | 2025-11-10 20:10:38 +0000 |
| commit | b1c7a57e72ab7359191249d76ec84d6cae524e2a (patch) | |
| tree | c533911f87b1641d893de5ab68ed05c2558b1cba /dungeon/src/lib.rs | |
| parent | graphics: decouple sdl & wayland (diff) | |
| download | DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.tar.gz DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.tar.bz2 DungeonCrawl-b1c7a57e72ab7359191249d76ec84d6cae524e2a.zip | |
Refactored some code, implemented player movement
Diffstat (limited to 'dungeon/src/lib.rs')
| -rw-r--r-- | dungeon/src/lib.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/dungeon/src/lib.rs b/dungeon/src/lib.rs index cfd2fbe..399807c 100644 --- a/dungeon/src/lib.rs +++ b/dungeon/src/lib.rs @@ -5,11 +5,13 @@ pub mod astar; pub mod bsp; pub mod entity; pub mod map; +pub mod player_input; pub mod pos; pub use bsp::*; pub use entity::*; pub use map::*; +pub use player_input::*; pub use pos::*; /// The `Dungeon` type represents the game state of the @@ -55,6 +57,51 @@ impl Dungeon { pub fn camera(&self) -> FPos { self.player.entity.fpos } + + pub fn update(&mut self, player_input: PlayerInput, delta_time: f32) { + self.act_player(player_input, delta_time); + self.act_non_players(delta_time); + } + + fn act_player(&mut self, player_input: PlayerInput, delta_time: f32) { + let mut move_distance = self.player.entity.kind.move_speed() * delta_time; + // having this be a loop is a *little* unnecessary, + // but is technically more correct if the entity is fast enough + // to move more than one tile in a single frame + // (plus, it was easier to write if i thought of this like a state machine) + loop { + match (self.player.moving_to, player_input.direction) { + (Some(pos), _) => { + move_distance -= self + .player + .entity + .fpos + .move_towards_manhattan(pos.into(), move_distance); + if move_distance == 0.0 { + // can't move any further + break; + } + // otherwise, reached `pos` + self.player.entity.pos = pos; + self.player.moving_to = None; + // continue moving + } + (None, Some(dir)) => { + // set direction & find out next position + self.player.entity.dir = dir; + self.player.moving_to = self.player.entity.pos.step(dir); + } + (None, None) => break, + } + } + // TODO: reuse a similar structure across all entities + } + + fn act_non_players(&mut self, delta_time: f32) { + for enemy in self.enemies.iter_mut() { + enemy.handle_movement(self.player.entity.pos, &mut self.floor, delta_time); + } + } } impl Default for Dungeon { fn default() -> Self { |