mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 11:15:46 -06:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e87d458121 | ||
|
|
44f0b5d373 | ||
|
|
c828034d18 | ||
|
|
823f480916 | ||
|
|
53306de155 | ||
|
|
6ddc6d1181 | ||
|
|
fff44faa05 | ||
|
|
ca17984d98 | ||
|
|
c8f389b163 | ||
|
|
9c274de901 | ||
|
|
9633611ae8 | ||
|
|
897b9b8621 | ||
|
|
ee2569b70c | ||
|
|
84caa6c25f | ||
|
|
f92c9175b9 | ||
|
|
d561b446c5 | ||
|
|
9219c771d7 | ||
|
|
cd501aafc4 | ||
|
|
feae1ee191 |
@@ -3,3 +3,10 @@ fail-fast = false
|
||||
|
||||
[profile.coverage]
|
||||
status-level = "none"
|
||||
|
||||
[[profile.default.overrides]]
|
||||
filter = 'test(pacman::game::)'
|
||||
test-group = 'serial'
|
||||
|
||||
[test-groups]
|
||||
serial = { max-threads = 1 }
|
||||
|
||||
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -1 +1,2 @@
|
||||
* text=auto eol=lf
|
||||
scripts/* linguist-detectable=false
|
||||
|
||||
40
.github/workflows/coverage.yaml
vendored
40
.github/workflows/coverage.yaml
vendored
@@ -4,13 +4,11 @@ on: ["push", "pull_request"]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
RUST_TOOLCHAIN: 1.86.0
|
||||
RUST_TOOLCHAIN: nightly
|
||||
|
||||
jobs:
|
||||
coverage:
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v5
|
||||
@@ -50,33 +48,9 @@ jobs:
|
||||
run: |
|
||||
just coverage
|
||||
|
||||
- name: Download Coveralls CLI
|
||||
if: ${{ env.COVERALLS_REPO_TOKEN != '' }}
|
||||
run: |
|
||||
# use GitHub Releases URL instead of coveralls.io because they can't maintain their own files; it 404s
|
||||
curl -L https://github.com/coverallsapp/coverage-reporter/releases/download/v0.6.15/coveralls-linux-x86_64.tar.gz | tar -xz -C /usr/local/bin
|
||||
|
||||
- name: Upload coverage to Coveralls
|
||||
if: ${{ env.COVERALLS_REPO_TOKEN != '' }}
|
||||
run: |
|
||||
if [ ! -f "lcov.info" ]; then
|
||||
echo "Error: lcov.info file not found. Coverage generation may have failed."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
for i in {1..10}; do
|
||||
echo "Attempt $i: Uploading coverage to Coveralls..."
|
||||
if coveralls -n report lcov.info; then
|
||||
echo "Successfully uploaded coverage report."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ $i -lt 10 ]; then
|
||||
delay=$((2**i))
|
||||
echo "Attempt $i failed. Retrying in $delay seconds..."
|
||||
sleep $delay
|
||||
fi
|
||||
done
|
||||
|
||||
echo "Failed to upload coverage report after 10 attempts."
|
||||
exit 1
|
||||
- name: Coveralls upload
|
||||
uses: coverallsapp/github-action@v2
|
||||
with:
|
||||
github-token: ${{ secrets.COVERALLS_REPO_TOKEN }}
|
||||
path-to-lcov: lcov.info
|
||||
debug: true
|
||||
|
||||
5
.gitignore
vendored
5
.gitignore
vendored
@@ -14,8 +14,13 @@ assets/site/build.css
|
||||
|
||||
# Coverage reports
|
||||
lcov.info
|
||||
codecov.json
|
||||
coverage.html
|
||||
|
||||
# Profiling output
|
||||
flamegraph.svg
|
||||
/profile.*
|
||||
|
||||
# temporary
|
||||
assets/game/sound/*.wav
|
||||
/*.py
|
||||
|
||||
@@ -12,6 +12,13 @@ repos:
|
||||
- id: forbid-submodules
|
||||
- id: mixed-line-ending
|
||||
|
||||
- repo: https://github.com/compilerla/conventional-pre-commit
|
||||
rev: v4.2.0
|
||||
hooks:
|
||||
- id: conventional-pre-commit
|
||||
stages: [commit-msg]
|
||||
args: []
|
||||
|
||||
- repo: local
|
||||
hooks:
|
||||
- id: cargo-fmt
|
||||
@@ -20,15 +27,31 @@ repos:
|
||||
language: system
|
||||
types: [rust]
|
||||
pass_filenames: false
|
||||
|
||||
- id: cargo-check
|
||||
name: cargo check
|
||||
entry: cargo check --all-targets
|
||||
language: system
|
||||
types_or: [rust, cargo, cargo-lock]
|
||||
pass_filenames: false
|
||||
|
||||
- id: cargo-check-wasm
|
||||
name: cargo check for wasm32-unknown-emscripten
|
||||
entry: cargo check --all-targets --target=wasm32-unknown-emscripten
|
||||
language: system
|
||||
types_or: [rust, cargo, cargo-lock]
|
||||
pass_filenames: false
|
||||
|
||||
- id: bump-version
|
||||
name: bump version based on commit message
|
||||
entry: python scripts/bump-version.py
|
||||
language: system
|
||||
stages: [commit-msg]
|
||||
always_run: true
|
||||
|
||||
- id: tag-version
|
||||
name: tag version based on commit message
|
||||
entry: python scripts/tag-version.py
|
||||
language: system
|
||||
stages: [post-commit]
|
||||
always_run: true
|
||||
|
||||
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -663,7 +663,7 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
|
||||
|
||||
[[package]]
|
||||
name = "pacman"
|
||||
version = "0.2.0"
|
||||
version = "0.77.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"bevy_ecs",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "pacman"
|
||||
version = "0.2.0"
|
||||
version = "0.77.1"
|
||||
authors = ["Xevion"]
|
||||
edition = "2021"
|
||||
rust-version = "1.86.0"
|
||||
@@ -98,3 +98,6 @@ x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
|
||||
x86_64-unknown-linux-gnu = { triplet = "x64-linux" }
|
||||
x86_64-apple-darwin = { triplet = "x64-osx" }
|
||||
aarch64-apple-darwin = { triplet = "arm64-osx" }
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(coverage,coverage_nightly)'] }
|
||||
|
||||
14
Justfile
14
Justfile
@@ -1,9 +1,6 @@
|
||||
set shell := ["bash", "-c"]
|
||||
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
|
||||
|
||||
# Regex to exclude files from coverage report, double escapes for Justfile + CLI
|
||||
# You can use src\\\\..., but the filename alone is acceptable too
|
||||
coverage_exclude_pattern := "src\\\\app\\.rs|audio\\.rs|src\\\\error\\.rs|platform\\\\emscripten\\.rs|bin\\\\.+\\.rs|main\\.rs|platform\\\\desktop\\.rs|platform\\\\tracing_buffer\\.rs|platform\\\\buffered_writer\\.rs|systems\\\\debug\\.rs|systems\\\\profiling\\.rs"
|
||||
|
||||
binary_extension := if os() == "windows" { ".exe" } else { "" }
|
||||
|
||||
@@ -14,22 +11,19 @@ binary_extension := if os() == "windows" { ".exe" } else { "" }
|
||||
html: coverage
|
||||
cargo llvm-cov report \
|
||||
--remap-path-prefix \
|
||||
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||
--html \
|
||||
--open
|
||||
|
||||
# Display report (for humans)
|
||||
report-coverage: coverage
|
||||
cargo llvm-cov report \
|
||||
--remap-path-prefix \
|
||||
--ignore-filename-regex "{{ coverage_exclude_pattern }}"
|
||||
cargo llvm-cov report --remap-path-prefix
|
||||
|
||||
# Run & generate report (for CI)
|
||||
# Run & generate LCOV report (as base report)
|
||||
coverage:
|
||||
cargo llvm-cov \
|
||||
cargo +nightly llvm-cov \
|
||||
--lcov \
|
||||
--remap-path-prefix \
|
||||
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||
--workspace \
|
||||
--output-path lcov.info \
|
||||
--profile coverage \
|
||||
--no-fail-fast nextest
|
||||
|
||||
BIN
assets/game/sound/pacman_death.wav
Normal file
BIN
assets/game/sound/pacman_death.wav
Normal file
Binary file not shown.
27
bacon.toml
27
bacon.toml
@@ -28,16 +28,18 @@ need_stdout = false
|
||||
|
||||
[jobs.test]
|
||||
command = [
|
||||
"cargo", "nextest", "run",
|
||||
"--hide-progress-bar", "--failure-output", "final"
|
||||
"cargo",
|
||||
"nextest",
|
||||
"run",
|
||||
"--hide-progress-bar",
|
||||
"--failure-output",
|
||||
"final",
|
||||
]
|
||||
need_stdout = true
|
||||
analyzer = "nextest"
|
||||
|
||||
[jobs.coverage]
|
||||
command = [
|
||||
"just", "report-coverage"
|
||||
]
|
||||
command = ["just", "report-coverage"]
|
||||
need_stdout = true
|
||||
ignored_lines = [
|
||||
"info:",
|
||||
@@ -54,7 +56,7 @@ ignored_lines = [
|
||||
"\\s*Finished.+in \\d+",
|
||||
"\\s*Summary\\s+\\[",
|
||||
"\\s*Blocking",
|
||||
"Finished report saved to"
|
||||
"Finished report saved to",
|
||||
]
|
||||
on_change_strategy = "wait_then_restart"
|
||||
|
||||
@@ -66,21 +68,26 @@ need_stdout = false
|
||||
[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
|
||||
on_success = "back" # so that we don't open the browser at each change
|
||||
|
||||
[jobs.run]
|
||||
command = [
|
||||
"cargo", "run",
|
||||
]
|
||||
command = ["cargo", "run"]
|
||||
need_stdout = true
|
||||
allow_warnings = true
|
||||
background = false
|
||||
on_change_strategy = "kill_then_restart"
|
||||
# kill = ["pkill", "-TERM", "-P"]'
|
||||
|
||||
[jobs.precommit]
|
||||
command = ["pre-commit", "run", "--all-files"]
|
||||
need_stdout = true
|
||||
background = false
|
||||
on_change_strategy = "kill_then_restart"
|
||||
|
||||
[keybindings]
|
||||
c = "job:clippy"
|
||||
alt-c = "job:check"
|
||||
ctrl-alt-c = "job:check-all"
|
||||
shift-c = "job:clippy-all"
|
||||
f = "job:coverage"
|
||||
p = "job:precommit"
|
||||
|
||||
143
scripts/bump-version.py
Normal file
143
scripts/bump-version.py
Normal file
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Pre-commit hook script to automatically bump Cargo.toml version based on commit message.
|
||||
|
||||
This script parses the commit message for version bump keywords and uses cargo set-version
|
||||
to update the version in Cargo.toml accordingly.
|
||||
|
||||
Supported keywords:
|
||||
- "major" or "breaking": Bump major version (1.0.0 -> 2.0.0)
|
||||
- "minor" or "feature": Bump minor version (1.0.0 -> 1.1.0)
|
||||
- "patch" or "fix" or "bugfix": Bump patch version (1.0.0 -> 1.0.1)
|
||||
|
||||
Usage: python scripts/bump-version.py <commit_message_file>
|
||||
"""
|
||||
|
||||
import sys
|
||||
import re
|
||||
import subprocess
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_current_version():
|
||||
"""Get the current version from Cargo.toml."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["cargo", "metadata", "--format-version", "1", "--no-deps"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
|
||||
# Parse the JSON output to get version
|
||||
import json
|
||||
metadata = json.loads(result.stdout)
|
||||
return metadata["packages"][0]["version"]
|
||||
except (subprocess.CalledProcessError, json.JSONDecodeError, KeyError) as e:
|
||||
print(f"Error getting current version: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
|
||||
def bump_version(current_version, bump_type):
|
||||
"""Calculate the new version based on bump type."""
|
||||
try:
|
||||
major, minor, patch = map(int, current_version.split('.'))
|
||||
|
||||
if bump_type == "major":
|
||||
return f"{major + 1}.0.0"
|
||||
elif bump_type == "minor":
|
||||
return f"{major}.{minor + 1}.0"
|
||||
elif bump_type == "patch":
|
||||
return f"{major}.{minor}.{patch + 1}"
|
||||
else:
|
||||
return None
|
||||
except ValueError:
|
||||
print(f"Invalid version format: {current_version}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
|
||||
def set_version(new_version):
|
||||
"""Set the new version using cargo set-version."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["cargo", "set-version", new_version],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
print(f"Successfully bumped version to {new_version}")
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error setting version: {e}", file=sys.stderr)
|
||||
print(f"stdout: {e.stdout}", file=sys.stderr)
|
||||
print(f"stderr: {e.stderr}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def parse_commit_message(commit_message_file):
|
||||
"""Parse the commit message file for version bump keywords."""
|
||||
try:
|
||||
with open(commit_message_file, 'r', encoding='utf-8') as f:
|
||||
message = f.read().lower()
|
||||
except FileNotFoundError:
|
||||
print(f"Commit message file not found: {commit_message_file}", file=sys.stderr)
|
||||
return None
|
||||
except Exception as e:
|
||||
print(f"Error reading commit message: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
# Check for version bump keywords
|
||||
if re.search(r'\b(major|breaking)\b', message):
|
||||
return "major"
|
||||
elif re.search(r'\b(minor|feature)\b', message):
|
||||
return "minor"
|
||||
elif re.search(r'\b(patch|fix|bugfix)\b', message):
|
||||
return "patch"
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python scripts/bump-version.py <commit_message_file>", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
commit_message_file = sys.argv[1]
|
||||
|
||||
# Parse commit message for version bump type
|
||||
bump_type = parse_commit_message(commit_message_file)
|
||||
|
||||
if not bump_type:
|
||||
print("No version bump keywords found in commit message")
|
||||
sys.exit(0)
|
||||
|
||||
print(f"Found version bump type: {bump_type}")
|
||||
|
||||
# Get current version
|
||||
current_version = get_current_version()
|
||||
if not current_version:
|
||||
print("Failed to get current version", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Current version: {current_version}")
|
||||
|
||||
# Calculate new version
|
||||
new_version = bump_version(current_version, bump_type)
|
||||
if not new_version:
|
||||
print("Failed to calculate new version", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print(f"New version: {new_version}")
|
||||
|
||||
# Set the new version
|
||||
if set_version(new_version):
|
||||
print("Version bump completed successfully")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("Version bump failed", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
125
scripts/tag-version.py
Normal file
125
scripts/tag-version.py
Normal file
@@ -0,0 +1,125 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Post-commit hook script to automatically create git tags based on the version in Cargo.toml.
|
||||
|
||||
This script reads the current version from Cargo.toml and creates a git tag with that version.
|
||||
It's designed to run after the version has been bumped by the bump-version.py script.
|
||||
|
||||
Usage: python scripts/tag-version.py
|
||||
"""
|
||||
|
||||
import sys
|
||||
import subprocess
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def get_version_from_cargo_toml():
|
||||
"""Get the current version from Cargo.toml."""
|
||||
cargo_toml_path = Path("Cargo.toml")
|
||||
|
||||
if not cargo_toml_path.exists():
|
||||
print("Cargo.toml not found", file=sys.stderr)
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(cargo_toml_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Look for version = "x.y.z" pattern
|
||||
version_match = re.search(r'version\s*=\s*["\']([^"\']+)["\']', content)
|
||||
|
||||
if version_match:
|
||||
return version_match.group(1)
|
||||
else:
|
||||
print("Could not find version in Cargo.toml", file=sys.stderr)
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error reading Cargo.toml: {e}", file=sys.stderr)
|
||||
return None
|
||||
|
||||
|
||||
def get_existing_tags():
|
||||
"""Get list of existing git tags."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", "tag", "--list"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
return result.stdout.strip().split('\n') if result.stdout.strip() else []
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error getting git tags: {e}", file=sys.stderr)
|
||||
return []
|
||||
|
||||
|
||||
def create_git_tag(version):
|
||||
"""Create a git tag with the specified version."""
|
||||
tag_name = f"v{version}"
|
||||
|
||||
try:
|
||||
# Check if tag already exists
|
||||
existing_tags = get_existing_tags()
|
||||
if tag_name in existing_tags:
|
||||
print(f"Tag {tag_name} already exists, skipping")
|
||||
return True
|
||||
|
||||
# Create the tag
|
||||
result = subprocess.run(
|
||||
["git", "tag", tag_name],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True
|
||||
)
|
||||
|
||||
print(f"Successfully created tag: {tag_name}")
|
||||
return True
|
||||
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Error creating git tag: {e}", file=sys.stderr)
|
||||
print(f"stdout: {e.stdout}", file=sys.stderr)
|
||||
print(f"stderr: {e.stderr}", file=sys.stderr)
|
||||
return False
|
||||
|
||||
|
||||
def is_git_repository():
|
||||
"""Check if we're in a git repository."""
|
||||
try:
|
||||
subprocess.run(
|
||||
["git", "rev-parse", "--git-dir"],
|
||||
capture_output=True,
|
||||
check=True
|
||||
)
|
||||
return True
|
||||
except subprocess.CalledProcessError:
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
# Check if we're in a git repository
|
||||
if not is_git_repository():
|
||||
print("Not in a git repository, skipping tag creation")
|
||||
sys.exit(0)
|
||||
|
||||
# Get the current version from Cargo.toml
|
||||
version = get_version_from_cargo_toml()
|
||||
|
||||
if not version:
|
||||
print("Could not determine version, skipping tag creation")
|
||||
sys.exit(0)
|
||||
|
||||
print(f"Current version: {version}")
|
||||
|
||||
# Create the git tag
|
||||
if create_git_tag(version):
|
||||
print("Tag creation completed successfully")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print("Tag creation failed", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -19,6 +19,8 @@ pub enum Asset {
|
||||
AtlasImage,
|
||||
/// Terminal Vector font for text rendering (TerminalVector.ttf)
|
||||
Font,
|
||||
/// Sound effect for Pac-Man's death
|
||||
DeathSound,
|
||||
}
|
||||
|
||||
impl Asset {
|
||||
@@ -37,6 +39,7 @@ impl Asset {
|
||||
Wav4 => "sound/waka/4.ogg",
|
||||
AtlasImage => "atlas.png",
|
||||
Font => "TerminalVector.ttf",
|
||||
DeathSound => "sound/pacman_death.wav",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
45
src/audio.rs
45
src/audio.rs
@@ -16,6 +16,7 @@ const SOUND_ASSETS: [Asset; 4] = [Asset::Wav1, Asset::Wav2, Asset::Wav3, Asset::
|
||||
pub struct Audio {
|
||||
_mixer_context: Option<mixer::Sdl2MixerContext>,
|
||||
sounds: Vec<Chunk>,
|
||||
death_sound: Option<Chunk>,
|
||||
next_sound_index: usize,
|
||||
muted: bool,
|
||||
disabled: bool,
|
||||
@@ -44,6 +45,7 @@ impl Audio {
|
||||
return Self {
|
||||
_mixer_context: None,
|
||||
sounds: Vec::new(),
|
||||
death_sound: None,
|
||||
next_sound_index: 0,
|
||||
muted: false,
|
||||
disabled: true,
|
||||
@@ -65,6 +67,7 @@ impl Audio {
|
||||
return Self {
|
||||
_mixer_context: None,
|
||||
sounds: Vec::new(),
|
||||
death_sound: None,
|
||||
next_sound_index: 0,
|
||||
muted: false,
|
||||
disabled: true,
|
||||
@@ -93,12 +96,33 @@ impl Audio {
|
||||
}
|
||||
}
|
||||
|
||||
let death_sound = match get_asset_bytes(Asset::DeathSound) {
|
||||
Ok(data) => match RWops::from_bytes(&data) {
|
||||
Ok(rwops) => match rwops.load_wav() {
|
||||
Ok(chunk) => Some(chunk),
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to load death sound from asset API: {}", e);
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to create RWops for death sound: {}", e);
|
||||
None
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to load death sound asset: {}", e);
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
// If no sounds loaded successfully, disable audio
|
||||
if sounds.is_empty() {
|
||||
if sounds.is_empty() && death_sound.is_none() {
|
||||
tracing::warn!("No sounds loaded successfully. Audio will be disabled.");
|
||||
return Self {
|
||||
_mixer_context: Some(mixer_context),
|
||||
sounds: Vec::new(),
|
||||
death_sound: None,
|
||||
next_sound_index: 0,
|
||||
muted: false,
|
||||
disabled: true,
|
||||
@@ -108,6 +132,7 @@ impl Audio {
|
||||
Audio {
|
||||
_mixer_context: Some(mixer_context),
|
||||
sounds,
|
||||
death_sound,
|
||||
next_sound_index: 0,
|
||||
muted: false,
|
||||
disabled: false,
|
||||
@@ -138,6 +163,24 @@ impl Audio {
|
||||
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
|
||||
}
|
||||
|
||||
/// Plays the death sound effect.
|
||||
pub fn death(&mut self) {
|
||||
if self.disabled || self.muted {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(chunk) = &self.death_sound {
|
||||
mixer::Channel::all().play(chunk, 0).ok();
|
||||
}
|
||||
}
|
||||
|
||||
/// Halts all currently playing audio channels.
|
||||
pub fn stop_all(&mut self) {
|
||||
if !self.disabled {
|
||||
mixer::Channel::all().halt();
|
||||
}
|
||||
}
|
||||
|
||||
/// Instantly mutes or unmutes all audio channels by adjusting their volume.
|
||||
///
|
||||
/// Sets all 4 mixer channels to zero volume when muting, or restores them to
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
|
||||
#![cfg_attr(coverage_nightly, coverage(off))]
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use sdl2::event::Event;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
|
||||
#![cfg_attr(coverage_nightly, coverage(off))]
|
||||
|
||||
use circular_buffer::CircularBuffer;
|
||||
use pacman::constants::CANVAS_SIZE;
|
||||
use sdl2::event::Event;
|
||||
|
||||
@@ -132,8 +132,6 @@ pub const RAW_BOARD: [&str; BOARD_CELL_SIZE.y as usize] = [
|
||||
pub mod startup {
|
||||
/// Number of frames for the startup sequence (3 seconds at 60 FPS)
|
||||
pub const STARTUP_FRAMES: u32 = 60 * 3;
|
||||
/// Number of ticks per frame during startup
|
||||
pub const STARTUP_TICKS_PER_FRAME: u32 = 60;
|
||||
}
|
||||
|
||||
/// Game mechanics constants
|
||||
|
||||
195
src/game.rs
195
src/game.rs
@@ -9,32 +9,26 @@ use crate::error::{GameError, GameResult};
|
||||
use crate::events::GameEvent;
|
||||
use crate::map::builder::Map;
|
||||
use crate::map::direction::Direction;
|
||||
use crate::systems::blinking::Blinking;
|
||||
use crate::systems::components::{GhostAnimation, GhostState, LastAnimationState};
|
||||
use crate::systems::movement::{BufferedDirection, Position, Velocity};
|
||||
use crate::systems::profiling::{SystemId, Timing};
|
||||
use crate::systems::render::touch_ui_render_system;
|
||||
use crate::systems::render::RenderDirty;
|
||||
use crate::systems::{
|
||||
self, combined_render_system, ghost_collision_system, present_system, Hidden, LinearAnimation, MovementModifiers, NodeId,
|
||||
TouchState,
|
||||
};
|
||||
use crate::systems::{
|
||||
audio_system, blinking_system, collision_system, directional_render_system, dirty_render_system, eaten_ghost_system,
|
||||
ghost_movement_system, ghost_state_system, hud_render_system, item_system, linear_render_system, profile, AudioEvent,
|
||||
AudioResource, AudioState, BackbufferResource, Collider, DebugState, DebugTextureResource, DeltaTime, DirectionalAnimation,
|
||||
EntityType, Frozen, Ghost, GhostAnimations, GhostBundle, GhostCollider, GlobalState, ItemBundle, ItemCollider,
|
||||
MapTextureResource, PacmanCollider, PlayerBundle, PlayerControlled, Renderable, ScoreResource, StartupSequence,
|
||||
SystemTimings,
|
||||
self, audio_system, blinking_system, collision_system, combined_render_system, directional_render_system,
|
||||
dirty_render_system, eaten_ghost_system, ghost_collision_system, ghost_movement_system, ghost_state_system,
|
||||
hud_render_system, item_system, linear_render_system, present_system, profile, touch_ui_render_system, AudioEvent,
|
||||
AudioResource, AudioState, BackbufferResource, Blinking, BufferedDirection, Collider, DebugState, DebugTextureResource,
|
||||
DeltaTime, DirectionalAnimation, EntityType, Frozen, GameStage, Ghost, GhostAnimation, GhostAnimations, GhostBundle,
|
||||
GhostCollider, GhostState, GlobalState, Hidden, ItemBundle, ItemCollider, LastAnimationState, LinearAnimation,
|
||||
MapTextureResource, MovementModifiers, NodeId, PacmanCollider, PlayerAnimation, PlayerBundle, PlayerControlled,
|
||||
PlayerDeathAnimation, PlayerLives, Position, RenderDirty, Renderable, ScoreResource, StartupSequence, SystemId,
|
||||
SystemTimings, Timing, TouchState, Velocity,
|
||||
};
|
||||
|
||||
use crate::texture::animated::{DirectionalTiles, TileSequence};
|
||||
use crate::texture::sprite::AtlasTile;
|
||||
use crate::texture::sprites::{FrightenedColor, GameSprite, GhostSprite, MazeSprite, PacmanSprite};
|
||||
use bevy_ecs::change_detection::DetectChanges;
|
||||
use bevy_ecs::event::EventRegistry;
|
||||
use bevy_ecs::observer::Trigger;
|
||||
use bevy_ecs::schedule::common_conditions::resource_changed;
|
||||
use bevy_ecs::schedule::{Condition, IntoScheduleConfigs, Schedule, SystemSet};
|
||||
use bevy_ecs::system::{Local, ResMut};
|
||||
use bevy_ecs::schedule::{IntoScheduleConfigs, Schedule, SystemSet};
|
||||
use bevy_ecs::system::{Local, Res, ResMut};
|
||||
use bevy_ecs::world::World;
|
||||
use sdl2::event::EventType;
|
||||
use sdl2::image::LoadTexture;
|
||||
@@ -54,7 +48,9 @@ use crate::{
|
||||
|
||||
/// System set for all rendering systems to ensure they run after gameplay logic
|
||||
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
|
||||
pub struct RenderSet;
|
||||
enum RenderSet {
|
||||
Animation,
|
||||
}
|
||||
|
||||
/// Core game state manager built on the Bevy ECS architecture.
|
||||
///
|
||||
@@ -112,6 +108,8 @@ impl Game {
|
||||
let (player_animation, player_start_sprite) = Self::create_player_animations(&atlas)?;
|
||||
let player_bundle = Self::create_player_bundle(&map, player_animation, player_start_sprite);
|
||||
|
||||
let death_animation = Self::create_death_animation(&atlas)?;
|
||||
|
||||
let mut world = World::default();
|
||||
let mut schedule = Schedule::default();
|
||||
|
||||
@@ -127,6 +125,7 @@ impl Game {
|
||||
map_texture,
|
||||
debug_texture,
|
||||
ttf_atlas,
|
||||
death_animation,
|
||||
)?;
|
||||
Self::configure_schedule(&mut schedule);
|
||||
|
||||
@@ -310,6 +309,18 @@ impl Game {
|
||||
Ok((player_animation, player_start_sprite))
|
||||
}
|
||||
|
||||
fn create_death_animation(atlas: &SpriteAtlas) -> GameResult<LinearAnimation> {
|
||||
let mut death_tiles = Vec::new();
|
||||
for i in 0..=10 {
|
||||
// Assuming death animation has 11 frames named pacman/die_0, pacman/die_1, etc.
|
||||
let tile = atlas.get_tile(&GameSprite::Pacman(PacmanSprite::Dying(i)).to_path())?;
|
||||
death_tiles.push(tile);
|
||||
}
|
||||
|
||||
let tile_sequence = TileSequence::new(&death_tiles);
|
||||
Ok(LinearAnimation::new(tile_sequence, 8)) // 8 ticks per frame, non-looping
|
||||
}
|
||||
|
||||
fn create_player_bundle(map: &Map, player_animation: DirectionalAnimation, player_start_sprite: AtlasTile) -> PlayerBundle {
|
||||
PlayerBundle {
|
||||
player: PlayerControlled,
|
||||
@@ -361,13 +372,19 @@ impl Game {
|
||||
map_texture: sdl2::render::Texture,
|
||||
debug_texture: sdl2::render::Texture,
|
||||
ttf_atlas: crate::texture::ttf::TtfAtlas,
|
||||
death_animation: LinearAnimation,
|
||||
) -> GameResult<()> {
|
||||
world.insert_non_send_resource(atlas);
|
||||
world.insert_resource(Self::create_ghost_animations(world.non_send_resource::<SpriteAtlas>())?);
|
||||
let player_animation = Self::create_player_animations(world.non_send_resource::<SpriteAtlas>())?.0;
|
||||
world.insert_resource(PlayerAnimation(player_animation));
|
||||
world.insert_resource(PlayerDeathAnimation(death_animation));
|
||||
|
||||
world.insert_resource(BatchedLinesResource::new(&map, constants::LARGE_SCALE));
|
||||
world.insert_resource(map);
|
||||
world.insert_resource(GlobalState { exit: false });
|
||||
world.insert_resource(GameStage::default());
|
||||
world.insert_resource(PlayerLives::default());
|
||||
world.insert_resource(ScoreResource(0));
|
||||
world.insert_resource(SystemTimings::default());
|
||||
world.insert_resource(Timing::default());
|
||||
@@ -378,10 +395,9 @@ impl Game {
|
||||
world.insert_resource(AudioState::default());
|
||||
world.insert_resource(CursorPosition::default());
|
||||
world.insert_resource(TouchState::default());
|
||||
world.insert_resource(StartupSequence::new(
|
||||
constants::startup::STARTUP_FRAMES,
|
||||
constants::startup::STARTUP_TICKS_PER_FRAME,
|
||||
));
|
||||
world.insert_resource(GameStage::Starting(StartupSequence::TextOnly {
|
||||
remaining_ticks: constants::startup::STARTUP_FRAMES,
|
||||
}));
|
||||
|
||||
world.insert_non_send_resource(event_pump);
|
||||
world.insert_non_send_resource::<&mut Canvas<Window>>(Box::leak(Box::new(canvas)));
|
||||
@@ -394,15 +410,14 @@ impl Game {
|
||||
}
|
||||
|
||||
fn configure_schedule(schedule: &mut Schedule) {
|
||||
let stage_system = profile(SystemId::Stage, systems::stage_system);
|
||||
let input_system = profile(SystemId::Input, systems::input::input_system);
|
||||
let player_control_system = profile(SystemId::PlayerControls, systems::player_control_system);
|
||||
let player_movement_system = profile(SystemId::PlayerMovement, systems::player_movement_system);
|
||||
let startup_stage_system = profile(SystemId::Stage, systems::startup_stage_system);
|
||||
let player_tunnel_slowdown_system = profile(SystemId::PlayerMovement, systems::player::player_tunnel_slowdown_system);
|
||||
let ghost_movement_system = profile(SystemId::Ghost, ghost_movement_system);
|
||||
let collision_system = profile(SystemId::Collision, collision_system);
|
||||
let ghost_collision_system = profile(SystemId::GhostCollision, ghost_collision_system);
|
||||
|
||||
let item_system = profile(SystemId::Item, item_system);
|
||||
let audio_system = profile(SystemId::Audio, audio_system);
|
||||
let blinking_system = profile(SystemId::Blinking, blinking_system);
|
||||
@@ -412,41 +427,55 @@ impl Game {
|
||||
let hud_render_system = profile(SystemId::HudRender, hud_render_system);
|
||||
let present_system = profile(SystemId::Present, present_system);
|
||||
let unified_ghost_state_system = profile(SystemId::GhostStateAnimation, ghost_state_system);
|
||||
// let death_sequence_system = profile(SystemId::DeathSequence, death_sequence_system);
|
||||
// let game_over_system = profile(SystemId::GameOver, systems::game_over_system);
|
||||
let eaten_ghost_system = profile(SystemId::EatenGhost, eaten_ghost_system);
|
||||
|
||||
let forced_dirty_system = |mut dirty: ResMut<RenderDirty>| {
|
||||
dirty.0 = true;
|
||||
};
|
||||
|
||||
schedule.add_systems((
|
||||
forced_dirty_system.run_if(resource_changed::<ScoreResource>.or(resource_changed::<StartupSequence>)),
|
||||
(
|
||||
input_system.run_if(|mut local: Local<u8>| {
|
||||
*local = local.wrapping_add(1u8);
|
||||
// run every nth frame
|
||||
*local % 2 == 0
|
||||
}),
|
||||
player_control_system,
|
||||
player_movement_system,
|
||||
startup_stage_system,
|
||||
)
|
||||
.chain(),
|
||||
player_tunnel_slowdown_system,
|
||||
ghost_movement_system,
|
||||
profile(SystemId::EatenGhost, eaten_ghost_system),
|
||||
unified_ghost_state_system,
|
||||
schedule.add_systems(
|
||||
forced_dirty_system
|
||||
.run_if(|score: Res<ScoreResource>, stage: Res<GameStage>| score.is_changed() || stage.is_changed()),
|
||||
);
|
||||
|
||||
// Input system should always run to prevent SDL event pump from blocking
|
||||
let input_systems = (
|
||||
input_system.run_if(|mut local: Local<u8>| {
|
||||
*local = local.wrapping_add(1u8);
|
||||
// run every nth frame
|
||||
*local % 2 == 0
|
||||
}),
|
||||
player_control_system,
|
||||
)
|
||||
.chain();
|
||||
|
||||
let gameplay_systems = (
|
||||
(player_movement_system, player_tunnel_slowdown_system, ghost_movement_system).chain(),
|
||||
eaten_ghost_system,
|
||||
(collision_system, ghost_collision_system, item_system).chain(),
|
||||
audio_system,
|
||||
blinking_system,
|
||||
unified_ghost_state_system,
|
||||
)
|
||||
.chain()
|
||||
.run_if(|game_state: Res<GameStage>| matches!(*game_state, GameStage::Playing));
|
||||
|
||||
schedule.add_systems((blinking_system, directional_render_system, linear_render_system).in_set(RenderSet::Animation));
|
||||
|
||||
schedule.add_systems((
|
||||
stage_system,
|
||||
input_systems,
|
||||
gameplay_systems,
|
||||
(
|
||||
directional_render_system,
|
||||
linear_render_system,
|
||||
dirty_render_system,
|
||||
combined_render_system,
|
||||
hud_render_system,
|
||||
touch_ui_render_system,
|
||||
present_system,
|
||||
)
|
||||
.chain(),
|
||||
.chain()
|
||||
.after(RenderSet::Animation),
|
||||
audio_system,
|
||||
));
|
||||
}
|
||||
|
||||
@@ -512,7 +541,7 @@ impl Game {
|
||||
for (ghost_type, start_node) in ghost_start_positions {
|
||||
// Create the ghost bundle in a separate scope to manage borrows
|
||||
let ghost = {
|
||||
let animations = *world.resource::<GhostAnimations>().get_normal(&ghost_type).unwrap();
|
||||
let animations = world.resource::<GhostAnimations>().get_normal(&ghost_type).unwrap().clone();
|
||||
let atlas = world.non_send_resource::<SpriteAtlas>();
|
||||
let sprite_path = GameSprite::Ghost(GhostSprite::Normal(ghost_type, Direction::Left, 0)).to_path();
|
||||
|
||||
@@ -557,7 +586,7 @@ impl Game {
|
||||
TileSequence::new(&[left_eye]),
|
||||
TileSequence::new(&[right_eye]),
|
||||
);
|
||||
let eyes = DirectionalAnimation::new(eyes_tiles, eyes_tiles, animation::GHOST_EATEN_SPEED);
|
||||
let eyes = DirectionalAnimation::new(eyes_tiles.clone(), eyes_tiles, animation::GHOST_EATEN_SPEED);
|
||||
|
||||
let mut animations = HashMap::new();
|
||||
|
||||
@@ -586,7 +615,7 @@ impl Game {
|
||||
TileSequence::new(&left_tiles),
|
||||
TileSequence::new(&right_tiles),
|
||||
);
|
||||
let normal = DirectionalAnimation::new(normal_moving, normal_moving, animation::GHOST_NORMAL_SPEED);
|
||||
let normal = DirectionalAnimation::new(normal_moving.clone(), normal_moving, animation::GHOST_NORMAL_SPEED);
|
||||
|
||||
animations.insert(ghost_type, normal);
|
||||
}
|
||||
@@ -658,68 +687,4 @@ impl Game {
|
||||
|
||||
state.exit
|
||||
}
|
||||
|
||||
// /// 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: sdl2::render::RenderTarget>(&self, canvas: &mut Canvas<T>) -> GameResult<()> {
|
||||
// let pacman_node = self.state.pacman.current_node_id();
|
||||
|
||||
// for ghost in self.state.ghosts.iter() {
|
||||
// if let Ok(path) = ghost.calculate_path_to_target(&self.state.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 => glam::Vec2::new(0.25, 0.5),
|
||||
// GhostType::Pinky => glam::Vec2::new(-0.25, -0.25),
|
||||
// GhostType::Inky => glam::Vec2::new(0.5, -0.5),
|
||||
// GhostType::Clyde => glam::Vec2::new(-0.5, 0.25),
|
||||
// } * 5.0;
|
||||
|
||||
// // Calculate offset positions for all nodes using the same perpendicular direction
|
||||
// let mut offset_positions = Vec::new();
|
||||
// for &node_id in &path {
|
||||
// let node = self
|
||||
// .state
|
||||
// .map
|
||||
// .graph
|
||||
// .get_node(node_id)
|
||||
// .ok_or(crate::error::EntityError::NodeNotFound(node_id))?;
|
||||
// let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||
// offset_positions.push(pos + offset);
|
||||
// }
|
||||
|
||||
// // 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) > (crate::constants::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| crate::error::GameError::Sdl(e.to_string()))?;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
||||
14
src/lib.rs
14
src/lib.rs
@@ -1,14 +1,22 @@
|
||||
//! Pac-Man game library crate.
|
||||
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
|
||||
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod app;
|
||||
pub mod asset;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod audio;
|
||||
pub mod constants;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod error;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod events;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod formatter;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod platform;
|
||||
|
||||
pub mod asset;
|
||||
pub mod constants;
|
||||
pub mod game;
|
||||
pub mod map;
|
||||
pub mod platform;
|
||||
pub mod systems;
|
||||
pub mod texture;
|
||||
|
||||
16
src/main.rs
16
src/main.rs
@@ -1,20 +1,27 @@
|
||||
// Note: This disables the console window on Windows. We manually re-attach to the parent terminal or process later on.
|
||||
#![windows_subsystem = "windows"]
|
||||
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
|
||||
|
||||
use crate::{app::App, constants::LOOP_TIME};
|
||||
use tracing::info;
|
||||
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod app;
|
||||
mod asset;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod audio;
|
||||
mod constants;
|
||||
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod error;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod events;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod formatter;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
mod platform;
|
||||
|
||||
mod asset;
|
||||
mod constants;
|
||||
mod game;
|
||||
mod map;
|
||||
mod platform;
|
||||
mod systems;
|
||||
mod texture;
|
||||
|
||||
@@ -22,6 +29,7 @@ mod texture;
|
||||
///
|
||||
/// This function initializes SDL, the window, the game state, and then enters
|
||||
/// the main game loop.
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub fn main() {
|
||||
// On Windows, this connects output streams to the console dynamically
|
||||
// On Emscripten, this connects the subscriber to the browser console
|
||||
|
||||
@@ -359,12 +359,7 @@ impl Map {
|
||||
+ IVec2::from(Direction::Left.as_ivec2()).as_vec2() * (CELL_SIZE as f32 * 2.0),
|
||||
},
|
||||
)
|
||||
.map_err(|e| {
|
||||
MapError::InvalidConfig(format!(
|
||||
"Failed to connect left tunnel entrance to left tunnel hidden node: {}",
|
||||
e
|
||||
))
|
||||
})?
|
||||
.expect("Failed to connect left tunnel entrance to left tunnel hidden node")
|
||||
};
|
||||
|
||||
// Create the right tunnel nodes
|
||||
@@ -384,12 +379,7 @@ impl Map {
|
||||
+ IVec2::from(Direction::Right.as_ivec2()).as_vec2() * (CELL_SIZE as f32 * 2.0),
|
||||
},
|
||||
)
|
||||
.map_err(|e| {
|
||||
MapError::InvalidConfig(format!(
|
||||
"Failed to connect right tunnel entrance to right tunnel hidden node: {}",
|
||||
e
|
||||
))
|
||||
})?
|
||||
.expect("Failed to connect right tunnel entrance to right tunnel hidden node")
|
||||
};
|
||||
|
||||
// Connect the left tunnel hidden node to the right tunnel hidden node
|
||||
@@ -401,12 +391,7 @@ impl Map {
|
||||
Some(0.0),
|
||||
Direction::Left,
|
||||
)
|
||||
.map_err(|e| {
|
||||
MapError::InvalidConfig(format!(
|
||||
"Failed to connect left tunnel hidden node to right tunnel hidden node: {}",
|
||||
e
|
||||
))
|
||||
})?;
|
||||
.expect("Failed to connect left tunnel hidden node to right tunnel hidden node");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
|
||||
Asset::Wav4 => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/waka/4.ogg"))),
|
||||
Asset::AtlasImage => Ok(Cow::Borrowed(include_bytes!("../../assets/game/atlas.png"))),
|
||||
Asset::Font => Ok(Cow::Borrowed(include_bytes!("../../assets/game/TerminalVector.ttf"))),
|
||||
Asset::DeathSound => Ok(Cow::Borrowed(include_bytes!("../../assets/game/sound/pacman_death.wav"))),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ pub struct AudioState {
|
||||
pub enum AudioEvent {
|
||||
/// Play the "eat" sound when Pac-Man consumes a pellet
|
||||
PlayEat,
|
||||
/// Play the death sound
|
||||
PlayDeath,
|
||||
/// Stop all currently playing sounds
|
||||
StopAll,
|
||||
}
|
||||
|
||||
/// Non-send resource wrapper for SDL2 audio system
|
||||
@@ -59,6 +63,16 @@ pub fn audio_system(
|
||||
// 4 eat sounds available
|
||||
}
|
||||
}
|
||||
AudioEvent::PlayDeath => {
|
||||
if !audio.0.is_disabled() && !audio_state.muted {
|
||||
audio.0.death();
|
||||
}
|
||||
}
|
||||
AudioEvent::StopAll => {
|
||||
if !audio.0.is_disabled() {
|
||||
audio.0.stop_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
use bevy_ecs::component::Component;
|
||||
use bevy_ecs::entity::Entity;
|
||||
use bevy_ecs::event::{EventReader, EventWriter};
|
||||
use bevy_ecs::query::With;
|
||||
use bevy_ecs::system::{Query, Res, ResMut};
|
||||
use bevy_ecs::{
|
||||
component::Component,
|
||||
entity::Entity,
|
||||
event::{EventReader, EventWriter},
|
||||
query::With,
|
||||
system::{Commands, Query, Res, ResMut},
|
||||
};
|
||||
|
||||
use crate::error::GameError;
|
||||
use crate::events::GameEvent;
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::movement::Position;
|
||||
use crate::systems::{AudioEvent, Ghost, GhostState, PlayerControlled, ScoreResource};
|
||||
use crate::systems::{
|
||||
components::GhostState, movement::Position, AudioEvent, DyingSequence, Frozen, GameStage, Ghost, PlayerControlled,
|
||||
ScoreResource,
|
||||
};
|
||||
|
||||
/// A component for defining the collision area of an entity.
|
||||
#[derive(Component)]
|
||||
pub struct Collider {
|
||||
pub size: f32,
|
||||
@@ -62,6 +67,7 @@ pub fn check_collision(
|
||||
///
|
||||
/// Also detects collisions between Pac-Man and ghosts for gameplay mechanics like
|
||||
/// power pellet effects, ghost eating, and player death.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn collision_system(
|
||||
map: Res<Map>,
|
||||
pacman_query: Query<(Entity, &Position, &Collider), With<PacmanCollider>>,
|
||||
@@ -107,10 +113,13 @@ pub fn collision_system(
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn ghost_collision_system(
|
||||
mut commands: Commands,
|
||||
mut collision_events: EventReader<GameEvent>,
|
||||
mut score: ResMut<ScoreResource>,
|
||||
pacman_query: Query<(), With<PlayerControlled>>,
|
||||
mut game_state: ResMut<GameStage>,
|
||||
pacman_query: Query<Entity, With<PlayerControlled>>,
|
||||
ghost_query: Query<(Entity, &Ghost), With<GhostCollider>>,
|
||||
mut ghost_state_query: Query<&mut GhostState>,
|
||||
mut events: EventWriter<AudioEvent>,
|
||||
@@ -118,7 +127,7 @@ pub fn ghost_collision_system(
|
||||
for event in collision_events.read() {
|
||||
if let GameEvent::Collision(entity1, entity2) = event {
|
||||
// Check if one is Pacman and the other is a ghost
|
||||
let (_pacman_entity, ghost_entity) = if pacman_query.get(*entity1).is_ok() && ghost_query.get(*entity2).is_ok() {
|
||||
let (pacman_entity, ghost_entity) = if pacman_query.get(*entity1).is_ok() && ghost_query.get(*entity2).is_ok() {
|
||||
(*entity1, *entity2)
|
||||
} else if pacman_query.get(*entity2).is_ok() && ghost_query.get(*entity1).is_ok() {
|
||||
(*entity2, *entity1)
|
||||
@@ -140,8 +149,12 @@ pub fn ghost_collision_system(
|
||||
|
||||
// Play eat sound
|
||||
events.write(AudioEvent::PlayEat);
|
||||
} else {
|
||||
// Pac-Man dies (this would need a death system)
|
||||
} else if matches!(*ghost_state, GhostState::Normal) {
|
||||
// Pac-Man dies
|
||||
*game_state = GameStage::PlayerDying(DyingSequence::Frozen { remaining_ticks: 60 });
|
||||
commands.entity(pacman_entity).insert(Frozen);
|
||||
commands.entity(ghost_entity).insert(Frozen);
|
||||
events.write(AudioEvent::StopAll);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,7 @@ pub struct Renderable {
|
||||
}
|
||||
|
||||
/// Directional animation component with shared timing across all directions
|
||||
#[derive(Component, Clone, Copy)]
|
||||
#[derive(Component, Clone)]
|
||||
pub struct DirectionalAnimation {
|
||||
pub moving_tiles: DirectionalTiles,
|
||||
pub stopped_tiles: DirectionalTiles,
|
||||
@@ -123,13 +123,18 @@ impl DirectionalAnimation {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tag component to mark animations that should loop when they reach the end
|
||||
#[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Looping;
|
||||
|
||||
/// Linear animation component for non-directional animations (frightened ghosts)
|
||||
#[derive(Component, Clone, Copy)]
|
||||
#[derive(Component, Resource, Clone)]
|
||||
pub struct LinearAnimation {
|
||||
pub tiles: TileSequence,
|
||||
pub current_frame: usize,
|
||||
pub time_bank: u16,
|
||||
pub frame_duration: u16,
|
||||
pub finished: bool,
|
||||
}
|
||||
|
||||
impl LinearAnimation {
|
||||
@@ -140,6 +145,7 @@ impl LinearAnimation {
|
||||
current_frame: 0,
|
||||
time_bank: 0,
|
||||
frame_duration,
|
||||
finished: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,6 +224,11 @@ pub struct Frozen;
|
||||
#[derive(Component, Debug, Clone, Copy)]
|
||||
pub struct Eaten;
|
||||
|
||||
/// Tag component for Pac-Man during his death animation.
|
||||
/// This is mainly because the Frozen tag would stop both movement and animation, while the Dying tag can signal that the animation should continue despite being frozen.
|
||||
#[derive(Component, Debug, Clone, Copy)]
|
||||
pub struct Dying;
|
||||
|
||||
#[derive(Component, Debug, Clone, Copy)]
|
||||
pub enum GhostState {
|
||||
/// Normal ghost behavior - chasing Pac-Man
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
//! Debug rendering system
|
||||
use std::cmp::Ordering;
|
||||
|
||||
#[cfg_attr(coverage_nightly, feature(coverage_attribute))]
|
||||
use crate::constants::{self, BOARD_PIXEL_OFFSET};
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::{Collider, CursorPosition, NodeId, Position, SystemTimings};
|
||||
@@ -13,6 +12,7 @@ use sdl2::rect::{Point, Rect};
|
||||
use sdl2::render::{Canvas, Texture};
|
||||
use sdl2::video::Window;
|
||||
use smallvec::SmallVec;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use tracing::warn;
|
||||
|
||||
@@ -149,6 +149,7 @@ fn transform_position_with_offset(pos: Vec2, scale: f32) -> IVec2 {
|
||||
}
|
||||
|
||||
/// Renders timing information in the top-left corner of the screen using the debug text atlas
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
fn render_timing_display(
|
||||
canvas: &mut Canvas<Window>,
|
||||
timings: &SystemTimings,
|
||||
@@ -203,6 +204,7 @@ fn render_timing_display(
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub fn debug_render_system(
|
||||
canvas: &mut Canvas<Window>,
|
||||
ttf_atlas: &mut TtfAtlasResource,
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use crate::platform;
|
||||
use crate::systems::components::{DirectionalAnimation, Frozen, GhostAnimation, GhostState, LastAnimationState, LinearAnimation};
|
||||
use crate::systems::components::{
|
||||
DirectionalAnimation, Frozen, GhostAnimation, GhostState, LastAnimationState, LinearAnimation, Looping,
|
||||
};
|
||||
use crate::{
|
||||
map::{
|
||||
builder::Map,
|
||||
@@ -194,22 +196,26 @@ pub fn ghost_state_system(
|
||||
if last_animation_state.0 != current_animation_state {
|
||||
match current_animation_state {
|
||||
GhostAnimation::Frightened { flash } => {
|
||||
// Remove DirectionalAnimation, add LinearAnimation
|
||||
// Remove DirectionalAnimation, add LinearAnimation with Looping component
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<DirectionalAnimation>()
|
||||
.insert(*animations.frightened(flash));
|
||||
.insert(animations.frightened(flash).clone())
|
||||
.insert(Looping);
|
||||
}
|
||||
GhostAnimation::Normal => {
|
||||
// Remove LinearAnimation, add DirectionalAnimation
|
||||
// Remove LinearAnimation and Looping, add DirectionalAnimation
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<LinearAnimation>()
|
||||
.insert(*animations.get_normal(ghost_type).unwrap());
|
||||
.remove::<(LinearAnimation, Looping)>()
|
||||
.insert(animations.get_normal(ghost_type).unwrap().clone());
|
||||
}
|
||||
GhostAnimation::Eyes => {
|
||||
// Remove LinearAnimation, add DirectionalAnimation (eyes animation)
|
||||
commands.entity(entity).remove::<LinearAnimation>().insert(*animations.eyes());
|
||||
// Remove LinearAnimation and Looping, add DirectionalAnimation (eyes animation)
|
||||
commands
|
||||
.entity(entity)
|
||||
.remove::<(LinearAnimation, Looping)>()
|
||||
.insert(animations.eyes().clone());
|
||||
}
|
||||
}
|
||||
last_animation_state.0 = current_animation_state;
|
||||
|
||||
@@ -20,10 +20,10 @@ use crate::{
|
||||
};
|
||||
|
||||
// Touch input constants
|
||||
const TOUCH_DIRECTION_THRESHOLD: f32 = 10.0;
|
||||
const TOUCH_EASING_DISTANCE_THRESHOLD: f32 = 1.0;
|
||||
const MAX_TOUCH_MOVEMENT_SPEED: f32 = 100.0;
|
||||
const TOUCH_EASING_FACTOR: f32 = 1.5;
|
||||
pub const TOUCH_DIRECTION_THRESHOLD: f32 = 10.0;
|
||||
pub const TOUCH_EASING_DISTANCE_THRESHOLD: f32 = 1.0;
|
||||
pub const MAX_TOUCH_MOVEMENT_SPEED: f32 = 100.0;
|
||||
pub const TOUCH_EASING_FACTOR: f32 = 1.5;
|
||||
|
||||
#[derive(Resource, Default, Debug, Copy, Clone)]
|
||||
pub enum CursorPosition {
|
||||
@@ -35,7 +35,7 @@ pub enum CursorPosition {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Resource, Default, Debug)]
|
||||
#[derive(Resource, Default, Debug, Clone)]
|
||||
pub struct TouchState {
|
||||
pub active_touch: Option<TouchData>,
|
||||
}
|
||||
@@ -160,7 +160,7 @@ pub fn process_simple_key_events(bindings: &mut Bindings, frame_events: &[Simple
|
||||
}
|
||||
|
||||
/// Calculates the primary direction from a 2D vector delta
|
||||
fn calculate_direction_from_delta(delta: Vec2) -> Direction {
|
||||
pub fn calculate_direction_from_delta(delta: Vec2) -> Direction {
|
||||
if delta.x.abs() > delta.y.abs() {
|
||||
if delta.x > 0.0 {
|
||||
Direction::Right
|
||||
@@ -179,7 +179,7 @@ fn calculate_direction_from_delta(delta: Vec2) -> Direction {
|
||||
/// This slowly moves the start_pos towards the current_pos, with the speed
|
||||
/// decreasing as the distance gets smaller. The maximum movement speed is capped.
|
||||
/// Returns the delta vector and its length for reuse by the caller.
|
||||
fn update_touch_reference_position(touch_data: &mut TouchData, delta_time: f32) -> (Vec2, f32) {
|
||||
pub fn update_touch_reference_position(touch_data: &mut TouchData, delta_time: f32) -> (Vec2, f32) {
|
||||
// Calculate the vector from start to current position
|
||||
let delta = touch_data.current_pos - touch_data.start_pos;
|
||||
let distance = delta.length();
|
||||
@@ -220,16 +220,6 @@ pub fn input_system(
|
||||
// Collect all events for this frame.
|
||||
let frame_events: SmallVec<[Event; 3]> = pump.poll_iter().collect();
|
||||
|
||||
// Warn if the smallvec was heap allocated due to exceeding stack capacity
|
||||
#[cfg(debug_assertions)]
|
||||
if frame_events.len() > frame_events.capacity() {
|
||||
tracing::warn!(
|
||||
"More than {} events in a frame, consider adjusting stack capacity: {:?}",
|
||||
frame_events.capacity(),
|
||||
frame_events
|
||||
);
|
||||
}
|
||||
|
||||
// Handle non-keyboard events inline and build a simplified keyboard event stream.
|
||||
let mut simple_key_events: SmallVec<[SimpleKeyEvent; 3]> = smallvec![];
|
||||
for event in &frame_events {
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
//! The Entity-Component-System (ECS) module.
|
||||
//!
|
||||
//! This module contains all the ECS-related logic, including components, systems,
|
||||
//! and resources.
|
||||
//! This module contains all the systems in the game.
|
||||
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod audio;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod debug;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod profiling;
|
||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||
pub mod render;
|
||||
|
||||
pub mod blinking;
|
||||
pub mod collision;
|
||||
pub mod components;
|
||||
pub mod debug;
|
||||
pub mod ghost;
|
||||
pub mod input;
|
||||
pub mod item;
|
||||
pub mod movement;
|
||||
pub mod player;
|
||||
pub mod profiling;
|
||||
pub mod render;
|
||||
pub mod stage;
|
||||
pub mod state;
|
||||
|
||||
// Re-export all the modules. Do not fine-tune the exports.
|
||||
|
||||
pub use self::audio::*;
|
||||
pub use self::blinking::*;
|
||||
@@ -29,4 +33,4 @@ pub use self::movement::*;
|
||||
pub use self::player::*;
|
||||
pub use self::profiling::*;
|
||||
pub use self::render::*;
|
||||
pub use self::stage::*;
|
||||
pub use self::state::*;
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
use crate::constants::CANVAS_SIZE;
|
||||
use crate::error::{GameError, TextureError};
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::input::TouchState;
|
||||
use crate::systems::{
|
||||
debug_render_system, BatchedLinesResource, Collider, CursorPosition, DebugState, DebugTextureResource, DeltaTime,
|
||||
DirectionalAnimation, LinearAnimation, Position, Renderable, ScoreResource, StartupSequence, SystemId, SystemTimings,
|
||||
TtfAtlasResource, Velocity,
|
||||
DirectionalAnimation, Dying, Frozen, GameStage, LinearAnimation, Looping, PlayerLives, Position, Renderable, ScoreResource,
|
||||
StartupSequence, SystemId, SystemTimings, TtfAtlasResource, Velocity,
|
||||
};
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
use crate::texture::text::TextTexture;
|
||||
use crate::{
|
||||
constants::CANVAS_SIZE,
|
||||
error::{GameError, TextureError},
|
||||
};
|
||||
use bevy_ecs::component::Component;
|
||||
use bevy_ecs::entity::Entity;
|
||||
use bevy_ecs::event::EventWriter;
|
||||
use bevy_ecs::query::{Changed, Or, Without};
|
||||
use bevy_ecs::query::{Changed, Has, Or, With, Without};
|
||||
use bevy_ecs::removal_detection::RemovedComponents;
|
||||
use bevy_ecs::resource::Resource;
|
||||
use bevy_ecs::system::{NonSendMut, Query, Res, ResMut};
|
||||
@@ -53,7 +55,7 @@ pub fn dirty_render_system(
|
||||
/// All directions share the same frame timing to ensure perfect synchronization.
|
||||
pub fn directional_render_system(
|
||||
dt: Res<DeltaTime>,
|
||||
mut query: Query<(&Position, &Velocity, &mut DirectionalAnimation, &mut Renderable)>,
|
||||
mut query: Query<(&Position, &Velocity, &mut DirectionalAnimation, &mut Renderable), Without<Frozen>>,
|
||||
) {
|
||||
let ticks = (dt.seconds * 60.0).round() as u16; // Convert from seconds to ticks at 60 ticks/sec
|
||||
|
||||
@@ -86,26 +88,35 @@ pub fn directional_render_system(
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates linear animated entities (used for non-directional animations like frightened ghosts).
|
||||
///
|
||||
/// This system handles entities that use LinearAnimation component for simple frame cycling.
|
||||
pub fn linear_render_system(dt: Res<DeltaTime>, mut query: Query<(&mut LinearAnimation, &mut Renderable)>) {
|
||||
let ticks = (dt.seconds * 60.0).round() as u16; // Convert from seconds to ticks at 60 ticks/sec
|
||||
|
||||
for (mut anim, mut renderable) in query.iter_mut() {
|
||||
// Tick animation
|
||||
anim.time_bank += ticks;
|
||||
while anim.time_bank >= anim.frame_duration {
|
||||
anim.time_bank -= anim.frame_duration;
|
||||
anim.current_frame += 1;
|
||||
/// System that updates `Renderable` sprites for entities with `LinearAnimation`.
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn linear_render_system(
|
||||
dt: Res<DeltaTime>,
|
||||
mut query: Query<(&mut LinearAnimation, &mut Renderable, Has<Looping>), Or<(Without<Frozen>, With<Dying>)>>,
|
||||
) {
|
||||
for (mut anim, mut renderable, looping) in query.iter_mut() {
|
||||
if anim.finished {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !anim.tiles.is_empty() {
|
||||
let new_tile = anim.tiles.get_tile(anim.current_frame);
|
||||
if renderable.sprite != new_tile {
|
||||
renderable.sprite = new_tile;
|
||||
}
|
||||
anim.time_bank += dt.ticks as u16;
|
||||
let frames_to_advance = (anim.time_bank / anim.frame_duration) as usize;
|
||||
|
||||
if frames_to_advance == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let total_frames = anim.tiles.len();
|
||||
|
||||
if !looping && anim.current_frame + frames_to_advance >= total_frames {
|
||||
anim.finished = true;
|
||||
anim.current_frame = total_frames - 1;
|
||||
} else {
|
||||
anim.current_frame += frames_to_advance;
|
||||
}
|
||||
|
||||
anim.time_bank %= anim.frame_duration;
|
||||
renderable.sprite = anim.tiles.get_tile(anim.current_frame);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,15 +204,16 @@ pub fn hud_render_system(
|
||||
mut backbuffer: NonSendMut<BackbufferResource>,
|
||||
mut canvas: NonSendMut<&mut Canvas<Window>>,
|
||||
mut atlas: NonSendMut<SpriteAtlas>,
|
||||
player_lives: Res<PlayerLives>,
|
||||
score: Res<ScoreResource>,
|
||||
startup: Res<StartupSequence>,
|
||||
stage: Res<GameStage>,
|
||||
mut errors: EventWriter<GameError>,
|
||||
) {
|
||||
let _ = canvas.with_texture_canvas(&mut backbuffer.0, |canvas| {
|
||||
let mut text_renderer = TextTexture::new(1.0);
|
||||
|
||||
// Render lives and high score text in white
|
||||
let lives = 3; // TODO: Get from actual lives resource
|
||||
let lives = player_lives.0;
|
||||
let lives_text = format!("{lives}UP HIGH SCORE ");
|
||||
let lives_position = glam::UVec2::new(4 + 8 * 3, 2); // x_offset + lives_offset * 8, y_offset
|
||||
|
||||
@@ -226,10 +238,21 @@ pub fn hud_render_system(
|
||||
errors.write(TextureError::RenderFailed(format!("Failed to render high score text: {}", e)).into());
|
||||
}
|
||||
|
||||
// Render GAME OVER text
|
||||
if matches!(*stage, GameStage::GameOver) {
|
||||
let game_over_text = "GAME OVER";
|
||||
let game_over_width = text_renderer.text_width(game_over_text);
|
||||
let game_over_position = glam::UVec2::new((CANVAS_SIZE.x - game_over_width) / 2, 160);
|
||||
if let Err(e) = text_renderer.render_with_color(canvas, &mut atlas, game_over_text, game_over_position, Color::RED) {
|
||||
errors.write(TextureError::RenderFailed(format!("Failed to render GAME OVER text: {}", e)).into());
|
||||
}
|
||||
}
|
||||
|
||||
// Render text based on StartupSequence stage
|
||||
if matches!(
|
||||
*startup,
|
||||
StartupSequence::TextOnly { .. } | StartupSequence::CharactersVisible { .. }
|
||||
*stage,
|
||||
GameStage::Starting(StartupSequence::TextOnly { .. })
|
||||
| GameStage::Starting(StartupSequence::CharactersVisible { .. })
|
||||
) {
|
||||
let ready_text = "READY!";
|
||||
let ready_width = text_renderer.text_width(ready_text);
|
||||
@@ -238,7 +261,7 @@ pub fn hud_render_system(
|
||||
errors.write(TextureError::RenderFailed(format!("Failed to render READY text: {}", e)).into());
|
||||
}
|
||||
|
||||
if matches!(*startup, StartupSequence::TextOnly { .. }) {
|
||||
if matches!(*stage, GameStage::Starting(StartupSequence::TextOnly { .. })) {
|
||||
let player_one_text = "PLAYER ONE";
|
||||
let player_one_width = text_renderer.text_width(player_one_text);
|
||||
let player_one_position = glam::UVec2::new((CANVAS_SIZE.x - player_one_width) / 2, 113);
|
||||
|
||||
@@ -1,101 +0,0 @@
|
||||
use bevy_ecs::{
|
||||
entity::Entity,
|
||||
query::With,
|
||||
resource::Resource,
|
||||
system::{Commands, Query, ResMut},
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
use crate::systems::{Blinking, Frozen, GhostCollider, Hidden, PlayerControlled};
|
||||
|
||||
#[derive(Resource, Debug, Clone, Copy)]
|
||||
pub enum StartupSequence {
|
||||
/// Stage 1: Text-only stage
|
||||
/// - Player & ghosts are hidden
|
||||
/// - READY! and PLAYER ONE text are shown
|
||||
/// - Energizers do not blink
|
||||
TextOnly {
|
||||
/// Remaining ticks in this stage
|
||||
remaining_ticks: u32,
|
||||
},
|
||||
/// Stage 2: Characters visible stage
|
||||
/// - PLAYER ONE text is hidden, READY! text remains
|
||||
/// - Ghosts and Pac-Man are now shown
|
||||
CharactersVisible {
|
||||
/// Remaining ticks in this stage
|
||||
remaining_ticks: u32,
|
||||
},
|
||||
/// Stage 3: Game begins
|
||||
/// - Final state, game is fully active
|
||||
GameActive,
|
||||
}
|
||||
|
||||
impl StartupSequence {
|
||||
/// Creates a new StartupSequence with the specified duration in ticks
|
||||
pub fn new(text_only_ticks: u32, _characters_visible_ticks: u32) -> Self {
|
||||
Self::TextOnly {
|
||||
remaining_ticks: text_only_ticks,
|
||||
}
|
||||
}
|
||||
|
||||
/// Ticks the timer by one frame, returning transition information if state changes
|
||||
pub fn tick(&mut self) -> Option<(StartupSequence, StartupSequence)> {
|
||||
match self {
|
||||
StartupSequence::TextOnly { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
*remaining_ticks -= 1;
|
||||
None
|
||||
} else {
|
||||
let from = *self;
|
||||
*self = StartupSequence::CharactersVisible {
|
||||
remaining_ticks: 60, // 1 second at 60 FPS
|
||||
};
|
||||
Some((from, *self))
|
||||
}
|
||||
}
|
||||
StartupSequence::CharactersVisible { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
*remaining_ticks -= 1;
|
||||
None
|
||||
} else {
|
||||
let from = *self;
|
||||
*self = StartupSequence::GameActive;
|
||||
Some((from, *self))
|
||||
}
|
||||
}
|
||||
StartupSequence::GameActive => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles startup sequence transitions and component management
|
||||
pub fn startup_stage_system(
|
||||
mut startup: ResMut<StartupSequence>,
|
||||
mut commands: Commands,
|
||||
mut blinking_query: Query<Entity, With<Blinking>>,
|
||||
mut player_query: Query<Entity, With<PlayerControlled>>,
|
||||
mut ghost_query: Query<Entity, With<GhostCollider>>,
|
||||
) {
|
||||
if let Some((from, to)) = startup.tick() {
|
||||
debug!("StartupSequence transition from {from:?} to {to:?}");
|
||||
match (from, to) {
|
||||
(StartupSequence::TextOnly { .. }, StartupSequence::CharactersVisible { .. }) => {
|
||||
// Unhide the player & ghosts
|
||||
for entity in player_query.iter_mut().chain(ghost_query.iter_mut()) {
|
||||
commands.entity(entity).remove::<Hidden>();
|
||||
}
|
||||
}
|
||||
(StartupSequence::CharactersVisible { .. }, StartupSequence::GameActive) => {
|
||||
// Unfreeze the player & ghosts & pellet blinking
|
||||
for entity in player_query
|
||||
.iter_mut()
|
||||
.chain(ghost_query.iter_mut())
|
||||
.chain(blinking_query.iter_mut())
|
||||
{
|
||||
commands.entity(entity).remove::<Frozen>();
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
315
src/systems/state.rs
Normal file
315
src/systems/state.rs
Normal file
@@ -0,0 +1,315 @@
|
||||
use std::mem::discriminant;
|
||||
|
||||
use crate::{
|
||||
map::builder::Map,
|
||||
systems::{
|
||||
AudioEvent, Blinking, DirectionalAnimation, Dying, Eaten, Frozen, Ghost, GhostCollider, GhostState, Hidden,
|
||||
LinearAnimation, Looping, PlayerControlled, Position,
|
||||
},
|
||||
};
|
||||
use bevy_ecs::{
|
||||
entity::Entity,
|
||||
event::EventWriter,
|
||||
query::{With, Without},
|
||||
resource::Resource,
|
||||
system::{Commands, Query, Res, ResMut},
|
||||
};
|
||||
|
||||
#[derive(Resource, Clone)]
|
||||
pub struct PlayerAnimation(pub DirectionalAnimation);
|
||||
|
||||
#[derive(Resource, Clone)]
|
||||
pub struct PlayerDeathAnimation(pub LinearAnimation);
|
||||
|
||||
/// A resource to track the overall stage of the game from a high-level perspective.
|
||||
#[derive(Resource, Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum GameStage {
|
||||
Starting(StartupSequence),
|
||||
/// The main gameplay loop is active.
|
||||
Playing,
|
||||
/// The player has died and the death sequence is in progress.
|
||||
PlayerDying(DyingSequence),
|
||||
/// The level is restarting after a death.
|
||||
LevelRestarting,
|
||||
/// The game has ended.
|
||||
GameOver,
|
||||
}
|
||||
|
||||
/// A resource that manages the multi-stage startup sequence of the game.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum StartupSequence {
|
||||
/// Stage 1: Text-only stage
|
||||
/// - Player & ghosts are hidden
|
||||
/// - READY! and PLAYER ONE text are shown
|
||||
/// - Energizers do not blink
|
||||
TextOnly {
|
||||
/// Remaining ticks in this stage
|
||||
remaining_ticks: u32,
|
||||
},
|
||||
/// Stage 2: Characters visible stage
|
||||
/// - PLAYER ONE text is hidden, READY! text remains
|
||||
/// - Ghosts and Pac-Man are now shown
|
||||
CharactersVisible {
|
||||
/// Remaining ticks in this stage
|
||||
remaining_ticks: u32,
|
||||
},
|
||||
}
|
||||
|
||||
impl Default for GameStage {
|
||||
fn default() -> Self {
|
||||
Self::Playing
|
||||
}
|
||||
}
|
||||
|
||||
/// The state machine for the multi-stage death sequence.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
pub enum DyingSequence {
|
||||
/// Initial stage: entities are frozen, waiting for a delay.
|
||||
Frozen { remaining_ticks: u32 },
|
||||
/// Second stage: Pac-Man's death animation is playing.
|
||||
Animating { remaining_ticks: u32 },
|
||||
/// Third stage: Pac-Man is now gone, waiting a moment before the level restarts.
|
||||
Hidden { remaining_ticks: u32 },
|
||||
}
|
||||
|
||||
/// A resource to store the number of player lives.
|
||||
#[derive(Resource, Debug)]
|
||||
pub struct PlayerLives(pub u8);
|
||||
|
||||
impl Default for PlayerLives {
|
||||
fn default() -> Self {
|
||||
Self(3)
|
||||
}
|
||||
}
|
||||
|
||||
/// Handles startup sequence transitions and component management
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn stage_system(
|
||||
mut game_state: ResMut<GameStage>,
|
||||
player_death_animation: Res<PlayerDeathAnimation>,
|
||||
player_animation: Res<PlayerAnimation>,
|
||||
mut player_lives: ResMut<PlayerLives>,
|
||||
map: Res<Map>,
|
||||
mut commands: Commands,
|
||||
mut audio_events: EventWriter<AudioEvent>,
|
||||
mut blinking_query: Query<Entity, With<Blinking>>,
|
||||
mut player_query: Query<(Entity, &mut Position), With<PlayerControlled>>,
|
||||
mut ghost_query: Query<(Entity, &Ghost, &mut Position), (With<GhostCollider>, Without<PlayerControlled>)>,
|
||||
) {
|
||||
let old_state = *game_state;
|
||||
let new_state: GameStage = match &mut *game_state {
|
||||
GameStage::Starting(startup) => match startup {
|
||||
StartupSequence::TextOnly { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
GameStage::Starting(StartupSequence::TextOnly {
|
||||
remaining_ticks: *remaining_ticks - 1,
|
||||
})
|
||||
} else {
|
||||
GameStage::Starting(StartupSequence::CharactersVisible { remaining_ticks: 60 })
|
||||
}
|
||||
}
|
||||
StartupSequence::CharactersVisible { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
GameStage::Starting(StartupSequence::CharactersVisible {
|
||||
remaining_ticks: *remaining_ticks - 1,
|
||||
})
|
||||
} else {
|
||||
GameStage::Playing
|
||||
}
|
||||
}
|
||||
},
|
||||
GameStage::Playing => GameStage::Playing,
|
||||
GameStage::PlayerDying(dying) => match dying {
|
||||
DyingSequence::Frozen { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
GameStage::PlayerDying(DyingSequence::Frozen {
|
||||
remaining_ticks: *remaining_ticks - 1,
|
||||
})
|
||||
} else {
|
||||
let death_animation = &player_death_animation.0;
|
||||
let remaining_ticks = (death_animation.tiles.len() * death_animation.frame_duration as usize) as u32;
|
||||
GameStage::PlayerDying(DyingSequence::Animating { remaining_ticks })
|
||||
}
|
||||
}
|
||||
DyingSequence::Animating { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
GameStage::PlayerDying(DyingSequence::Animating {
|
||||
remaining_ticks: *remaining_ticks - 1,
|
||||
})
|
||||
} else {
|
||||
GameStage::PlayerDying(DyingSequence::Hidden { remaining_ticks: 60 })
|
||||
}
|
||||
}
|
||||
DyingSequence::Hidden { remaining_ticks } => {
|
||||
if *remaining_ticks > 0 {
|
||||
GameStage::PlayerDying(DyingSequence::Hidden {
|
||||
remaining_ticks: *remaining_ticks - 1,
|
||||
})
|
||||
} else {
|
||||
player_lives.0 = player_lives.0.saturating_sub(1);
|
||||
|
||||
if player_lives.0 > 0 {
|
||||
GameStage::LevelRestarting
|
||||
} else {
|
||||
GameStage::GameOver
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
GameStage::LevelRestarting => GameStage::Starting(StartupSequence::CharactersVisible { remaining_ticks: 60 }),
|
||||
GameStage::GameOver => GameStage::GameOver,
|
||||
};
|
||||
|
||||
if old_state == new_state {
|
||||
return;
|
||||
}
|
||||
|
||||
match (old_state, new_state) {
|
||||
(GameStage::Playing, GameStage::PlayerDying(DyingSequence::Frozen { .. })) => {
|
||||
// Freeze the player & ghosts
|
||||
for entity in player_query
|
||||
.iter_mut()
|
||||
.map(|(e, _)| e)
|
||||
.chain(ghost_query.iter_mut().map(|(e, _, _)| e))
|
||||
{
|
||||
commands.entity(entity).insert(Frozen);
|
||||
}
|
||||
}
|
||||
(GameStage::PlayerDying(DyingSequence::Frozen { .. }), GameStage::PlayerDying(DyingSequence::Animating { .. })) => {
|
||||
// Hide the ghosts
|
||||
for (entity, _, _) in ghost_query.iter_mut() {
|
||||
commands.entity(entity).insert(Hidden);
|
||||
}
|
||||
|
||||
// Start Pac-Man's death animation
|
||||
if let Ok((player_entity, _)) = player_query.single_mut() {
|
||||
commands
|
||||
.entity(player_entity)
|
||||
.insert((Dying, player_death_animation.0.clone()));
|
||||
}
|
||||
|
||||
// Play the death sound
|
||||
audio_events.write(AudioEvent::PlayDeath);
|
||||
}
|
||||
(GameStage::PlayerDying(DyingSequence::Animating { .. }), GameStage::PlayerDying(DyingSequence::Hidden { .. })) => {
|
||||
// Hide the player
|
||||
if let Ok((player_entity, _)) = player_query.single_mut() {
|
||||
commands.entity(player_entity).insert(Hidden);
|
||||
}
|
||||
}
|
||||
(_, GameStage::LevelRestarting) => {
|
||||
if let Ok((player_entity, mut pos)) = player_query.single_mut() {
|
||||
*pos = Position::Stopped {
|
||||
node: map.start_positions.pacman,
|
||||
};
|
||||
|
||||
// Freeze the blinking, force them to be visible (if they were hidden by blinking)
|
||||
for entity in blinking_query.iter_mut() {
|
||||
commands.entity(entity).insert(Frozen).remove::<Hidden>();
|
||||
}
|
||||
|
||||
// Reset the player animation
|
||||
commands
|
||||
.entity(player_entity)
|
||||
.remove::<(Frozen, Dying, Hidden, LinearAnimation, Looping)>()
|
||||
.insert(player_animation.0.clone());
|
||||
}
|
||||
|
||||
// Reset ghost positions and state
|
||||
for (ghost_entity, ghost, mut ghost_pos) in ghost_query.iter_mut() {
|
||||
*ghost_pos = Position::Stopped {
|
||||
node: match ghost {
|
||||
Ghost::Blinky => map.start_positions.blinky,
|
||||
Ghost::Pinky => map.start_positions.pinky,
|
||||
Ghost::Inky => map.start_positions.inky,
|
||||
Ghost::Clyde => map.start_positions.clyde,
|
||||
},
|
||||
};
|
||||
commands
|
||||
.entity(ghost_entity)
|
||||
.remove::<(Frozen, Hidden, Eaten)>()
|
||||
.insert(GhostState::Normal);
|
||||
}
|
||||
}
|
||||
(
|
||||
GameStage::Starting(StartupSequence::TextOnly { .. }),
|
||||
GameStage::Starting(StartupSequence::CharactersVisible { .. }),
|
||||
) => {
|
||||
// Unhide the player & ghosts
|
||||
for entity in player_query
|
||||
.iter_mut()
|
||||
.map(|(e, _)| e)
|
||||
.chain(ghost_query.iter_mut().map(|(e, _, _)| e))
|
||||
{
|
||||
commands.entity(entity).remove::<Hidden>();
|
||||
}
|
||||
}
|
||||
(GameStage::Starting(StartupSequence::CharactersVisible { .. }), GameStage::Playing) => {
|
||||
// Unfreeze the player & ghosts & blinking
|
||||
for entity in player_query
|
||||
.iter_mut()
|
||||
.map(|(e, _)| e)
|
||||
.chain(ghost_query.iter_mut().map(|(e, _, _)| e))
|
||||
.chain(blinking_query.iter_mut())
|
||||
{
|
||||
commands.entity(entity).remove::<Frozen>();
|
||||
}
|
||||
}
|
||||
(GameStage::PlayerDying(..), GameStage::GameOver) => {
|
||||
// Freeze blinking
|
||||
for entity in blinking_query.iter_mut() {
|
||||
commands.entity(entity).insert(Frozen);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let different = discriminant(&old_state) != discriminant(&new_state);
|
||||
if different {
|
||||
tracing::warn!(
|
||||
new_state = ?new_state,
|
||||
old_state = ?old_state,
|
||||
"Unhandled game stage transition");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*game_state = new_state;
|
||||
}
|
||||
|
||||
// if let GameState::LevelRestarting = &*game_state {
|
||||
// // When restarting, jump straight to the CharactersVisible stage
|
||||
// // and unhide the entities.
|
||||
// *startup = StartupSequence::new(0, 60 * 2); // 2 seconds for READY! text
|
||||
// if let StartupSequence::TextOnly { .. } = *startup {
|
||||
// // This will immediately transition to CharactersVisible on the next line
|
||||
// } else {
|
||||
// // Should be unreachable as we just set it
|
||||
// }
|
||||
|
||||
// // Freeze Pac-Man and ghosts
|
||||
// for entity in player_query.iter().chain(ghost_query.iter()) {
|
||||
// commands.entity(entity).insert(Frozen);
|
||||
// }
|
||||
|
||||
// *game_state = GameState::Playing;
|
||||
// }
|
||||
|
||||
// if let Some((old_state, new_state)) = startup.tick() {
|
||||
// debug!("StartupSequence transition from {old_state:?} to {new_state:?}");
|
||||
// match (old_state, new_state) {
|
||||
// (StartupSequence::TextOnly { .. }, StartupSequence::CharactersVisible { .. }) => {
|
||||
// // Unhide the player & ghosts
|
||||
// for entity in player_query.iter().chain(ghost_query.iter()) {
|
||||
// commands.entity(entity).remove::<Hidden>();
|
||||
// }
|
||||
// }
|
||||
// (StartupSequence::CharactersVisible { .. }, StartupSequence::GameActive) => {
|
||||
// // Unfreeze Pac-Man, ghosts and energizers
|
||||
// for entity in player_query.iter().chain(ghost_query.iter()).chain(blinking_query.iter()) {
|
||||
// commands.entity(entity).remove::<Frozen>();
|
||||
// }
|
||||
// *game_state = GameState::Playing;
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
@@ -1,53 +1,45 @@
|
||||
use crate::map::direction::Direction;
|
||||
use crate::texture::sprite::AtlasTile;
|
||||
use glam::U16Vec2;
|
||||
|
||||
/// Fixed-size tile sequence that avoids heap allocation
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
use crate::{map::direction::Direction, texture::sprite::AtlasTile};
|
||||
|
||||
/// A sequence of tiles for animation, backed by a vector.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TileSequence {
|
||||
tiles: [AtlasTile; 4], // Fixed array, max 4 frames
|
||||
count: usize, // Actual number of frames used
|
||||
tiles: Vec<AtlasTile>,
|
||||
}
|
||||
|
||||
impl TileSequence {
|
||||
/// Creates a new tile sequence from a slice of tiles
|
||||
/// Creates a new tile sequence from a slice of tiles.
|
||||
pub fn new(tiles: &[AtlasTile]) -> Self {
|
||||
let mut tile_array = [AtlasTile {
|
||||
pos: glam::U16Vec2::ZERO,
|
||||
size: glam::U16Vec2::ZERO,
|
||||
color: None,
|
||||
}; 4];
|
||||
|
||||
let count = tiles.len().min(4);
|
||||
tile_array[..count].copy_from_slice(&tiles[..count]);
|
||||
|
||||
Self {
|
||||
tiles: tile_array,
|
||||
count,
|
||||
}
|
||||
Self { tiles: tiles.to_vec() }
|
||||
}
|
||||
|
||||
/// Returns the tile at the given frame index, wrapping if necessary
|
||||
pub fn get_tile(&self, frame: usize) -> AtlasTile {
|
||||
if self.count == 0 {
|
||||
// Return a default empty tile if no tiles
|
||||
AtlasTile {
|
||||
pos: glam::U16Vec2::ZERO,
|
||||
size: glam::U16Vec2::ZERO,
|
||||
if self.tiles.is_empty() {
|
||||
// Return a default or handle the error appropriately
|
||||
// For now, let's return a default tile, assuming it's a sensible default
|
||||
return AtlasTile {
|
||||
pos: U16Vec2::ZERO,
|
||||
size: U16Vec2::ZERO,
|
||||
color: None,
|
||||
}
|
||||
} else {
|
||||
self.tiles[frame % self.count]
|
||||
};
|
||||
}
|
||||
self.tiles[frame % self.tiles.len()]
|
||||
}
|
||||
|
||||
/// Returns true if this sequence has no tiles
|
||||
pub fn len(&self) -> usize {
|
||||
self.tiles.len()
|
||||
}
|
||||
|
||||
/// Checks if the sequence contains any tiles.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.count == 0
|
||||
self.tiles.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Type-safe directional tile storage with named fields
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
/// A collection of tile sequences for each cardinal direction.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DirectionalTiles {
|
||||
pub up: TileSequence,
|
||||
pub down: TileSequence,
|
||||
|
||||
@@ -15,6 +15,8 @@ pub enum PacmanSprite {
|
||||
Moving(Direction, u8),
|
||||
/// The full, closed-mouth Pac-Man sprite.
|
||||
Full,
|
||||
/// A single frame of the dying animation.
|
||||
Dying(u8),
|
||||
}
|
||||
|
||||
/// Represents the color of a frightened ghost.
|
||||
@@ -60,45 +62,50 @@ impl GameSprite {
|
||||
/// This path corresponds to the filename in the texture atlas JSON file.
|
||||
pub fn to_path(self) -> String {
|
||||
match self {
|
||||
GameSprite::Pacman(sprite) => match sprite {
|
||||
PacmanSprite::Moving(dir, frame) => {
|
||||
let frame_char = match frame {
|
||||
0 => 'a',
|
||||
1 => 'b',
|
||||
_ => panic!("Invalid animation frame"),
|
||||
};
|
||||
format!("pacman/{}_{}.png", dir.as_ref().to_lowercase(), frame_char)
|
||||
GameSprite::Pacman(PacmanSprite::Moving(dir, frame)) => format!(
|
||||
"pacman/{}_{}.png",
|
||||
dir.as_ref(),
|
||||
match frame {
|
||||
0 => "a",
|
||||
1 => "b",
|
||||
_ => panic!("Invalid animation frame"),
|
||||
}
|
||||
PacmanSprite::Full => "pacman/full.png".to_string(),
|
||||
},
|
||||
GameSprite::Ghost(sprite) => match sprite {
|
||||
GhostSprite::Normal(ghost, dir, frame) => {
|
||||
let frame_char = match frame {
|
||||
0 => 'a',
|
||||
1 => 'b',
|
||||
_ => panic!("Invalid animation frame"),
|
||||
};
|
||||
format!("ghost/{}/{}_{}.png", ghost.as_str(), dir.as_ref().to_lowercase(), frame_char)
|
||||
}
|
||||
GhostSprite::Frightened(color, frame) => {
|
||||
let frame_char = match frame {
|
||||
0 => 'a',
|
||||
1 => 'b',
|
||||
_ => panic!("Invalid animation frame"),
|
||||
};
|
||||
let color_str = match color {
|
||||
FrightenedColor::Blue => "blue",
|
||||
FrightenedColor::White => "white",
|
||||
};
|
||||
format!("ghost/frightened/{}_{}.png", color_str, frame_char)
|
||||
}
|
||||
GhostSprite::Eyes(dir) => format!("ghost/eyes/{}.png", dir.as_ref().to_lowercase()),
|
||||
},
|
||||
GameSprite::Maze(sprite) => match sprite {
|
||||
MazeSprite::Tile(index) => format!("maze/tiles/{}.png", index),
|
||||
MazeSprite::Pellet => "maze/pellet.png".to_string(),
|
||||
MazeSprite::Energizer => "maze/energizer.png".to_string(),
|
||||
},
|
||||
),
|
||||
GameSprite::Pacman(PacmanSprite::Full) => "pacman/full.png".to_string(),
|
||||
GameSprite::Pacman(PacmanSprite::Dying(frame)) => format!("pacman/death/{}.png", frame),
|
||||
|
||||
// Ghost sprites
|
||||
GameSprite::Ghost(GhostSprite::Normal(ghost_type, dir, frame)) => {
|
||||
let frame_char = match frame {
|
||||
0 => 'a',
|
||||
1 => 'b',
|
||||
_ => panic!("Invalid animation frame"),
|
||||
};
|
||||
format!(
|
||||
"ghost/{}/{}_{}.png",
|
||||
ghost_type.as_str(),
|
||||
dir.as_ref().to_lowercase(),
|
||||
frame_char
|
||||
)
|
||||
}
|
||||
GameSprite::Ghost(GhostSprite::Frightened(color, frame)) => {
|
||||
let frame_char = match frame {
|
||||
0 => 'a',
|
||||
1 => 'b',
|
||||
_ => panic!("Invalid animation frame"),
|
||||
};
|
||||
let color_str = match color {
|
||||
FrightenedColor::Blue => "blue",
|
||||
FrightenedColor::White => "white",
|
||||
};
|
||||
format!("ghost/frightened/{}_{}.png", color_str, frame_char)
|
||||
}
|
||||
GameSprite::Ghost(GhostSprite::Eyes(dir)) => format!("ghost/eyes/{}.png", dir.as_ref().to_lowercase()),
|
||||
|
||||
// Maze sprites
|
||||
GameSprite::Maze(MazeSprite::Tile(index)) => format!("maze/tiles/{}.png", index),
|
||||
GameSprite::Maze(MazeSprite::Pellet) => "maze/pellet.png".to_string(),
|
||||
GameSprite::Maze(MazeSprite::Energizer) => "maze/energizer.png".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
17
tests/asset.rs
Normal file
17
tests/asset.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
use pacman::asset::Asset;
|
||||
use speculoos::prelude::*;
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
#[test]
|
||||
fn all_asset_paths_exist() {
|
||||
for asset in Asset::iter() {
|
||||
let path = asset.path();
|
||||
let full_path = format!("assets/game/{}", path);
|
||||
|
||||
let metadata = std::fs::metadata(&full_path)
|
||||
.map_err(|e| format!("Error getting metadata for {}: {}", full_path, e))
|
||||
.unwrap();
|
||||
assert_that(&metadata.is_file()).is_true();
|
||||
assert_that(&metadata.len()).is_greater_than(1024);
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use pacman::constants::*;
|
||||
use speculoos::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn test_raw_board_structure() {
|
||||
// Test board dimensions match expected size
|
||||
assert_that(&RAW_BOARD.len()).is_equal_to(BOARD_CELL_SIZE.y as usize);
|
||||
for row in RAW_BOARD.iter() {
|
||||
assert_that(&row.len()).is_equal_to(BOARD_CELL_SIZE.x as usize);
|
||||
}
|
||||
|
||||
// Test boundaries are properly walled
|
||||
assert_that(&RAW_BOARD[0].chars().all(|c| c == '#')).is_true();
|
||||
assert_that(&RAW_BOARD[RAW_BOARD.len() - 1].chars().all(|c| c == '#')).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_raw_board_contains_required_elements() {
|
||||
// Test that essential game elements are present
|
||||
assert_that(&RAW_BOARD.iter().any(|row| row.contains('X'))).is_true();
|
||||
assert_that(&RAW_BOARD.iter().any(|row| row.contains("=="))).is_true();
|
||||
assert_that(&RAW_BOARD.iter().any(|row| row.chars().any(|c| c == 'T'))).is_true();
|
||||
assert_that(&RAW_BOARD.iter().any(|row| row.chars().any(|c| c == 'o'))).is_true();
|
||||
}
|
||||
@@ -1,76 +1,7 @@
|
||||
use pacman::error::{
|
||||
AssetError, EntityError, GameError, GameResult, IntoGameError, MapError, OptionExt, ParseError, ResultExt, TextureError,
|
||||
};
|
||||
use pacman::error::{GameError, GameResult, IntoGameError, OptionExt, ResultExt};
|
||||
use speculoos::prelude::*;
|
||||
use std::io;
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_asset_error() {
|
||||
let asset_error = AssetError::NotFound("test.png".to_string());
|
||||
let game_error: GameError = asset_error.into();
|
||||
assert_that(&matches!(game_error, GameError::Asset(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_parse_error() {
|
||||
let parse_error = ParseError::UnknownCharacter('Z');
|
||||
let game_error: GameError = parse_error.into();
|
||||
assert_that(&matches!(game_error, GameError::MapParse(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_map_error() {
|
||||
let map_error = MapError::NodeNotFound(42);
|
||||
let game_error: GameError = map_error.into();
|
||||
assert_that(&matches!(game_error, GameError::Map(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_texture_error() {
|
||||
let texture_error = TextureError::LoadFailed("Failed to load".to_string());
|
||||
let game_error: GameError = texture_error.into();
|
||||
assert_that(&matches!(game_error, GameError::Texture(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_entity_error() {
|
||||
let entity_error = EntityError::NodeNotFound(10);
|
||||
let game_error: GameError = entity_error.into();
|
||||
assert_that(&matches!(game_error, GameError::Entity(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_error_from_io_error() {
|
||||
let io_error = io::Error::new(io::ErrorKind::NotFound, "File not found");
|
||||
let game_error: GameError = io_error.into();
|
||||
assert_that(&matches!(game_error, GameError::Io(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_asset_error_from_io_error() {
|
||||
let io_error = io::Error::new(io::ErrorKind::PermissionDenied, "Permission denied");
|
||||
let asset_error: AssetError = io_error.into();
|
||||
assert_that(&matches!(asset_error, AssetError::Io(_))).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_error_display() {
|
||||
let error = ParseError::UnknownCharacter('!');
|
||||
assert_that(&error.to_string()).is_equal_to("Unknown character in board: !".to_string());
|
||||
|
||||
let error = ParseError::InvalidHouseDoorCount(3);
|
||||
assert_that(&error.to_string()).is_equal_to("House door must have exactly 2 positions, found 3".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_entity_error_display() {
|
||||
let error = EntityError::NodeNotFound(42);
|
||||
assert_that(&error.to_string()).is_equal_to("Node not found in graph: 42".to_string());
|
||||
|
||||
let error = EntityError::EdgeNotFound { from: 1, to: 2 };
|
||||
assert_that(&error.to_string()).is_equal_to("Edge not found: from 1 to 2".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_into_game_error_trait() {
|
||||
let result: Result<i32, io::Error> = Err(io::Error::new(io::ErrorKind::Other, "test error"));
|
||||
|
||||
79
tests/game.rs
Normal file
79
tests/game.rs
Normal file
@@ -0,0 +1,79 @@
|
||||
use pacman::error::{GameError, GameResult};
|
||||
use pacman::game::Game;
|
||||
use speculoos::prelude::*;
|
||||
|
||||
mod common;
|
||||
|
||||
use common::setup_sdl;
|
||||
|
||||
#[test]
|
||||
fn test_game_30_seconds_60fps() -> GameResult<()> {
|
||||
let (canvas, texture_creator, _sdl_context) = setup_sdl().map_err(GameError::Sdl)?;
|
||||
let ttf_context = sdl2::ttf::init().map_err(GameError::Sdl)?;
|
||||
let event_pump = _sdl_context
|
||||
.event_pump()
|
||||
.map_err(|e| pacman::error::GameError::Sdl(e.to_string()))?;
|
||||
|
||||
let mut game = Game::new(canvas, ttf_context, texture_creator, event_pump)?;
|
||||
|
||||
// Run for 30 seconds at 60 FPS = 1800 frames
|
||||
let frame_time = 1.0 / 60.0;
|
||||
let total_frames = 1800;
|
||||
let mut frame_count = 0;
|
||||
|
||||
for _ in 0..total_frames {
|
||||
let should_exit = game.tick(frame_time);
|
||||
|
||||
if should_exit {
|
||||
break;
|
||||
}
|
||||
|
||||
frame_count += 1;
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
frame_count, total_frames,
|
||||
"Should have processed exactly {} frames",
|
||||
total_frames
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Test that runs the game for 30 seconds with variable frame timing
|
||||
#[test]
|
||||
fn test_game_30_seconds_variable_timing() -> GameResult<()> {
|
||||
let (canvas, texture_creator, _sdl_context) = setup_sdl().map_err(GameError::Sdl)?;
|
||||
let ttf_context = sdl2::ttf::init().map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
let event_pump = _sdl_context
|
||||
.event_pump()
|
||||
.map_err(|e| pacman::error::GameError::Sdl(e.to_string()))?;
|
||||
|
||||
let mut game = Game::new(canvas, ttf_context, texture_creator, event_pump)?;
|
||||
|
||||
// Simulate 30 seconds with variable frame timing
|
||||
let mut total_time = 0.0;
|
||||
let target_time = 30.0;
|
||||
let mut frame_count = 0;
|
||||
|
||||
while total_time < target_time {
|
||||
// Alternate between different frame rates to simulate real gameplay
|
||||
let frame_time = match frame_count % 4 {
|
||||
0 => 1.0 / 60.0, // 60 FPS
|
||||
1 => 1.0 / 30.0, // 30 FPS (lag spike)
|
||||
2 => 1.0 / 120.0, // 120 FPS (very fast)
|
||||
_ => 1.0 / 60.0, // 60 FPS
|
||||
};
|
||||
|
||||
let should_exit = game.tick(frame_time);
|
||||
|
||||
if should_exit {
|
||||
break;
|
||||
}
|
||||
|
||||
total_time += frame_time;
|
||||
frame_count += 1;
|
||||
}
|
||||
|
||||
assert_that(&total_time).is_greater_than_or_equal_to(target_time);
|
||||
Ok(())
|
||||
}
|
||||
332
tests/input.rs
332
tests/input.rs
@@ -1,39 +1,321 @@
|
||||
use glam::Vec2;
|
||||
use pacman::events::{GameCommand, GameEvent};
|
||||
use pacman::map::direction::Direction;
|
||||
use pacman::systems::input::{process_simple_key_events, Bindings, SimpleKeyEvent};
|
||||
use pacman::systems::input::{
|
||||
calculate_direction_from_delta, process_simple_key_events, update_touch_reference_position, Bindings, CursorPosition,
|
||||
SimpleKeyEvent, TouchData, TouchState, TOUCH_DIRECTION_THRESHOLD, TOUCH_EASING_DISTANCE_THRESHOLD,
|
||||
};
|
||||
use sdl2::keyboard::Keycode;
|
||||
use speculoos::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn resumes_previous_direction_when_secondary_key_released() {
|
||||
let mut bindings = Bindings::default();
|
||||
// Test modules for better organization
|
||||
mod keyboard_tests {
|
||||
use super::*;
|
||||
|
||||
// Frame 1: Press W (Up) => emits Move Up
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::W)]);
|
||||
assert_that(&events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Up)))).is_true();
|
||||
#[test]
|
||||
fn key_down_emits_bound_command() {
|
||||
let mut bindings = Bindings::default();
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::W)]);
|
||||
assert_that(&events).contains(GameEvent::Command(GameCommand::MovePlayer(Direction::Up)));
|
||||
}
|
||||
|
||||
// Frame 2: Press D (Right) => emits Move Right
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::D)]);
|
||||
assert_that(&events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Right)))).is_true();
|
||||
#[test]
|
||||
fn key_down_emits_non_movement_commands() {
|
||||
let mut bindings = Bindings::default();
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::P)]);
|
||||
assert_that(&events).contains(GameEvent::Command(GameCommand::TogglePause));
|
||||
}
|
||||
|
||||
// Frame 3: Release D, no new key this frame => should continue previous key W (Up)
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::D)]);
|
||||
assert_that(&events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Up)))).is_true();
|
||||
#[test]
|
||||
fn unbound_key_emits_nothing() {
|
||||
let mut bindings = Bindings::default();
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::Z)]);
|
||||
assert_that(&events).is_empty();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn movement_key_held_continues_across_frames() {
|
||||
let mut bindings = Bindings::default();
|
||||
process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::Left)]);
|
||||
let events = process_simple_key_events(&mut bindings, &[]);
|
||||
assert_that(&events).contains(GameEvent::Command(GameCommand::MovePlayer(Direction::Left)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn releasing_movement_key_stops_continuation() {
|
||||
let mut bindings = Bindings::default();
|
||||
process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::Up)]);
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::Up)]);
|
||||
assert_that(&events).is_empty();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_movement_keys_resumes_previous_when_current_released() {
|
||||
let mut bindings = Bindings::default();
|
||||
process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::W)]);
|
||||
process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::D)]);
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::D)]);
|
||||
assert_that(&events).contains(GameEvent::Command(GameCommand::MovePlayer(Direction::Up)));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn holds_last_pressed_key_across_frames_when_no_new_input() {
|
||||
let mut bindings = Bindings::default();
|
||||
mod direction_calculation_tests {
|
||||
use super::*;
|
||||
|
||||
// Frame 1: Press Left
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyDown(Keycode::Left)]);
|
||||
assert_that(&events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Left)))).is_true();
|
||||
#[test]
|
||||
fn prioritizes_horizontal_movement() {
|
||||
let test_cases = vec![
|
||||
(Vec2::new(6.0, 5.0), Direction::Right),
|
||||
(Vec2::new(-6.0, 5.0), Direction::Left),
|
||||
];
|
||||
|
||||
// Frame 2: No input => continues Left
|
||||
let events = process_simple_key_events(&mut bindings, &[]);
|
||||
assert_that(&events.contains(&GameEvent::Command(GameCommand::MovePlayer(Direction::Left)))).is_true();
|
||||
for (delta, expected) in test_cases {
|
||||
assert_that(&calculate_direction_from_delta(delta)).is_equal_to(expected);
|
||||
}
|
||||
}
|
||||
|
||||
// Frame 3: Release Left, no input remains => nothing emitted
|
||||
let events = process_simple_key_events(&mut bindings, &[SimpleKeyEvent::KeyUp(Keycode::Left)]);
|
||||
assert_that(&events.is_empty()).is_true();
|
||||
#[test]
|
||||
fn uses_vertical_when_dominant() {
|
||||
let test_cases = vec![
|
||||
(Vec2::new(3.0, 10.0), Direction::Down),
|
||||
(Vec2::new(3.0, -10.0), Direction::Up),
|
||||
];
|
||||
|
||||
for (delta, expected) in test_cases {
|
||||
assert_that(&calculate_direction_from_delta(delta)).is_equal_to(expected);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_zero_delta() {
|
||||
let delta = Vec2::ZERO;
|
||||
// Should default to Up when both components are zero
|
||||
assert_that(&calculate_direction_from_delta(delta)).is_equal_to(Direction::Up);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handles_equal_magnitudes() {
|
||||
// When x and y have equal absolute values, should prioritize vertical
|
||||
let delta = Vec2::new(5.0, 5.0);
|
||||
assert_that(&calculate_direction_from_delta(delta)).is_equal_to(Direction::Down);
|
||||
|
||||
let delta = Vec2::new(-5.0, 5.0);
|
||||
assert_that(&calculate_direction_from_delta(delta)).is_equal_to(Direction::Down);
|
||||
}
|
||||
}
|
||||
|
||||
mod touch_easing_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn easing_within_threshold_does_nothing() {
|
||||
let mut touch_data = TouchData::new(0, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(100.0 + TOUCH_EASING_DISTANCE_THRESHOLD - 0.1, 100.0);
|
||||
|
||||
let (_delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
assert_that(&distance).is_less_than(TOUCH_EASING_DISTANCE_THRESHOLD);
|
||||
assert_that(&touch_data.start_pos).is_equal_to(Vec2::new(100.0, 100.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn easing_beyond_threshold_moves_towards_target() {
|
||||
let mut touch_data = TouchData::new(0, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(150.0, 100.0);
|
||||
|
||||
let original_start_pos = touch_data.start_pos;
|
||||
let (_delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
assert_that(&distance).is_greater_than(TOUCH_EASING_DISTANCE_THRESHOLD);
|
||||
assert_that(&touch_data.start_pos.x).is_greater_than(original_start_pos.x);
|
||||
assert_that(&touch_data.start_pos.x).is_less_than(touch_data.current_pos.x);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn easing_overshoot_sets_to_target() {
|
||||
let mut touch_data = TouchData::new(0, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(101.0, 100.0);
|
||||
|
||||
let (_delta, _distance) = update_touch_reference_position(&mut touch_data, 10.0);
|
||||
|
||||
assert_that(&touch_data.start_pos).is_equal_to(touch_data.current_pos);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn easing_returns_correct_delta() {
|
||||
let mut touch_data = TouchData::new(0, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(120.0, 110.0);
|
||||
|
||||
let (delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
let expected_delta = Vec2::new(20.0, 10.0);
|
||||
let expected_distance = expected_delta.length();
|
||||
|
||||
assert_that(&delta).is_equal_to(expected_delta);
|
||||
assert_that(&distance).is_equal_to(expected_distance);
|
||||
}
|
||||
}
|
||||
|
||||
// Integration tests for the full input system
|
||||
mod integration_tests {
|
||||
use super::*;
|
||||
|
||||
fn mouse_motion_event(x: i32, y: i32) -> sdl2::event::Event {
|
||||
sdl2::event::Event::MouseMotion {
|
||||
x,
|
||||
y,
|
||||
xrel: 0,
|
||||
yrel: 0,
|
||||
mousestate: sdl2::mouse::MouseState::from_sdl_state(0),
|
||||
which: 0,
|
||||
window_id: 0,
|
||||
timestamp: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_button_down_event(x: i32, y: i32) -> sdl2::event::Event {
|
||||
sdl2::event::Event::MouseButtonDown {
|
||||
x,
|
||||
y,
|
||||
mouse_btn: sdl2::mouse::MouseButton::Left,
|
||||
clicks: 1,
|
||||
which: 0,
|
||||
window_id: 0,
|
||||
timestamp: 0,
|
||||
}
|
||||
}
|
||||
|
||||
fn mouse_button_up_event(x: i32, y: i32) -> sdl2::event::Event {
|
||||
sdl2::event::Event::MouseButtonUp {
|
||||
x,
|
||||
y,
|
||||
mouse_btn: sdl2::mouse::MouseButton::Left,
|
||||
clicks: 1,
|
||||
which: 0,
|
||||
window_id: 0,
|
||||
timestamp: 0,
|
||||
}
|
||||
}
|
||||
|
||||
// Simplified helper for testing SDL integration
|
||||
fn run_input_system_with_events(events: Vec<sdl2::event::Event>, delta_time: f32) -> (CursorPosition, TouchState) {
|
||||
use bevy_ecs::{event::Events, system::RunSystemOnce, world::World};
|
||||
use pacman::systems::components::DeltaTime;
|
||||
use pacman::systems::input::input_system;
|
||||
|
||||
let sdl_context = sdl2::init().expect("Failed to initialize SDL");
|
||||
let event_subsystem = sdl_context.event().expect("Failed to get event subsystem");
|
||||
let event_pump = sdl_context.event_pump().expect("Failed to create event pump");
|
||||
|
||||
let mut world = World::new();
|
||||
world.insert_resource(Events::<GameEvent>::default());
|
||||
world.insert_resource(DeltaTime {
|
||||
seconds: delta_time,
|
||||
ticks: 1,
|
||||
});
|
||||
world.insert_resource(Bindings::default());
|
||||
world.insert_resource(CursorPosition::None);
|
||||
world.insert_resource(TouchState::default());
|
||||
world.insert_non_send_resource(event_pump);
|
||||
|
||||
// Inject events into SDL's event queue
|
||||
for event in events {
|
||||
event_subsystem.push_event(event).expect("Failed to push event");
|
||||
}
|
||||
|
||||
// Run the real input system
|
||||
world
|
||||
.run_system_once(input_system)
|
||||
.expect("Input system should run successfully");
|
||||
|
||||
let cursor = *world.resource::<CursorPosition>();
|
||||
let touch_state = world.resource::<TouchState>().clone();
|
||||
|
||||
(cursor, touch_state)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_motion_updates_cursor_position() {
|
||||
let events = vec![mouse_motion_event(100, 200)];
|
||||
let (cursor, _touch_state) = run_input_system_with_events(events, 0.016);
|
||||
|
||||
match cursor {
|
||||
CursorPosition::Some {
|
||||
position,
|
||||
remaining_time,
|
||||
} => {
|
||||
assert_that(&position).is_equal_to(Vec2::new(100.0, 200.0));
|
||||
assert_that(&remaining_time).is_equal_to(0.20);
|
||||
}
|
||||
CursorPosition::None => panic!("Expected cursor position to be set"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_button_down_starts_touch() {
|
||||
let events = vec![mouse_button_down_event(150, 250)];
|
||||
let (_cursor, touch_state) = run_input_system_with_events(events, 0.016);
|
||||
|
||||
assert_that(&touch_state.active_touch).is_some();
|
||||
if let Some(touch_data) = &touch_state.active_touch {
|
||||
assert_that(&touch_data.finger_id).is_equal_to(0);
|
||||
assert_that(&touch_data.start_pos).is_equal_to(Vec2::new(150.0, 250.0));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mouse_button_up_ends_touch() {
|
||||
let events = vec![mouse_button_down_event(150, 250), mouse_button_up_event(150, 250)];
|
||||
let (_cursor, touch_state) = run_input_system_with_events(events, 0.016);
|
||||
|
||||
assert_that(&touch_state.active_touch).is_none();
|
||||
}
|
||||
}
|
||||
|
||||
// Touch direction tests
|
||||
mod touch_direction_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn movement_above_threshold_emits_direction() {
|
||||
let mut touch_data = TouchData::new(1, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(100.0 + TOUCH_DIRECTION_THRESHOLD + 5.0, 100.0);
|
||||
|
||||
let (delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
assert_that(&distance).is_greater_than_or_equal_to(TOUCH_DIRECTION_THRESHOLD);
|
||||
let direction = calculate_direction_from_delta(delta);
|
||||
assert_that(&direction).is_equal_to(Direction::Right);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn movement_below_threshold_no_direction() {
|
||||
let mut touch_data = TouchData::new(1, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(100.0 + TOUCH_DIRECTION_THRESHOLD - 1.0, 100.0);
|
||||
|
||||
let (_delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
assert_that(&distance).is_less_than(TOUCH_DIRECTION_THRESHOLD);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_directions_work_correctly() {
|
||||
let test_cases = vec![
|
||||
(Vec2::new(TOUCH_DIRECTION_THRESHOLD + 5.0, 0.0), Direction::Right),
|
||||
(Vec2::new(-TOUCH_DIRECTION_THRESHOLD - 5.0, 0.0), Direction::Left),
|
||||
(Vec2::new(0.0, TOUCH_DIRECTION_THRESHOLD + 5.0), Direction::Down),
|
||||
(Vec2::new(0.0, -TOUCH_DIRECTION_THRESHOLD - 5.0), Direction::Up),
|
||||
];
|
||||
|
||||
for (offset, expected_direction) in test_cases {
|
||||
let mut touch_data = TouchData::new(1, Vec2::new(100.0, 100.0));
|
||||
touch_data.current_pos = Vec2::new(100.0, 100.0) + offset;
|
||||
|
||||
let (delta, distance) = update_touch_reference_position(&mut touch_data, 0.016);
|
||||
|
||||
assert_that(&distance).is_greater_than_or_equal_to(TOUCH_DIRECTION_THRESHOLD);
|
||||
let direction = calculate_direction_from_delta(delta);
|
||||
assert_that(&direction).is_equal_to(expected_direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
use glam::Vec2;
|
||||
use pacman::constants::{CELL_SIZE, RAW_BOARD};
|
||||
use pacman::map::builder::Map;
|
||||
use pacman::map::graph::TraversalFlags;
|
||||
use speculoos::prelude::*;
|
||||
|
||||
#[test]
|
||||
fn test_map_creation() {
|
||||
fn test_map_creation_success() {
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
|
||||
assert_that(&map.graph.nodes().count()).is_greater_than(0);
|
||||
@@ -22,7 +23,7 @@ fn test_map_creation() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map_node_positions() {
|
||||
fn test_map_node_positions_accuracy() {
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
|
||||
for (grid_pos, &node_id) in &map.grid_to_node {
|
||||
@@ -35,3 +36,54 @@ fn test_map_node_positions() {
|
||||
assert_that(&node.position).is_equal_to(expected_pos);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_start_positions_are_valid() {
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
let positions = &map.start_positions;
|
||||
|
||||
// All start positions should exist in the graph
|
||||
assert_that(&map.graph.get_node(positions.pacman)).is_some();
|
||||
assert_that(&map.graph.get_node(positions.blinky)).is_some();
|
||||
assert_that(&map.graph.get_node(positions.pinky)).is_some();
|
||||
assert_that(&map.graph.get_node(positions.inky)).is_some();
|
||||
assert_that(&map.graph.get_node(positions.clyde)).is_some();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghost_house_has_ghost_only_entrance() {
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
|
||||
// Find the house entrance node
|
||||
let house_entrance = map.start_positions.blinky;
|
||||
|
||||
// Check that there's a ghost-only connection from the house entrance
|
||||
let mut has_ghost_only_connection = false;
|
||||
for edge in map.graph.adjacency_list[house_entrance as usize].edges() {
|
||||
if edge.traversal_flags == TraversalFlags::GHOST {
|
||||
has_ghost_only_connection = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_that(&has_ghost_only_connection).is_true();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunnel_connections_exist() {
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
|
||||
// Find tunnel nodes by looking for nodes with zero-distance connections
|
||||
let mut has_tunnel_connection = false;
|
||||
for intersection in &map.graph.adjacency_list {
|
||||
for edge in intersection.edges() {
|
||||
if edge.distance == 0.0f32 {
|
||||
has_tunnel_connection = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if has_tunnel_connection {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert_that(&has_tunnel_connection).is_true();
|
||||
}
|
||||
|
||||
@@ -73,25 +73,6 @@ fn test_default_zero_timing_for_unused_systems() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pre_populated_timing_entries() {
|
||||
let timings = SystemTimings::default();
|
||||
|
||||
// Verify that we can add timing to any SystemId without panicking
|
||||
// (this would fail with the old implementation if the entry didn't exist)
|
||||
// Use the same tick for all systems to avoid zero-padding
|
||||
for id in SystemId::iter() {
|
||||
timings.add_timing(id, Duration::from_nanos(1), 1);
|
||||
}
|
||||
|
||||
// Verify all systems now have non-zero timing
|
||||
let stats = timings.get_stats(1);
|
||||
for id in SystemId::iter() {
|
||||
let (avg, _) = stats.get(&id).unwrap();
|
||||
assert_that(&(*avg > Duration::ZERO)).is_true();
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_total_system_timing() {
|
||||
let timings = SystemTimings::default();
|
||||
|
||||
73
tests/sprites.rs
Normal file
73
tests/sprites.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
//! Tests for the sprite path generation.
|
||||
use pacman::{
|
||||
game::ATLAS_FRAMES,
|
||||
map::direction::Direction,
|
||||
systems::components::Ghost,
|
||||
texture::sprites::{FrightenedColor, GameSprite, GhostSprite, MazeSprite, PacmanSprite},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_all_sprite_paths_exist() {
|
||||
let mut sprites_to_test = Vec::new();
|
||||
|
||||
// Pac-Man sprites
|
||||
for &dir in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
||||
for frame in 0..2 {
|
||||
sprites_to_test.push(GameSprite::Pacman(PacmanSprite::Moving(dir, frame)));
|
||||
}
|
||||
}
|
||||
sprites_to_test.push(GameSprite::Pacman(PacmanSprite::Full));
|
||||
for frame in 0..=10 {
|
||||
sprites_to_test.push(GameSprite::Pacman(PacmanSprite::Dying(frame)));
|
||||
}
|
||||
|
||||
// Ghost sprites
|
||||
for &ghost in &[Ghost::Blinky, Ghost::Pinky, Ghost::Inky, Ghost::Clyde] {
|
||||
for &dir in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
||||
for frame in 0..2 {
|
||||
sprites_to_test.push(GameSprite::Ghost(GhostSprite::Normal(ghost, dir, frame)));
|
||||
}
|
||||
sprites_to_test.push(GameSprite::Ghost(GhostSprite::Eyes(dir)));
|
||||
}
|
||||
}
|
||||
for &color in &[FrightenedColor::Blue, FrightenedColor::White] {
|
||||
for frame in 0..2 {
|
||||
sprites_to_test.push(GameSprite::Ghost(GhostSprite::Frightened(color, frame)));
|
||||
}
|
||||
}
|
||||
|
||||
// Maze sprites
|
||||
for i in 0..=34 {
|
||||
sprites_to_test.push(GameSprite::Maze(MazeSprite::Tile(i)));
|
||||
}
|
||||
sprites_to_test.push(GameSprite::Maze(MazeSprite::Pellet));
|
||||
sprites_to_test.push(GameSprite::Maze(MazeSprite::Energizer));
|
||||
|
||||
for sprite in sprites_to_test {
|
||||
let path = sprite.to_path();
|
||||
assert!(
|
||||
ATLAS_FRAMES.contains_key(&path),
|
||||
"Sprite path '{}' does not exist in the atlas.",
|
||||
path
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_sprite_paths_do_not_exist() {
|
||||
let invalid_sprites = vec![
|
||||
// An invalid Pac-Man dying frame
|
||||
GameSprite::Pacman(PacmanSprite::Dying(99)),
|
||||
// An invalid maze tile
|
||||
GameSprite::Maze(MazeSprite::Tile(99)),
|
||||
];
|
||||
|
||||
for sprite in invalid_sprites {
|
||||
let path = sprite.to_path();
|
||||
assert!(
|
||||
!ATLAS_FRAMES.contains_key(&path),
|
||||
"Invalid sprite path '{}' was found in the atlas, but it should not exist.",
|
||||
path
|
||||
);
|
||||
}
|
||||
}
|
||||
115
tests/ttf.rs
Normal file
115
tests/ttf.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
use pacman::texture::ttf::{TtfAtlas, TtfRenderer};
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
mod common;
|
||||
|
||||
#[test]
|
||||
fn text_width_calculates_correctly_for_empty_string() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer = TtfRenderer::new(1.0);
|
||||
let width = renderer.text_width(&atlas, "");
|
||||
|
||||
assert_eq!(width, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_width_calculates_correctly_for_single_character() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer = TtfRenderer::new(1.0);
|
||||
let width = renderer.text_width(&atlas, "A");
|
||||
|
||||
assert!(width > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_width_scales_correctly() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer1 = TtfRenderer::new(1.0);
|
||||
let renderer2 = TtfRenderer::new(2.0);
|
||||
|
||||
let width1 = renderer1.text_width(&atlas, "Test");
|
||||
let width2 = renderer2.text_width(&atlas, "Test");
|
||||
|
||||
assert_eq!(width2, width1 * 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_height_returns_non_zero_for_valid_atlas() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer = TtfRenderer::new(1.0);
|
||||
let height = renderer.text_height(&atlas);
|
||||
|
||||
assert!(height > 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_height_scales_correctly() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer1 = TtfRenderer::new(1.0);
|
||||
let renderer2 = TtfRenderer::new(2.0);
|
||||
|
||||
let height1 = renderer1.text_height(&atlas);
|
||||
let height2 = renderer2.text_height(&atlas);
|
||||
|
||||
assert_eq!(height2, height1 * 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_text_handles_empty_string() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer = TtfRenderer::new(1.0);
|
||||
let result = renderer.render_text(&mut canvas, &mut atlas, "", glam::Vec2::new(0.0, 0.0), Color::WHITE);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_text_handles_single_character() {
|
||||
let (mut canvas, texture_creator, _sdl) = common::setup_sdl().unwrap();
|
||||
let _ttf_context = sdl2::ttf::init().unwrap();
|
||||
let font = _ttf_context.load_font("assets/game/TerminalVector.ttf", 16).unwrap();
|
||||
|
||||
let mut atlas = TtfAtlas::new(&texture_creator, &font).unwrap();
|
||||
atlas.populate_atlas(&mut canvas, &texture_creator, &font).unwrap();
|
||||
|
||||
let renderer = TtfRenderer::new(1.0);
|
||||
let result = renderer.render_text(&mut canvas, &mut atlas, "A", glam::Vec2::new(10.0, 10.0), Color::RED);
|
||||
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
Reference in New Issue
Block a user