Compare commits
68 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5109457fcd | ||
|
|
5497e4b0b9 | ||
| d72b6eec06 | |||
| ae42f6ead0 | |||
| 471b118efd | |||
| 13a9c165f7 | |||
| da3c8e8284 | |||
| 9c0711a54c | |||
| 4598dc07e2 | |||
| 9c9dc5f423 | |||
| 12ee16faab | |||
| 398d041d96 | |||
| 7a02d6b0b5 | |||
| d47d70ff5b | |||
| 313ca4f3e6 | |||
| f940f01d9b | |||
| 90adaf9e84 | |||
| 2140fbec1b | |||
| 78300bdf9c | |||
| 514a447162 | |||
| 3d0bc66e40 | |||
| e0a15c1ca8 | |||
| fa12611c69 | |||
| 342f378860 | |||
| e8944598cc | |||
| 6af25af5f3 | |||
| f1935ad016 | |||
| 4d397bba5f | |||
| 80930ddd35 | |||
| 0133dd5329 | |||
| 635418a4da | |||
| 31193160a9 | |||
| 3086453c7b | |||
| a8b83b8e2b | |||
| 8ce2af89c8 | |||
| 5f0ee87dd9 | |||
| b88895e82f | |||
| 2f0c734d13 | |||
| e96b3159d7 | |||
| 8c95ecc547 | |||
| 02a98c9f32 | |||
| 7f95c0233e | |||
| a531228b95 | |||
| de86f383bf | |||
| bd811ee783 | |||
| 57d7f75940 | |||
| c5d6ea28e1 | |||
| 730daed20a | |||
| b9bae99a4c | |||
| 2c65048fb0 | |||
| 3388d77ec5 | |||
| 242da2e263 | |||
| 70fb2b9503 | |||
| 0aa056a0ae | |||
| b270318640 | |||
| bc759f1ed4 | |||
| 2f1ff85d8f | |||
| b7429cd9ec | |||
| 12a63374a8 | |||
| d80d7061e7 | |||
| abdefe0af0 | |||
| 4f76de7c9f | |||
| db8cd6220a | |||
| ced4e87d41 | |||
| 09e3d85821 | |||
| c1e421bbbb | |||
| 3a9381a56c | |||
| 90bdfbd2ae |
15
.gitignore
vendored
@@ -1,8 +1,17 @@
|
|||||||
|
# IDE, Other files
|
||||||
|
.vscode
|
||||||
|
.idea
|
||||||
|
rust-sdl2-emscripten/
|
||||||
|
|
||||||
|
# Build files
|
||||||
target/
|
target/
|
||||||
dist/
|
dist/
|
||||||
emsdk/
|
emsdk/
|
||||||
.idea
|
|
||||||
rust-sdl2-emscripten/
|
# Site build f iles
|
||||||
assets/site/build.css
|
|
||||||
tailwindcss-*
|
tailwindcss-*
|
||||||
|
assets/site/build.css
|
||||||
|
|
||||||
|
# Coverage reports
|
||||||
lcov.info
|
lcov.info
|
||||||
|
coverage.html
|
||||||
|
|||||||
@@ -20,3 +20,15 @@ repos:
|
|||||||
language: system
|
language: system
|
||||||
types: [rust]
|
types: [rust]
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
- id: cargo-check
|
||||||
|
name: cargo check
|
||||||
|
entry: cargo check --all-targets
|
||||||
|
language: system
|
||||||
|
types_or: [rust, cargo, cargo-lock]
|
||||||
|
pass_filenames: false
|
||||||
|
- id: cargo-check-wasm
|
||||||
|
name: cargo check for wasm32-unknown-emscripten
|
||||||
|
entry: cargo check --all-targets --target=wasm32-unknown-emscripten
|
||||||
|
language: system
|
||||||
|
types_or: [rust, cargo, cargo-lock]
|
||||||
|
pass_filenames: false
|
||||||
|
|||||||
814
Cargo.lock
generated
20
Cargo.toml
@@ -6,7 +6,7 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = { version = "0.1.40", features = ["max_level_debug", "release_max_level_debug"]}
|
tracing = { version = "0.1.41", features = ["max_level_debug", "release_max_level_debug"]}
|
||||||
tracing-error = "0.2.0"
|
tracing-error = "0.2.0"
|
||||||
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
@@ -15,14 +15,23 @@ spin_sleep = "1.3.2"
|
|||||||
rand = { version = "0.9.2", default-features = false, features = ["small_rng", "os_rng"] }
|
rand = { version = "0.9.2", default-features = false, features = ["small_rng", "os_rng"] }
|
||||||
pathfinding = "4.14"
|
pathfinding = "4.14"
|
||||||
once_cell = "1.21.3"
|
once_cell = "1.21.3"
|
||||||
thiserror = "2.0"
|
thiserror = "2.0.14"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
glam = { version = "0.30.5", features = [] }
|
glam = "0.30.5"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.142"
|
serde_json = "1.0.142"
|
||||||
smallvec = "1.15.1"
|
smallvec = "1.15.1"
|
||||||
strum = "0.27.2"
|
strum = "0.27.2"
|
||||||
strum_macros = "0.27.2"
|
strum_macros = "0.27.2"
|
||||||
|
phf = { version = "0.12.1", features = ["macros"] }
|
||||||
|
bevy_ecs = "0.16.1"
|
||||||
|
bitflags = "2.9.1"
|
||||||
|
parking_lot = "0.12.3"
|
||||||
|
micromap = "0.1.0"
|
||||||
|
thousands = "0.2.0"
|
||||||
|
pretty_assertions = "1.4.1"
|
||||||
|
num-width = "0.1.0"
|
||||||
|
circular-buffer = "1.1.0"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
@@ -57,3 +66,8 @@ aarch64-apple-darwin = { triplet = "arm64-osx" }
|
|||||||
|
|
||||||
[target.'cfg(target_os = "emscripten")'.dependencies]
|
[target.'cfg(target_os = "emscripten")'.dependencies]
|
||||||
libc = "0.2.175"
|
libc = "0.2.175"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
phf = { version = "0.12.1", features = ["macros"] }
|
||||||
|
|||||||
17
Justfile
@@ -1,17 +1,32 @@
|
|||||||
set shell := ["bash", "-c"]
|
set shell := ["bash", "-c"]
|
||||||
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
|
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
|
||||||
|
|
||||||
coverage_exclude_pattern := "app.rs|audio.rs|error.rs"
|
# Regex to exclude files from coverage report, double escapes for Justfile + CLI
|
||||||
|
# You can use src\\\\..., but the filename alone is acceptable too
|
||||||
|
coverage_exclude_pattern := "src\\\\app.rs|audio.rs|src\\\\error.rs|platform\\\\emscripten.rs"
|
||||||
|
|
||||||
|
# !!! --ignore-filename-regex should be used on both reports & coverage testing
|
||||||
|
# !!! --remap-path-prefix prevents the absolute path from being used in the generated report
|
||||||
|
|
||||||
|
# Generate HTML report (for humans, source line inspection)
|
||||||
|
html: coverage
|
||||||
|
cargo llvm-cov report \
|
||||||
|
--remap-path-prefix \
|
||||||
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||||
|
--html \
|
||||||
|
--open
|
||||||
|
|
||||||
# Display report (for humans)
|
# Display report (for humans)
|
||||||
report-coverage: coverage
|
report-coverage: coverage
|
||||||
cargo llvm-cov report \
|
cargo llvm-cov report \
|
||||||
|
--remap-path-prefix \
|
||||||
--ignore-filename-regex "{{ coverage_exclude_pattern }}"
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}"
|
||||||
|
|
||||||
# Run & generate report (for CI)
|
# Run & generate report (for CI)
|
||||||
coverage:
|
coverage:
|
||||||
cargo llvm-cov \
|
cargo llvm-cov \
|
||||||
--lcov \
|
--lcov \
|
||||||
|
--remap-path-prefix \
|
||||||
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||||
--output-path lcov.info \
|
--output-path lcov.info \
|
||||||
--profile coverage \
|
--profile coverage \
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Pac-Man
|
# Pac-Man
|
||||||
|
|
||||||
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![Code Coverage][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![If you're seeing this, Coveralls.io is broken again and it's not my fault.][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
||||||
|
|
||||||
[badge-test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml/badge.svg
|
[badge-test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml/badge.svg
|
||||||
[badge-build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml/badge.svg
|
[badge-build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml/badge.svg
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 16 KiB |
|
Before Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
BIN
assets/unpacked/maze/tiles/0.png
Normal file
|
After Width: | Height: | Size: 102 B |
BIN
assets/unpacked/maze/tiles/1.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/10.png
Normal file
|
After Width: | Height: | Size: 84 B |
BIN
assets/unpacked/maze/tiles/11.png
Normal file
|
After Width: | Height: | Size: 77 B |
BIN
assets/unpacked/maze/tiles/12.png
Normal file
|
After Width: | Height: | Size: 80 B |
BIN
assets/unpacked/maze/tiles/13.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/14.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/15.png
Normal file
|
After Width: | Height: | Size: 89 B |
BIN
assets/unpacked/maze/tiles/16.png
Normal file
|
After Width: | Height: | Size: 91 B |
BIN
assets/unpacked/maze/tiles/17.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/18.png
Normal file
|
After Width: | Height: | Size: 107 B |
BIN
assets/unpacked/maze/tiles/19.png
Normal file
|
After Width: | Height: | Size: 77 B |
BIN
assets/unpacked/maze/tiles/2.png
Normal file
|
After Width: | Height: | Size: 93 B |
BIN
assets/unpacked/maze/tiles/20.png
Normal file
|
After Width: | Height: | Size: 91 B |
BIN
assets/unpacked/maze/tiles/21.png
Normal file
|
After Width: | Height: | Size: 97 B |
BIN
assets/unpacked/maze/tiles/22.png
Normal file
|
After Width: | Height: | Size: 107 B |
BIN
assets/unpacked/maze/tiles/23.png
Normal file
|
After Width: | Height: | Size: 88 B |
BIN
assets/unpacked/maze/tiles/24.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/25.png
Normal file
|
After Width: | Height: | Size: 80 B |
BIN
assets/unpacked/maze/tiles/26.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/27.png
Normal file
|
After Width: | Height: | Size: 93 B |
BIN
assets/unpacked/maze/tiles/28.png
Normal file
|
After Width: | Height: | Size: 89 B |
BIN
assets/unpacked/maze/tiles/29.png
Normal file
|
After Width: | Height: | Size: 90 B |
BIN
assets/unpacked/maze/tiles/3.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/30.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/31.png
Normal file
|
After Width: | Height: | Size: 100 B |
BIN
assets/unpacked/maze/tiles/32.png
Normal file
|
After Width: | Height: | Size: 98 B |
BIN
assets/unpacked/maze/tiles/33.png
Normal file
|
After Width: | Height: | Size: 96 B |
BIN
assets/unpacked/maze/tiles/34.png
Normal file
|
After Width: | Height: | Size: 100 B |
BIN
assets/unpacked/maze/tiles/4.png
Normal file
|
After Width: | Height: | Size: 105 B |
BIN
assets/unpacked/maze/tiles/5.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/6.png
Normal file
|
After Width: | Height: | Size: 71 B |
BIN
assets/unpacked/maze/tiles/7.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/8.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/9.png
Normal file
|
After Width: | Height: | Size: 82 B |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
50
build.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufWriter, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct AtlasMapper {
|
||||||
|
frames: HashMap<String, MapperFrame>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, Deserialize)]
|
||||||
|
struct MapperFrame {
|
||||||
|
x: u16,
|
||||||
|
y: u16,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atlas_data.rs");
|
||||||
|
let mut file = BufWriter::new(File::create(&path).unwrap());
|
||||||
|
|
||||||
|
let atlas_json = include_str!("./assets/game/atlas.json");
|
||||||
|
let atlas_mapper: AtlasMapper = serde_json::from_str(atlas_json).unwrap();
|
||||||
|
|
||||||
|
writeln!(&mut file, "use phf::phf_map;").unwrap();
|
||||||
|
|
||||||
|
writeln!(&mut file, "use crate::texture::sprite::MapperFrame;").unwrap();
|
||||||
|
|
||||||
|
writeln!(
|
||||||
|
&mut file,
|
||||||
|
"pub static ATLAS_FRAMES: phf::Map<&'static str, MapperFrame> = phf_map! {{"
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
for (name, frame) in atlas_mapper.frames {
|
||||||
|
writeln!(
|
||||||
|
&mut file,
|
||||||
|
" \"{}\" => MapperFrame {{ x: {}, y: {}, width: {}, height: {} }},",
|
||||||
|
name, frame.x, frame.y, frame.width, frame.height
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(&mut file, "}};").unwrap();
|
||||||
|
println!("cargo:rerun-if-changed=assets/game/atlas.json");
|
||||||
|
}
|
||||||
166
src/app.rs
@@ -1,13 +1,10 @@
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use glam::Vec2;
|
use glam::Vec2;
|
||||||
use sdl2::event::{Event, WindowEvent};
|
use sdl2::render::TextureCreator;
|
||||||
use sdl2::keyboard::Keycode;
|
|
||||||
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
|
|
||||||
use sdl2::ttf::Sdl2TtfContext;
|
use sdl2::ttf::Sdl2TtfContext;
|
||||||
use sdl2::video::{Window, WindowContext};
|
use sdl2::video::WindowContext;
|
||||||
use sdl2::{AudioSubsystem, EventPump, Sdl, VideoSubsystem};
|
use sdl2::{AudioSubsystem, EventPump, Sdl, VideoSubsystem};
|
||||||
use tracing::{error, event};
|
|
||||||
|
|
||||||
use crate::error::{GameError, GameResult};
|
use crate::error::{GameError, GameResult};
|
||||||
|
|
||||||
@@ -15,17 +12,29 @@ use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
|
|||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
use crate::platform::get_platform;
|
use crate::platform::get_platform;
|
||||||
|
|
||||||
|
/// Main application wrapper that manages SDL initialization, window lifecycle, and the game loop.
|
||||||
|
///
|
||||||
|
/// Handles platform-specific setup, maintains consistent frame timing, and delegates
|
||||||
|
/// game logic to the contained `Game` instance. The app manages focus state to
|
||||||
|
/// optimize CPU usage when the window loses focus.
|
||||||
pub struct App {
|
pub struct App {
|
||||||
game: Game,
|
pub game: Game,
|
||||||
canvas: Canvas<Window>,
|
|
||||||
event_pump: &'static mut EventPump,
|
|
||||||
backbuffer: Texture<'static>,
|
|
||||||
paused: bool,
|
|
||||||
last_tick: Instant,
|
last_tick: Instant,
|
||||||
cursor_pos: Vec2,
|
focused: bool,
|
||||||
|
_cursor_pos: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
|
/// Initializes SDL subsystems, creates the game window, and sets up the game state.
|
||||||
|
///
|
||||||
|
/// Performs comprehensive initialization including video/audio subsystems, platform-specific
|
||||||
|
/// console setup, window creation with proper scaling, and canvas configuration. All SDL
|
||||||
|
/// resources are leaked to maintain 'static lifetimes required by the game architecture.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns `GameError::Sdl` if any SDL initialization step fails, or propagates
|
||||||
|
/// errors from `Game::new()` during game state setup.
|
||||||
pub fn new() -> GameResult<Self> {
|
pub fn new() -> GameResult<Self> {
|
||||||
let sdl_context: &'static Sdl = Box::leak(Box::new(sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?));
|
let sdl_context: &'static Sdl = Box::leak(Box::new(sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
let video_subsystem: &'static VideoSubsystem =
|
let video_subsystem: &'static VideoSubsystem =
|
||||||
@@ -51,114 +60,85 @@ impl App {
|
|||||||
.build()
|
.build()
|
||||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let mut canvas = window.into_canvas().build().map_err(|e| GameError::Sdl(e.to_string()))?;
|
let canvas = Box::leak(Box::new(
|
||||||
|
window
|
||||||
|
.into_canvas()
|
||||||
|
.accelerated()
|
||||||
|
.build()
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?,
|
||||||
|
));
|
||||||
|
|
||||||
canvas
|
canvas
|
||||||
.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)
|
.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let texture_creator: &'static TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
let texture_creator: &'static mut TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
||||||
|
|
||||||
let mut game = Game::new(texture_creator)?;
|
let game = Game::new(canvas, texture_creator, event_pump)?;
|
||||||
// game.audio.set_mute(cfg!(debug_assertions));
|
// game.audio.set_mute(cfg!(debug_assertions));
|
||||||
|
|
||||||
let mut backbuffer = texture_creator
|
Ok(App {
|
||||||
.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)
|
|
||||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
|
||||||
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
|
||||||
|
|
||||||
// Initial draw
|
|
||||||
game.draw(&mut canvas, &mut backbuffer)
|
|
||||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
|
||||||
game.present_backbuffer(&mut canvas, &backbuffer, glam::Vec2::ZERO)
|
|
||||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
game,
|
game,
|
||||||
canvas,
|
focused: true,
|
||||||
event_pump,
|
|
||||||
backbuffer,
|
|
||||||
paused: false,
|
|
||||||
last_tick: Instant::now(),
|
last_tick: Instant::now(),
|
||||||
cursor_pos: Vec2::ZERO,
|
_cursor_pos: Vec2::ZERO,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Executes a single frame of the game loop with consistent timing and optional sleep.
|
||||||
|
///
|
||||||
|
/// Calculates delta time since the last frame, runs game logic via `game.tick()`,
|
||||||
|
/// and implements frame rate limiting by sleeping for remaining time if the frame
|
||||||
|
/// completed faster than the target `LOOP_TIME`. Sleep behavior varies based on
|
||||||
|
/// window focus to conserve CPU when the game is not active.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `true` if the game should continue running, `false` if the game requested exit.
|
||||||
pub fn run(&mut self) -> bool {
|
pub fn run(&mut self) -> bool {
|
||||||
{
|
{
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
for event in self.event_pump.poll_iter() {
|
// for event in self
|
||||||
match event {
|
// .game
|
||||||
Event::Window { win_event, .. } => match win_event {
|
// .world
|
||||||
WindowEvent::Hidden => {
|
// .get_non_send_resource_mut::<&'static mut EventPump>()
|
||||||
event!(tracing::Level::DEBUG, "Window hidden");
|
// .unwrap()
|
||||||
}
|
// .poll_iter()
|
||||||
WindowEvent::Shown => {
|
// {
|
||||||
event!(tracing::Level::DEBUG, "Window shown");
|
// match event {
|
||||||
}
|
// Event::Window { win_event, .. } => match win_event {
|
||||||
_ => {}
|
// WindowEvent::FocusGained => {
|
||||||
},
|
// self.focused = true;
|
||||||
// It doesn't really make sense to have this available in the browser
|
// }
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
// WindowEvent::FocusLost => {
|
||||||
Event::Quit { .. }
|
// self.focused = false;
|
||||||
| Event::KeyDown {
|
// }
|
||||||
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
|
// _ => {}
|
||||||
..
|
// },
|
||||||
} => {
|
// Event::MouseMotion { x, y, .. } => {
|
||||||
event!(tracing::Level::INFO, "Exit requested. Exiting...");
|
// // Convert window coordinates to logical coordinates
|
||||||
return false;
|
// self.cursor_pos = Vec2::new(x as f32, y as f32);
|
||||||
}
|
// }
|
||||||
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);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let dt = self.last_tick.elapsed().as_secs_f32();
|
let dt = self.last_tick.elapsed().as_secs_f32();
|
||||||
self.last_tick = Instant::now();
|
self.last_tick = Instant::now();
|
||||||
|
|
||||||
if !self.paused {
|
let exit = self.game.tick(dt);
|
||||||
self.game.tick(dt);
|
|
||||||
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
if exit {
|
||||||
error!("Failed to draw game: {}", e);
|
return false;
|
||||||
}
|
|
||||||
if let Err(e) = self
|
|
||||||
.game
|
|
||||||
.present_backbuffer(&mut self.canvas, &self.backbuffer, self.cursor_pos)
|
|
||||||
{
|
|
||||||
error!("Failed to present backbuffer: {}", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep if we still have time left
|
||||||
if start.elapsed() < LOOP_TIME {
|
if start.elapsed() < LOOP_TIME {
|
||||||
let time = LOOP_TIME.saturating_sub(start.elapsed());
|
let time = LOOP_TIME.saturating_sub(start.elapsed());
|
||||||
if time != Duration::ZERO {
|
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
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|||||||
31
src/asset.rs
@@ -5,18 +5,28 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use strum_macros::EnumIter;
|
use strum_macros::EnumIter;
|
||||||
|
|
||||||
|
/// Enumeration of all game assets with cross-platform loading support.
|
||||||
|
///
|
||||||
|
/// Each variant corresponds to a specific file that can be loaded either from
|
||||||
|
/// binary-embedded data or embedded filesystem (Emscripten).
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EnumIter)]
|
||||||
pub enum Asset {
|
pub enum Asset {
|
||||||
Wav1,
|
Wav1,
|
||||||
Wav2,
|
Wav2,
|
||||||
Wav3,
|
Wav3,
|
||||||
Wav4,
|
Wav4,
|
||||||
Atlas,
|
/// Main sprite atlas containing all game graphics (atlas.png)
|
||||||
AtlasJson,
|
AtlasImage,
|
||||||
// Add more as needed
|
/// Terminal Vector font for text rendering (TerminalVector.ttf)
|
||||||
|
Font,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Asset {
|
impl Asset {
|
||||||
|
/// Returns the relative file path for this asset within the game's asset directory.
|
||||||
|
///
|
||||||
|
/// Paths are consistent across platforms and used by the Emscripten backend
|
||||||
|
/// for filesystem loading. Desktop builds embed assets directly and don't
|
||||||
|
/// use these paths at runtime.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn path(&self) -> &str {
|
pub fn path(&self) -> &str {
|
||||||
use Asset::*;
|
use Asset::*;
|
||||||
@@ -25,8 +35,8 @@ impl Asset {
|
|||||||
Wav2 => "sound/waka/2.ogg",
|
Wav2 => "sound/waka/2.ogg",
|
||||||
Wav3 => "sound/waka/3.ogg",
|
Wav3 => "sound/waka/3.ogg",
|
||||||
Wav4 => "sound/waka/4.ogg",
|
Wav4 => "sound/waka/4.ogg",
|
||||||
Atlas => "atlas.png",
|
AtlasImage => "atlas.png",
|
||||||
AtlasJson => "atlas.json",
|
Font => "TerminalVector.ttf",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -36,6 +46,17 @@ mod imp {
|
|||||||
use crate::error::AssetError;
|
use crate::error::AssetError;
|
||||||
use crate::platform::get_platform;
|
use crate::platform::get_platform;
|
||||||
|
|
||||||
|
/// Loads asset bytes using the appropriate platform-specific method.
|
||||||
|
///
|
||||||
|
/// On desktop platforms, returns embedded compile-time data via `include_bytes!`.
|
||||||
|
/// On Emscripten, loads from the filesystem using the asset's path. The returned
|
||||||
|
/// `Cow` allows zero-copy access to embedded data while supporting owned data
|
||||||
|
/// when loaded from disk.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns `AssetError::NotFound` if the asset file cannot be located (Emscripten only),
|
||||||
|
/// or `AssetError::Io` for filesystem I/O failures.
|
||||||
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
||||||
get_platform().get_asset_bytes(asset)
|
get_platform().get_asset_bytes(asset)
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/audio.rs
@@ -114,9 +114,11 @@ impl Audio {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Plays the "eat" sound effect.
|
/// Plays the next waka eating sound in the cycle of four variants.
|
||||||
///
|
///
|
||||||
/// If audio is disabled or muted, this function does nothing.
|
/// Automatically rotates through the four eating sound assets. The sound plays on channel 0 and the internal sound index
|
||||||
|
/// advances to the next variant. Silently returns if audio is disabled, muted,
|
||||||
|
/// or no sounds were loaded successfully.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn eat(&mut self) {
|
pub fn eat(&mut self) {
|
||||||
if self.disabled || self.muted || self.sounds.is_empty() {
|
if self.disabled || self.muted || self.sounds.is_empty() {
|
||||||
@@ -136,9 +138,11 @@ impl Audio {
|
|||||||
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
|
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Instantly mute or unmute all channels.
|
/// Instantly mutes or unmutes all audio channels by adjusting their volume.
|
||||||
///
|
///
|
||||||
/// If audio is disabled, this function does nothing.
|
/// Sets all 4 mixer channels to zero volume when muting, or restores them to
|
||||||
|
/// their default volume (32) when unmuting. The mute state is tracked internally
|
||||||
|
/// regardless of whether audio is disabled, allowing the state to be preserved.
|
||||||
pub fn set_mute(&mut self, mute: bool) {
|
pub fn set_mute(&mut self, mute: bool) {
|
||||||
if !self.disabled {
|
if !self.disabled {
|
||||||
let channels = 4;
|
let channels = 4;
|
||||||
@@ -151,12 +155,19 @@ impl Audio {
|
|||||||
self.muted = mute;
|
self.muted = mute;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the audio is muted.
|
/// Returns the current mute state regardless of whether audio is functional.
|
||||||
|
///
|
||||||
|
/// This tracks the user's mute preference and will return `true` if muted
|
||||||
|
/// even when the audio system is disabled due to initialization failures.
|
||||||
pub fn is_muted(&self) -> bool {
|
pub fn is_muted(&self) -> bool {
|
||||||
self.muted
|
self.muted
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the audio system is disabled.
|
/// Returns whether the audio system failed to initialize and is non-functional.
|
||||||
|
///
|
||||||
|
/// Audio can be disabled due to SDL2_mixer initialization failures, missing
|
||||||
|
/// audio device, or failure to load any sound assets. When disabled, all
|
||||||
|
/// audio operations become no-ops.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn is_disabled(&self) -> bool {
|
pub fn is_disabled(&self) -> bool {
|
||||||
self.disabled
|
self.disabled
|
||||||
|
|||||||
@@ -4,6 +4,11 @@ use std::time::Duration;
|
|||||||
|
|
||||||
use glam::UVec2;
|
use glam::UVec2;
|
||||||
|
|
||||||
|
/// Target frame duration for 60 FPS game loop timing.
|
||||||
|
///
|
||||||
|
/// Calculated as 1/60th of a second (≈16.67ms).
|
||||||
|
///
|
||||||
|
/// Written out explicitly to satisfy const-eval constraints.
|
||||||
pub const LOOP_TIME: Duration = Duration::from_nanos((1_000_000_000.0 / 60.0) as u64);
|
pub const LOOP_TIME: Duration = Duration::from_nanos((1_000_000_000.0 / 60.0) as u64);
|
||||||
|
|
||||||
/// The size of each cell, in pixels.
|
/// The size of each cell, in pixels.
|
||||||
@@ -14,34 +19,41 @@ pub const BOARD_CELL_SIZE: UVec2 = UVec2::new(28, 31);
|
|||||||
/// The scale factor for the window (integer zoom)
|
/// The scale factor for the window (integer zoom)
|
||||||
pub const SCALE: f32 = 2.6;
|
pub const SCALE: f32 = 2.6;
|
||||||
|
|
||||||
/// The offset of the game board from the top-left corner of the window, in cells.
|
/// Game board offset from window origin to reserve space for HUD elements.
|
||||||
|
///
|
||||||
|
/// The 3-cell vertical offset (24 pixels) provides space at the top of the
|
||||||
|
/// screen for score display, player lives, and other UI elements.
|
||||||
pub const BOARD_CELL_OFFSET: UVec2 = UVec2::new(0, 3);
|
pub const BOARD_CELL_OFFSET: UVec2 = UVec2::new(0, 3);
|
||||||
/// The offset of the game board from the top-left corner of the window, in pixels.
|
|
||||||
|
/// Pixel-space equivalent of `BOARD_CELL_OFFSET` for rendering calculations.
|
||||||
|
///
|
||||||
|
/// Automatically calculated from the cell offset to maintain consistency
|
||||||
|
/// when the cell size changes. Used for positioning sprites and debug overlays.
|
||||||
pub const BOARD_PIXEL_OFFSET: UVec2 = UVec2::new(BOARD_CELL_OFFSET.x * CELL_SIZE, BOARD_CELL_OFFSET.y * CELL_SIZE);
|
pub const BOARD_PIXEL_OFFSET: UVec2 = UVec2::new(BOARD_CELL_OFFSET.x * CELL_SIZE, BOARD_CELL_OFFSET.y * CELL_SIZE);
|
||||||
/// The size of the game board, in pixels.
|
|
||||||
pub const BOARD_PIXEL_SIZE: UVec2 = UVec2::new(BOARD_CELL_SIZE.x * CELL_SIZE, BOARD_CELL_SIZE.y * CELL_SIZE);
|
|
||||||
/// The size of the canvas, in pixels.
|
/// The size of the canvas, in pixels.
|
||||||
pub const CANVAS_SIZE: UVec2 = UVec2::new(
|
pub const CANVAS_SIZE: UVec2 = UVec2::new(
|
||||||
(BOARD_CELL_SIZE.x + BOARD_CELL_OFFSET.x) * CELL_SIZE,
|
(BOARD_CELL_SIZE.x + BOARD_CELL_OFFSET.x) * CELL_SIZE,
|
||||||
(BOARD_CELL_SIZE.y + BOARD_CELL_OFFSET.y) * CELL_SIZE,
|
(BOARD_CELL_SIZE.y + BOARD_CELL_OFFSET.y) * CELL_SIZE,
|
||||||
);
|
);
|
||||||
|
|
||||||
/// An enum representing the different types of tiles on the map.
|
/// Map tile types that define gameplay behavior and collision properties.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum MapTile {
|
pub enum MapTile {
|
||||||
/// An empty tile.
|
/// Traversable space with no collectible items
|
||||||
Empty,
|
Empty,
|
||||||
/// A wall tile.
|
|
||||||
Wall,
|
Wall,
|
||||||
/// A regular pellet.
|
/// Small collectible. Implicitly a traversable tile.
|
||||||
Pellet,
|
Pellet,
|
||||||
/// A power pellet.
|
/// Large collectible. Implicitly a traversable tile.
|
||||||
PowerPellet,
|
PowerPellet,
|
||||||
/// A tunnel tile.
|
/// Special traversable tile that connects to tunnel portals.
|
||||||
Tunnel,
|
Tunnel,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The raw layout of the game board, as a 2D array of characters.
|
/// ASCII art representation of the classic Pac-Man maze layout.
|
||||||
|
///
|
||||||
|
/// Uses character symbols to define the game world. This layout is parsed by `MapTileParser`
|
||||||
|
/// to generate the navigable graph and collision geometry.
|
||||||
pub const RAW_BOARD: [&str; BOARD_CELL_SIZE.y as usize] = [
|
pub const RAW_BOARD: [&str; BOARD_CELL_SIZE.y as usize] = [
|
||||||
"############################",
|
"############################",
|
||||||
"#............##............#",
|
"#............##............#",
|
||||||
|
|||||||
@@ -1,128 +0,0 @@
|
|||||||
use smallvec::SmallVec;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::entity::traversal::Position;
|
|
||||||
|
|
||||||
/// Trait for entities that can participate in collision detection.
|
|
||||||
pub trait Collidable {
|
|
||||||
/// Returns the current position of this entity.
|
|
||||||
fn position(&self) -> Position;
|
|
||||||
|
|
||||||
/// Checks if this entity is colliding with another entity.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
fn is_colliding_with(&self, other: &dyn Collidable) -> bool {
|
|
||||||
positions_overlap(&self.position(), &other.position())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// System for tracking entities by their positions for efficient collision detection.
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct CollisionSystem {
|
|
||||||
/// Maps node IDs to lists of entity IDs that are at that node
|
|
||||||
node_entities: HashMap<usize, Vec<EntityId>>,
|
|
||||||
/// Maps entity IDs to their current positions
|
|
||||||
entity_positions: HashMap<EntityId, Position>,
|
|
||||||
/// Next available entity ID
|
|
||||||
next_id: EntityId,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Unique identifier for an entity in the collision system
|
|
||||||
pub type EntityId = u32;
|
|
||||||
|
|
||||||
impl CollisionSystem {
|
|
||||||
/// Registers an entity with the collision system and returns its ID
|
|
||||||
pub fn register_entity(&mut self, position: Position) -> EntityId {
|
|
||||||
let id = self.next_id;
|
|
||||||
self.next_id += 1;
|
|
||||||
|
|
||||||
self.entity_positions.insert(id, position);
|
|
||||||
self.update_node_entities(id, position);
|
|
||||||
|
|
||||||
id
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Updates an entity's position
|
|
||||||
pub fn update_position(&mut self, entity_id: EntityId, new_position: Position) {
|
|
||||||
if let Some(old_position) = self.entity_positions.get(&entity_id) {
|
|
||||||
// Remove from old nodes
|
|
||||||
self.remove_from_nodes(entity_id, *old_position);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update position and add to new nodes
|
|
||||||
self.entity_positions.insert(entity_id, new_position);
|
|
||||||
self.update_node_entities(entity_id, new_position);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes an entity from the collision system
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn remove_entity(&mut self, entity_id: EntityId) {
|
|
||||||
if let Some(position) = self.entity_positions.remove(&entity_id) {
|
|
||||||
self.remove_from_nodes(entity_id, position);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets all entity IDs at a specific node
|
|
||||||
pub fn entities_at_node(&self, node: usize) -> &[EntityId] {
|
|
||||||
self.node_entities.get(&node).map(|v| v.as_slice()).unwrap_or(&[])
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets all entity IDs that could collide with an entity at the given position
|
|
||||||
pub fn potential_collisions(&self, position: &Position) -> Vec<EntityId> {
|
|
||||||
let mut collisions = Vec::new();
|
|
||||||
let nodes = get_nodes(position);
|
|
||||||
|
|
||||||
for node in nodes {
|
|
||||||
collisions.extend(self.entities_at_node(node));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove duplicates
|
|
||||||
collisions.sort_unstable();
|
|
||||||
collisions.dedup();
|
|
||||||
collisions
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Updates the node_entities map when an entity's position changes
|
|
||||||
fn update_node_entities(&mut self, entity_id: EntityId, position: Position) {
|
|
||||||
let nodes = get_nodes(&position);
|
|
||||||
for node in nodes {
|
|
||||||
self.node_entities.entry(node).or_default().push(entity_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Removes an entity from all nodes it was previously at
|
|
||||||
fn remove_from_nodes(&mut self, entity_id: EntityId, position: Position) {
|
|
||||||
let nodes = get_nodes(&position);
|
|
||||||
for node in nodes {
|
|
||||||
if let Some(entities) = self.node_entities.get_mut(&node) {
|
|
||||||
entities.retain(|&id| id != entity_id);
|
|
||||||
if entities.is_empty() {
|
|
||||||
self.node_entities.remove(&node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Checks if two positions overlap (entities are at the same location).
|
|
||||||
fn positions_overlap(a: &Position, b: &Position) -> bool {
|
|
||||||
let a_nodes = get_nodes(a);
|
|
||||||
let b_nodes = get_nodes(b);
|
|
||||||
|
|
||||||
// Check if any nodes overlap
|
|
||||||
a_nodes.iter().any(|a_node| b_nodes.contains(a_node))
|
|
||||||
|
|
||||||
// TODO: More complex overlap detection, the above is a simple check, but it could become an early filter for more precise calculations later
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets all nodes that an entity is currently at or between.
|
|
||||||
fn get_nodes(pos: &Position) -> SmallVec<[usize; 2]> {
|
|
||||||
let mut nodes = SmallVec::new();
|
|
||||||
match pos {
|
|
||||||
Position::AtNode(node) => nodes.push(*node),
|
|
||||||
Position::BetweenNodes { from, to, .. } => {
|
|
||||||
nodes.push(*from);
|
|
||||||
nodes.push(*to);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nodes
|
|
||||||
}
|
|
||||||
@@ -1,254 +0,0 @@
|
|||||||
//! Ghost entity implementation.
|
|
||||||
//!
|
|
||||||
//! This module contains the ghost character logic, including movement,
|
|
||||||
//! animation, and rendering. Ghosts move through the game graph using
|
|
||||||
//! a traverser and display directional animated textures.
|
|
||||||
|
|
||||||
use pathfinding::prelude::dijkstra;
|
|
||||||
use rand::prelude::*;
|
|
||||||
use smallvec::SmallVec;
|
|
||||||
use tracing::error;
|
|
||||||
|
|
||||||
use crate::entity::{
|
|
||||||
collision::Collidable,
|
|
||||||
direction::Direction,
|
|
||||||
graph::{Edge, EdgePermissions, Graph, NodeId},
|
|
||||||
r#trait::Entity,
|
|
||||||
traversal::Traverser,
|
|
||||||
};
|
|
||||||
use crate::texture::animated::AnimatedTexture;
|
|
||||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
|
||||||
use crate::texture::sprite::SpriteAtlas;
|
|
||||||
|
|
||||||
use crate::error::{EntityError, GameError, GameResult, TextureError};
|
|
||||||
|
|
||||||
/// Determines if a ghost can traverse a given edge.
|
|
||||||
///
|
|
||||||
/// Ghosts can move through edges that allow all entities or ghost-only edges.
|
|
||||||
fn can_ghost_traverse(edge: Edge) -> bool {
|
|
||||||
matches!(edge.permissions, EdgePermissions::All | EdgePermissions::GhostsOnly)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The four classic ghost types.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub enum GhostType {
|
|
||||||
Blinky,
|
|
||||||
Pinky,
|
|
||||||
Inky,
|
|
||||||
Clyde,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GhostType {
|
|
||||||
/// Returns the ghost type name for atlas lookups.
|
|
||||||
pub fn as_str(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
GhostType::Blinky => "blinky",
|
|
||||||
GhostType::Pinky => "pinky",
|
|
||||||
GhostType::Inky => "inky",
|
|
||||||
GhostType::Clyde => "clyde",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the base movement speed for this ghost type.
|
|
||||||
pub fn base_speed(self) -> f32 {
|
|
||||||
match self {
|
|
||||||
GhostType::Blinky => 1.0,
|
|
||||||
GhostType::Pinky => 0.95,
|
|
||||||
GhostType::Inky => 0.9,
|
|
||||||
GhostType::Clyde => 0.85,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A ghost entity that roams the game world.
|
|
||||||
///
|
|
||||||
/// Ghosts move through the game world using a graph-based navigation system
|
|
||||||
/// and display directional animated sprites. They randomly choose directions
|
|
||||||
/// at each intersection.
|
|
||||||
pub struct Ghost {
|
|
||||||
/// Handles movement through the game graph
|
|
||||||
pub traverser: Traverser,
|
|
||||||
/// The type of ghost (affects appearance and speed)
|
|
||||||
pub ghost_type: GhostType,
|
|
||||||
/// Manages directional animated textures for different movement states
|
|
||||||
texture: DirectionalAnimatedTexture,
|
|
||||||
/// Current movement speed
|
|
||||||
speed: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Entity for Ghost {
|
|
||||||
fn traverser(&self) -> &Traverser {
|
|
||||||
&self.traverser
|
|
||||||
}
|
|
||||||
|
|
||||||
fn traverser_mut(&mut self) -> &mut Traverser {
|
|
||||||
&mut self.traverser
|
|
||||||
}
|
|
||||||
|
|
||||||
fn texture(&self) -> &DirectionalAnimatedTexture {
|
|
||||||
&self.texture
|
|
||||||
}
|
|
||||||
|
|
||||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
|
||||||
&mut self.texture
|
|
||||||
}
|
|
||||||
|
|
||||||
fn speed(&self) -> f32 {
|
|
||||||
self.speed
|
|
||||||
}
|
|
||||||
|
|
||||||
fn can_traverse(&self, edge: Edge) -> bool {
|
|
||||||
can_ghost_traverse(edge)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tick(&mut self, dt: f32, graph: &Graph) {
|
|
||||||
// Choose random direction when at a node
|
|
||||||
if self.traverser.position.is_at_node() {
|
|
||||||
self.choose_random_direction(graph);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Err(e) = self.traverser.advance(graph, dt * 60.0 * self.speed, &can_ghost_traverse) {
|
|
||||||
error!("Ghost movement error: {}", e);
|
|
||||||
}
|
|
||||||
self.texture.tick(dt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ghost {
|
|
||||||
/// Creates a new ghost instance at the specified starting node.
|
|
||||||
///
|
|
||||||
/// Sets up animated textures for all four directions with moving and stopped states.
|
|
||||||
/// The moving animation cycles through two sprite variants.
|
|
||||||
pub fn new(graph: &Graph, start_node: NodeId, ghost_type: GhostType, atlas: &SpriteAtlas) -> GameResult<Self> {
|
|
||||||
let mut textures = [None, None, None, None];
|
|
||||||
let mut stopped_textures = [None, None, None, None];
|
|
||||||
|
|
||||||
for direction in Direction::DIRECTIONS {
|
|
||||||
let moving_prefix = match direction {
|
|
||||||
Direction::Up => "up",
|
|
||||||
Direction::Down => "down",
|
|
||||||
Direction::Left => "left",
|
|
||||||
Direction::Right => "right",
|
|
||||||
};
|
|
||||||
let moving_tiles = vec![
|
|
||||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/{}_{}.png",
|
|
||||||
ghost_type.as_str(),
|
|
||||||
moving_prefix,
|
|
||||||
"a"
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "b"))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/{}_{}.png",
|
|
||||||
ghost_type.as_str(),
|
|
||||||
moving_prefix,
|
|
||||||
"b"
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
|
||||||
|
|
||||||
let stopped_tiles =
|
|
||||||
vec![
|
|
||||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
|
||||||
.ok_or_else(|| {
|
|
||||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
|
||||||
"ghost/{}/{}_{}.png",
|
|
||||||
ghost_type.as_str(),
|
|
||||||
moving_prefix,
|
|
||||||
"a"
|
|
||||||
)))
|
|
||||||
})?,
|
|
||||||
];
|
|
||||||
|
|
||||||
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.2)?);
|
|
||||||
stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
traverser: Traverser::new(graph, start_node, Direction::Left, &can_ghost_traverse),
|
|
||||||
ghost_type,
|
|
||||||
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
|
||||||
speed: ghost_type.base_speed(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Chooses a random available direction at the current intersection.
|
|
||||||
fn choose_random_direction(&mut self, graph: &Graph) {
|
|
||||||
let current_node = self.traverser.position.from_node_id();
|
|
||||||
let intersection = &graph.adjacency_list[current_node];
|
|
||||||
|
|
||||||
// Collect all available directions
|
|
||||||
let mut available_directions = SmallVec::<[_; 4]>::new();
|
|
||||||
for direction in Direction::DIRECTIONS {
|
|
||||||
if let Some(edge) = intersection.get(direction) {
|
|
||||||
if can_ghost_traverse(edge) {
|
|
||||||
available_directions.push(direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Choose a random direction (avoid reversing unless necessary)
|
|
||||||
if !available_directions.is_empty() {
|
|
||||||
let mut rng = SmallRng::from_os_rng();
|
|
||||||
|
|
||||||
// Filter out the opposite direction if possible, but allow it if we have limited options
|
|
||||||
let opposite = self.traverser.direction.opposite();
|
|
||||||
let filtered_directions: Vec<_> = available_directions
|
|
||||||
.iter()
|
|
||||||
.filter(|&&dir| dir != opposite || available_directions.len() <= 2)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if let Some(&random_direction) = filtered_directions.choose(&mut rng) {
|
|
||||||
self.traverser.set_next_direction(*random_direction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculates the shortest path from the ghost's current position to a target node using Dijkstra's algorithm.
|
|
||||||
///
|
|
||||||
/// Returns a vector of NodeIds representing the path, or an error if pathfinding fails.
|
|
||||||
/// The path includes the current node and the target node.
|
|
||||||
pub fn calculate_path_to_target(&self, graph: &Graph, target: NodeId) -> GameResult<Vec<NodeId>> {
|
|
||||||
let start_node = self.traverser.position.from_node_id();
|
|
||||||
|
|
||||||
// Use Dijkstra's algorithm to find the shortest path
|
|
||||||
let result = dijkstra(
|
|
||||||
&start_node,
|
|
||||||
|&node_id| {
|
|
||||||
// Get all edges from the current node
|
|
||||||
graph.adjacency_list[node_id]
|
|
||||||
.edges()
|
|
||||||
.filter(|edge| can_ghost_traverse(*edge))
|
|
||||||
.map(|edge| (edge.target, (edge.distance * 100.0) as u32))
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
},
|
|
||||||
|&node_id| node_id == target,
|
|
||||||
);
|
|
||||||
|
|
||||||
result.map(|(path, _cost)| path).ok_or_else(|| {
|
|
||||||
GameError::Entity(EntityError::PathfindingFailed(format!(
|
|
||||||
"No path found from node {} to target {}",
|
|
||||||
start_node, target
|
|
||||||
)))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the ghost's color for debug rendering.
|
|
||||||
pub fn debug_color(&self) -> sdl2::pixels::Color {
|
|
||||||
match self.ghost_type {
|
|
||||||
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
|
|
||||||
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
|
|
||||||
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
|
|
||||||
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Collidable for Ghost {
|
|
||||||
fn position(&self) -> crate::entity::traversal::Position {
|
|
||||||
self.traverser.position
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
use crate::{
|
|
||||||
constants,
|
|
||||||
entity::{collision::Collidable, graph::Graph},
|
|
||||||
error::{EntityError, GameResult},
|
|
||||||
texture::sprite::{Sprite, SpriteAtlas},
|
|
||||||
};
|
|
||||||
use sdl2::render::{Canvas, RenderTarget};
|
|
||||||
use strum_macros::{EnumCount, EnumIter};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub enum ItemType {
|
|
||||||
Pellet,
|
|
||||||
Energizer,
|
|
||||||
#[allow(dead_code)]
|
|
||||||
Fruit {
|
|
||||||
kind: FruitKind,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ItemType {
|
|
||||||
pub fn get_score(self) -> u32 {
|
|
||||||
match self {
|
|
||||||
ItemType::Pellet => 10,
|
|
||||||
ItemType::Energizer => 50,
|
|
||||||
ItemType::Fruit { kind } => kind.get_score(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, EnumCount)]
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub enum FruitKind {
|
|
||||||
Apple,
|
|
||||||
Strawberry,
|
|
||||||
Orange,
|
|
||||||
Melon,
|
|
||||||
Bell,
|
|
||||||
Key,
|
|
||||||
Galaxian,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FruitKind {
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn index(self) -> u8 {
|
|
||||||
match self {
|
|
||||||
FruitKind::Apple => 0,
|
|
||||||
FruitKind::Strawberry => 1,
|
|
||||||
FruitKind::Orange => 2,
|
|
||||||
FruitKind::Melon => 3,
|
|
||||||
FruitKind::Bell => 4,
|
|
||||||
FruitKind::Key => 5,
|
|
||||||
FruitKind::Galaxian => 6,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_score(self) -> u32 {
|
|
||||||
match self {
|
|
||||||
FruitKind::Apple => 100,
|
|
||||||
FruitKind::Strawberry => 300,
|
|
||||||
FruitKind::Orange => 500,
|
|
||||||
FruitKind::Melon => 700,
|
|
||||||
FruitKind::Bell => 1000,
|
|
||||||
FruitKind::Key => 2000,
|
|
||||||
FruitKind::Galaxian => 3000,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Item {
|
|
||||||
pub node_index: usize,
|
|
||||||
pub item_type: ItemType,
|
|
||||||
pub sprite: Sprite,
|
|
||||||
pub collected: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Item {
|
|
||||||
pub fn new(node_index: usize, item_type: ItemType, sprite: Sprite) -> Self {
|
|
||||||
Self {
|
|
||||||
node_index,
|
|
||||||
item_type,
|
|
||||||
sprite,
|
|
||||||
collected: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_collected(&self) -> bool {
|
|
||||||
self.collected
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn collect(&mut self) {
|
|
||||||
self.collected = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_score(&self) -> u32 {
|
|
||||||
self.item_type.get_score()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
|
||||||
if self.collected {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
let node = graph
|
|
||||||
.get_node(self.node_index)
|
|
||||||
.ok_or(EntityError::NodeNotFound(self.node_index))?;
|
|
||||||
let position = node.position + constants::BOARD_PIXEL_OFFSET.as_vec2();
|
|
||||||
|
|
||||||
self.sprite.render(canvas, atlas, position)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Collidable for Item {
|
|
||||||
fn position(&self) -> crate::entity::traversal::Position {
|
|
||||||
crate::entity::traversal::Position::AtNode(self.node_index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
pub mod collision;
|
|
||||||
pub mod direction;
|
|
||||||
pub mod ghost;
|
|
||||||
pub mod graph;
|
|
||||||
pub mod item;
|
|
||||||
pub mod pacman;
|
|
||||||
pub mod r#trait;
|
|
||||||
pub mod traversal;
|
|
||||||
@@ -1,134 +0,0 @@
|
|||||||
//! Pac-Man entity implementation.
|
|
||||||
//!
|
|
||||||
//! This module contains the main player character logic, including movement,
|
|
||||||
//! animation, and rendering. Pac-Man moves through the game graph using
|
|
||||||
//! a traverser and displays directional animated textures.
|
|
||||||
|
|
||||||
use crate::entity::{
|
|
||||||
collision::Collidable,
|
|
||||||
direction::Direction,
|
|
||||||
graph::{Edge, EdgePermissions, Graph, NodeId},
|
|
||||||
r#trait::Entity,
|
|
||||||
traversal::Traverser,
|
|
||||||
};
|
|
||||||
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};
|
|
||||||
|
|
||||||
/// Determines if Pac-Man can traverse a given edge.
|
|
||||||
///
|
|
||||||
/// Pac-Man can only move through edges that allow all entities.
|
|
||||||
fn can_pacman_traverse(edge: Edge) -> bool {
|
|
||||||
matches!(edge.permissions, EdgePermissions::All)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The main player character entity.
|
|
||||||
///
|
|
||||||
/// Pac-Man moves through the game world using a graph-based navigation system
|
|
||||||
/// and displays directional animated sprites based on movement state.
|
|
||||||
pub struct Pacman {
|
|
||||||
/// Handles movement through the game graph
|
|
||||||
pub traverser: Traverser,
|
|
||||||
/// Manages directional animated textures for different movement states
|
|
||||||
texture: DirectionalAnimatedTexture,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Entity for Pacman {
|
|
||||||
fn traverser(&self) -> &Traverser {
|
|
||||||
&self.traverser
|
|
||||||
}
|
|
||||||
|
|
||||||
fn traverser_mut(&mut self) -> &mut Traverser {
|
|
||||||
&mut self.traverser
|
|
||||||
}
|
|
||||||
|
|
||||||
fn texture(&self) -> &DirectionalAnimatedTexture {
|
|
||||||
&self.texture
|
|
||||||
}
|
|
||||||
|
|
||||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
|
||||||
&mut self.texture
|
|
||||||
}
|
|
||||||
|
|
||||||
fn speed(&self) -> f32 {
|
|
||||||
1.125
|
|
||||||
}
|
|
||||||
|
|
||||||
fn can_traverse(&self, edge: Edge) -> bool {
|
|
||||||
can_pacman_traverse(edge)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn tick(&mut self, dt: f32, graph: &Graph) {
|
|
||||||
if let Err(e) = self.traverser.advance(graph, dt * 60.0 * 1.125, &can_pacman_traverse) {
|
|
||||||
error!("Pac-Man movement error: {}", e);
|
|
||||||
}
|
|
||||||
self.texture.tick(dt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Pacman {
|
|
||||||
/// Creates a new Pac-Man instance at the specified starting node.
|
|
||||||
///
|
|
||||||
/// Sets up animated textures for all four directions with moving and stopped states.
|
|
||||||
/// The moving animation cycles through open mouth, closed mouth, and full sprites.
|
|
||||||
pub fn new(graph: &Graph, start_node: NodeId, atlas: &SpriteAtlas) -> GameResult<Self> {
|
|
||||||
let mut textures = [None, None, None, None];
|
|
||||||
let mut stopped_textures = [None, None, None, None];
|
|
||||||
|
|
||||||
for direction in Direction::DIRECTIONS {
|
|
||||||
let moving_prefix = match direction {
|
|
||||||
Direction::Up => "pacman/up",
|
|
||||||
Direction::Down => "pacman/down",
|
|
||||||
Direction::Left => "pacman/left",
|
|
||||||
Direction::Right => "pacman/right",
|
|
||||||
};
|
|
||||||
let moving_tiles = vec![
|
|
||||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_a.png"))
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_a.png"))))?,
|
|
||||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?,
|
|
||||||
SpriteAtlas::get_tile(atlas, "pacman/full.png")
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
|
||||||
];
|
|
||||||
|
|
||||||
let stopped_tiles = vec![SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
|
||||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?];
|
|
||||||
|
|
||||||
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.08)?);
|
|
||||||
stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
traverser: Traverser::new(graph, start_node, Direction::Left, &can_pacman_traverse),
|
|
||||||
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 {
|
|
||||||
fn position(&self) -> crate::entity::traversal::Position {
|
|
||||||
self.traverser.position
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,114 +0,0 @@
|
|||||||
//! Entity trait for common movement and rendering functionality.
|
|
||||||
//!
|
|
||||||
//! This module defines a trait that captures the shared behavior between
|
|
||||||
//! different game entities like Ghosts and Pac-Man, including movement,
|
|
||||||
//! rendering, and position calculations.
|
|
||||||
|
|
||||||
use glam::Vec2;
|
|
||||||
use sdl2::render::{Canvas, RenderTarget};
|
|
||||||
|
|
||||||
use crate::entity::direction::Direction;
|
|
||||||
use crate::entity::graph::{Edge, Graph, NodeId};
|
|
||||||
use crate::entity::traversal::{Position, Traverser};
|
|
||||||
use crate::error::{EntityError, GameError, GameResult, TextureError};
|
|
||||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
|
||||||
use crate::texture::sprite::SpriteAtlas;
|
|
||||||
|
|
||||||
/// Trait defining common functionality for game entities that move through the graph.
|
|
||||||
///
|
|
||||||
/// This trait provides a unified interface for entities that:
|
|
||||||
/// - Move through the game graph using a traverser
|
|
||||||
/// - Render using directional animated textures
|
|
||||||
/// - Have position calculations and movement speed
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub trait Entity {
|
|
||||||
/// Returns a reference to the entity's traverser for movement control.
|
|
||||||
fn traverser(&self) -> &Traverser;
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the entity's traverser for movement control.
|
|
||||||
fn traverser_mut(&mut self) -> &mut Traverser;
|
|
||||||
|
|
||||||
/// Returns a reference to the entity's directional animated texture.
|
|
||||||
fn texture(&self) -> &DirectionalAnimatedTexture;
|
|
||||||
|
|
||||||
/// Returns a mutable reference to the entity's directional animated texture.
|
|
||||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture;
|
|
||||||
|
|
||||||
/// Returns the movement speed multiplier for this entity.
|
|
||||||
fn speed(&self) -> f32;
|
|
||||||
|
|
||||||
/// Determines if this entity can traverse a given edge.
|
|
||||||
fn can_traverse(&self, edge: Edge) -> bool;
|
|
||||||
|
|
||||||
/// Updates the entity's position and animation state.
|
|
||||||
///
|
|
||||||
/// This method advances movement through the graph and updates texture animation.
|
|
||||||
fn tick(&mut self, dt: f32, graph: &Graph);
|
|
||||||
|
|
||||||
/// Calculates the current pixel position in the game world.
|
|
||||||
///
|
|
||||||
/// Converts the graph position to screen coordinates, accounting for
|
|
||||||
/// the board offset and centering the sprite.
|
|
||||||
fn get_pixel_pos(&self, graph: &Graph) -> GameResult<Vec2> {
|
|
||||||
let pos = match self.traverser().position {
|
|
||||||
Position::AtNode(node_id) => {
|
|
||||||
let node = graph.get_node(node_id).ok_or(EntityError::NodeNotFound(node_id))?;
|
|
||||||
node.position
|
|
||||||
}
|
|
||||||
Position::BetweenNodes { from, to, traversed } => {
|
|
||||||
let from_node = graph.get_node(from).ok_or(EntityError::NodeNotFound(from))?;
|
|
||||||
let to_node = graph.get_node(to).ok_or(EntityError::NodeNotFound(to))?;
|
|
||||||
let edge = graph.find_edge(from, to).ok_or(EntityError::EdgeNotFound { from, to })?;
|
|
||||||
from_node.position + (to_node.position - from_node.position) * (traversed / edge.distance)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Vec2::new(
|
|
||||||
pos.x + crate::constants::BOARD_PIXEL_OFFSET.x as f32,
|
|
||||||
pos.y + crate::constants::BOARD_PIXEL_OFFSET.y as f32,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current node ID that the entity is at or moving towards.
|
|
||||||
///
|
|
||||||
/// If the entity is at a node, returns that node ID.
|
|
||||||
/// If the entity is between nodes, returns the node it's moving towards.
|
|
||||||
fn current_node_id(&self) -> NodeId {
|
|
||||||
match self.traverser().position {
|
|
||||||
Position::AtNode(node_id) => node_id,
|
|
||||||
Position::BetweenNodes { to, .. } => to,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the next direction for the entity to take.
|
|
||||||
///
|
|
||||||
/// The direction is buffered and will be applied at the next opportunity,
|
|
||||||
/// typically when the entity reaches a new node.
|
|
||||||
fn set_next_direction(&mut self, direction: Direction) {
|
|
||||||
self.traverser_mut().set_next_direction(direction);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Renders the entity at its current position.
|
|
||||||
///
|
|
||||||
/// Draws the appropriate directional sprite based on the entity's
|
|
||||||
/// current movement state and direction.
|
|
||||||
fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
|
||||||
let pixel_pos = self.get_pixel_pos(graph)?;
|
|
||||||
let dest = crate::helpers::centered_with_size(
|
|
||||||
glam::IVec2::new(pixel_pos.x as i32, pixel_pos.y as i32),
|
|
||||||
glam::UVec2::new(16, 16),
|
|
||||||
);
|
|
||||||
|
|
||||||
if self.traverser().position.is_stopped() {
|
|
||||||
self.texture()
|
|
||||||
.render_stopped(canvas, atlas, dest, self.traverser().direction)
|
|
||||||
.map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
|
||||||
} else {
|
|
||||||
self.texture()
|
|
||||||
.render(canvas, atlas, dest, self.traverser().direction)
|
|
||||||
.map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||