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.rs37
1 files changed, 34 insertions, 3 deletions
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;
+ }
}
}