diff options
Diffstat (limited to 'dungeon/src/entity.rs')
| -rw-r--r-- | dungeon/src/entity.rs | 64 |
1 files changed, 28 insertions, 36 deletions
diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs index c84fc42..762355e 100644 --- a/dungeon/src/entity.rs +++ b/dungeon/src/entity.rs @@ -35,7 +35,7 @@ impl EntityKind { // Tiles/s pub fn move_speed(&self) -> f32 { match &self { - Self::Player => 2., + Self::Player => 5., Self::Zombie(_) => 4., _ => 0., } @@ -252,50 +252,38 @@ impl Entity { } fn movement_helper( - entity: &mut Self, - next_move: Pos, + &mut self, + mut next_move: Pos, moves: &mut Vec<Pos>, delta_time: f32, ) { - if entity.pos.y() > next_move.y() { - entity.move_by_dir(Direction::North, delta_time); - if entity.fpos.y() <= next_move.y() as f32 { - if let Some(p) = moves.pop() { - entity.pos = p; - entity.fpos = FPos::from_pos(p); - } - } - } else if entity.pos.y() < next_move.y() { - entity.move_by_dir(Direction::South, delta_time); - if entity.fpos.y() >= next_move.y() as f32 { - if let Some(p) = moves.pop() { - entity.pos = p; - entity.fpos = FPos::from_pos(p); - } - } - } else if entity.pos.x() < next_move.x() { - entity.move_by_dir(Direction::East, delta_time); - if entity.fpos.x() >= next_move.x() as f32 { - if let Some(p) = moves.pop() { - entity.pos = p; - entity.fpos = FPos::from_pos(p); - } - } - } else { - entity.move_by_dir(Direction::West, delta_time); - if entity.fpos.x() <= next_move.x() as f32 { - if let Some(p) = moves.pop() { - entity.pos = p; - entity.fpos = FPos::from_pos(p); - } + let mut move_distance = self.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 + loop { + move_distance -= self + .fpos + .move_towards_manhattan(FPos::from(next_move), move_distance); + if move_distance == 0.0 { + // can't move any further + break; } - }; + // otherwise, we reached `next_move`, set position & pop from vec + self.pos = next_move; + let _ = moves.pop(); + + // there is likely more distance to travel + let Some(last) = moves.last() else { break }; + next_move = *last; + } } } /// The `Player` type represents the main player entity #[derive(Clone, Debug, PartialEq)] pub struct Player { + pub moving_to: Option<Pos>, pub entity: Entity, pub inventory: Vec<Item>, } @@ -313,7 +301,11 @@ impl Player { pub fn new(pos: Pos) -> Self { let entity = Entity::player(pos); let inventory = vec![]; - Self { entity, inventory } + Self { + entity, + inventory, + moving_to: None, + } } } impl Default for Player { |