diff options
| -rw-r--r-- | assets/atlas.bmp | bin | 8330 -> 8330 bytes | |||
| -rw-r--r-- | assets/font.bmp | bin | 3218 -> 3218 bytes | |||
| -rw-r--r-- | assets/player.bmp | bin | 2186 -> 4234 bytes | |||
| -rw-r--r-- | dungeon/src/entity.rs | 5 | ||||
| -rw-r--r-- | graphics/src/render.rs | 29 | ||||
| -rw-r--r-- | graphics/src/timer.rs | 31 |
6 files changed, 46 insertions, 19 deletions
diff --git a/assets/atlas.bmp b/assets/atlas.bmp Binary files differindex bb55fc1..fcf1ae1 100644 --- a/assets/atlas.bmp +++ b/assets/atlas.bmp diff --git a/assets/font.bmp b/assets/font.bmp Binary files differindex dfe194e..eb8e838 100644 --- a/assets/font.bmp +++ b/assets/font.bmp diff --git a/assets/player.bmp b/assets/player.bmp Binary files differindex 5965a6b..b6d39db 100644 --- a/assets/player.bmp +++ b/assets/player.bmp diff --git a/dungeon/src/entity.rs b/dungeon/src/entity.rs index 592c1ee..f56f8dd 100644 --- a/dungeon/src/entity.rs +++ b/dungeon/src/entity.rs @@ -323,6 +323,11 @@ impl Entity { next_move = *last; } } + + /// Returns the position in front of the entity + pub const fn in_front(&self) -> Option<Pos> { + self.pos.step(self.dir) + } } /// The `Player` type represents the main player entity diff --git a/graphics/src/render.rs b/graphics/src/render.rs index a206e04..340189b 100644 --- a/graphics/src/render.rs +++ b/graphics/src/render.rs @@ -92,8 +92,8 @@ pub const RENDER_WIDTH: u16 = RENDER_HEIGHT * 4 / 3; // Tile atlas.bmp textures const ATLAS_WALL_SIDE: (u16, u16) = (0, 0); -const ATLAS_FLOOR_FULL: (u16, u16) = (1, 0); -const ATLAS_FLOOR_EMPTY: (u16, u16) = (2, 0); +const ATLAS_FLOOR: (u16, u16) = (1, 0); +const ATLAS_STAIR: (u16, u16) = (2, 0); const ATLAS_WALL_TOP: (u16, u16) = (3, 0); const ATLAS_WALL_EDGE: (u16, u16) = (0, 1); @@ -103,7 +103,7 @@ const ATLAS_HEART_ICON: (u16, u16) = (2, 1); const ATLAS_DAMAGE_ICON: (u16, u16) = (3, 1); // Misc atlas.bmp textures -const ATLAS_ERROR: (u16, u16) = (3, 3); +//const ATLAS_ERROR: (u16, u16) = (3, 3); /// Full source rec for any texture const FULL_SOURCE_REC: Rectangle = rect! { @@ -376,11 +376,9 @@ impl Renderer { let idx = match tile { Tile::Wall if y + 1 == MAP_SIZE => ATLAS_WALL_TOP, Tile::Wall => ATLAS_WALL_SIDE, - Tile::Room if (x + y) % 2 == 0 => ATLAS_FLOOR_FULL, - Tile::Room if (x + y) % 2 == 1 => ATLAS_FLOOR_EMPTY, - Tile::Hallway if (x + y) % 2 == 0 => ATLAS_FLOOR_FULL, - Tile::Hallway if (x + y) % 2 == 1 => ATLAS_FLOOR_EMPTY, - _ => ATLAS_ERROR, + Tile::Room | Tile::Hallway => ATLAS_FLOOR, + Tile::Stairs => ATLAS_STAIR, + //_ => ATLAS_ERROR, }; rt.draw_atlas( &self.textures.atlas, @@ -458,19 +456,30 @@ impl Renderer { EntityKind::Player => &self.textures.player, _ => &self.textures.error, }; - let (ax, ay) = match entity.dir { + let (mut ax, ay) = match entity.dir { Direction::North => (0, 0), Direction::South => (0, 1), Direction::East => (1, 0), Direction::West => (1, 1), }; + if self.timer.since_start().as_millis() / 250 % 2 == 0 { + // entity animtion frame every 250 millis + ax += 2; + } let dest_rec = rect! { fx * size, fy * size, size, size, }; - r.draw_atlas(texture, (ax, ay), dest_rec.x, dest_rec.y, size, 0.0); + r.draw_atlas( + texture, + (ax, ay), + dest_rec.x, + dest_rec.y - WALL_HEIGHT as f32, + size, + 0.0, + ); if self.debug { r.draw_rectangle_lines_ex(dest_rec, 1.0, Color::YELLOW); diff --git a/graphics/src/timer.rs b/graphics/src/timer.rs index e2209c7..9d4fcfc 100644 --- a/graphics/src/timer.rs +++ b/graphics/src/timer.rs @@ -5,6 +5,8 @@ const MAX_SAMPLES: usize = 100; /// Keeps track of frame intervals and FPS pub struct Timer { + start: Instant, + since_start: Duration, previous: Instant, tick_index: usize, tick_sum: Duration, @@ -13,13 +15,17 @@ pub struct Timer { } impl Timer { pub fn new() -> Self { - let previous = Instant::now(); + let start = Instant::now(); + let since_start = Duration::ZERO; + let previous = start; let tick_index = 0usize; let tick_sum = Duration::ZERO; let tick_list = Box::new([tick_sum; MAX_SAMPLES]); let frame = 0u32; Self { + start, + since_start, previous, tick_index, tick_sum, @@ -39,8 +45,11 @@ impl Timer { self.tick_sum += time; self.tick_list[self.tick_index] = time; - // update frame frame + // update frame count self.frame += 1; + + // update since + self.since_start = self.previous.duration_since(self.start); } pub const fn frame(&self) -> u32 { @@ -49,13 +58,13 @@ impl Timer { #[expect(clippy::cast_possible_truncation)] pub const fn fps(&self) -> u32 { - match self.frame_time().as_nanos() { - 0 => u32::MAX, - // fps can never be bigger than u32::MAX since we're - // computing in nanoseconds - nanos => 1_000_000_000u128.div_ceil(nanos) as u32, - } - } + match self.frame_time().as_nanos() { + 0 => u32::MAX, + // fps can never be bigger than u32::MAX since we're + // computing in nanoseconds + nanos => 1_000_000_000u128.div_ceil(nanos) as u32, + } + } #[expect(clippy::cast_possible_truncation)] pub const fn frame_time(&self) -> Duration { @@ -68,4 +77,8 @@ impl Timer { pub const fn delta_time(&self) -> Duration { self.tick_list[self.tick_index] } + + pub const fn since_start(&self) -> Duration { + self.since_start + } } |