mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-07 20:07:46 -06:00
Compare commits
22 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1f8e7c6d71 | |||
| 27079e127d | |||
| 5e9bb3535e | |||
| 250cf2fc89 | |||
| 57975495a9 | |||
| f3e7a780e2 | |||
| ee6cb0a670 | |||
| b3df34b405 | |||
| dbafa17670 | |||
| d9c8f97903 | |||
| ad2ec35bfb | |||
| 6331ba0b2f | |||
| 3d275b8e85 | |||
| bd61db9aae | |||
| ed8bd07518 | |||
| 27705f1ba2 | |||
| e964adc818 | |||
| c5213320ac | |||
| e0f8443e75 | |||
| 6702b3723a | |||
| f6e7228f75 | |||
| 14cebe4462 |
20
.github/dependabot.yml
vendored
Normal file
20
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "cargo"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
dependencies:
|
||||||
|
patterns:
|
||||||
|
- "*"
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
dependencies:
|
||||||
|
patterns:
|
||||||
|
- "*"
|
||||||
27
.github/workflows/audit.yaml
vendored
27
.github/workflows/audit.yaml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: Audit
|
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
RUST_TOOLCHAIN: 1.88.0
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
audit:
|
|
||||||
name: Audit
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install Rust toolchain
|
|
||||||
uses: dtolnay/rust-toolchain@master
|
|
||||||
with:
|
|
||||||
toolchain: ${{ env.RUST_TOOLCHAIN }}
|
|
||||||
|
|
||||||
- name: Install cargo-audit
|
|
||||||
run: cargo install cargo-audit
|
|
||||||
|
|
||||||
- name: Run security audit
|
|
||||||
run: cargo audit
|
|
||||||
1
.github/workflows/build.yaml
vendored
1
.github/workflows/build.yaml
vendored
@@ -1,5 +1,4 @@
|
|||||||
name: Builds
|
name: Builds
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
|
|||||||
3
.github/workflows/coverage.yaml
vendored
3
.github/workflows/coverage.yaml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: Coverage
|
name: Code Coverage
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
@@ -8,7 +8,6 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
coverage:
|
coverage:
|
||||||
name: Code Coverage
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
name: Tests
|
name: Tests & Checks
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
@@ -8,7 +8,6 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: Test
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
@@ -52,3 +51,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Check formatting
|
- name: Check formatting
|
||||||
run: cargo fmt -- --check
|
run: cargo fmt -- --check
|
||||||
|
|
||||||
|
- uses: taiki-e/install-action@cargo-audit
|
||||||
|
|
||||||
|
- name: Run security audit
|
||||||
|
run: cargo audit
|
||||||
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -192,6 +192,7 @@ dependencies = [
|
|||||||
"sdl2",
|
"sdl2",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"smallvec",
|
||||||
"spin_sleep",
|
"spin_sleep",
|
||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
"tracing",
|
"tracing",
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ anyhow = "1.0"
|
|||||||
glam = { version = "0.30.4", features = [] }
|
glam = { version = "0.30.4", features = [] }
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
serde_json = "1.0.141"
|
serde_json = "1.0.141"
|
||||||
|
smallvec = "1.15.1"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
[![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] [![Code Coverage][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/test.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
|
||||||
[badge-coverage]: https://coveralls.io/repos/github/Xevion/Pac-Man/badge.svg?branch=master
|
[badge-coverage]: https://coveralls.io/repos/github/Xevion/Pac-Man/badge.svg?branch=master
|
||||||
[badge-demo]: https://img.shields.io/github/deployments/Xevion/Pac-Man/github-pages?label=GitHub%20Pages
|
[badge-demo]: https://img.shields.io/github/deployments/Xevion/Pac-Man/github-pages?label=GitHub%20Pages
|
||||||
[badge-online-demo]: https://img.shields.io/badge/GitHub%20Pages-Demo-brightgreen
|
[badge-online-demo]: https://img.shields.io/badge/GitHub%20Pages-Demo-brightgreen
|
||||||
[badge-last-commit]: https://img.shields.io/github/last-commit/Xevion/Pac-Man
|
[badge-last-commit]: https://img.shields.io/github/last-commit/Xevion/Pac-Man
|
||||||
[build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml
|
[build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml
|
||||||
[test]: https://github.com/Xevion/Pac-Man/actions/workflows/test.yaml
|
[test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml
|
||||||
[coverage]: https://coveralls.io/github/Xevion/Pac-Man?branch=master
|
[coverage]: https://coveralls.io/github/Xevion/Pac-Man?branch=master
|
||||||
[demo]: https://xevion.github.io/Pac-Man/
|
[demo]: https://xevion.github.io/Pac-Man/
|
||||||
[commits]: https://github.com/Xevion/Pac-Man/commits/master
|
[commits]: https://github.com/Xevion/Pac-Man/commits/master
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
width="80"
|
width="80"
|
||||||
height="80"
|
height="80"
|
||||||
viewBox="0 0 250 250"
|
viewBox="0 0 250 250"
|
||||||
class="fill-yellow-400 text-white"
|
class="fill-yellow-400 [&>.octo-arm,.octo-body]:fill-black"
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
>
|
>
|
||||||
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
|
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
|
||||||
@@ -46,16 +46,12 @@
|
|||||||
</a>
|
</a>
|
||||||
|
|
||||||
<div class="min-h-screen flex flex-col">
|
<div class="min-h-screen flex flex-col">
|
||||||
<header class="pt-10">
|
|
||||||
<h1 class="text-4xl arcade-title scaled-text">Pac-Man in Rust</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<main class="flex-1 flex items-center justify-center px-4">
|
<main class="flex-1 flex items-center justify-center px-4">
|
||||||
<div class="w-full max-w-5xl">
|
<div class="w-full max-w-5xl">
|
||||||
<canvas
|
<canvas
|
||||||
id="canvas"
|
id="canvas"
|
||||||
oncontextmenu="event.preventDefault()"
|
oncontextmenu="event.preventDefault()"
|
||||||
class="block bg-black w-full max-w-[90vw] h-auto rounded-xl shadow-[inset_0_0_0_2px_rgba(255,255,255,0.12),0_10px_30px_rgba(0,0,0,0.8)]"
|
class="block w-full h-full max-h-[90vh] aspect-square"
|
||||||
></canvas>
|
></canvas>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
61
bacon.toml
Normal file
61
bacon.toml
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# This is a configuration file for the bacon tool
|
||||||
|
#
|
||||||
|
# Complete help on configuration: https://dystroy.org/bacon/config/
|
||||||
|
#
|
||||||
|
# You may check the current default at
|
||||||
|
# https://github.com/Canop/bacon/blob/main/defaults/default-bacon.toml
|
||||||
|
|
||||||
|
default_job = "check"
|
||||||
|
env.CARGO_TERM_COLOR = "always"
|
||||||
|
|
||||||
|
[jobs.check]
|
||||||
|
command = ["cargo", "check"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
[jobs.check-all]
|
||||||
|
command = ["cargo", "check", "--all-targets"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# Run clippy on the default target
|
||||||
|
[jobs.clippy]
|
||||||
|
command = ["cargo", "clippy"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# Run clippy on all targets
|
||||||
|
[jobs.clippy-all]
|
||||||
|
command = ["cargo", "clippy", "--all-targets"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
[jobs.test]
|
||||||
|
command = [
|
||||||
|
"cargo", "nextest", "run",
|
||||||
|
"--hide-progress-bar", "--failure-output", "final"
|
||||||
|
]
|
||||||
|
need_stdout = true
|
||||||
|
analyzer = "nextest"
|
||||||
|
|
||||||
|
[jobs.doc]
|
||||||
|
command = ["cargo", "doc", "--no-deps"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# If the doc compiles, then it opens in your browser and bacon switches to the previous job
|
||||||
|
[jobs.doc-open]
|
||||||
|
command = ["cargo", "doc", "--no-deps", "--open"]
|
||||||
|
need_stdout = false
|
||||||
|
on_success = "back" # so that we don't open the browser at each change
|
||||||
|
|
||||||
|
[jobs.run]
|
||||||
|
command = [
|
||||||
|
"cargo", "run",
|
||||||
|
]
|
||||||
|
need_stdout = true
|
||||||
|
allow_warnings = true
|
||||||
|
background = false
|
||||||
|
on_change_strategy = "kill_then_restart"
|
||||||
|
# kill = ["pkill", "-TERM", "-P"]'
|
||||||
|
|
||||||
|
[keybindings]
|
||||||
|
c = "job:clippy"
|
||||||
|
alt-c = "job:check"
|
||||||
|
ctrl-alt-c = "job:check-all"
|
||||||
|
shift-c = "job:clippy-all"
|
||||||
75
src/app.rs
75
src/app.rs
@@ -1,6 +1,6 @@
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use glam::Vec2;
|
||||||
use sdl2::event::{Event, WindowEvent};
|
use sdl2::event::{Event, WindowEvent};
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
|
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
|
||||||
@@ -8,21 +8,11 @@ use sdl2::video::{Window, WindowContext};
|
|||||||
use sdl2::EventPump;
|
use sdl2::EventPump;
|
||||||
use tracing::{error, event};
|
use tracing::{error, event};
|
||||||
|
|
||||||
|
use crate::error::{GameError, GameResult};
|
||||||
|
|
||||||
use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
|
use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
|
||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
|
use crate::platform::get_platform;
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
use crate::emscripten;
|
|
||||||
|
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
|
||||||
fn sleep(value: Duration) {
|
|
||||||
spin_sleep::sleep(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
fn sleep(value: Duration) {
|
|
||||||
emscripten::emscripten::sleep(value.as_millis() as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
game: Game,
|
game: Game,
|
||||||
@@ -31,14 +21,18 @@ pub struct App<'a> {
|
|||||||
backbuffer: Texture<'a>,
|
backbuffer: Texture<'a>,
|
||||||
paused: bool,
|
paused: bool,
|
||||||
last_tick: Instant,
|
last_tick: Instant,
|
||||||
|
cursor_pos: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App<'_> {
|
impl App<'_> {
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> GameResult<Self> {
|
||||||
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
|
// Initialize platform-specific console
|
||||||
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
|
get_platform().init_console()?;
|
||||||
let audio_subsystem = sdl_context.audio().map_err(|e| anyhow!(e))?;
|
|
||||||
let ttf_context = sdl2::ttf::init().map_err(|e| anyhow!(e.to_string()))?;
|
let sdl_context = sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
let video_subsystem = sdl_context.video().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
let audio_subsystem = sdl_context.audio().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
let ttf_context = sdl2::ttf::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let window = video_subsystem
|
let window = video_subsystem
|
||||||
.window(
|
.window(
|
||||||
@@ -48,24 +42,31 @@ impl App<'_> {
|
|||||||
)
|
)
|
||||||
.resizable()
|
.resizable()
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.build()?;
|
.build()
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let mut canvas = window.into_canvas().build()?;
|
let mut canvas = window.into_canvas().build().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
canvas.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)?;
|
canvas
|
||||||
|
.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let texture_creator_static: &'static TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
let texture_creator_static: &'static TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
||||||
|
|
||||||
let mut game = Game::new(texture_creator_static, &ttf_context, &audio_subsystem);
|
let mut game = Game::new(texture_creator_static, &ttf_context, &audio_subsystem)?;
|
||||||
game.audio.set_mute(cfg!(debug_assertions));
|
game.audio.set_mute(cfg!(debug_assertions));
|
||||||
|
|
||||||
let mut backbuffer = texture_creator_static.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)?;
|
let mut backbuffer = texture_creator_static
|
||||||
|
.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
||||||
|
|
||||||
let event_pump = sdl_context.event_pump().map_err(|e| anyhow!(e))?;
|
let event_pump = sdl_context.event_pump().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
// Initial draw
|
// Initial draw
|
||||||
game.draw(&mut canvas, &mut backbuffer)?;
|
game.draw(&mut canvas, &mut backbuffer)
|
||||||
game.present_backbuffer(&mut canvas, &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 {
|
Ok(Self {
|
||||||
game,
|
game,
|
||||||
@@ -74,6 +75,7 @@ impl App<'_> {
|
|||||||
backbuffer,
|
backbuffer,
|
||||||
paused: false,
|
paused: false,
|
||||||
last_tick: Instant::now(),
|
last_tick: Instant::now(),
|
||||||
|
cursor_pos: Vec2::ZERO,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,8 +117,12 @@ impl App<'_> {
|
|||||||
} => {
|
} => {
|
||||||
self.game.debug_mode = !self.game.debug_mode;
|
self.game.debug_mode = !self.game.debug_mode;
|
||||||
}
|
}
|
||||||
Event::KeyDown { keycode, .. } => {
|
Event::KeyDown { keycode: Some(key), .. } => {
|
||||||
self.game.keyboard_event(keycode.unwrap());
|
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);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -128,17 +134,20 @@ impl App<'_> {
|
|||||||
if !self.paused {
|
if !self.paused {
|
||||||
self.game.tick(dt);
|
self.game.tick(dt);
|
||||||
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
||||||
error!("Failed to draw game: {e}");
|
error!("Failed to draw game: {}", e);
|
||||||
}
|
}
|
||||||
if let Err(e) = self.game.present_backbuffer(&mut self.canvas, &self.backbuffer) {
|
if let Err(e) = self
|
||||||
error!("Failed to present backbuffer: {e}");
|
.game
|
||||||
|
.present_backbuffer(&mut self.canvas, &self.backbuffer, self.cursor_pos)
|
||||||
|
{
|
||||||
|
error!("Failed to present backbuffer: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
||||||
sleep(time);
|
get_platform().sleep(time);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event!(
|
event!(
|
||||||
|
|||||||
32
src/asset.rs
32
src/asset.rs
@@ -42,40 +42,12 @@ impl Asset {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
|
||||||
mod imp {
|
mod imp {
|
||||||
use super::*;
|
use super::*;
|
||||||
macro_rules! asset_bytes_enum {
|
use crate::platform::get_platform;
|
||||||
( $asset:expr ) => {
|
|
||||||
match $asset {
|
|
||||||
Asset::Wav1 => Cow::Borrowed(include_bytes!("../assets/game/sound/waka/1.ogg")),
|
|
||||||
Asset::Wav2 => Cow::Borrowed(include_bytes!("../assets/game/sound/waka/2.ogg")),
|
|
||||||
Asset::Wav3 => Cow::Borrowed(include_bytes!("../assets/game/sound/waka/3.ogg")),
|
|
||||||
Asset::Wav4 => Cow::Borrowed(include_bytes!("../assets/game/sound/waka/4.ogg")),
|
|
||||||
Asset::Atlas => Cow::Borrowed(include_bytes!("../assets/game/atlas.png")),
|
|
||||||
Asset::AtlasJson => Cow::Borrowed(include_bytes!("../assets/game/atlas.json")),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
|
||||||
Ok(asset_bytes_enum!(asset))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
mod imp {
|
|
||||||
use super::*;
|
|
||||||
use sdl2::rwops::RWops;
|
|
||||||
use std::io::Read;
|
|
||||||
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
||||||
let path = format!("assets/game/{}", asset.path());
|
get_platform().get_asset_bytes(asset)
|
||||||
let mut rwops = RWops::from_file(&path, "rb").map_err(|_| AssetError::NotFound(asset.path().to_string()))?;
|
|
||||||
let len = rwops.len().ok_or_else(|| AssetError::NotFound(asset.path().to_string()))?;
|
|
||||||
let mut buf = vec![0u8; len];
|
|
||||||
rwops
|
|
||||||
.read_exact(&mut buf)
|
|
||||||
.map_err(|e| AssetError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;
|
|
||||||
Ok(Cow::Owned(buf))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
#[allow(dead_code)]
|
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
pub mod emscripten {
|
|
||||||
use std::os::raw::c_uint;
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
pub fn emscripten_get_now() -> f64;
|
|
||||||
pub fn emscripten_sleep(ms: c_uint);
|
|
||||||
pub fn emscripten_get_element_css_size(target: *const u8, width: *mut f64, height: *mut f64) -> i32;
|
|
||||||
}
|
|
||||||
|
|
||||||
// milliseconds since start of program
|
|
||||||
pub fn now() -> f64 {
|
|
||||||
unsafe { emscripten_get_now() }
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn sleep(ms: u32) {
|
|
||||||
unsafe {
|
|
||||||
emscripten_sleep(ms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_canvas_size() -> (u32, u32) {
|
|
||||||
let mut width = 0.0;
|
|
||||||
let mut height = 0.0;
|
|
||||||
unsafe {
|
|
||||||
emscripten_get_element_css_size("canvas\0".as_ptr(), &mut width, &mut height);
|
|
||||||
}
|
|
||||||
(width as u32, height as u32)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use glam::IVec2;
|
use glam::IVec2;
|
||||||
|
|
||||||
|
/// The four cardinal directions.
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||||
pub enum Direction {
|
pub enum Direction {
|
||||||
Up,
|
Up,
|
||||||
@@ -9,7 +10,12 @@ pub enum Direction {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Direction {
|
impl Direction {
|
||||||
pub fn opposite(&self) -> Direction {
|
/// The four cardinal directions.
|
||||||
|
/// This is just a convenience constant for iterating over the directions.
|
||||||
|
pub const DIRECTIONS: [Direction; 4] = [Direction::Up, Direction::Down, Direction::Left, Direction::Right];
|
||||||
|
|
||||||
|
/// Returns the opposite direction. Constant time.
|
||||||
|
pub const fn opposite(self) -> Direction {
|
||||||
match self {
|
match self {
|
||||||
Direction::Up => Direction::Down,
|
Direction::Up => Direction::Down,
|
||||||
Direction::Down => Direction::Up,
|
Direction::Down => Direction::Up,
|
||||||
@@ -18,8 +24,20 @@ impl Direction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_ivec2(&self) -> IVec2 {
|
/// Returns the direction as an IVec2.
|
||||||
(*self).into()
|
pub fn as_ivec2(self) -> IVec2 {
|
||||||
|
self.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the direction as a usize (0-3). Constant time.
|
||||||
|
/// This is useful for indexing into arrays.
|
||||||
|
pub const fn as_usize(self) -> usize {
|
||||||
|
match self {
|
||||||
|
Direction::Up => 0,
|
||||||
|
Direction::Down => 1,
|
||||||
|
Direction::Left => 2,
|
||||||
|
Direction::Right => 3,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,5 +51,3 @@ impl From<Direction> for IVec2 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const DIRECTIONS: [Direction; 4] = [Direction::Up, Direction::Down, Direction::Left, Direction::Right];
|
|
||||||
|
|||||||
247
src/entity/ghost.rs
Normal file
247
src/entity/ghost.rs
Normal file
@@ -0,0 +1,247 @@
|
|||||||
|
//! 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::direction::Direction;
|
||||||
|
use crate::entity::graph::{Edge, EdgePermissions, Graph, NodeId};
|
||||||
|
use crate::entity::r#trait::Entity;
|
||||||
|
use crate::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).map_err(|e| GameError::Texture(TextureError::Animated(e)))?);
|
||||||
|
stopped_textures[direction.as_usize()] =
|
||||||
|
Some(AnimatedTexture::new(stopped_tiles, 0.1).map_err(|e| GameError::Texture(TextureError::Animated(e)))?);
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -111,7 +111,7 @@ impl Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Connects a new node to the graph and adds an edge between the existing node and the new node.
|
/// Connects a new node to the graph and adds an edge between the existing node and the new node.
|
||||||
pub fn connect_node(&mut self, from: NodeId, direction: Direction, new_node: Node) -> Result<NodeId, &'static str> {
|
pub fn add_connected(&mut self, from: NodeId, direction: Direction, new_node: Node) -> Result<NodeId, &'static str> {
|
||||||
let to = self.add_node(new_node);
|
let to = self.add_node(new_node);
|
||||||
self.connect(from, to, false, None, direction)?;
|
self.connect(from, to, false, None, direction)?;
|
||||||
Ok(to)
|
Ok(to)
|
||||||
@@ -236,208 +236,3 @@ impl Default for Graph {
|
|||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Traversal State and Logic ---
|
|
||||||
|
|
||||||
/// Represents the current position of an entity traversing the graph.
|
|
||||||
///
|
|
||||||
/// This enum allows for precise tracking of whether an entity is exactly at a node
|
|
||||||
/// or moving along an edge between two nodes.
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
|
||||||
pub enum Position {
|
|
||||||
/// The traverser is located exactly at a node.
|
|
||||||
AtNode(NodeId),
|
|
||||||
/// The traverser is on an edge between two nodes.
|
|
||||||
BetweenNodes {
|
|
||||||
from: NodeId,
|
|
||||||
to: NodeId,
|
|
||||||
/// The floating-point distance traversed along the edge from the `from` node.
|
|
||||||
traversed: f32,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
impl Position {
|
|
||||||
/// Returns `true` if the position is exactly at a node.
|
|
||||||
pub fn is_at_node(&self) -> bool {
|
|
||||||
matches!(self, Position::AtNode(_))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the `NodeId` of the current or most recently departed node.
|
|
||||||
#[allow(clippy::wrong_self_convention)]
|
|
||||||
pub fn from_node_id(&self) -> NodeId {
|
|
||||||
match self {
|
|
||||||
Position::AtNode(id) => *id,
|
|
||||||
Position::BetweenNodes { from, .. } => *from,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the `NodeId` of the destination node, if currently on an edge.
|
|
||||||
#[allow(clippy::wrong_self_convention)]
|
|
||||||
pub fn to_node_id(&self) -> Option<NodeId> {
|
|
||||||
match self {
|
|
||||||
Position::AtNode(_) => None,
|
|
||||||
Position::BetweenNodes { to, .. } => Some(*to),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns `true` if the traverser is stopped at a node.
|
|
||||||
pub fn is_stopped(&self) -> bool {
|
|
||||||
matches!(self, Position::AtNode(_))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Manages an entity's movement through the graph.
|
|
||||||
///
|
|
||||||
/// A `Traverser` encapsulates the state of an entity's position and direction,
|
|
||||||
/// providing a way to advance along the graph's paths based on a given distance.
|
|
||||||
/// It also handles direction changes, buffering the next intended direction.
|
|
||||||
pub struct Traverser {
|
|
||||||
/// The current position of the traverser in the graph.
|
|
||||||
pub position: Position,
|
|
||||||
/// The current direction of movement.
|
|
||||||
pub direction: Direction,
|
|
||||||
/// Buffered direction change with remaining frame count for timing.
|
|
||||||
///
|
|
||||||
/// The `u8` value represents the number of frames remaining before
|
|
||||||
/// the buffered direction expires. This allows for responsive controls
|
|
||||||
/// by storing direction changes for a limited time.
|
|
||||||
pub next_direction: Option<(Direction, u8)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Traverser {
|
|
||||||
/// Creates a new traverser starting at the given node ID.
|
|
||||||
///
|
|
||||||
/// The traverser will immediately attempt to start moving in the initial direction.
|
|
||||||
pub fn new<F>(graph: &Graph, start_node: NodeId, initial_direction: Direction, can_traverse: &F) -> Self
|
|
||||||
where
|
|
||||||
F: Fn(Edge) -> bool,
|
|
||||||
{
|
|
||||||
let mut traverser = Traverser {
|
|
||||||
position: Position::AtNode(start_node),
|
|
||||||
direction: initial_direction,
|
|
||||||
next_direction: Some((initial_direction, 1)),
|
|
||||||
};
|
|
||||||
|
|
||||||
// This will kickstart the traverser into motion
|
|
||||||
traverser.advance(graph, 0.0, can_traverse);
|
|
||||||
|
|
||||||
traverser
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Sets the next direction for the traverser to take.
|
|
||||||
///
|
|
||||||
/// The direction is buffered and will be applied at the next opportunity,
|
|
||||||
/// typically when the traverser reaches a new node. This allows for responsive
|
|
||||||
/// controls, as the new direction is stored for a limited time.
|
|
||||||
pub fn set_next_direction(&mut self, new_direction: Direction) {
|
|
||||||
if self.direction != new_direction {
|
|
||||||
self.next_direction = Some((new_direction, 30));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Advances the traverser along the graph by a specified distance.
|
|
||||||
///
|
|
||||||
/// This method updates the traverser's position based on its current state
|
|
||||||
/// and the distance to travel.
|
|
||||||
///
|
|
||||||
/// - If at a node, it checks for a buffered direction to start moving.
|
|
||||||
/// - If between nodes, it moves along the current edge.
|
|
||||||
/// - If it reaches a node, it attempts to transition to a new edge based on
|
|
||||||
/// the buffered direction or by continuing straight.
|
|
||||||
/// - If no valid move is possible, it stops at the node.
|
|
||||||
pub fn advance<F>(&mut self, graph: &Graph, distance: f32, can_traverse: &F)
|
|
||||||
where
|
|
||||||
F: Fn(Edge) -> bool,
|
|
||||||
{
|
|
||||||
// Decrement the remaining frames for the next direction
|
|
||||||
if let Some((direction, remaining)) = self.next_direction {
|
|
||||||
if remaining > 0 {
|
|
||||||
self.next_direction = Some((direction, remaining - 1));
|
|
||||||
} else {
|
|
||||||
self.next_direction = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.position {
|
|
||||||
Position::AtNode(node_id) => {
|
|
||||||
// We're not moving, but a buffered direction is available.
|
|
||||||
if let Some((next_direction, _)) = self.next_direction {
|
|
||||||
if let Some(edge) = graph.find_edge_in_direction(node_id, next_direction) {
|
|
||||||
if can_traverse(edge) {
|
|
||||||
// Start moving in that direction
|
|
||||||
self.position = Position::BetweenNodes {
|
|
||||||
from: node_id,
|
|
||||||
to: edge.target,
|
|
||||||
traversed: distance.max(0.0),
|
|
||||||
};
|
|
||||||
self.direction = next_direction;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.next_direction = None; // Consume the buffered direction regardless of whether we started moving with it
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Position::BetweenNodes { from, to, traversed } => {
|
|
||||||
// There is no point in any of the next logic if we don't travel at all
|
|
||||||
if distance <= 0.0 {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let edge = graph
|
|
||||||
.find_edge(from, to)
|
|
||||||
.expect("Inconsistent state: Traverser is on a non-existent edge.");
|
|
||||||
|
|
||||||
let new_traversed = traversed + distance;
|
|
||||||
|
|
||||||
if new_traversed < edge.distance {
|
|
||||||
// Still on the same edge, just update the distance.
|
|
||||||
self.position = Position::BetweenNodes {
|
|
||||||
from,
|
|
||||||
to,
|
|
||||||
traversed: new_traversed,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
let overflow = new_traversed - edge.distance;
|
|
||||||
let mut moved = false;
|
|
||||||
|
|
||||||
// If we buffered a direction, try to find an edge in that direction
|
|
||||||
if let Some((next_dir, _)) = self.next_direction {
|
|
||||||
if let Some(edge) = graph.find_edge_in_direction(to, next_dir) {
|
|
||||||
if can_traverse(edge) {
|
|
||||||
self.position = Position::BetweenNodes {
|
|
||||||
from: to,
|
|
||||||
to: edge.target,
|
|
||||||
traversed: overflow,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.direction = next_dir; // Remember our new direction
|
|
||||||
self.next_direction = None; // Consume the buffered direction
|
|
||||||
moved = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we didn't move, try to continue in the current direction
|
|
||||||
if !moved {
|
|
||||||
if let Some(edge) = graph.find_edge_in_direction(to, self.direction) {
|
|
||||||
if can_traverse(edge) {
|
|
||||||
self.position = Position::BetweenNodes {
|
|
||||||
from: to,
|
|
||||||
to: edge.target,
|
|
||||||
traversed: overflow,
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
self.position = Position::AtNode(to);
|
|
||||||
self.next_direction = None;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
self.position = Position::AtNode(to);
|
|
||||||
self.next_direction = None;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
pub mod direction;
|
pub mod direction;
|
||||||
|
pub mod ghost;
|
||||||
pub mod graph;
|
pub mod graph;
|
||||||
pub mod pacman;
|
pub mod pacman;
|
||||||
|
pub mod r#trait;
|
||||||
|
pub mod traversal;
|
||||||
|
|||||||
@@ -1,31 +1,82 @@
|
|||||||
use glam::{UVec2, Vec2};
|
//! 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::constants::BOARD_PIXEL_OFFSET;
|
|
||||||
use crate::entity::direction::Direction;
|
use crate::entity::direction::Direction;
|
||||||
use crate::entity::graph::{Edge, EdgePermissions, Graph, NodeId, Position, Traverser};
|
use crate::entity::graph::{Edge, EdgePermissions, Graph, NodeId};
|
||||||
use crate::helpers::centered_with_size;
|
use crate::entity::r#trait::Entity;
|
||||||
|
use crate::entity::traversal::Traverser;
|
||||||
use crate::texture::animated::AnimatedTexture;
|
use crate::texture::animated::AnimatedTexture;
|
||||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
use crate::texture::directional::DirectionalAnimatedTexture;
|
||||||
use crate::texture::sprite::SpriteAtlas;
|
use crate::texture::sprite::SpriteAtlas;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::keyboard::Keycode;
|
||||||
use sdl2::render::{Canvas, RenderTarget};
|
use tracing::error;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
|
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 {
|
fn can_pacman_traverse(edge: Edge) -> bool {
|
||||||
matches!(edge.permissions, EdgePermissions::All)
|
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 {
|
pub struct Pacman {
|
||||||
|
/// Handles movement through the game graph
|
||||||
pub traverser: Traverser,
|
pub traverser: Traverser,
|
||||||
|
/// Manages directional animated textures for different movement states
|
||||||
texture: DirectionalAnimatedTexture,
|
texture: DirectionalAnimatedTexture,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pacman {
|
impl Entity for Pacman {
|
||||||
pub fn new(graph: &Graph, start_node: NodeId, atlas: &SpriteAtlas) -> Self {
|
fn traverser(&self) -> &Traverser {
|
||||||
let mut textures = HashMap::new();
|
&self.traverser
|
||||||
let mut stopped_textures = HashMap::new();
|
}
|
||||||
|
|
||||||
for &direction in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
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 {
|
let moving_prefix = match direction {
|
||||||
Direction::Up => "pacman/up",
|
Direction::Up => "pacman/up",
|
||||||
Direction::Down => "pacman/down",
|
Direction::Down => "pacman/down",
|
||||||
@@ -33,34 +84,33 @@ impl Pacman {
|
|||||||
Direction::Right => "pacman/right",
|
Direction::Right => "pacman/right",
|
||||||
};
|
};
|
||||||
let moving_tiles = vec![
|
let moving_tiles = vec![
|
||||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_a.png")).unwrap(),
|
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_a.png"))
|
||||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png")).unwrap(),
|
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_a.png"))))?,
|
||||||
SpriteAtlas::get_tile(atlas, "pacman/full.png").unwrap(),
|
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")).unwrap()];
|
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.insert(
|
textures[direction.as_usize()] =
|
||||||
direction,
|
Some(AnimatedTexture::new(moving_tiles, 0.08).map_err(|e| GameError::Texture(TextureError::Animated(e)))?);
|
||||||
AnimatedTexture::new(moving_tiles, 0.08).expect("Invalid frame duration"),
|
stopped_textures[direction.as_usize()] =
|
||||||
);
|
Some(AnimatedTexture::new(stopped_tiles, 0.1).map_err(|e| GameError::Texture(TextureError::Animated(e)))?);
|
||||||
stopped_textures.insert(
|
|
||||||
direction,
|
|
||||||
AnimatedTexture::new(stopped_tiles, 0.1).expect("Invalid frame duration"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Ok(Self {
|
||||||
traverser: Traverser::new(graph, start_node, Direction::Left, &can_pacman_traverse),
|
traverser: Traverser::new(graph, start_node, Direction::Left, &can_pacman_traverse),
|
||||||
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
pub fn tick(&mut self, dt: f32, graph: &Graph) {
|
|
||||||
self.traverser.advance(graph, dt * 60.0 * 1.125, &can_pacman_traverse);
|
|
||||||
self.texture.tick(dt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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) {
|
pub fn handle_key(&mut self, keycode: Keycode) {
|
||||||
let direction = match keycode {
|
let direction = match keycode {
|
||||||
Keycode::Up => Some(Direction::Up),
|
Keycode::Up => Some(Direction::Up),
|
||||||
@@ -74,29 +124,4 @@ impl Pacman {
|
|||||||
self.traverser.set_next_direction(direction);
|
self.traverser.set_next_direction(direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_pixel_pos(&self, graph: &Graph) -> Vec2 {
|
|
||||||
match self.traverser.position {
|
|
||||||
Position::AtNode(node_id) => graph.get_node(node_id).unwrap().position,
|
|
||||||
Position::BetweenNodes { from, to, traversed } => {
|
|
||||||
let from_pos = graph.get_node(from).unwrap().position;
|
|
||||||
let to_pos = graph.get_node(to).unwrap().position;
|
|
||||||
from_pos.lerp(to_pos, traversed / from_pos.distance(to_pos))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) {
|
|
||||||
let pixel_pos = self.get_pixel_pos(graph).round().as_ivec2() + BOARD_PIXEL_OFFSET.as_ivec2();
|
|
||||||
let dest = centered_with_size(pixel_pos, UVec2::new(16, 16));
|
|
||||||
let is_stopped = self.traverser.position.is_stopped();
|
|
||||||
|
|
||||||
if is_stopped {
|
|
||||||
self.texture
|
|
||||||
.render_stopped(canvas, atlas, dest, self.traverser.direction)
|
|
||||||
.unwrap();
|
|
||||||
} else {
|
|
||||||
self.texture.render(canvas, atlas, dest, self.traverser.direction).unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
114
src/entity/trait.rs
Normal file
114
src/entity/trait.rs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
//! 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
229
src/entity/traversal.rs
Normal file
229
src/entity/traversal.rs
Normal file
@@ -0,0 +1,229 @@
|
|||||||
|
use tracing::error;
|
||||||
|
|
||||||
|
use crate::error::GameResult;
|
||||||
|
|
||||||
|
use super::direction::Direction;
|
||||||
|
use super::graph::{Edge, Graph, NodeId};
|
||||||
|
|
||||||
|
/// Represents the current position of an entity traversing the graph.
|
||||||
|
///
|
||||||
|
/// This enum allows for precise tracking of whether an entity is exactly at a node
|
||||||
|
/// or moving along an edge between two nodes.
|
||||||
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
|
pub enum Position {
|
||||||
|
/// The traverser is located exactly at a node.
|
||||||
|
AtNode(NodeId),
|
||||||
|
/// The traverser is on an edge between two nodes.
|
||||||
|
BetweenNodes {
|
||||||
|
from: NodeId,
|
||||||
|
to: NodeId,
|
||||||
|
/// The floating-point distance traversed along the edge from the `from` node.
|
||||||
|
traversed: f32,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl Position {
|
||||||
|
/// Returns `true` if the position is exactly at a node.
|
||||||
|
pub fn is_at_node(&self) -> bool {
|
||||||
|
matches!(self, Position::AtNode(_))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the `NodeId` of the current or most recently departed node.
|
||||||
|
#[allow(clippy::wrong_self_convention)]
|
||||||
|
pub fn from_node_id(&self) -> NodeId {
|
||||||
|
match self {
|
||||||
|
Position::AtNode(id) => *id,
|
||||||
|
Position::BetweenNodes { from, .. } => *from,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the `NodeId` of the destination node, if currently on an edge.
|
||||||
|
#[allow(clippy::wrong_self_convention)]
|
||||||
|
pub fn to_node_id(&self) -> Option<NodeId> {
|
||||||
|
match self {
|
||||||
|
Position::AtNode(_) => None,
|
||||||
|
Position::BetweenNodes { to, .. } => Some(*to),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns `true` if the traverser is stopped at a node.
|
||||||
|
pub fn is_stopped(&self) -> bool {
|
||||||
|
matches!(self, Position::AtNode(_))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Manages an entity's movement through the graph.
|
||||||
|
///
|
||||||
|
/// A `Traverser` encapsulates the state of an entity's position and direction,
|
||||||
|
/// providing a way to advance along the graph's paths based on a given distance.
|
||||||
|
/// It also handles direction changes, buffering the next intended direction.
|
||||||
|
pub struct Traverser {
|
||||||
|
/// The current position of the traverser in the graph.
|
||||||
|
pub position: Position,
|
||||||
|
/// The current direction of movement.
|
||||||
|
pub direction: Direction,
|
||||||
|
/// Buffered direction change with remaining frame count for timing.
|
||||||
|
///
|
||||||
|
/// The `u8` value represents the number of frames remaining before
|
||||||
|
/// the buffered direction expires. This allows for responsive controls
|
||||||
|
/// by storing direction changes for a limited time.
|
||||||
|
pub next_direction: Option<(Direction, u8)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Traverser {
|
||||||
|
/// Creates a new traverser starting at the given node ID.
|
||||||
|
///
|
||||||
|
/// The traverser will immediately attempt to start moving in the initial direction.
|
||||||
|
pub fn new<F>(graph: &Graph, start_node: NodeId, initial_direction: Direction, can_traverse: &F) -> Self
|
||||||
|
where
|
||||||
|
F: Fn(Edge) -> bool,
|
||||||
|
{
|
||||||
|
let mut traverser = Traverser {
|
||||||
|
position: Position::AtNode(start_node),
|
||||||
|
direction: initial_direction,
|
||||||
|
next_direction: Some((initial_direction, 1)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// This will kickstart the traverser into motion
|
||||||
|
if let Err(e) = traverser.advance(graph, 0.0, can_traverse) {
|
||||||
|
error!("Traverser initialization error: {}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
traverser
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the next direction for the traverser to take.
|
||||||
|
///
|
||||||
|
/// The direction is buffered and will be applied at the next opportunity,
|
||||||
|
/// typically when the traverser reaches a new node. This allows for responsive
|
||||||
|
/// controls, as the new direction is stored for a limited time.
|
||||||
|
pub fn set_next_direction(&mut self, new_direction: Direction) {
|
||||||
|
if self.direction != new_direction {
|
||||||
|
self.next_direction = Some((new_direction, 30));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Advances the traverser along the graph by a specified distance.
|
||||||
|
///
|
||||||
|
/// This method updates the traverser's position based on its current state
|
||||||
|
/// and the distance to travel.
|
||||||
|
///
|
||||||
|
/// - If at a node, it checks for a buffered direction to start moving.
|
||||||
|
/// - If between nodes, it moves along the current edge.
|
||||||
|
/// - If it reaches a node, it attempts to transition to a new edge based on
|
||||||
|
/// the buffered direction or by continuing straight.
|
||||||
|
/// - If no valid move is possible, it stops at the node.
|
||||||
|
///
|
||||||
|
/// Returns an error if the movement is invalid (e.g., trying to move in an impossible direction).
|
||||||
|
pub fn advance<F>(&mut self, graph: &Graph, distance: f32, can_traverse: &F) -> GameResult<()>
|
||||||
|
where
|
||||||
|
F: Fn(Edge) -> bool,
|
||||||
|
{
|
||||||
|
// Decrement the remaining frames for the next direction
|
||||||
|
if let Some((direction, remaining)) = self.next_direction {
|
||||||
|
if remaining > 0 {
|
||||||
|
self.next_direction = Some((direction, remaining - 1));
|
||||||
|
} else {
|
||||||
|
self.next_direction = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.position {
|
||||||
|
Position::AtNode(node_id) => {
|
||||||
|
// We're not moving, but a buffered direction is available.
|
||||||
|
if let Some((next_direction, _)) = self.next_direction {
|
||||||
|
if let Some(edge) = graph.find_edge_in_direction(node_id, next_direction) {
|
||||||
|
if can_traverse(edge) {
|
||||||
|
// Start moving in that direction
|
||||||
|
self.position = Position::BetweenNodes {
|
||||||
|
from: node_id,
|
||||||
|
to: edge.target,
|
||||||
|
traversed: distance.max(0.0),
|
||||||
|
};
|
||||||
|
self.direction = next_direction;
|
||||||
|
} else {
|
||||||
|
return Err(crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(
|
||||||
|
format!(
|
||||||
|
"Cannot traverse edge from {} to {} in direction {:?}",
|
||||||
|
node_id, edge.target, next_direction
|
||||||
|
),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Err(crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(
|
||||||
|
format!("No edge found in direction {:?} from node {}", next_direction, node_id),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.next_direction = None; // Consume the buffered direction regardless of whether we started moving with it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Position::BetweenNodes { from, to, traversed } => {
|
||||||
|
// There is no point in any of the next logic if we don't travel at all
|
||||||
|
if distance <= 0.0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let edge = graph.find_edge(from, to).ok_or_else(|| {
|
||||||
|
crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(format!(
|
||||||
|
"Inconsistent state: Traverser is on a non-existent edge from {} to {}.",
|
||||||
|
from, to
|
||||||
|
)))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
let new_traversed = traversed + distance;
|
||||||
|
|
||||||
|
if new_traversed < edge.distance {
|
||||||
|
// Still on the same edge, just update the distance.
|
||||||
|
self.position = Position::BetweenNodes {
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
traversed: new_traversed,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
let overflow = new_traversed - edge.distance;
|
||||||
|
let mut moved = false;
|
||||||
|
|
||||||
|
// If we buffered a direction, try to find an edge in that direction
|
||||||
|
if let Some((next_dir, _)) = self.next_direction {
|
||||||
|
if let Some(edge) = graph.find_edge_in_direction(to, next_dir) {
|
||||||
|
if can_traverse(edge) {
|
||||||
|
self.position = Position::BetweenNodes {
|
||||||
|
from: to,
|
||||||
|
to: edge.target,
|
||||||
|
traversed: overflow,
|
||||||
|
};
|
||||||
|
|
||||||
|
self.direction = next_dir; // Remember our new direction
|
||||||
|
self.next_direction = None; // Consume the buffered direction
|
||||||
|
moved = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we didn't move, try to continue in the current direction
|
||||||
|
if !moved {
|
||||||
|
if let Some(edge) = graph.find_edge_in_direction(to, self.direction) {
|
||||||
|
if can_traverse(edge) {
|
||||||
|
self.position = Position::BetweenNodes {
|
||||||
|
from: to,
|
||||||
|
to: edge.target,
|
||||||
|
traversed: overflow,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
self.position = Position::AtNode(to);
|
||||||
|
self.next_direction = None;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.position = Position::AtNode(to);
|
||||||
|
self.next_direction = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
156
src/error.rs
Normal file
156
src/error.rs
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
//! Centralized error types for the Pac-Man game.
|
||||||
|
//!
|
||||||
|
//! This module defines all error types used throughout the application,
|
||||||
|
//! providing a consistent error handling approach.
|
||||||
|
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
/// Main error type for the Pac-Man game.
|
||||||
|
///
|
||||||
|
/// This is the primary error type that should be used in public APIs.
|
||||||
|
/// It can represent any error that can occur during game operation.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum GameError {
|
||||||
|
#[error("Asset error: {0}")]
|
||||||
|
Asset(#[from] crate::asset::AssetError),
|
||||||
|
|
||||||
|
#[error("Platform error: {0}")]
|
||||||
|
Platform(#[from] crate::platform::PlatformError),
|
||||||
|
|
||||||
|
#[error("Map parsing error: {0}")]
|
||||||
|
MapParse(#[from] crate::map::parser::ParseError),
|
||||||
|
|
||||||
|
#[error("Map error: {0}")]
|
||||||
|
Map(#[from] MapError),
|
||||||
|
|
||||||
|
#[error("Texture error: {0}")]
|
||||||
|
Texture(#[from] TextureError),
|
||||||
|
|
||||||
|
#[error("Entity error: {0}")]
|
||||||
|
Entity(#[from] EntityError),
|
||||||
|
|
||||||
|
#[error("Game state error: {0}")]
|
||||||
|
GameState(#[from] GameStateError),
|
||||||
|
|
||||||
|
#[error("SDL error: {0}")]
|
||||||
|
Sdl(String),
|
||||||
|
|
||||||
|
#[error("IO error: {0}")]
|
||||||
|
Io(#[from] std::io::Error),
|
||||||
|
|
||||||
|
#[error("Serialization error: {0}")]
|
||||||
|
Serialization(#[from] serde_json::Error),
|
||||||
|
|
||||||
|
#[error("Invalid state: {0}")]
|
||||||
|
InvalidState(String),
|
||||||
|
|
||||||
|
#[error("Resource not found: {0}")]
|
||||||
|
NotFound(String),
|
||||||
|
|
||||||
|
#[error("Configuration error: {0}")]
|
||||||
|
Config(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Errors related to texture operations.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum TextureError {
|
||||||
|
#[error("Animated texture error: {0}")]
|
||||||
|
Animated(#[from] crate::texture::animated::AnimatedTextureError),
|
||||||
|
|
||||||
|
#[error("Failed to load texture: {0}")]
|
||||||
|
LoadFailed(String),
|
||||||
|
|
||||||
|
#[error("Texture not found in atlas: {0}")]
|
||||||
|
AtlasTileNotFound(String),
|
||||||
|
|
||||||
|
#[error("Invalid texture format: {0}")]
|
||||||
|
InvalidFormat(String),
|
||||||
|
|
||||||
|
#[error("Rendering failed: {0}")]
|
||||||
|
RenderFailed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Errors related to entity operations.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum EntityError {
|
||||||
|
#[error("Node not found in graph: {0}")]
|
||||||
|
NodeNotFound(usize),
|
||||||
|
|
||||||
|
#[error("Edge not found: from {from} to {to}")]
|
||||||
|
EdgeNotFound { from: usize, to: usize },
|
||||||
|
|
||||||
|
#[error("Invalid movement: {0}")]
|
||||||
|
InvalidMovement(String),
|
||||||
|
|
||||||
|
#[error("Pathfinding failed: {0}")]
|
||||||
|
PathfindingFailed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Errors related to game state operations.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum GameStateError {}
|
||||||
|
|
||||||
|
/// Errors related to map operations.
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum MapError {
|
||||||
|
#[error("Node not found: {0}")]
|
||||||
|
NodeNotFound(usize),
|
||||||
|
|
||||||
|
#[error("Invalid map configuration: {0}")]
|
||||||
|
InvalidConfig(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Result type for game operations.
|
||||||
|
pub type GameResult<T> = Result<T, GameError>;
|
||||||
|
|
||||||
|
/// Helper trait for converting other error types to GameError.
|
||||||
|
pub trait IntoGameError<T> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn into_game_error(self) -> GameResult<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> IntoGameError<T> for Result<T, E>
|
||||||
|
where
|
||||||
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
fn into_game_error(self) -> GameResult<T> {
|
||||||
|
self.map_err(|e| GameError::InvalidState(e.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper trait for converting Option to GameResult with a custom error.
|
||||||
|
pub trait OptionExt<T> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn ok_or_game_error<F>(self, f: F) -> GameResult<T>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> GameError;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> OptionExt<T> for Option<T> {
|
||||||
|
fn ok_or_game_error<F>(self, f: F) -> GameResult<T>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> GameError,
|
||||||
|
{
|
||||||
|
self.ok_or_else(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper trait for converting Result to GameResult with context.
|
||||||
|
pub trait ResultExt<T, E> {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn with_context<F>(self, f: F) -> GameResult<T>
|
||||||
|
where
|
||||||
|
F: FnOnce(&E) -> GameError;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, E> ResultExt<T, E> for Result<T, E>
|
||||||
|
where
|
||||||
|
E: std::error::Error + Send + Sync + 'static,
|
||||||
|
{
|
||||||
|
fn with_context<F>(self, f: F) -> GameResult<T>
|
||||||
|
where
|
||||||
|
F: FnOnce(&E) -> GameError,
|
||||||
|
{
|
||||||
|
self.map_err(|e| f(&e))
|
||||||
|
}
|
||||||
|
}
|
||||||
234
src/game.rs
234
src/game.rs
@@ -1,7 +1,7 @@
|
|||||||
//! This module contains the main game logic and state.
|
//! This module contains the main game logic and state.
|
||||||
|
|
||||||
use anyhow::Result;
|
use glam::{UVec2, Vec2};
|
||||||
use glam::UVec2;
|
use rand::{rngs::SmallRng, Rng, SeedableRng};
|
||||||
use sdl2::{
|
use sdl2::{
|
||||||
image::LoadTexture,
|
image::LoadTexture,
|
||||||
keyboard::Keycode,
|
keyboard::Keycode,
|
||||||
@@ -10,11 +10,17 @@ use sdl2::{
|
|||||||
video::WindowContext,
|
video::WindowContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::error::{EntityError, GameError, GameResult, TextureError};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
asset::{get_asset_bytes, Asset},
|
asset::{get_asset_bytes, Asset},
|
||||||
audio::Audio,
|
audio::Audio,
|
||||||
constants::RAW_BOARD,
|
constants::{CELL_SIZE, RAW_BOARD},
|
||||||
entity::pacman::Pacman,
|
entity::{
|
||||||
|
ghost::{Ghost, GhostType},
|
||||||
|
pacman::Pacman,
|
||||||
|
r#trait::Entity,
|
||||||
|
},
|
||||||
map::Map,
|
map::Map,
|
||||||
texture::{
|
texture::{
|
||||||
sprite::{self, AtlasMapper, AtlasTile, SpriteAtlas},
|
sprite::{self, AtlasMapper, AtlasTile, SpriteAtlas},
|
||||||
@@ -30,6 +36,7 @@ pub struct Game {
|
|||||||
pub score: u32,
|
pub score: u32,
|
||||||
pub map: Map,
|
pub map: Map,
|
||||||
pub pacman: Pacman,
|
pub pacman: Pacman,
|
||||||
|
pub ghosts: Vec<Ghost>,
|
||||||
pub debug_mode: bool,
|
pub debug_mode: bool,
|
||||||
|
|
||||||
// Rendering resources
|
// Rendering resources
|
||||||
@@ -46,43 +53,68 @@ impl Game {
|
|||||||
texture_creator: &TextureCreator<WindowContext>,
|
texture_creator: &TextureCreator<WindowContext>,
|
||||||
_ttf_context: &sdl2::ttf::Sdl2TtfContext,
|
_ttf_context: &sdl2::ttf::Sdl2TtfContext,
|
||||||
_audio_subsystem: &sdl2::AudioSubsystem,
|
_audio_subsystem: &sdl2::AudioSubsystem,
|
||||||
) -> Game {
|
) -> GameResult<Game> {
|
||||||
let map = Map::new(RAW_BOARD);
|
let map = Map::new(RAW_BOARD)?;
|
||||||
|
|
||||||
let pacman_start_pos = map.find_starting_position(0).unwrap();
|
let pacman_start_pos = map
|
||||||
|
.find_starting_position(0)
|
||||||
|
.ok_or_else(|| GameError::NotFound("Pac-Man starting position".to_string()))?;
|
||||||
let pacman_start_node = *map
|
let pacman_start_node = *map
|
||||||
.grid_to_node
|
.grid_to_node
|
||||||
.get(&glam::IVec2::new(pacman_start_pos.x as i32, pacman_start_pos.y as i32))
|
.get(&glam::IVec2::new(pacman_start_pos.x as i32, pacman_start_pos.y as i32))
|
||||||
.expect("Pac-Man starting position not found in graph");
|
.ok_or_else(|| GameError::NotFound("Pac-Man starting position not found in graph".to_string()))?;
|
||||||
|
|
||||||
let atlas_bytes = get_asset_bytes(Asset::Atlas).expect("Failed to load asset");
|
let atlas_bytes = get_asset_bytes(Asset::Atlas)?;
|
||||||
let atlas_texture = unsafe {
|
let atlas_texture = unsafe {
|
||||||
let texture = texture_creator
|
let texture = texture_creator.load_texture_bytes(&atlas_bytes).map_err(|e| {
|
||||||
.load_texture_bytes(&atlas_bytes)
|
if e.to_string().contains("format") || e.to_string().contains("unsupported") {
|
||||||
.expect("Could not load atlas texture from asset API");
|
GameError::Texture(TextureError::InvalidFormat(format!("Unsupported texture format: {e}")))
|
||||||
|
} else {
|
||||||
|
GameError::Texture(TextureError::LoadFailed(e.to_string()))
|
||||||
|
}
|
||||||
|
})?;
|
||||||
sprite::texture_to_static(texture)
|
sprite::texture_to_static(texture)
|
||||||
};
|
};
|
||||||
let atlas_json = get_asset_bytes(Asset::AtlasJson).expect("Failed to load asset");
|
let atlas_json = get_asset_bytes(Asset::AtlasJson)?;
|
||||||
let atlas_mapper: AtlasMapper = serde_json::from_slice(&atlas_json).expect("Could not parse atlas JSON");
|
let atlas_mapper: AtlasMapper = serde_json::from_slice(&atlas_json)?;
|
||||||
let atlas = SpriteAtlas::new(atlas_texture, atlas_mapper);
|
let atlas = SpriteAtlas::new(atlas_texture, atlas_mapper);
|
||||||
|
|
||||||
let mut map_texture = SpriteAtlas::get_tile(&atlas, "maze/full.png").expect("Failed to load map tile");
|
let mut map_texture = SpriteAtlas::get_tile(&atlas, "maze/full.png")
|
||||||
|
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("maze/full.png".to_string())))?;
|
||||||
map_texture.color = Some(Color::RGB(0x20, 0x20, 0xf9));
|
map_texture.color = Some(Color::RGB(0x20, 0x20, 0xf9));
|
||||||
|
|
||||||
let text_texture = TextTexture::new(1.0);
|
let text_texture = TextTexture::new(1.0);
|
||||||
let audio = Audio::new();
|
let audio = Audio::new();
|
||||||
let pacman = Pacman::new(&map.graph, pacman_start_node, &atlas);
|
let pacman = Pacman::new(&map.graph, pacman_start_node, &atlas)?;
|
||||||
|
|
||||||
Game {
|
// Create ghosts at random positions
|
||||||
|
let mut ghosts = Vec::new();
|
||||||
|
let ghost_types = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde];
|
||||||
|
let mut rng = SmallRng::from_os_rng();
|
||||||
|
|
||||||
|
if map.graph.node_count() == 0 {
|
||||||
|
return Err(GameError::Config("Game map has no nodes - invalid configuration".to_string()));
|
||||||
|
// TODO: This is a bug, we should handle this better
|
||||||
|
}
|
||||||
|
|
||||||
|
for &ghost_type in &ghost_types {
|
||||||
|
// Find a random node for the ghost to start at
|
||||||
|
let random_node = rng.random_range(0..map.graph.node_count());
|
||||||
|
let ghost = Ghost::new(&map.graph, random_node, ghost_type, &atlas)?;
|
||||||
|
ghosts.push(ghost);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(Game {
|
||||||
score: 0,
|
score: 0,
|
||||||
map,
|
map,
|
||||||
pacman,
|
pacman,
|
||||||
|
ghosts,
|
||||||
debug_mode: false,
|
debug_mode: false,
|
||||||
map_texture,
|
map_texture,
|
||||||
text_texture,
|
text_texture,
|
||||||
audio,
|
audio,
|
||||||
atlas,
|
atlas,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyboard_event(&mut self, keycode: Keycode) {
|
pub fn keyboard_event(&mut self, keycode: Keycode) {
|
||||||
@@ -91,34 +123,160 @@ impl Game {
|
|||||||
if keycode == Keycode::M {
|
if keycode == Keycode::M {
|
||||||
self.audio.set_mute(!self.audio.is_muted());
|
self.audio.set_mute(!self.audio.is_muted());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if keycode == Keycode::R {
|
||||||
|
if let Err(e) = self.reset_game_state() {
|
||||||
|
tracing::error!("Failed to reset game state: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick(&mut self, dt: f32) {
|
/// Resets the game state, randomizing ghost positions and resetting Pac-Man
|
||||||
self.pacman.tick(dt, &self.map.graph);
|
fn reset_game_state(&mut self) -> GameResult<()> {
|
||||||
}
|
// Reset Pac-Man to starting position
|
||||||
|
let pacman_start_pos = self
|
||||||
|
.map
|
||||||
|
.find_starting_position(0)
|
||||||
|
.ok_or_else(|| GameError::NotFound("Pac-Man starting position".to_string()))?;
|
||||||
|
let pacman_start_node = *self
|
||||||
|
.map
|
||||||
|
.grid_to_node
|
||||||
|
.get(&glam::IVec2::new(pacman_start_pos.x as i32, pacman_start_pos.y as i32))
|
||||||
|
.ok_or_else(|| GameError::NotFound("Pac-Man starting position not found in graph".to_string()))?;
|
||||||
|
|
||||||
pub fn draw<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> Result<()> {
|
self.pacman = Pacman::new(&self.map.graph, pacman_start_node, &self.atlas)?;
|
||||||
canvas.with_texture_canvas(backbuffer, |canvas| {
|
|
||||||
canvas.set_draw_color(Color::BLACK);
|
// Randomize ghost positions
|
||||||
canvas.clear();
|
let ghost_types = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde];
|
||||||
self.map.render(canvas, &mut self.atlas, &mut self.map_texture);
|
let mut rng = SmallRng::from_os_rng();
|
||||||
self.pacman.render(canvas, &mut self.atlas, &self.map.graph);
|
|
||||||
})?;
|
for (i, ghost) in self.ghosts.iter_mut().enumerate() {
|
||||||
|
let random_node = rng.random_range(0..self.map.graph.node_count());
|
||||||
|
*ghost = Ghost::new(&self.map.graph, random_node, ghost_types[i], &self.atlas)?;
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn present_backbuffer<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &Texture) -> Result<()> {
|
pub fn tick(&mut self, dt: f32) {
|
||||||
canvas.copy(backbuffer, None, None).map_err(anyhow::Error::msg)?;
|
self.pacman.tick(dt, &self.map.graph);
|
||||||
|
|
||||||
|
// Update all ghosts
|
||||||
|
for ghost in &mut self.ghosts {
|
||||||
|
ghost.tick(dt, &self.map.graph);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> GameResult<()> {
|
||||||
|
canvas
|
||||||
|
.with_texture_canvas(backbuffer, |canvas| {
|
||||||
|
canvas.set_draw_color(Color::BLACK);
|
||||||
|
canvas.clear();
|
||||||
|
self.map.render(canvas, &mut self.atlas, &mut self.map_texture);
|
||||||
|
|
||||||
|
// Render all ghosts
|
||||||
|
for ghost in &self.ghosts {
|
||||||
|
if let Err(e) = ghost.render(canvas, &mut self.atlas, &self.map.graph) {
|
||||||
|
tracing::error!("Failed to render ghost: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = self.pacman.render(canvas, &mut self.atlas, &self.map.graph) {
|
||||||
|
tracing::error!("Failed to render pacman: {}", e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn present_backbuffer<T: RenderTarget>(
|
||||||
|
&mut self,
|
||||||
|
canvas: &mut Canvas<T>,
|
||||||
|
backbuffer: &Texture,
|
||||||
|
cursor_pos: glam::Vec2,
|
||||||
|
) -> GameResult<()> {
|
||||||
|
canvas
|
||||||
|
.copy(backbuffer, None, None)
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
if self.debug_mode {
|
if self.debug_mode {
|
||||||
self.map.debug_render_nodes(canvas);
|
if let Err(e) = self
|
||||||
|
.map
|
||||||
|
.debug_render_with_cursor(canvas, &mut self.text_texture, &mut self.atlas, cursor_pos)
|
||||||
|
{
|
||||||
|
tracing::error!("Failed to render debug cursor: {}", e);
|
||||||
|
}
|
||||||
|
self.render_pathfinding_debug(canvas)?;
|
||||||
}
|
}
|
||||||
self.draw_hud(canvas)?;
|
self.draw_hud(canvas)?;
|
||||||
canvas.present();
|
canvas.present();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_hud<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> Result<()> {
|
/// Renders pathfinding debug lines from each ghost to Pac-Man.
|
||||||
|
///
|
||||||
|
/// 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<()> {
|
||||||
|
let pacman_node = self.pacman.current_node_id();
|
||||||
|
|
||||||
|
for ghost in self.ghosts.iter() {
|
||||||
|
if let Ok(path) = ghost.calculate_path_to_target(&self.map.graph, pacman_node) {
|
||||||
|
if path.len() < 2 {
|
||||||
|
continue; // Skip if path is too short
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the ghost's color
|
||||||
|
canvas.set_draw_color(ghost.debug_color());
|
||||||
|
|
||||||
|
// Calculate offset based on ghost index to prevent overlapping lines
|
||||||
|
// let offset = (i as f32) * 2.0 - 3.0; // Offset range: -3.0 to 3.0
|
||||||
|
|
||||||
|
// Calculate a consistent offset direction for the entire path
|
||||||
|
// let first_node = self.map.graph.get_node(path[0]).unwrap();
|
||||||
|
// let last_node = self.map.graph.get_node(path[path.len() - 1]).unwrap();
|
||||||
|
|
||||||
|
// 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),
|
||||||
|
} * 5.0;
|
||||||
|
|
||||||
|
// Calculate offset positions for all nodes using the same perpendicular direction
|
||||||
|
let mut offset_positions = Vec::new();
|
||||||
|
for &node_id in &path {
|
||||||
|
let node = self
|
||||||
|
.map
|
||||||
|
.graph
|
||||||
|
.get_node(node_id)
|
||||||
|
.ok_or(GameError::Entity(EntityError::NodeNotFound(node_id)))?;
|
||||||
|
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
offset_positions.push(pos + offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw lines between the offset positions
|
||||||
|
for window in offset_positions.windows(2) {
|
||||||
|
if let (Some(from), Some(to)) = (window.first(), window.get(1)) {
|
||||||
|
// Skip if the distance is too far (used for preventing lines between tunnel portals)
|
||||||
|
if from.distance_squared(*to) > (CELL_SIZE * 16).pow(2) as f32 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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()))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_hud<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> GameResult<()> {
|
||||||
let lives = 3;
|
let lives = 3;
|
||||||
let score_text = format!("{:02}", self.score);
|
let score_text = format!("{:02}", self.score);
|
||||||
let x_offset = 4;
|
let x_offset = 4;
|
||||||
@@ -126,18 +284,22 @@ impl Game {
|
|||||||
let lives_offset = 3;
|
let lives_offset = 3;
|
||||||
let score_offset = 7 - (score_text.len() as i32);
|
let score_offset = 7 - (score_text.len() as i32);
|
||||||
self.text_texture.set_scale(1.0);
|
self.text_texture.set_scale(1.0);
|
||||||
let _ = self.text_texture.render(
|
if let Err(e) = self.text_texture.render(
|
||||||
canvas,
|
canvas,
|
||||||
&mut self.atlas,
|
&mut self.atlas,
|
||||||
&format!("{lives}UP HIGH SCORE "),
|
&format!("{lives}UP HIGH SCORE "),
|
||||||
UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
|
UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
|
||||||
);
|
) {
|
||||||
let _ = self.text_texture.render(
|
tracing::error!("Failed to render HUD text: {}", e);
|
||||||
|
}
|
||||||
|
if let Err(e) = self.text_texture.render(
|
||||||
canvas,
|
canvas,
|
||||||
&mut self.atlas,
|
&mut self.atlas,
|
||||||
&score_text,
|
&score_text,
|
||||||
UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
|
UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
|
||||||
);
|
) {
|
||||||
|
tracing::error!("Failed to render score text: {}", e);
|
||||||
|
}
|
||||||
|
|
||||||
// Display FPS information in top-left corner
|
// Display FPS information in top-left corner
|
||||||
// let fps_text = format!("FPS: {:.1} (1s) / {:.1} (10s)", self.fps_1s, self.fps_10s);
|
// let fps_text = format!("FPS: {:.1} (1s) / {:.1} (10s)", self.fps_1s, self.fps_10s);
|
||||||
|
|||||||
@@ -2,10 +2,9 @@ use glam::{IVec2, UVec2};
|
|||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
|
|
||||||
pub fn centered_with_size(pixel_pos: IVec2, size: UVec2) -> Rect {
|
pub fn centered_with_size(pixel_pos: IVec2, size: UVec2) -> Rect {
|
||||||
Rect::new(
|
// Ensure the position doesn't cause integer overflow when centering
|
||||||
pixel_pos.x - size.x as i32 / 2,
|
let x = pixel_pos.x.saturating_sub(size.x as i32 / 2);
|
||||||
pixel_pos.y - size.y as i32 / 2,
|
let y = pixel_pos.y.saturating_sub(size.y as i32 / 2);
|
||||||
size.x,
|
|
||||||
size.y,
|
Rect::new(x, y, size.x, size.y)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ pub mod app;
|
|||||||
pub mod asset;
|
pub mod asset;
|
||||||
pub mod audio;
|
pub mod audio;
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod emscripten;
|
|
||||||
pub mod entity;
|
pub mod entity;
|
||||||
|
pub mod error;
|
||||||
pub mod game;
|
pub mod game;
|
||||||
pub mod helpers;
|
pub mod helpers;
|
||||||
pub mod map;
|
pub mod map;
|
||||||
|
pub mod platform;
|
||||||
pub mod texture;
|
pub mod texture;
|
||||||
|
|||||||
54
src/main.rs
54
src/main.rs
@@ -5,59 +5,17 @@ use tracing::info;
|
|||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
use tracing_subscriber::layer::SubscriberExt;
|
||||||
|
|
||||||
#[cfg(windows)]
|
|
||||||
use winapi::{
|
|
||||||
shared::ntdef::NULL,
|
|
||||||
um::{
|
|
||||||
fileapi::{CreateFileA, OPEN_EXISTING},
|
|
||||||
handleapi::INVALID_HANDLE_VALUE,
|
|
||||||
processenv::SetStdHandle,
|
|
||||||
winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE},
|
|
||||||
wincon::{AttachConsole, GetConsoleWindow},
|
|
||||||
winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Attaches the process to the parent console on Windows.
|
|
||||||
///
|
|
||||||
/// This allows the application to print to the console when run from a terminal,
|
|
||||||
/// which is useful for debugging purposes. If the application is not run from a
|
|
||||||
/// terminal, this function does nothing.
|
|
||||||
#[cfg(windows)]
|
|
||||||
unsafe fn attach_console() {
|
|
||||||
if !std::ptr::eq(GetConsoleWindow(), std::ptr::null_mut()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if AttachConsole(winapi::um::wincon::ATTACH_PARENT_PROCESS) != 0 {
|
|
||||||
let handle = CreateFileA(
|
|
||||||
c"CONOUT$".as_ptr(),
|
|
||||||
GENERIC_READ | GENERIC_WRITE,
|
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
||||||
std::ptr::null_mut(),
|
|
||||||
OPEN_EXISTING,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
);
|
|
||||||
|
|
||||||
if handle != INVALID_HANDLE_VALUE {
|
|
||||||
SetStdHandle(STD_OUTPUT_HANDLE, handle);
|
|
||||||
SetStdHandle(STD_ERROR_HANDLE, handle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Do NOT call AllocConsole here - we don't want a console when launched from Explorer
|
|
||||||
}
|
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod asset;
|
mod asset;
|
||||||
mod audio;
|
mod audio;
|
||||||
mod constants;
|
mod constants;
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
mod emscripten;
|
|
||||||
mod entity;
|
mod entity;
|
||||||
|
mod error;
|
||||||
mod game;
|
mod game;
|
||||||
mod helpers;
|
mod helpers;
|
||||||
mod map;
|
mod map;
|
||||||
|
mod platform;
|
||||||
mod texture;
|
mod texture;
|
||||||
|
|
||||||
/// The main entry point of the application.
|
/// The main entry point of the application.
|
||||||
@@ -65,12 +23,6 @@ mod texture;
|
|||||||
/// This function initializes SDL, the window, the game state, and then enters
|
/// This function initializes SDL, the window, the game state, and then enters
|
||||||
/// the main game loop.
|
/// the main game loop.
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
// Attaches the console on Windows for debugging purposes.
|
|
||||||
#[cfg(windows)]
|
|
||||||
unsafe {
|
|
||||||
attach_console();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup tracing
|
// Setup tracing
|
||||||
let subscriber = tracing_subscriber::fmt()
|
let subscriber = tracing_subscriber::fmt()
|
||||||
.with_ansi(cfg!(not(target_os = "emscripten")))
|
.with_ansi(cfg!(not(target_os = "emscripten")))
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//! Map construction and building functionality.
|
//! Map construction and building functionality.
|
||||||
|
|
||||||
use crate::constants::{MapTile, BOARD_CELL_SIZE, CELL_SIZE};
|
use crate::constants::{MapTile, BOARD_CELL_SIZE, CELL_SIZE};
|
||||||
use crate::entity::direction::{Direction, DIRECTIONS};
|
use crate::entity::direction::Direction;
|
||||||
use crate::entity::graph::{EdgePermissions, Graph, Node, NodeId};
|
use crate::entity::graph::{EdgePermissions, Graph, Node, NodeId};
|
||||||
use crate::map::parser::MapTileParser;
|
use crate::map::parser::MapTileParser;
|
||||||
use crate::map::render::MapRenderer;
|
use crate::map::render::MapRenderer;
|
||||||
@@ -11,6 +11,8 @@ use sdl2::render::{Canvas, RenderTarget};
|
|||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
use tracing::debug;
|
use tracing::debug;
|
||||||
|
|
||||||
|
use crate::error::{GameResult, MapError};
|
||||||
|
|
||||||
/// The starting positions of the entities in the game.
|
/// The starting positions of the entities in the game.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub struct NodePositions {
|
pub struct NodePositions {
|
||||||
@@ -47,8 +49,8 @@ impl Map {
|
|||||||
///
|
///
|
||||||
/// This function will panic if the board layout contains unknown characters or if
|
/// This function will panic if the board layout contains unknown characters or if
|
||||||
/// the house door is not defined by exactly two '=' characters.
|
/// the house door is not defined by exactly two '=' characters.
|
||||||
pub fn new(raw_board: [&str; BOARD_CELL_SIZE.y as usize]) -> Map {
|
pub fn new(raw_board: [&str; BOARD_CELL_SIZE.y as usize]) -> GameResult<Map> {
|
||||||
let parsed_map = MapTileParser::parse_board(raw_board).expect("Failed to parse board layout");
|
let parsed_map = MapTileParser::parse_board(raw_board)?;
|
||||||
|
|
||||||
let map = parsed_map.tiles;
|
let map = parsed_map.tiles;
|
||||||
let house_door = parsed_map.house_door;
|
let house_door = parsed_map.house_door;
|
||||||
@@ -61,7 +63,8 @@ impl Map {
|
|||||||
let cell_offset = Vec2::splat(CELL_SIZE as f32 / 2.0);
|
let cell_offset = Vec2::splat(CELL_SIZE as f32 / 2.0);
|
||||||
|
|
||||||
// Find a starting point for the graph generation, preferably Pac-Man's position.
|
// Find a starting point for the graph generation, preferably Pac-Man's position.
|
||||||
let start_pos = pacman_start.expect("Pac-Man's starting position not found");
|
let start_pos =
|
||||||
|
pacman_start.ok_or_else(|| MapError::InvalidConfig("Pac-Man's starting position not found".to_string()))?;
|
||||||
|
|
||||||
// Add the starting position to the graph/queue
|
// Add the starting position to the graph/queue
|
||||||
let mut queue = VecDeque::new();
|
let mut queue = VecDeque::new();
|
||||||
@@ -75,7 +78,7 @@ impl Map {
|
|||||||
|
|
||||||
// Iterate over the queue, adding nodes to the graph and connecting them to their neighbors
|
// Iterate over the queue, adding nodes to the graph and connecting them to their neighbors
|
||||||
while let Some(source_position) = queue.pop_front() {
|
while let Some(source_position) = queue.pop_front() {
|
||||||
for &dir in DIRECTIONS.iter() {
|
for dir in Direction::DIRECTIONS {
|
||||||
let new_position = source_position + dir.as_ivec2();
|
let new_position = source_position + dir.as_ivec2();
|
||||||
|
|
||||||
// Skip if the new position is out of bounds
|
// Skip if the new position is out of bounds
|
||||||
@@ -114,14 +117,14 @@ impl Map {
|
|||||||
// Connect the new node to the source node
|
// Connect the new node to the source node
|
||||||
graph
|
graph
|
||||||
.connect(*source_node_id, new_node_id, false, None, dir)
|
.connect(*source_node_id, new_node_id, false, None, dir)
|
||||||
.expect("Failed to add edge");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to add edge: {e}")))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// While most nodes are already connected to their neighbors, some may not be, so we need to connect them
|
// While most nodes are already connected to their neighbors, some may not be, so we need to connect them
|
||||||
for (grid_pos, &node_id) in &grid_to_node {
|
for (grid_pos, &node_id) in &grid_to_node {
|
||||||
for dir in DIRECTIONS {
|
for dir in Direction::DIRECTIONS {
|
||||||
// If the node doesn't have an edge in this direction, look for a neighbor in that direction
|
// If the node doesn't have an edge in this direction, look for a neighbor in that direction
|
||||||
if graph.adjacency_list[node_id].get(dir).is_none() {
|
if graph.adjacency_list[node_id].get(dir).is_none() {
|
||||||
let neighbor = grid_pos + dir.as_ivec2();
|
let neighbor = grid_pos + dir.as_ivec2();
|
||||||
@@ -129,7 +132,7 @@ impl Map {
|
|||||||
if let Some(&neighbor_id) = grid_to_node.get(&neighbor) {
|
if let Some(&neighbor_id) = grid_to_node.get(&neighbor) {
|
||||||
graph
|
graph
|
||||||
.connect(node_id, neighbor_id, false, None, dir)
|
.connect(node_id, neighbor_id, false, None, dir)
|
||||||
.expect("Failed to add edge");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to add edge: {e}")))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,7 +140,7 @@ impl Map {
|
|||||||
|
|
||||||
// Build house structure
|
// Build house structure
|
||||||
let (house_entrance_node_id, left_center_node_id, center_center_node_id, right_center_node_id) =
|
let (house_entrance_node_id, left_center_node_id, center_center_node_id, right_center_node_id) =
|
||||||
Self::build_house(&mut graph, &grid_to_node, &house_door);
|
Self::build_house(&mut graph, &grid_to_node, &house_door)?;
|
||||||
|
|
||||||
let start_positions = NodePositions {
|
let start_positions = NodePositions {
|
||||||
pacman: grid_to_node[&start_pos],
|
pacman: grid_to_node[&start_pos],
|
||||||
@@ -148,15 +151,15 @@ impl Map {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Build tunnel connections
|
// Build tunnel connections
|
||||||
Self::build_tunnels(&mut graph, &grid_to_node, &tunnel_ends);
|
Self::build_tunnels(&mut graph, &grid_to_node, &tunnel_ends)?;
|
||||||
|
|
||||||
Map {
|
Ok(Map {
|
||||||
current: map,
|
current: map,
|
||||||
graph,
|
graph,
|
||||||
grid_to_node,
|
grid_to_node,
|
||||||
start_positions,
|
start_positions,
|
||||||
pacman_start,
|
pacman_start,
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the starting position for a given entity ID.
|
/// Finds the starting position for a given entity ID.
|
||||||
@@ -184,13 +187,18 @@ impl Map {
|
|||||||
MapRenderer::render_map(canvas, atlas, map_texture);
|
MapRenderer::render_map(canvas, atlas, map_texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Renders a debug visualization of the navigation graph.
|
/// Renders a debug visualization with cursor-based highlighting.
|
||||||
///
|
///
|
||||||
/// This function is intended for development and debugging purposes. It draws the
|
/// This function provides interactive debugging by highlighting the nearest node
|
||||||
/// nodes and edges of the graph on top of the map, allowing for visual
|
/// to the cursor, showing its ID, and highlighting its connections.
|
||||||
/// inspection of the navigation paths.
|
pub fn debug_render_with_cursor<T: RenderTarget>(
|
||||||
pub fn debug_render_nodes<T: RenderTarget>(&self, canvas: &mut Canvas<T>) {
|
&self,
|
||||||
MapRenderer::debug_render_nodes(&self.graph, canvas);
|
canvas: &mut Canvas<T>,
|
||||||
|
text_renderer: &mut crate::texture::text::TextTexture,
|
||||||
|
atlas: &mut SpriteAtlas,
|
||||||
|
cursor_pos: glam::Vec2,
|
||||||
|
) -> GameResult<()> {
|
||||||
|
MapRenderer::debug_render_with_cursor(&self.graph, canvas, text_renderer, atlas, cursor_pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the house structure in the graph.
|
/// Builds the house structure in the graph.
|
||||||
@@ -198,21 +206,32 @@ impl Map {
|
|||||||
graph: &mut Graph,
|
graph: &mut Graph,
|
||||||
grid_to_node: &HashMap<IVec2, NodeId>,
|
grid_to_node: &HashMap<IVec2, NodeId>,
|
||||||
house_door: &[Option<IVec2>; 2],
|
house_door: &[Option<IVec2>; 2],
|
||||||
) -> (usize, usize, usize, usize) {
|
) -> GameResult<(usize, usize, usize, usize)> {
|
||||||
// Calculate the position of the house entrance node
|
// Calculate the position of the house entrance node
|
||||||
let (house_entrance_node_id, house_entrance_node_position) = {
|
let (house_entrance_node_id, house_entrance_node_position) = {
|
||||||
// Translate the grid positions to the actual node ids
|
// Translate the grid positions to the actual node ids
|
||||||
let left_node = grid_to_node
|
let left_node = grid_to_node
|
||||||
.get(&(house_door[0].expect("First house door position not acquired") + Direction::Left.as_ivec2()))
|
.get(
|
||||||
.expect("Left house door node not found");
|
&(house_door[0]
|
||||||
|
.ok_or_else(|| MapError::InvalidConfig("First house door position not acquired".to_string()))?
|
||||||
|
+ Direction::Left.as_ivec2()),
|
||||||
|
)
|
||||||
|
.ok_or_else(|| MapError::InvalidConfig("Left house door node not found".to_string()))?;
|
||||||
let right_node = grid_to_node
|
let right_node = grid_to_node
|
||||||
.get(&(house_door[1].expect("Second house door position not acquired") + Direction::Right.as_ivec2()))
|
.get(
|
||||||
.expect("Right house door node not found");
|
&(house_door[1]
|
||||||
|
.ok_or_else(|| MapError::InvalidConfig("Second house door position not acquired".to_string()))?
|
||||||
|
+ Direction::Right.as_ivec2()),
|
||||||
|
)
|
||||||
|
.ok_or_else(|| MapError::InvalidConfig("Right house door node not found".to_string()))?;
|
||||||
|
|
||||||
// Calculate the position of the house node
|
// Calculate the position of the house node
|
||||||
let (node_id, node_position) = {
|
let (node_id, node_position) = {
|
||||||
let left_pos = graph.get_node(*left_node).unwrap().position;
|
let left_pos = graph.get_node(*left_node).ok_or(MapError::NodeNotFound(*left_node))?.position;
|
||||||
let right_pos = graph.get_node(*right_node).unwrap().position;
|
let right_pos = graph
|
||||||
|
.get_node(*right_node)
|
||||||
|
.ok_or(MapError::NodeNotFound(*right_node))?
|
||||||
|
.position;
|
||||||
let house_node = graph.add_node(Node {
|
let house_node = graph.add_node(Node {
|
||||||
position: left_pos.lerp(right_pos, 0.5),
|
position: left_pos.lerp(right_pos, 0.5),
|
||||||
});
|
});
|
||||||
@@ -222,16 +241,16 @@ impl Map {
|
|||||||
// Connect the house door to the left and right nodes
|
// Connect the house door to the left and right nodes
|
||||||
graph
|
graph
|
||||||
.connect(node_id, *left_node, true, None, Direction::Left)
|
.connect(node_id, *left_node, true, None, Direction::Left)
|
||||||
.expect("Failed to connect house door to left node");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house door to left node: {e}")))?;
|
||||||
graph
|
graph
|
||||||
.connect(node_id, *right_node, true, None, Direction::Right)
|
.connect(node_id, *right_node, true, None, Direction::Right)
|
||||||
.expect("Failed to connect house door to right node");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house door to right node: {e}")))?;
|
||||||
|
|
||||||
(node_id, node_position)
|
(node_id, node_position)
|
||||||
};
|
};
|
||||||
|
|
||||||
// A helper function to help create the various 'lines' of nodes within the house
|
// A helper function to help create the various 'lines' of nodes within the house
|
||||||
let create_house_line = |graph: &mut Graph, center_pos: Vec2| -> (NodeId, NodeId) {
|
let create_house_line = |graph: &mut Graph, center_pos: Vec2| -> GameResult<(NodeId, NodeId)> {
|
||||||
// Place the nodes at, above, and below the center position
|
// Place the nodes at, above, and below the center position
|
||||||
let center_node_id = graph.add_node(Node { position: center_pos });
|
let center_node_id = graph.add_node(Node { position: center_pos });
|
||||||
let top_node_id = graph.add_node(Node {
|
let top_node_id = graph.add_node(Node {
|
||||||
@@ -244,12 +263,12 @@ impl Map {
|
|||||||
// Connect the center node to the top and bottom nodes
|
// Connect the center node to the top and bottom nodes
|
||||||
graph
|
graph
|
||||||
.connect(center_node_id, top_node_id, false, None, Direction::Up)
|
.connect(center_node_id, top_node_id, false, None, Direction::Up)
|
||||||
.expect("Failed to connect house line to left node");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house line to top node: {e}")))?;
|
||||||
graph
|
graph
|
||||||
.connect(center_node_id, bottom_node_id, false, None, Direction::Down)
|
.connect(center_node_id, bottom_node_id, false, None, Direction::Down)
|
||||||
.expect("Failed to connect house line to right node");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house line to bottom node: {e}")))?;
|
||||||
|
|
||||||
(center_node_id, top_node_id)
|
Ok((center_node_id, top_node_id))
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calculate the position of the center line's center node
|
// Calculate the position of the center line's center node
|
||||||
@@ -257,7 +276,7 @@ impl Map {
|
|||||||
house_entrance_node_position + (Direction::Down.as_ivec2() * (3 * CELL_SIZE as i32)).as_vec2();
|
house_entrance_node_position + (Direction::Down.as_ivec2() * (3 * CELL_SIZE as i32)).as_vec2();
|
||||||
|
|
||||||
// Create the center line
|
// Create the center line
|
||||||
let (center_center_node_id, center_top_node_id) = create_house_line(graph, center_line_center_position);
|
let (center_center_node_id, center_top_node_id) = create_house_line(graph, center_line_center_position)?;
|
||||||
|
|
||||||
// Create a ghost-only, two-way connection for the house door.
|
// Create a ghost-only, two-way connection for the house door.
|
||||||
// This prevents Pac-Man from entering or exiting through the door.
|
// This prevents Pac-Man from entering or exiting through the door.
|
||||||
@@ -270,7 +289,7 @@ impl Map {
|
|||||||
Direction::Down,
|
Direction::Down,
|
||||||
EdgePermissions::GhostsOnly,
|
EdgePermissions::GhostsOnly,
|
||||||
)
|
)
|
||||||
.expect("Failed to create ghost-only entrance to house");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to create ghost-only entrance to house: {e}")))?;
|
||||||
|
|
||||||
graph
|
graph
|
||||||
.add_edge(
|
.add_edge(
|
||||||
@@ -281,52 +300,57 @@ impl Map {
|
|||||||
Direction::Up,
|
Direction::Up,
|
||||||
EdgePermissions::GhostsOnly,
|
EdgePermissions::GhostsOnly,
|
||||||
)
|
)
|
||||||
.expect("Failed to create ghost-only exit from house");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to create ghost-only exit from house: {e}")))?;
|
||||||
|
|
||||||
// Create the left line
|
// Create the left line
|
||||||
let (left_center_node_id, _) = create_house_line(
|
let (left_center_node_id, _) = create_house_line(
|
||||||
graph,
|
graph,
|
||||||
center_line_center_position + (Direction::Left.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
center_line_center_position + (Direction::Left.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
||||||
);
|
)?;
|
||||||
|
|
||||||
// Create the right line
|
// Create the right line
|
||||||
let (right_center_node_id, _) = create_house_line(
|
let (right_center_node_id, _) = create_house_line(
|
||||||
graph,
|
graph,
|
||||||
center_line_center_position + (Direction::Right.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
center_line_center_position + (Direction::Right.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
||||||
);
|
)?;
|
||||||
|
|
||||||
debug!("Left center node id: {left_center_node_id}");
|
debug!("Left center node id: {left_center_node_id}");
|
||||||
|
|
||||||
// Connect the center line to the left and right lines
|
// Connect the center line to the left and right lines
|
||||||
graph
|
graph
|
||||||
.connect(center_center_node_id, left_center_node_id, false, None, Direction::Left)
|
.connect(center_center_node_id, left_center_node_id, false, None, Direction::Left)
|
||||||
.expect("Failed to connect house entrance to left top line");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house entrance to left top line: {e}")))?;
|
||||||
|
|
||||||
graph
|
graph
|
||||||
.connect(center_center_node_id, right_center_node_id, false, None, Direction::Right)
|
.connect(center_center_node_id, right_center_node_id, false, None, Direction::Right)
|
||||||
.expect("Failed to connect house entrance to right top line");
|
.map_err(|e| MapError::InvalidConfig(format!("Failed to connect house entrance to right top line: {e}")))?;
|
||||||
|
|
||||||
debug!("House entrance node id: {house_entrance_node_id}");
|
debug!("House entrance node id: {house_entrance_node_id}");
|
||||||
|
|
||||||
(
|
Ok((
|
||||||
house_entrance_node_id,
|
house_entrance_node_id,
|
||||||
left_center_node_id,
|
left_center_node_id,
|
||||||
center_center_node_id,
|
center_center_node_id,
|
||||||
right_center_node_id,
|
right_center_node_id,
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Builds the tunnel connections in the graph.
|
/// Builds the tunnel connections in the graph.
|
||||||
fn build_tunnels(graph: &mut Graph, grid_to_node: &HashMap<IVec2, NodeId>, tunnel_ends: &[Option<IVec2>; 2]) {
|
fn build_tunnels(
|
||||||
|
graph: &mut Graph,
|
||||||
|
grid_to_node: &HashMap<IVec2, NodeId>,
|
||||||
|
tunnel_ends: &[Option<IVec2>; 2],
|
||||||
|
) -> GameResult<()> {
|
||||||
// Create the hidden tunnel nodes
|
// Create the hidden tunnel nodes
|
||||||
let left_tunnel_hidden_node_id = {
|
let left_tunnel_hidden_node_id = {
|
||||||
let left_tunnel_entrance_node_id = grid_to_node[&tunnel_ends[0].expect("Left tunnel end not found")];
|
let left_tunnel_entrance_node_id =
|
||||||
|
grid_to_node[&tunnel_ends[0].ok_or_else(|| MapError::InvalidConfig("Left tunnel end not found".to_string()))?];
|
||||||
let left_tunnel_entrance_node = graph
|
let left_tunnel_entrance_node = graph
|
||||||
.get_node(left_tunnel_entrance_node_id)
|
.get_node(left_tunnel_entrance_node_id)
|
||||||
.expect("Left tunnel entrance node not found");
|
.ok_or_else(|| MapError::InvalidConfig("Left tunnel entrance node not found".to_string()))?;
|
||||||
|
|
||||||
graph
|
graph
|
||||||
.connect_node(
|
.add_connected(
|
||||||
left_tunnel_entrance_node_id,
|
left_tunnel_entrance_node_id,
|
||||||
Direction::Left,
|
Direction::Left,
|
||||||
Node {
|
Node {
|
||||||
@@ -334,18 +358,24 @@ impl Map {
|
|||||||
+ (Direction::Left.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
+ (Direction::Left.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.expect("Failed to connect left tunnel entrance to left tunnel hidden node")
|
.map_err(|e| {
|
||||||
|
MapError::InvalidConfig(format!(
|
||||||
|
"Failed to connect left tunnel entrance to left tunnel hidden node: {}",
|
||||||
|
e
|
||||||
|
))
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create the right tunnel nodes
|
// Create the right tunnel nodes
|
||||||
let right_tunnel_hidden_node_id = {
|
let right_tunnel_hidden_node_id = {
|
||||||
let right_tunnel_entrance_node_id = grid_to_node[&tunnel_ends[1].expect("Right tunnel end not found")];
|
let right_tunnel_entrance_node_id =
|
||||||
|
grid_to_node[&tunnel_ends[1].ok_or_else(|| MapError::InvalidConfig("Right tunnel end not found".to_string()))?];
|
||||||
let right_tunnel_entrance_node = graph
|
let right_tunnel_entrance_node = graph
|
||||||
.get_node(right_tunnel_entrance_node_id)
|
.get_node(right_tunnel_entrance_node_id)
|
||||||
.expect("Right tunnel entrance node not found");
|
.ok_or_else(|| MapError::InvalidConfig("Right tunnel entrance node not found".to_string()))?;
|
||||||
|
|
||||||
graph
|
graph
|
||||||
.connect_node(
|
.add_connected(
|
||||||
right_tunnel_entrance_node_id,
|
right_tunnel_entrance_node_id,
|
||||||
Direction::Right,
|
Direction::Right,
|
||||||
Node {
|
Node {
|
||||||
@@ -353,7 +383,12 @@ impl Map {
|
|||||||
+ (Direction::Right.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
+ (Direction::Right.as_ivec2() * (CELL_SIZE as i32 * 2)).as_vec2(),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.expect("Failed to connect right tunnel entrance to right tunnel hidden node")
|
.map_err(|e| {
|
||||||
|
MapError::InvalidConfig(format!(
|
||||||
|
"Failed to connect right tunnel entrance to right tunnel hidden node: {}",
|
||||||
|
e
|
||||||
|
))
|
||||||
|
})?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Connect the left tunnel hidden node to the right tunnel hidden node
|
// Connect the left tunnel hidden node to the right tunnel hidden node
|
||||||
@@ -365,6 +400,13 @@ impl Map {
|
|||||||
Some(0.0),
|
Some(0.0),
|
||||||
Direction::Left,
|
Direction::Left,
|
||||||
)
|
)
|
||||||
.expect("Failed to connect left tunnel hidden node to right tunnel hidden node");
|
.map_err(|e| {
|
||||||
|
MapError::InvalidConfig(format!(
|
||||||
|
"Failed to connect left tunnel hidden node to right tunnel hidden node: {}",
|
||||||
|
e
|
||||||
|
))
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ pub enum ParseError {
|
|||||||
UnknownCharacter(char),
|
UnknownCharacter(char),
|
||||||
#[error("House door must have exactly 2 positions, found {0}")]
|
#[error("House door must have exactly 2 positions, found {0}")]
|
||||||
InvalidHouseDoorCount(usize),
|
InvalidHouseDoorCount(usize),
|
||||||
|
#[error("Map parsing failed: {0}")]
|
||||||
|
ParseFailed(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the parsed data from a raw board layout.
|
/// Represents the parsed data from a raw board layout.
|
||||||
@@ -67,6 +69,25 @@ impl MapTileParser {
|
|||||||
/// Returns an error if the board contains unknown characters or if the house door
|
/// Returns an error if the board contains unknown characters or if the house door
|
||||||
/// is not properly defined by exactly two '=' characters.
|
/// is not properly defined by exactly two '=' characters.
|
||||||
pub fn parse_board(raw_board: [&str; BOARD_CELL_SIZE.y as usize]) -> Result<ParsedMap, ParseError> {
|
pub fn parse_board(raw_board: [&str; BOARD_CELL_SIZE.y as usize]) -> Result<ParsedMap, ParseError> {
|
||||||
|
// Validate board dimensions
|
||||||
|
if raw_board.len() != BOARD_CELL_SIZE.y as usize {
|
||||||
|
return Err(ParseError::ParseFailed(format!(
|
||||||
|
"Invalid board height: expected {}, got {}",
|
||||||
|
BOARD_CELL_SIZE.y,
|
||||||
|
raw_board.len()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i, line) in raw_board.iter().enumerate() {
|
||||||
|
if line.len() != BOARD_CELL_SIZE.x as usize {
|
||||||
|
return Err(ParseError::ParseFailed(format!(
|
||||||
|
"Invalid board width at line {}: expected {}, got {}",
|
||||||
|
i,
|
||||||
|
BOARD_CELL_SIZE.x,
|
||||||
|
line.len()
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
let mut tiles = [[MapTile::Empty; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize];
|
let mut tiles = [[MapTile::Empty; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize];
|
||||||
let mut house_door = [None; 2];
|
let mut house_door = [None; 2];
|
||||||
let mut tunnel_ends = [None; 2];
|
let mut tunnel_ends = [None; 2];
|
||||||
|
|||||||
@@ -1,10 +1,14 @@
|
|||||||
//! Map rendering functionality.
|
//! Map rendering functionality.
|
||||||
|
|
||||||
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
||||||
|
use crate::texture::text::TextTexture;
|
||||||
|
use glam::Vec2;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use sdl2::rect::{Point, Rect};
|
use sdl2::rect::{Point, Rect};
|
||||||
use sdl2::render::{Canvas, RenderTarget};
|
use sdl2::render::{Canvas, RenderTarget};
|
||||||
|
|
||||||
|
use crate::error::{EntityError, GameError, GameResult};
|
||||||
|
|
||||||
/// Handles rendering operations for the map.
|
/// Handles rendering operations for the map.
|
||||||
pub struct MapRenderer;
|
pub struct MapRenderer;
|
||||||
|
|
||||||
@@ -20,48 +24,115 @@ impl MapRenderer {
|
|||||||
crate::constants::BOARD_PIXEL_SIZE.x,
|
crate::constants::BOARD_PIXEL_SIZE.x,
|
||||||
crate::constants::BOARD_PIXEL_SIZE.y,
|
crate::constants::BOARD_PIXEL_SIZE.y,
|
||||||
);
|
);
|
||||||
let _ = map_texture.render(canvas, atlas, dest);
|
if let Err(e) = map_texture.render(canvas, atlas, dest) {
|
||||||
}
|
tracing::error!("Failed to render map: {}", e);
|
||||||
|
|
||||||
/// Renders a debug visualization of the navigation graph.
|
|
||||||
///
|
|
||||||
/// This function is intended for development and debugging purposes. It draws the
|
|
||||||
/// nodes and edges of the graph on top of the map, allowing for visual
|
|
||||||
/// inspection of the navigation paths.
|
|
||||||
pub fn debug_render_nodes<T: RenderTarget>(graph: &crate::entity::graph::Graph, canvas: &mut Canvas<T>) {
|
|
||||||
for i in 0..graph.node_count() {
|
|
||||||
let node = graph.get_node(i).unwrap();
|
|
||||||
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
|
||||||
|
|
||||||
// Draw connections
|
|
||||||
canvas.set_draw_color(Color::BLUE);
|
|
||||||
|
|
||||||
for edge in graph.adjacency_list[i].edges() {
|
|
||||||
let end_pos = graph.get_node(edge.target).unwrap().position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
|
||||||
canvas
|
|
||||||
.draw_line((pos.x as i32, pos.y as i32), (end_pos.x as i32, end_pos.y as i32))
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Draw node
|
|
||||||
// let color = if pacman.position.from_node_idx() == i.into() {
|
|
||||||
// Color::GREEN
|
|
||||||
// } else if let Some(to_idx) = pacman.position.to_node_idx() {
|
|
||||||
// if to_idx == i.into() {
|
|
||||||
// Color::CYAN
|
|
||||||
// } else {
|
|
||||||
// Color::RED
|
|
||||||
// }
|
|
||||||
// } else {
|
|
||||||
// Color::RED
|
|
||||||
// };
|
|
||||||
canvas.set_draw_color(Color::GREEN);
|
|
||||||
canvas
|
|
||||||
.fill_rect(Rect::new(0, 0, 3, 3).centered_on(Point::new(pos.x as i32, pos.y as i32)))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Draw node index
|
|
||||||
// text.render(canvas, atlas, &i.to_string(), pos.as_uvec2()).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Renders a debug visualization with cursor-based highlighting.
|
||||||
|
///
|
||||||
|
/// This function provides interactive debugging by highlighting the nearest node
|
||||||
|
/// to the cursor, showing its ID, and highlighting its connections.
|
||||||
|
pub fn debug_render_with_cursor<T: RenderTarget>(
|
||||||
|
graph: &crate::entity::graph::Graph,
|
||||||
|
canvas: &mut Canvas<T>,
|
||||||
|
text_renderer: &mut TextTexture,
|
||||||
|
atlas: &mut SpriteAtlas,
|
||||||
|
cursor_pos: Vec2,
|
||||||
|
) -> GameResult<()> {
|
||||||
|
// Find the nearest node to the cursor
|
||||||
|
let nearest_node = Self::find_nearest_node(graph, cursor_pos);
|
||||||
|
|
||||||
|
// Draw all connections in blue
|
||||||
|
canvas.set_draw_color(Color::RGB(0, 0, 128)); // Dark blue for regular connections
|
||||||
|
for i in 0..graph.node_count() {
|
||||||
|
let node = graph.get_node(i).ok_or(GameError::Entity(EntityError::NodeNotFound(i)))?;
|
||||||
|
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
|
||||||
|
for edge in graph.adjacency_list[i].edges() {
|
||||||
|
let end_pos = graph
|
||||||
|
.get_node(edge.target)
|
||||||
|
.ok_or(GameError::Entity(EntityError::NodeNotFound(edge.target)))?
|
||||||
|
.position
|
||||||
|
+ crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
canvas
|
||||||
|
.draw_line((pos.x as i32, pos.y as i32), (end_pos.x as i32, end_pos.y as i32))
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw all nodes in green
|
||||||
|
canvas.set_draw_color(Color::RGB(0, 128, 0)); // Dark green for regular nodes
|
||||||
|
for i in 0..graph.node_count() {
|
||||||
|
let node = graph.get_node(i).ok_or(GameError::Entity(EntityError::NodeNotFound(i)))?;
|
||||||
|
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
|
||||||
|
canvas
|
||||||
|
.fill_rect(Rect::new(0, 0, 3, 3).centered_on(Point::new(pos.x as i32, pos.y as i32)))
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Highlight connections from the nearest node in bright blue
|
||||||
|
if let Some(nearest_id) = nearest_node {
|
||||||
|
let nearest_pos = graph
|
||||||
|
.get_node(nearest_id)
|
||||||
|
.ok_or(GameError::Entity(EntityError::NodeNotFound(nearest_id)))?
|
||||||
|
.position
|
||||||
|
+ crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
|
||||||
|
canvas.set_draw_color(Color::RGB(0, 255, 255)); // Bright cyan for highlighted connections
|
||||||
|
for edge in graph.adjacency_list[nearest_id].edges() {
|
||||||
|
let end_pos = graph
|
||||||
|
.get_node(edge.target)
|
||||||
|
.ok_or(GameError::Entity(EntityError::NodeNotFound(edge.target)))?
|
||||||
|
.position
|
||||||
|
+ crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
canvas
|
||||||
|
.draw_line(
|
||||||
|
(nearest_pos.x as i32, nearest_pos.y as i32),
|
||||||
|
(end_pos.x as i32, end_pos.y as i32),
|
||||||
|
)
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Highlight the nearest node in bright green
|
||||||
|
canvas.set_draw_color(Color::RGB(0, 255, 0)); // Bright green for highlighted node
|
||||||
|
canvas
|
||||||
|
.fill_rect(Rect::new(0, 0, 5, 5).centered_on(Point::new(nearest_pos.x as i32, nearest_pos.y as i32)))
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
|
// Draw node ID text (small, offset to top right)
|
||||||
|
text_renderer.set_scale(0.5); // Small text
|
||||||
|
let id_text = format!("#{nearest_id}");
|
||||||
|
let text_pos = glam::UVec2::new(
|
||||||
|
(nearest_pos.x + 4.0) as u32, // Offset to the right
|
||||||
|
(nearest_pos.y - 6.0) as u32, // Offset to the top
|
||||||
|
);
|
||||||
|
if let Err(e) = text_renderer.render(canvas, atlas, &id_text, text_pos) {
|
||||||
|
tracing::error!("Failed to render node ID text: {}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Finds the nearest node to the given cursor position.
|
||||||
|
pub fn find_nearest_node(graph: &crate::entity::graph::Graph, cursor_pos: Vec2) -> Option<usize> {
|
||||||
|
let mut nearest_id = None;
|
||||||
|
let mut nearest_distance = f32::INFINITY;
|
||||||
|
|
||||||
|
for i in 0..graph.node_count() {
|
||||||
|
if let Some(node) = graph.get_node(i) {
|
||||||
|
let node_pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||||
|
let distance = cursor_pos.distance(node_pos);
|
||||||
|
|
||||||
|
if distance < nearest_distance {
|
||||||
|
nearest_distance = distance;
|
||||||
|
nearest_id = Some(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nearest_id
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
77
src/platform/desktop.rs
Normal file
77
src/platform/desktop.rs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
//! Desktop platform implementation.
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::asset::{Asset, AssetError};
|
||||||
|
use crate::platform::{Platform, PlatformError};
|
||||||
|
|
||||||
|
/// Desktop platform implementation.
|
||||||
|
pub struct DesktopPlatform;
|
||||||
|
|
||||||
|
impl Platform for DesktopPlatform {
|
||||||
|
fn sleep(&self, duration: Duration) {
|
||||||
|
spin_sleep::sleep(duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_time(&self) -> f64 {
|
||||||
|
std::time::Instant::now().elapsed().as_secs_f64()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_console(&self) -> Result<(), PlatformError> {
|
||||||
|
#[cfg(windows)]
|
||||||
|
{
|
||||||
|
unsafe {
|
||||||
|
use winapi::{
|
||||||
|
shared::ntdef::NULL,
|
||||||
|
um::{
|
||||||
|
fileapi::{CreateFileA, OPEN_EXISTING},
|
||||||
|
handleapi::INVALID_HANDLE_VALUE,
|
||||||
|
processenv::SetStdHandle,
|
||||||
|
winbase::{STD_ERROR_HANDLE, STD_OUTPUT_HANDLE},
|
||||||
|
wincon::{AttachConsole, GetConsoleWindow},
|
||||||
|
winnt::{FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
if !std::ptr::eq(GetConsoleWindow(), std::ptr::null_mut()) {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if AttachConsole(winapi::um::wincon::ATTACH_PARENT_PROCESS) != 0 {
|
||||||
|
let handle = CreateFileA(
|
||||||
|
c"CONOUT$".as_ptr(),
|
||||||
|
GENERIC_READ | GENERIC_WRITE,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||||
|
std::ptr::null_mut(),
|
||||||
|
OPEN_EXISTING,
|
||||||
|
0,
|
||||||
|
NULL,
|
||||||
|
);
|
||||||
|
|
||||||
|
if handle != INVALID_HANDLE_VALUE {
|
||||||
|
SetStdHandle(STD_OUTPUT_HANDLE, handle);
|
||||||
|
SetStdHandle(STD_ERROR_HANDLE, handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_canvas_size(&self) -> Option<(u32, u32)> {
|
||||||
|
None // Desktop doesn't need this
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
||||||
|
match asset {
|
||||||
|
Asset::Wav1 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/1.ogg"))),
|
||||||
|
Asset::Wav2 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/2.ogg"))),
|
||||||
|
Asset::Wav3 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/3.ogg"))),
|
||||||
|
Asset::Wav4 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/4.ogg"))),
|
||||||
|
Asset::Atlas => Ok(Cow::Borrowed(include_bytes!("../../assets/game/atlas.png"))),
|
||||||
|
Asset::AtlasJson => Ok(Cow::Borrowed(include_bytes!("../../assets/game/atlas.json"))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/platform/emscripten.rs
Normal file
61
src/platform/emscripten.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
//! Emscripten platform implementation.
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::asset::{Asset, AssetError};
|
||||||
|
use crate::platform::{Platform, PlatformError};
|
||||||
|
|
||||||
|
/// Emscripten platform implementation.
|
||||||
|
pub struct EmscriptenPlatform;
|
||||||
|
|
||||||
|
impl Platform for EmscriptenPlatform {
|
||||||
|
fn sleep(&self, duration: Duration) {
|
||||||
|
unsafe {
|
||||||
|
emscripten_sleep(duration.as_millis() as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_time(&self) -> f64 {
|
||||||
|
unsafe { emscripten_get_now() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_console(&self) -> Result<(), PlatformError> {
|
||||||
|
Ok(()) // No-op for Emscripten
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_canvas_size(&self) -> Option<(u32, u32)> {
|
||||||
|
Some(unsafe { get_canvas_size() })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
||||||
|
use sdl2::rwops::RWops;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
let path = format!("assets/game/{}", asset.path());
|
||||||
|
let mut rwops = RWops::from_file(&path, "rb").map_err(|_| AssetError::NotFound(asset.path().to_string()))?;
|
||||||
|
|
||||||
|
let len = rwops.len().ok_or_else(|| AssetError::NotFound(asset.path().to_string()))?;
|
||||||
|
|
||||||
|
let mut buf = vec![0u8; len];
|
||||||
|
rwops
|
||||||
|
.read_exact(&mut buf)
|
||||||
|
.map_err(|e| AssetError::Io(std::io::Error::other(e)))?;
|
||||||
|
|
||||||
|
Ok(Cow::Owned(buf))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Emscripten FFI functions
|
||||||
|
extern "C" {
|
||||||
|
fn emscripten_get_now() -> f64;
|
||||||
|
fn emscripten_sleep(ms: u32);
|
||||||
|
fn emscripten_get_element_css_size(target: *const u8, width: *mut f64, height: *mut f64) -> i32;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn get_canvas_size() -> (u32, u32) {
|
||||||
|
let mut width = 0.0;
|
||||||
|
let mut height = 0.0;
|
||||||
|
emscripten_get_element_css_size(c"canvas".as_ptr().cast(), &mut width, &mut height);
|
||||||
|
(width as u32, height as u32)
|
||||||
|
}
|
||||||
58
src/platform/mod.rs
Normal file
58
src/platform/mod.rs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
//! Platform abstraction layer for cross-platform functionality.
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use crate::asset::{Asset, AssetError};
|
||||||
|
|
||||||
|
pub mod desktop;
|
||||||
|
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);
|
||||||
|
|
||||||
|
/// Get the current time in seconds since some reference point.
|
||||||
|
/// This is available for future use in timing and performance monitoring.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn get_time(&self) -> f64;
|
||||||
|
|
||||||
|
/// Initialize platform-specific console functionality.
|
||||||
|
fn init_console(&self) -> Result<(), PlatformError>;
|
||||||
|
|
||||||
|
/// Get canvas size for platforms that need it (e.g., Emscripten).
|
||||||
|
/// This is available for future use in responsive design.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn get_canvas_size(&self) -> Option<(u32, u32)>;
|
||||||
|
|
||||||
|
/// Load asset bytes using platform-appropriate method.
|
||||||
|
fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Platform-specific errors.
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum PlatformError {
|
||||||
|
#[error("Console initialization failed: {0}")]
|
||||||
|
ConsoleInit(String),
|
||||||
|
#[error("Platform-specific error: {0}")]
|
||||||
|
Other(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the current platform implementation.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn get_platform() -> &'static dyn Platform {
|
||||||
|
static DESKTOP: desktop::DesktopPlatform = desktop::DesktopPlatform;
|
||||||
|
static EMSCRIPTEN: emscripten::EmscriptenPlatform = emscripten::EmscriptenPlatform;
|
||||||
|
|
||||||
|
#[cfg(not(target_os = "emscripten"))]
|
||||||
|
{
|
||||||
|
&DESKTOP
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "emscripten")]
|
||||||
|
{
|
||||||
|
&EMSCRIPTEN
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Rect;
|
||||||
use sdl2::render::{Canvas, RenderTarget};
|
use sdl2::render::{Canvas, RenderTarget};
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use crate::entity::direction::Direction;
|
use crate::entity::direction::Direction;
|
||||||
use crate::texture::animated::AnimatedTexture;
|
use crate::texture::animated::AnimatedTexture;
|
||||||
@@ -9,12 +8,12 @@ use crate::texture::sprite::SpriteAtlas;
|
|||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct DirectionalAnimatedTexture {
|
pub struct DirectionalAnimatedTexture {
|
||||||
textures: HashMap<Direction, AnimatedTexture>,
|
textures: [Option<AnimatedTexture>; 4],
|
||||||
stopped_textures: HashMap<Direction, AnimatedTexture>,
|
stopped_textures: [Option<AnimatedTexture>; 4],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirectionalAnimatedTexture {
|
impl DirectionalAnimatedTexture {
|
||||||
pub fn new(textures: HashMap<Direction, AnimatedTexture>, stopped_textures: HashMap<Direction, AnimatedTexture>) -> Self {
|
pub fn new(textures: [Option<AnimatedTexture>; 4], stopped_textures: [Option<AnimatedTexture>; 4]) -> Self {
|
||||||
Self {
|
Self {
|
||||||
textures,
|
textures,
|
||||||
stopped_textures,
|
stopped_textures,
|
||||||
@@ -22,7 +21,7 @@ impl DirectionalAnimatedTexture {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick(&mut self, dt: f32) {
|
pub fn tick(&mut self, dt: f32) {
|
||||||
for texture in self.textures.values_mut() {
|
for texture in self.textures.iter_mut().flatten() {
|
||||||
texture.tick(dt);
|
texture.tick(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -34,7 +33,7 @@ impl DirectionalAnimatedTexture {
|
|||||||
dest: Rect,
|
dest: Rect,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if let Some(texture) = self.textures.get(&direction) {
|
if let Some(texture) = &self.textures[direction.as_usize()] {
|
||||||
texture.render(canvas, atlas, dest)
|
texture.render(canvas, atlas, dest)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -48,7 +47,7 @@ impl DirectionalAnimatedTexture {
|
|||||||
dest: Rect,
|
dest: Rect,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if let Some(texture) = self.stopped_textures.get(&direction) {
|
if let Some(texture) = &self.stopped_textures[direction.as_usize()] {
|
||||||
texture.render(canvas, atlas, dest)
|
texture.render(canvas, atlas, dest)
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -58,24 +57,24 @@ impl DirectionalAnimatedTexture {
|
|||||||
/// Returns true if the texture has a direction.
|
/// Returns true if the texture has a direction.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn has_direction(&self, direction: Direction) -> bool {
|
pub fn has_direction(&self, direction: Direction) -> bool {
|
||||||
self.textures.contains_key(&direction)
|
self.textures[direction.as_usize()].is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the texture has a stopped direction.
|
/// Returns true if the texture has a stopped direction.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn has_stopped_direction(&self, direction: Direction) -> bool {
|
pub fn has_stopped_direction(&self, direction: Direction) -> bool {
|
||||||
self.stopped_textures.contains_key(&direction)
|
self.stopped_textures[direction.as_usize()].is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of textures.
|
/// Returns the number of textures.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn texture_count(&self) -> usize {
|
pub fn texture_count(&self) -> usize {
|
||||||
self.textures.len()
|
self.textures.iter().filter(|t| t.is_some()).count()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of stopped textures.
|
/// Returns the number of stopped textures.
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn stopped_texture_count(&self) -> usize {
|
pub fn stopped_texture_count(&self) -> usize {
|
||||||
self.stopped_textures.len()
|
self.stopped_textures.iter().filter(|t| t.is_some()).count()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,16 +16,16 @@ fn test_blinking_texture() {
|
|||||||
let tile = mock_atlas_tile(1);
|
let tile = mock_atlas_tile(1);
|
||||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||||
|
|
||||||
assert_eq!(texture.is_on(), true);
|
assert!(texture.is_on());
|
||||||
|
|
||||||
texture.tick(0.5);
|
texture.tick(0.5);
|
||||||
assert_eq!(texture.is_on(), false);
|
assert!(!texture.is_on());
|
||||||
|
|
||||||
texture.tick(0.5);
|
texture.tick(0.5);
|
||||||
assert_eq!(texture.is_on(), true);
|
assert!(texture.is_on());
|
||||||
|
|
||||||
texture.tick(0.5);
|
texture.tick(0.5);
|
||||||
assert_eq!(texture.is_on(), false);
|
assert!(!texture.is_on());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -34,7 +34,7 @@ fn test_blinking_texture_partial_duration() {
|
|||||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||||
|
|
||||||
texture.tick(0.625);
|
texture.tick(0.625);
|
||||||
assert_eq!(texture.is_on(), false);
|
assert!(!texture.is_on());
|
||||||
assert_eq!(texture.time_bank(), 0.125);
|
assert_eq!(texture.time_bank(), 0.125);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,6 +44,6 @@ fn test_blinking_texture_negative_time() {
|
|||||||
let mut texture = BlinkingTexture::new(tile, 0.5);
|
let mut texture = BlinkingTexture::new(tile, 0.5);
|
||||||
|
|
||||||
texture.tick(-0.1);
|
texture.tick(-0.1);
|
||||||
assert_eq!(texture.is_on(), true);
|
assert!(texture.is_on());
|
||||||
assert_eq!(texture.time_bank(), -0.1);
|
assert_eq!(texture.time_bank(), -0.1);
|
||||||
}
|
}
|
||||||
|
|||||||
34
tests/debug_rendering.rs
Normal file
34
tests/debug_rendering.rs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
use glam::Vec2;
|
||||||
|
use pacman::entity::graph::{Graph, Node};
|
||||||
|
use pacman::map::render::MapRenderer;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_nearest_node() {
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
|
||||||
|
// Add some test nodes
|
||||||
|
let node1 = graph.add_node(Node {
|
||||||
|
position: Vec2::new(10.0, 10.0),
|
||||||
|
});
|
||||||
|
let node2 = graph.add_node(Node {
|
||||||
|
position: Vec2::new(50.0, 50.0),
|
||||||
|
});
|
||||||
|
let node3 = graph.add_node(Node {
|
||||||
|
position: Vec2::new(100.0, 100.0),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Test cursor near node1
|
||||||
|
let cursor_pos = Vec2::new(12.0, 8.0);
|
||||||
|
let nearest = MapRenderer::find_nearest_node(&graph, cursor_pos);
|
||||||
|
assert_eq!(nearest, Some(node1));
|
||||||
|
|
||||||
|
// Test cursor near node2
|
||||||
|
let cursor_pos = Vec2::new(45.0, 55.0);
|
||||||
|
let nearest = MapRenderer::find_nearest_node(&graph, cursor_pos);
|
||||||
|
assert_eq!(nearest, Some(node2));
|
||||||
|
|
||||||
|
// Test cursor near node3
|
||||||
|
let cursor_pos = Vec2::new(98.0, 102.0);
|
||||||
|
let nearest = MapRenderer::find_nearest_node(&graph, cursor_pos);
|
||||||
|
assert_eq!(nearest, Some(node3));
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ use pacman::texture::animated::AnimatedTexture;
|
|||||||
use pacman::texture::directional::DirectionalAnimatedTexture;
|
use pacman::texture::directional::DirectionalAnimatedTexture;
|
||||||
use pacman::texture::sprite::AtlasTile;
|
use pacman::texture::sprite::AtlasTile;
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
fn mock_atlas_tile(id: u32) -> AtlasTile {
|
fn mock_atlas_tile(id: u32) -> AtlasTile {
|
||||||
AtlasTile {
|
AtlasTile {
|
||||||
@@ -20,10 +19,10 @@ fn mock_animated_texture(id: u32) -> AnimatedTexture {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_directional_texture_partial_directions() {
|
fn test_directional_texture_partial_directions() {
|
||||||
let mut textures = HashMap::new();
|
let mut textures = [None, None, None, None];
|
||||||
textures.insert(Direction::Up, mock_animated_texture(1));
|
textures[Direction::Up.as_usize()] = Some(mock_animated_texture(1));
|
||||||
|
|
||||||
let texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
let texture = DirectionalAnimatedTexture::new(textures, [None, None, None, None]);
|
||||||
|
|
||||||
assert_eq!(texture.texture_count(), 1);
|
assert_eq!(texture.texture_count(), 1);
|
||||||
assert!(texture.has_direction(Direction::Up));
|
assert!(texture.has_direction(Direction::Up));
|
||||||
@@ -34,7 +33,7 @@ fn test_directional_texture_partial_directions() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_directional_texture_all_directions() {
|
fn test_directional_texture_all_directions() {
|
||||||
let mut textures = HashMap::new();
|
let mut textures = [None, None, None, None];
|
||||||
let directions = [
|
let directions = [
|
||||||
(Direction::Up, 1),
|
(Direction::Up, 1),
|
||||||
(Direction::Down, 2),
|
(Direction::Down, 2),
|
||||||
@@ -43,10 +42,10 @@ fn test_directional_texture_all_directions() {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for (direction, id) in directions {
|
for (direction, id) in directions {
|
||||||
textures.insert(direction, mock_animated_texture(id));
|
textures[direction.as_usize()] = Some(mock_animated_texture(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
let texture = DirectionalAnimatedTexture::new(textures, HashMap::new());
|
let texture = DirectionalAnimatedTexture::new(textures, [None, None, None, None]);
|
||||||
|
|
||||||
assert_eq!(texture.texture_count(), 4);
|
assert_eq!(texture.texture_count(), 4);
|
||||||
for direction in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
for direction in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use pacman::map::Map;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_game_map_creation() {
|
fn test_game_map_creation() {
|
||||||
let map = Map::new(RAW_BOARD);
|
let map = Map::new(RAW_BOARD).unwrap();
|
||||||
|
|
||||||
assert!(map.graph.node_count() > 0);
|
assert!(map.graph.node_count() > 0);
|
||||||
assert!(!map.grid_to_node.is_empty());
|
assert!(!map.grid_to_node.is_empty());
|
||||||
@@ -16,6 +16,6 @@ fn test_game_map_creation() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_game_score_initialization() {
|
fn test_game_score_initialization() {
|
||||||
// This would require creating a full Game instance, but we can test the concept
|
// This would require creating a full Game instance, but we can test the concept
|
||||||
let map = Map::new(RAW_BOARD);
|
let map = Map::new(RAW_BOARD).unwrap();
|
||||||
assert!(map.find_starting_position(0).is_some());
|
assert!(map.find_starting_position(0).is_some());
|
||||||
}
|
}
|
||||||
|
|||||||
48
tests/ghost.rs
Normal file
48
tests/ghost.rs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
use pacman::entity::ghost::{Ghost, GhostType};
|
||||||
|
use pacman::entity::graph::Graph;
|
||||||
|
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
fn create_test_atlas() -> SpriteAtlas {
|
||||||
|
let mut frames = HashMap::new();
|
||||||
|
let directions = ["up", "down", "left", "right"];
|
||||||
|
let ghost_types = ["blinky", "pinky", "inky", "clyde"];
|
||||||
|
|
||||||
|
for ghost_type in &ghost_types {
|
||||||
|
for (i, dir) in directions.iter().enumerate() {
|
||||||
|
frames.insert(
|
||||||
|
format!("ghost/{}/{}_{}.png", ghost_type, dir, "a"),
|
||||||
|
MapperFrame {
|
||||||
|
x: i as u16 * 16,
|
||||||
|
y: 0,
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
frames.insert(
|
||||||
|
format!("ghost/{}/{}_{}.png", ghost_type, dir, "b"),
|
||||||
|
MapperFrame {
|
||||||
|
x: i as u16 * 16,
|
||||||
|
y: 16,
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mapper = AtlasMapper { frames };
|
||||||
|
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||||
|
SpriteAtlas::new(dummy_texture, mapper)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ghost_creation() {
|
||||||
|
let graph = Graph::new();
|
||||||
|
let atlas = create_test_atlas();
|
||||||
|
|
||||||
|
let ghost = Ghost::new(&graph, 0, GhostType::Blinky, &atlas).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(ghost.ghost_type, GhostType::Blinky);
|
||||||
|
assert_eq!(ghost.traverser.position.from_node_id(), 0);
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
use pacman::entity::direction::Direction;
|
use pacman::entity::direction::Direction;
|
||||||
use pacman::entity::graph::{EdgePermissions, Graph, Node, Position, Traverser};
|
use pacman::entity::graph::{EdgePermissions, Graph, Node};
|
||||||
|
use pacman::entity::traversal::{Position, Traverser};
|
||||||
|
|
||||||
fn create_test_graph() -> Graph {
|
fn create_test_graph() -> Graph {
|
||||||
let mut graph = Graph::new();
|
let mut graph = Graph::new();
|
||||||
@@ -100,7 +101,7 @@ fn test_traverser_advance() {
|
|||||||
let graph = create_test_graph();
|
let graph = create_test_graph();
|
||||||
let mut traverser = Traverser::new(&graph, 0, Direction::Right, &|_| true);
|
let mut traverser = Traverser::new(&graph, 0, Direction::Right, &|_| true);
|
||||||
|
|
||||||
traverser.advance(&graph, 5.0, &|_| true);
|
traverser.advance(&graph, 5.0, &|_| true).unwrap();
|
||||||
|
|
||||||
match traverser.position {
|
match traverser.position {
|
||||||
Position::BetweenNodes { from, to, traversed } => {
|
Position::BetweenNodes { from, to, traversed } => {
|
||||||
@@ -111,7 +112,7 @@ fn test_traverser_advance() {
|
|||||||
_ => panic!("Expected to be between nodes"),
|
_ => panic!("Expected to be between nodes"),
|
||||||
}
|
}
|
||||||
|
|
||||||
traverser.advance(&graph, 3.0, &|_| true);
|
traverser.advance(&graph, 3.0, &|_| true).unwrap();
|
||||||
|
|
||||||
match traverser.position {
|
match traverser.position {
|
||||||
Position::BetweenNodes { from, to, traversed } => {
|
Position::BetweenNodes { from, to, traversed } => {
|
||||||
@@ -142,7 +143,9 @@ fn test_traverser_with_permissions() {
|
|||||||
matches!(edge.permissions, EdgePermissions::All)
|
matches!(edge.permissions, EdgePermissions::All)
|
||||||
});
|
});
|
||||||
|
|
||||||
traverser.advance(&graph, 5.0, &|edge| matches!(edge.permissions, EdgePermissions::All));
|
traverser
|
||||||
|
.advance(&graph, 5.0, &|edge| matches!(edge.permissions, EdgePermissions::All))
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// Should still be at the node since it can't traverse
|
// Should still be at the node since it can't traverse
|
||||||
assert!(traverser.position.is_at_node());
|
assert!(traverser.position.is_at_node());
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ fn create_minimal_test_board() -> [&'static str; BOARD_CELL_SIZE.y as usize] {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_map_creation() {
|
fn test_map_creation() {
|
||||||
let board = create_minimal_test_board();
|
let board = create_minimal_test_board();
|
||||||
let map = Map::new(board);
|
let map = Map::new(board).unwrap();
|
||||||
|
|
||||||
assert!(map.graph.node_count() > 0);
|
assert!(map.graph.node_count() > 0);
|
||||||
assert!(!map.grid_to_node.is_empty());
|
assert!(!map.grid_to_node.is_empty());
|
||||||
@@ -60,7 +60,7 @@ fn test_map_creation() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_map_starting_positions() {
|
fn test_map_starting_positions() {
|
||||||
let board = create_minimal_test_board();
|
let board = create_minimal_test_board();
|
||||||
let map = Map::new(board);
|
let map = Map::new(board).unwrap();
|
||||||
|
|
||||||
let pacman_pos = map.find_starting_position(0);
|
let pacman_pos = map.find_starting_position(0);
|
||||||
assert!(pacman_pos.is_some());
|
assert!(pacman_pos.is_some());
|
||||||
@@ -74,7 +74,7 @@ fn test_map_starting_positions() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_map_node_positions() {
|
fn test_map_node_positions() {
|
||||||
let board = create_minimal_test_board();
|
let board = create_minimal_test_board();
|
||||||
let map = Map::new(board);
|
let map = Map::new(board).unwrap();
|
||||||
|
|
||||||
for (grid_pos, &node_id) in &map.grid_to_node {
|
for (grid_pos, &node_id) in &map.grid_to_node {
|
||||||
let node = map.graph.get_node(node_id).unwrap();
|
let node = map.graph.get_node(node_id).unwrap();
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ fn create_test_atlas() -> SpriteAtlas {
|
|||||||
fn test_pacman_creation() {
|
fn test_pacman_creation() {
|
||||||
let graph = create_test_graph();
|
let graph = create_test_graph();
|
||||||
let atlas = create_test_atlas();
|
let atlas = create_test_atlas();
|
||||||
let pacman = Pacman::new(&graph, 0, &atlas);
|
let pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||||
|
|
||||||
assert!(pacman.traverser.position.is_at_node());
|
assert!(pacman.traverser.position.is_at_node());
|
||||||
assert_eq!(pacman.traverser.direction, Direction::Left);
|
assert_eq!(pacman.traverser.direction, Direction::Left);
|
||||||
@@ -77,7 +77,7 @@ fn test_pacman_creation() {
|
|||||||
fn test_pacman_key_handling() {
|
fn test_pacman_key_handling() {
|
||||||
let graph = create_test_graph();
|
let graph = create_test_graph();
|
||||||
let atlas = create_test_atlas();
|
let atlas = create_test_atlas();
|
||||||
let mut pacman = Pacman::new(&graph, 0, &atlas);
|
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||||
|
|
||||||
let test_cases = [
|
let test_cases = [
|
||||||
(Keycode::Up, Direction::Up),
|
(Keycode::Up, Direction::Up),
|
||||||
@@ -96,7 +96,7 @@ fn test_pacman_key_handling() {
|
|||||||
fn test_pacman_invalid_key() {
|
fn test_pacman_invalid_key() {
|
||||||
let graph = create_test_graph();
|
let graph = create_test_graph();
|
||||||
let atlas = create_test_atlas();
|
let atlas = create_test_atlas();
|
||||||
let mut pacman = Pacman::new(&graph, 0, &atlas);
|
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||||
|
|
||||||
let original_direction = pacman.traverser.direction;
|
let original_direction = pacman.traverser.direction;
|
||||||
let original_next_direction = pacman.traverser.next_direction;
|
let original_next_direction = pacman.traverser.next_direction;
|
||||||
|
|||||||
@@ -37,10 +37,10 @@ fn test_parse_board() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_board_invalid_character() {
|
fn test_parse_board_invalid_character() {
|
||||||
let mut invalid_board = RAW_BOARD.clone();
|
let mut invalid_board = RAW_BOARD.map(|s| s.to_string());
|
||||||
invalid_board[0] = "###########################Z";
|
invalid_board[0] = "###########################Z".to_string();
|
||||||
|
|
||||||
let result = MapTileParser::parse_board(invalid_board);
|
let result = MapTileParser::parse_board(invalid_board.each_ref().map(|s| s.as_str()));
|
||||||
assert!(result.is_err());
|
assert!(result.is_err());
|
||||||
assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('Z')));
|
assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('Z')));
|
||||||
}
|
}
|
||||||
|
|||||||
120
tests/pathfinding.rs
Normal file
120
tests/pathfinding.rs
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
use pacman::entity::direction::Direction;
|
||||||
|
use pacman::entity::ghost::{Ghost, GhostType};
|
||||||
|
use pacman::entity::graph::{Graph, Node};
|
||||||
|
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
fn create_test_atlas() -> SpriteAtlas {
|
||||||
|
let mut frames = HashMap::new();
|
||||||
|
let directions = ["up", "down", "left", "right"];
|
||||||
|
let ghost_types = ["blinky", "pinky", "inky", "clyde"];
|
||||||
|
|
||||||
|
for ghost_type in &ghost_types {
|
||||||
|
for (i, dir) in directions.iter().enumerate() {
|
||||||
|
frames.insert(
|
||||||
|
format!("ghost/{}/{}_{}.png", ghost_type, dir, "a"),
|
||||||
|
MapperFrame {
|
||||||
|
x: i as u16 * 16,
|
||||||
|
y: 0,
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
frames.insert(
|
||||||
|
format!("ghost/{}/{}_{}.png", ghost_type, dir, "b"),
|
||||||
|
MapperFrame {
|
||||||
|
x: i as u16 * 16,
|
||||||
|
y: 16,
|
||||||
|
width: 16,
|
||||||
|
height: 16,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mapper = AtlasMapper { frames };
|
||||||
|
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||||
|
SpriteAtlas::new(dummy_texture, mapper)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ghost_pathfinding() {
|
||||||
|
// Create a simple test graph
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
|
||||||
|
// Add nodes in a simple line: 0 -> 1 -> 2
|
||||||
|
let node0 = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(0.0, 0.0),
|
||||||
|
});
|
||||||
|
let node1 = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(10.0, 0.0),
|
||||||
|
});
|
||||||
|
let node2 = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(20.0, 0.0),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Connect the nodes
|
||||||
|
graph.connect(node0, node1, false, None, Direction::Right).unwrap();
|
||||||
|
graph.connect(node1, node2, false, None, Direction::Right).unwrap();
|
||||||
|
|
||||||
|
// Create a test atlas for the ghost
|
||||||
|
let atlas = create_test_atlas();
|
||||||
|
|
||||||
|
// Create a ghost at node 0
|
||||||
|
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
|
||||||
|
|
||||||
|
// Test pathfinding from node 0 to node 2
|
||||||
|
let path = ghost.calculate_path_to_target(&graph, node2);
|
||||||
|
|
||||||
|
assert!(path.is_ok());
|
||||||
|
let path = path.unwrap();
|
||||||
|
assert!(
|
||||||
|
path == vec![node0, node1, node2] || path == vec![node2, node1, node0],
|
||||||
|
"Path was not what was expected"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ghost_pathfinding_no_path() {
|
||||||
|
// Create a test graph with disconnected components
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
|
||||||
|
let node0 = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(0.0, 0.0),
|
||||||
|
});
|
||||||
|
let node1 = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(10.0, 0.0),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Don't connect the nodes
|
||||||
|
let atlas = create_test_atlas();
|
||||||
|
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
|
||||||
|
|
||||||
|
// Test pathfinding when no path exists
|
||||||
|
let path = ghost.calculate_path_to_target(&graph, node1);
|
||||||
|
|
||||||
|
assert!(path.is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_ghost_debug_colors() {
|
||||||
|
let atlas = create_test_atlas();
|
||||||
|
let mut graph = Graph::new();
|
||||||
|
let node = graph.add_node(Node {
|
||||||
|
position: glam::Vec2::new(0.0, 0.0),
|
||||||
|
});
|
||||||
|
|
||||||
|
let blinky = Ghost::new(&graph, node, GhostType::Blinky, &atlas).unwrap();
|
||||||
|
let pinky = Ghost::new(&graph, node, GhostType::Pinky, &atlas).unwrap();
|
||||||
|
let inky = Ghost::new(&graph, node, GhostType::Inky, &atlas).unwrap();
|
||||||
|
let clyde = Ghost::new(&graph, node, GhostType::Clyde, &atlas).unwrap();
|
||||||
|
|
||||||
|
// Test that each ghost has a different debug color
|
||||||
|
let colors = std::collections::HashSet::from([
|
||||||
|
blinky.debug_color(),
|
||||||
|
pinky.debug_color(),
|
||||||
|
inky.debug_color(),
|
||||||
|
clyde.debug_color(),
|
||||||
|
]);
|
||||||
|
assert_eq!(colors.len(), 4, "All ghost colors should be unique");
|
||||||
|
}
|
||||||
74
web.build.ts
74
web.build.ts
@@ -3,7 +3,7 @@ import { existsSync, promises as fs } from "fs";
|
|||||||
import { platform } from "os";
|
import { platform } from "os";
|
||||||
import { dirname, join, relative, resolve } from "path";
|
import { dirname, join, relative, resolve } from "path";
|
||||||
import { match, P } from "ts-pattern";
|
import { match, P } from "ts-pattern";
|
||||||
import { configure, getConsoleSink } from "@logtape/logtape";
|
import { configure, getConsoleSink, getLogger } from "@logtape/logtape";
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
const TAILWIND_UPDATE_WINDOW_DAYS = 60; // 2 months
|
const TAILWIND_UPDATE_WINDOW_DAYS = 60; // 2 months
|
||||||
@@ -11,7 +11,7 @@ const TAILWIND_UPDATE_WINDOW_DAYS = 60; // 2 months
|
|||||||
await configure({
|
await configure({
|
||||||
sinks: { console: getConsoleSink() },
|
sinks: { console: getConsoleSink() },
|
||||||
loggers: [
|
loggers: [
|
||||||
{ category: "web.build", lowestLevel: "debug", sinks: ["console"] },
|
{ category: "web", lowestLevel: "debug", sinks: ["console"] },
|
||||||
{
|
{
|
||||||
category: ["logtape", "meta"],
|
category: ["logtape", "meta"],
|
||||||
lowestLevel: "warning",
|
lowestLevel: "warning",
|
||||||
@@ -20,6 +20,8 @@ await configure({
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const logger = getLogger("web");
|
||||||
|
|
||||||
type Os =
|
type Os =
|
||||||
| { type: "linux"; wsl: boolean }
|
| { type: "linux"; wsl: boolean }
|
||||||
| { type: "windows" }
|
| { type: "windows" }
|
||||||
@@ -38,10 +40,6 @@ const os: Os = match(platform())
|
|||||||
throw new Error(`Unsupported platform: ${platform()}`);
|
throw new Error(`Unsupported platform: ${platform()}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
function log(msg: string) {
|
|
||||||
console.log(`[web.build] ${msg}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build the application with Emscripten, generate the CSS, and copy the files into 'dist'.
|
* Build the application with Emscripten, generate the CSS, and copy the files into 'dist'.
|
||||||
*
|
*
|
||||||
@@ -49,7 +47,7 @@ function log(msg: string) {
|
|||||||
* @param env - The environment variables to inject into build commands.
|
* @param env - The environment variables to inject into build commands.
|
||||||
*/
|
*/
|
||||||
async function build(release: boolean, env: Record<string, string> | null) {
|
async function build(release: boolean, env: Record<string, string> | null) {
|
||||||
log(
|
logger.info(
|
||||||
`Building for 'wasm32-unknown-emscripten' for ${
|
`Building for 'wasm32-unknown-emscripten' for ${
|
||||||
release ? "release" : "debug"
|
release ? "release" : "debug"
|
||||||
}`
|
}`
|
||||||
@@ -71,7 +69,7 @@ async function build(release: boolean, env: Record<string, string> | null) {
|
|||||||
})
|
})
|
||||||
.exhaustive();
|
.exhaustive();
|
||||||
|
|
||||||
log(`Invoking ${tailwindExecutable}...`);
|
logger.debug(`Invoking ${tailwindExecutable}...`);
|
||||||
await $`${tailwindExecutable} --minify --input styles.css --output build.css --cwd assets/site`;
|
await $`${tailwindExecutable} --minify --input styles.css --output build.css --cwd assets/site`;
|
||||||
|
|
||||||
const buildType = release ? "release" : "debug";
|
const buildType = release ? "release" : "debug";
|
||||||
@@ -108,20 +106,20 @@ async function build(release: boolean, env: Record<string, string> | null) {
|
|||||||
.map(async (dir) => {
|
.map(async (dir) => {
|
||||||
// If the folder doesn't exist, create it
|
// If the folder doesn't exist, create it
|
||||||
if (!(await fs.exists(dir))) {
|
if (!(await fs.exists(dir))) {
|
||||||
log(`Creating folder ${dir}`);
|
logger.debug(`Creating folder ${dir}`);
|
||||||
await fs.mkdir(dir, { recursive: true });
|
await fs.mkdir(dir, { recursive: true });
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// Copy the files to the dist folder
|
// Copy the files to the dist folder
|
||||||
log("Copying files into dist");
|
logger.debug("Copying files into dist");
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
files.map(async ({ optional, src, dest }) => {
|
files.map(async ({ optional, src, dest }) => {
|
||||||
match({ optional, exists: await fs.exists(src) })
|
match({ optional, exists: await fs.exists(src) })
|
||||||
// If optional and doesn't exist, skip
|
// If optional and doesn't exist, skip
|
||||||
.with({ optional: true, exists: false }, () => {
|
.with({ optional: true, exists: false }, () => {
|
||||||
log(
|
logger.debug(
|
||||||
`Optional file ${os.type === "windows" ? "\\" : "/"}${relative(
|
`Optional file ${os.type === "windows" ? "\\" : "/"}${relative(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
src
|
src
|
||||||
@@ -189,17 +187,19 @@ async function downloadTailwind(
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (fileModifiedTime < updateWindowAgo) {
|
if (fileModifiedTime < updateWindowAgo) {
|
||||||
log(
|
logger.debug(
|
||||||
`File is older than ${TAILWIND_UPDATE_WINDOW_DAYS} days, checking for updates...`
|
`File is older than ${TAILWIND_UPDATE_WINDOW_DAYS} days, checking for updates...`
|
||||||
);
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
} else {
|
} else {
|
||||||
log(
|
logger.debug(
|
||||||
`File is recent (${fileModifiedTime.toISOString()}), checking if newer version available...`
|
`File is recent (${fileModifiedTime.toISOString()}), checking if newer version available...`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log(`Error checking file timestamp: ${error}, will download anyway`);
|
logger.debug(
|
||||||
|
`Error checking file timestamp: ${error}, will download anyway`
|
||||||
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -220,7 +220,7 @@ async function downloadTailwind(
|
|||||||
|
|
||||||
// If server timestamp is in the future, something is wrong - download anyway
|
// If server timestamp is in the future, something is wrong - download anyway
|
||||||
if (serverTime > now) {
|
if (serverTime > now) {
|
||||||
log(
|
logger.debug(
|
||||||
`Server timestamp is in the future (${serverTime.toISOString()}), downloading anyway`
|
`Server timestamp is in the future (${serverTime.toISOString()}), downloading anyway`
|
||||||
);
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
@@ -230,21 +230,25 @@ async function downloadTailwind(
|
|||||||
const fileModifiedTime = new Date(fileStats.mtime.getTime());
|
const fileModifiedTime = new Date(fileStats.mtime.getTime());
|
||||||
|
|
||||||
if (serverTime > fileModifiedTime) {
|
if (serverTime > fileModifiedTime) {
|
||||||
log(
|
logger.debug(
|
||||||
`Server has newer version (${serverTime.toISOString()} vs local ${fileModifiedTime.toISOString()})`
|
`Server has newer version (${serverTime.toISOString()} vs local ${fileModifiedTime.toISOString()})`
|
||||||
);
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
} else {
|
} else {
|
||||||
log(`Local file is up to date (${fileModifiedTime.toISOString()})`);
|
logger.debug(
|
||||||
|
`Local file is up to date (${fileModifiedTime.toISOString()})`
|
||||||
|
);
|
||||||
shouldDownload = false;
|
shouldDownload = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log(`No last-modified header available, downloading to be safe`);
|
logger.debug(
|
||||||
|
`No last-modified header available, downloading to be safe`
|
||||||
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log(
|
logger.debug(
|
||||||
`Failed to check server headers: ${response.status} ${response.statusText}`
|
`Failed to check server headers: ${response.status} ${response.statusText}`
|
||||||
);
|
);
|
||||||
shouldDownload = true;
|
shouldDownload = true;
|
||||||
@@ -258,7 +262,9 @@ async function downloadTailwind(
|
|||||||
// Otherwise, display the relative path
|
// Otherwise, display the relative path
|
||||||
.otherwise((relative) => relative);
|
.otherwise((relative) => relative);
|
||||||
|
|
||||||
log(`Tailwind CSS CLI already exists and is up to date at ${displayPath}`);
|
logger.debug(
|
||||||
|
`Tailwind CSS CLI already exists and is up to date at ${displayPath}`
|
||||||
|
);
|
||||||
return { path };
|
return { path };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,16 +276,16 @@ async function downloadTailwind(
|
|||||||
.otherwise((relative) => relative);
|
.otherwise((relative) => relative);
|
||||||
|
|
||||||
if (force) {
|
if (force) {
|
||||||
log(`Overwriting Tailwind CSS CLI at ${displayPath}`);
|
logger.debug(`Overwriting Tailwind CSS CLI at ${displayPath}`);
|
||||||
} else {
|
} else {
|
||||||
log(`Downloading updated Tailwind CSS CLI to ${displayPath}`);
|
logger.debug(`Downloading updated Tailwind CSS CLI to ${displayPath}`);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log(`Downloading Tailwind CSS CLI to ${path}`);
|
logger.debug(`Downloading Tailwind CSS CLI to ${path}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log(`Fetching ${url}...`);
|
logger.debug(`Fetching ${url}...`);
|
||||||
const response = await fetch(url, { headers });
|
const response = await fetch(url, { headers });
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -297,10 +303,10 @@ async function downloadTailwind(
|
|||||||
if (isNaN(expectedSize)) {
|
if (isNaN(expectedSize)) {
|
||||||
return { err: `Invalid Content-Length header: ${contentLength}` };
|
return { err: `Invalid Content-Length header: ${contentLength}` };
|
||||||
}
|
}
|
||||||
log(`Expected file size: ${expectedSize} bytes`);
|
logger.debug(`Expected file size: ${expectedSize} bytes`);
|
||||||
}
|
}
|
||||||
|
|
||||||
log(`Writing to ${path}...`);
|
logger.debug(`Writing to ${path}...`);
|
||||||
await fs.mkdir(dir, { recursive: true });
|
await fs.mkdir(dir, { recursive: true });
|
||||||
|
|
||||||
const file = Bun.file(path);
|
const file = Bun.file(path);
|
||||||
@@ -331,7 +337,9 @@ async function downloadTailwind(
|
|||||||
try {
|
try {
|
||||||
await fs.unlink(path);
|
await fs.unlink(path);
|
||||||
} catch (unlinkError) {
|
} catch (unlinkError) {
|
||||||
log(`Warning: Failed to clean up corrupted file: ${unlinkError}`);
|
logger.debug(
|
||||||
|
`Warning: Failed to clean up corrupted file: ${unlinkError}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@@ -339,7 +347,7 @@ async function downloadTailwind(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
log(`File size validation passed: ${actualSize} bytes`);
|
logger.debug(`File size validation passed: ${actualSize} bytes`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the file executable on Unix-like systems
|
// Make the file executable on Unix-like systems
|
||||||
@@ -354,7 +362,7 @@ async function downloadTailwind(
|
|||||||
if ((await fs.stat(path)).size > 0) break;
|
if ((await fs.stat(path)).size > 0) break;
|
||||||
} catch {
|
} catch {
|
||||||
// File might not be ready yet
|
// File might not be ready yet
|
||||||
log(`File ${path} is not ready yet, waiting...`);
|
logger.debug(`File ${path} is not ready yet, waiting...`);
|
||||||
}
|
}
|
||||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||||
} while (Date.now() < timeout);
|
} while (Date.now() < timeout);
|
||||||
@@ -398,7 +406,7 @@ async function activateEmsdk(
|
|||||||
): Promise<{ vars: Record<string, string> | null } | { err: string }> {
|
): Promise<{ vars: Record<string, string> | null } | { err: string }> {
|
||||||
// If the EMSDK environment variable is set already & the path specified exists, return nothing
|
// If the EMSDK environment variable is set already & the path specified exists, return nothing
|
||||||
if (process.env.EMSDK && (await fs.exists(resolve(process.env.EMSDK)))) {
|
if (process.env.EMSDK && (await fs.exists(resolve(process.env.EMSDK)))) {
|
||||||
log(
|
logger.debug(
|
||||||
"Emscripten SDK already activated in environment, using existing configuration"
|
"Emscripten SDK already activated in environment, using existing configuration"
|
||||||
);
|
);
|
||||||
return { vars: null };
|
return { vars: null };
|
||||||
@@ -493,7 +501,7 @@ async function activateEmsdk(
|
|||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
// Print the OS detected
|
// Print the OS detected
|
||||||
log(
|
logger.debug(
|
||||||
"OS Detected: " +
|
"OS Detected: " +
|
||||||
match(os)
|
match(os)
|
||||||
.with({ type: "windows" }, () => "Windows")
|
.with({ type: "windows" }, () => "Windows")
|
||||||
@@ -511,7 +519,7 @@ async function main() {
|
|||||||
const vars = match(await activateEmsdk(emsdkDir))
|
const vars = match(await activateEmsdk(emsdkDir))
|
||||||
.with({ vars: P.select() }, (vars) => vars)
|
.with({ vars: P.select() }, (vars) => vars)
|
||||||
.with({ err: P.any }, ({ err }) => {
|
.with({ err: P.any }, ({ err }) => {
|
||||||
log("Error activating Emscripten SDK: " + err);
|
logger.debug("Error activating Emscripten SDK: " + err);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
})
|
})
|
||||||
.exhaustive();
|
.exhaustive();
|
||||||
@@ -524,6 +532,6 @@ async function main() {
|
|||||||
* Main entry point.
|
* Main entry point.
|
||||||
*/
|
*/
|
||||||
main().catch((err) => {
|
main().catch((err) => {
|
||||||
console.error("[web.build] Error:", err);
|
console.error({ msg: "fatal error", error: err });
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user