Compare commits

...

4 Commits

17 changed files with 203 additions and 153 deletions

View File

@@ -2,26 +2,29 @@ use std::time::{Duration, Instant};
use glam::Vec2;
use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
use sdl2::ttf::Sdl2TtfContext;
use sdl2::video::{Window, WindowContext};
use sdl2::{AudioSubsystem, EventPump, Sdl, VideoSubsystem};
use tracing::{error, event};
use tracing::{error, info, warn};
use crate::error::{GameError, GameResult};
use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
use crate::game::Game;
use crate::input::commands::GameCommand;
use crate::input::InputSystem;
use crate::platform::get_platform;
pub struct App {
game: Game,
input_system: InputSystem,
canvas: Canvas<Window>,
event_pump: &'static mut EventPump,
backbuffer: Texture<'static>,
paused: bool,
event_pump: &'static mut EventPump,
last_tick: Instant,
focused: bool,
cursor_pos: Vec2,
}
@@ -78,12 +81,13 @@ impl App {
game.present_backbuffer(&mut canvas, &backbuffer, glam::Vec2::ZERO)
.map_err(|e| GameError::Sdl(e.to_string()))?;
Ok(Self {
Ok(App {
game,
input_system: InputSystem::new(),
canvas,
event_pump,
backbuffer,
paused: false,
focused: true,
last_tick: Instant::now(),
cursor_pos: Vec2::ZERO,
})
@@ -96,75 +100,58 @@ impl App {
for event in self.event_pump.poll_iter() {
match event {
Event::Window { win_event, .. } => match win_event {
WindowEvent::Hidden => {
event!(tracing::Level::DEBUG, "Window hidden");
WindowEvent::FocusGained => {
self.focused = true;
}
WindowEvent::Shown => {
event!(tracing::Level::DEBUG, "Window shown");
WindowEvent::FocusLost => {
self.focused = false;
}
_ => {}
},
// It doesn't really make sense to have this available in the browser
#[cfg(not(target_os = "emscripten"))]
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
..
} => {
event!(tracing::Level::INFO, "Exit requested. Exiting...");
return false;
}
Event::KeyDown {
keycode: Some(Keycode::P),
..
} => {
self.paused = !self.paused;
event!(tracing::Level::INFO, "{}", if self.paused { "Paused" } else { "Unpaused" });
}
Event::KeyDown {
keycode: Some(Keycode::Space),
..
} => {
self.game.toggle_debug_mode();
}
Event::KeyDown { keycode: Some(key), .. } => {
self.game.keyboard_event(key);
}
Event::MouseMotion { x, y, .. } => {
// Convert window coordinates to logical coordinates
self.cursor_pos = Vec2::new(x as f32, y as f32);
}
_ => {}
}
if let Some(command) = self.input_system.handle_event(&event) {
match command {
GameCommand::Exit => {
info!("Exit requested. Exiting...");
return false;
}
_ => self.game.post_event(command.into()),
}
}
}
let dt = self.last_tick.elapsed().as_secs_f32();
self.last_tick = Instant::now();
if !self.paused {
self.game.tick(dt);
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
error!("Failed to draw game: {}", e);
}
if let Err(e) = self
.game
.present_backbuffer(&mut self.canvas, &self.backbuffer, self.cursor_pos)
{
error!("Failed to present backbuffer: {}", e);
}
let exit = self.game.tick(dt);
if exit {
return false;
}
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
error!("Failed to draw game: {}", e);
}
if let Err(e) = self
.game
.present_backbuffer(&mut self.canvas, &self.backbuffer, self.cursor_pos)
{
error!("Failed to present backbuffer: {}", e);
}
if start.elapsed() < LOOP_TIME {
let time = LOOP_TIME.saturating_sub(start.elapsed());
if time != Duration::ZERO {
get_platform().sleep(time);
get_platform().sleep(time, self.focused);
}
} else {
event!(
tracing::Level::WARN,
"Game loop behind schedule by: {:?}",
start.elapsed() - LOOP_TIME
);
warn!("Game loop behind schedule by: {:?}", start.elapsed() - LOOP_TIME);
}
true

View File

@@ -1,7 +1,8 @@
use glam::IVec2;
/// The four cardinal directions.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(usize)]
pub enum Direction {
Up,
Down,

View File

@@ -14,7 +14,6 @@ use crate::entity::{
use crate::texture::animated::AnimatedTexture;
use crate::texture::directional::DirectionalAnimatedTexture;
use crate::texture::sprite::SpriteAtlas;
use sdl2::keyboard::Keycode;
use tracing::error;
use crate::error::{GameError, GameResult, TextureError};
@@ -107,24 +106,6 @@ impl Pacman {
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
})
}
/// Handles keyboard input to change Pac-Man's direction.
///
/// Maps arrow keys to directions and queues the direction change
/// for the next valid intersection.
pub fn handle_key(&mut self, keycode: Keycode) {
let direction = match keycode {
Keycode::Up => Some(Direction::Up),
Keycode::Down => Some(Direction::Down),
Keycode::Left => Some(Direction::Left),
Keycode::Right => Some(Direction::Right),
_ => None,
};
if let Some(direction) = direction {
self.traverser.set_next_direction(direction);
}
}
}
impl Collidable for Pacman {

12
src/game/events.rs Normal file
View File

@@ -0,0 +1,12 @@
use crate::input::commands::GameCommand;
#[derive(Debug, Clone, Copy)]
pub enum GameEvent {
Command(GameCommand),
}
impl From<GameCommand> for GameEvent {
fn from(command: GameCommand) -> Self {
GameEvent::Command(command)
}
}

View File

@@ -1,16 +1,12 @@
//! This module contains the main game logic and state.
use glam::{UVec2, Vec2};
use rand::{rngs::SmallRng, Rng, SeedableRng};
use sdl2::{
keyboard::Keycode,
pixels::Color,
render::{Canvas, RenderTarget, Texture, TextureCreator},
video::WindowContext,
};
use sdl2::pixels::Color;
use sdl2::render::{Canvas, Texture, TextureCreator};
use sdl2::video::WindowContext;
use crate::entity::r#trait::Entity;
use crate::error::{EntityError, GameError, GameResult};
use crate::error::GameResult;
use crate::entity::{
collision::{Collidable, CollisionSystem, EntityId},
@@ -21,15 +17,18 @@ use crate::entity::{
use crate::map::render::MapRenderer;
use crate::{constants, texture::sprite::SpriteAtlas};
use self::events::GameEvent;
use self::state::GameState;
pub mod events;
pub mod state;
use state::GameState;
/// The `Game` struct is the main entry point for the game.
///
/// It contains the game's state and logic, and is responsible for
/// handling user input, updating the game state, and rendering the game.
pub struct Game {
state: GameState,
state: state::GameState,
}
impl Game {
@@ -39,16 +38,39 @@ impl Game {
Ok(Game { state })
}
pub fn keyboard_event(&mut self, keycode: Keycode) {
self.state.pacman.handle_key(keycode);
pub fn post_event(&mut self, event: GameEvent) {
self.state.event_queue.push_back(event);
}
if keycode == Keycode::M {
self.state.audio.set_mute(!self.state.audio.is_muted());
fn handle_command(&mut self, command: crate::input::commands::GameCommand) {
use crate::input::commands::GameCommand;
match command {
GameCommand::MovePlayer(direction) => {
self.state.pacman.set_next_direction(direction);
}
GameCommand::ToggleDebug => {
self.toggle_debug_mode();
}
GameCommand::MuteAudio => {
let is_muted = self.state.audio.is_muted();
self.state.audio.set_mute(!is_muted);
}
GameCommand::ResetLevel => {
if let Err(e) = self.reset_game_state() {
tracing::error!("Failed to reset game state: {}", e);
}
}
GameCommand::TogglePause => {
self.state.paused = !self.state.paused;
}
GameCommand::Exit => {}
}
}
if keycode == Keycode::R {
if let Err(e) = self.reset_game_state() {
tracing::error!("Failed to reset game state: {}", e);
fn process_events(&mut self) {
while let Some(event) = self.state.event_queue.pop_front() {
match event {
GameEvent::Command(command) => self.handle_command(command),
}
}
}
@@ -93,7 +115,18 @@ impl Game {
Ok(())
}
pub fn tick(&mut self, dt: f32) {
/// Ticks the game state.
///
/// Returns true if the game should exit.
pub fn tick(&mut self, dt: f32) -> bool {
// Process any events that have been posted (such as unpausing)
self.process_events();
// If the game is paused, we don't need to do anything beyond returning
if self.state.paused {
return false;
}
self.state.pacman.tick(dt, &self.state.map.graph);
// Update all ghosts
@@ -106,6 +139,8 @@ impl Game {
// Check for collisions
self.check_collisions();
false
}
/// Toggles the debug mode on and off.
@@ -170,14 +205,14 @@ impl Game {
self.state.ghost_ids.iter().position(|&id| id == entity_id)
}
pub fn draw<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> GameResult<()> {
pub fn draw<T: sdl2::render::RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> GameResult<()> {
// Only render the map texture once and cache it
if !self.state.map_rendered {
let mut map_texture = self
.state
.texture_creator
.create_texture_target(None, constants::CANVAS_SIZE.x, constants::CANVAS_SIZE.y)
.map_err(|e| GameError::Sdl(e.to_string()))?;
.map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
canvas
.with_texture_canvas(&mut map_texture, |map_canvas| {
@@ -189,7 +224,7 @@ impl Game {
}
MapRenderer::render_map(map_canvas, &mut self.state.atlas, &mut map_tiles);
})
.map_err(|e| GameError::Sdl(e.to_string()))?;
.map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
self.state.map_texture = Some(map_texture);
self.state.map_rendered = true;
}
@@ -220,12 +255,12 @@ impl Game {
tracing::error!("Failed to render pacman: {}", e);
}
})
.map_err(|e| GameError::Sdl(e.to_string()))?;
.map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
Ok(())
}
pub fn present_backbuffer<T: RenderTarget>(
pub fn present_backbuffer<T: sdl2::render::RenderTarget>(
&mut self,
canvas: &mut Canvas<T>,
backbuffer: &Texture,
@@ -233,7 +268,7 @@ impl Game {
) -> GameResult<()> {
canvas
.copy(backbuffer, None, None)
.map_err(|e| GameError::Sdl(e.to_string()))?;
.map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
if self.state.debug_mode {
if let Err(e) =
self.state
@@ -253,7 +288,7 @@ impl Game {
///
/// Each ghost's path is drawn in its respective color with a small offset
/// to prevent overlapping lines.
fn render_pathfinding_debug<T: RenderTarget>(&self, canvas: &mut Canvas<T>) -> GameResult<()> {
fn render_pathfinding_debug<T: sdl2::render::RenderTarget>(&self, canvas: &mut Canvas<T>) -> GameResult<()> {
let pacman_node = self.state.pacman.current_node_id();
for ghost in self.state.ghosts.iter() {
@@ -274,10 +309,10 @@ impl Game {
// Use the overall direction from start to end to determine the perpendicular offset
let offset = match ghost.ghost_type {
GhostType::Blinky => Vec2::new(0.25, 0.5),
GhostType::Pinky => Vec2::new(-0.25, -0.25),
GhostType::Inky => Vec2::new(0.5, -0.5),
GhostType::Clyde => Vec2::new(-0.5, 0.25),
GhostType::Blinky => glam::Vec2::new(0.25, 0.5),
GhostType::Pinky => glam::Vec2::new(-0.25, -0.25),
GhostType::Inky => glam::Vec2::new(0.5, -0.5),
GhostType::Clyde => glam::Vec2::new(-0.5, 0.25),
} * 5.0;
// Calculate offset positions for all nodes using the same perpendicular direction
@@ -288,7 +323,7 @@ impl Game {
.map
.graph
.get_node(node_id)
.ok_or(GameError::Entity(EntityError::NodeNotFound(node_id)))?;
.ok_or(crate::error::EntityError::NodeNotFound(node_id))?;
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
offset_positions.push(pos + offset);
}
@@ -304,7 +339,7 @@ impl Game {
// Draw the line
canvas
.draw_line((from.x as i32, from.y as i32), (to.x as i32, to.y as i32))
.map_err(|e| GameError::Sdl(e.to_string()))?;
.map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
}
}
}
@@ -313,7 +348,7 @@ impl Game {
Ok(())
}
fn draw_hud<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> GameResult<()> {
fn draw_hud<T: sdl2::render::RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> GameResult<()> {
let lives = 3;
let score_text = format!("{:02}", self.state.score);
let x_offset = 4;
@@ -325,7 +360,7 @@ impl Game {
canvas,
&mut self.state.atlas,
&format!("{lives}UP HIGH SCORE "),
UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
glam::UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
) {
tracing::error!("Failed to render HUD text: {}", e);
}
@@ -333,7 +368,7 @@ impl Game {
canvas,
&mut self.state.atlas,
&score_text,
UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
glam::UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
) {
tracing::error!("Failed to render score text: {}", e);
}

View File

@@ -1,3 +1,5 @@
use std::collections::VecDeque;
use sdl2::{
image::LoadTexture,
render::{Texture, TextureCreator},
@@ -10,14 +12,14 @@ use crate::{
audio::Audio,
constants::RAW_BOARD,
entity::{
collision::{Collidable, CollisionSystem},
collision::{Collidable, CollisionSystem, EntityId},
ghost::{Ghost, GhostType},
item::Item,
pacman::Pacman,
},
error::{GameError, GameResult, TextureError},
game::EntityId,
map::Map,
game::events::GameEvent,
map::builder::Map,
texture::{
sprite::{AtlasMapper, SpriteAtlas},
text::TextTexture,
@@ -33,6 +35,8 @@ include!(concat!(env!("OUT_DIR"), "/atlas_data.rs"));
/// we can cleanly separate it from the game's logic, making it easier to manage
/// and reason about.
pub struct GameState {
pub paused: bool,
pub score: u32,
pub map: Map,
pub pacman: Pacman,
@@ -42,6 +46,7 @@ pub struct GameState {
pub items: Vec<Item>,
pub item_ids: Vec<EntityId>,
pub debug_mode: bool,
pub event_queue: VecDeque<GameEvent>,
// Collision system
pub(crate) collision_system: CollisionSystem,
@@ -125,6 +130,7 @@ impl GameState {
.collect();
Ok(Self {
paused: false,
map,
atlas,
pacman,
@@ -141,6 +147,7 @@ impl GameState {
map_texture: None,
map_rendered: false,
texture_creator,
event_queue: VecDeque::new(),
})
}
}

11
src/input/commands.rs Normal file
View File

@@ -0,0 +1,11 @@
use crate::entity::direction::Direction;
#[derive(Debug, Clone, Copy)]
pub enum GameCommand {
MovePlayer(Direction),
TogglePause,
ToggleDebug,
MuteAudio,
ResetLevel,
Exit,
}

47
src/input/mod.rs Normal file
View File

@@ -0,0 +1,47 @@
use std::collections::HashMap;
use sdl2::{event::Event, keyboard::Keycode};
use crate::{entity::direction::Direction, input::commands::GameCommand};
pub mod commands;
#[derive(Debug, Clone, Default)]
pub struct InputSystem {
key_bindings: HashMap<Keycode, GameCommand>,
}
impl InputSystem {
pub fn new() -> Self {
let mut key_bindings = HashMap::new();
// Player movement
key_bindings.insert(Keycode::Up, GameCommand::MovePlayer(Direction::Up));
key_bindings.insert(Keycode::W, GameCommand::MovePlayer(Direction::Up));
key_bindings.insert(Keycode::Down, GameCommand::MovePlayer(Direction::Down));
key_bindings.insert(Keycode::S, GameCommand::MovePlayer(Direction::Down));
key_bindings.insert(Keycode::Left, GameCommand::MovePlayer(Direction::Left));
key_bindings.insert(Keycode::A, GameCommand::MovePlayer(Direction::Left));
key_bindings.insert(Keycode::Right, GameCommand::MovePlayer(Direction::Right));
key_bindings.insert(Keycode::D, GameCommand::MovePlayer(Direction::Right));
// Game actions
key_bindings.insert(Keycode::P, GameCommand::TogglePause);
key_bindings.insert(Keycode::Space, GameCommand::ToggleDebug);
key_bindings.insert(Keycode::M, GameCommand::MuteAudio);
key_bindings.insert(Keycode::R, GameCommand::ResetLevel);
key_bindings.insert(Keycode::Escape, GameCommand::Exit);
key_bindings.insert(Keycode::Q, GameCommand::Exit);
Self { key_bindings }
}
/// Handles an event and returns a command if one is bound to the event.
pub fn handle_event(&self, event: &Event) -> Option<GameCommand> {
match event {
Event::Quit { .. } => Some(GameCommand::Exit),
Event::KeyDown { keycode: Some(key), .. } => self.key_bindings.get(key).copied(),
_ => None,
}
}
}

View File

@@ -8,6 +8,7 @@ pub mod entity;
pub mod error;
pub mod game;
pub mod helpers;
pub mod input;
pub mod map;
pub mod platform;
pub mod texture;

View File

@@ -14,6 +14,7 @@ mod entity;
mod error;
mod game;
mod helpers;
mod input;
mod map;
mod platform;
mod texture;

View File

@@ -4,6 +4,3 @@ pub mod builder;
pub mod layout;
pub mod parser;
pub mod render;
// Re-export main types for convenience
pub use builder::Map;

View File

@@ -11,8 +11,12 @@ use crate::platform::Platform;
pub struct DesktopPlatform;
impl Platform for DesktopPlatform {
fn sleep(&self, duration: Duration) {
spin_sleep::sleep(duration);
fn sleep(&self, duration: Duration, focused: bool) {
if focused {
spin_sleep::sleep(duration);
} else {
std::thread::sleep(duration);
}
}
fn get_time(&self) -> f64 {

View File

@@ -11,7 +11,7 @@ use crate::platform::Platform;
pub struct EmscriptenPlatform;
impl Platform for EmscriptenPlatform {
fn sleep(&self, duration: Duration) {
fn sleep(&self, duration: Duration, _focused: bool) {
unsafe {
emscripten_sleep(duration.as_millis() as u32);
}

View File

@@ -11,7 +11,7 @@ pub mod emscripten;
/// Platform abstraction trait that defines cross-platform functionality.
pub trait Platform {
/// Sleep for the specified duration using platform-appropriate method.
fn sleep(&self, duration: Duration);
fn sleep(&self, duration: Duration, focused: bool);
/// Get the current time in seconds since some reference point.
/// This is available for future use in timing and performance monitoring.

View File

@@ -1,5 +1,5 @@
use pacman::constants::RAW_BOARD;
use pacman::map::Map;
use pacman::map::builder::Map;
mod collision;
mod item;

View File

@@ -1,6 +1,6 @@
use glam::Vec2;
use pacman::constants::{CELL_SIZE, RAW_BOARD};
use pacman::map::Map;
use pacman::map::builder::Map;
use sdl2::render::Texture;
#[test]

View File

@@ -2,7 +2,6 @@ use pacman::entity::direction::Direction;
use pacman::entity::graph::{Graph, Node};
use pacman::entity::pacman::Pacman;
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
use sdl2::keyboard::Keycode;
use std::collections::HashMap;
fn create_test_graph() -> Graph {
@@ -72,36 +71,3 @@ fn test_pacman_creation() {
assert!(pacman.traverser.position.is_at_node());
assert_eq!(pacman.traverser.direction, Direction::Left);
}
#[test]
fn test_pacman_key_handling() {
let graph = create_test_graph();
let atlas = create_test_atlas();
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
let test_cases = [
(Keycode::Up, Direction::Up),
(Keycode::Down, Direction::Down),
(Keycode::Left, Direction::Left),
(Keycode::Right, Direction::Right),
];
for (key, expected_direction) in test_cases {
pacman.handle_key(key);
assert!(pacman.traverser.next_direction.is_some() || pacman.traverser.direction == expected_direction);
}
}
#[test]
fn test_pacman_invalid_key() {
let graph = create_test_graph();
let atlas = create_test_atlas();
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
let original_direction = pacman.traverser.direction;
let original_next_direction = pacman.traverser.next_direction;
pacman.handle_key(Keycode::Space);
assert_eq!(pacman.traverser.direction, original_direction);
assert_eq!(pacman.traverser.next_direction, original_next_direction);
}