mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 09:15:46 -06:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 730daed20a | |||
| b9bae99a4c | |||
| 2c65048fb0 | |||
| 3388d77ec5 | |||
| 242da2e263 | |||
| 70fb2b9503 | |||
| 0aa056a0ae | |||
| b270318640 | |||
| bc759f1ed4 | |||
| 2f1ff85d8f | |||
| b7429cd9ec | |||
| 12a63374a8 | |||
| d80d7061e7 | |||
| abdefe0af0 |
703
Cargo.lock
generated
703
Cargo.lock
generated
@@ -17,12 +17,202 @@ version = "1.0.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0674a1ddeecb70197781e945de4b3b8ffb61fa939a5597bcf48503737663100"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
|
||||
|
||||
[[package]]
|
||||
name = "assert_type_match"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f548ad2c4031f2902e3edc1f29c29e835829437de49562d8eb5dc5584d3a1043"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-executor"
|
||||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bb812ffb58524bdd10860d7d974e2f01cc0950c2438a74ee5ec2e2280c6c4ffa"
|
||||
dependencies = [
|
||||
"async-task",
|
||||
"concurrent-queue",
|
||||
"fastrand",
|
||||
"futures-lite",
|
||||
"pin-project-lite",
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-task"
|
||||
version = "4.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b75356056920673b02621b35afd0f7dda9306d03c79a30f5c56c44cf256e3de"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atomic-waker"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "bevy_ecs"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c2bf6521aae57a0ec3487c4bfb59e36c4a378e834b626a4bea6a885af2fdfe7"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"bevy_ecs_macros",
|
||||
"bevy_platform",
|
||||
"bevy_ptr",
|
||||
"bevy_reflect",
|
||||
"bevy_tasks",
|
||||
"bevy_utils",
|
||||
"bitflags 2.9.1",
|
||||
"bumpalo",
|
||||
"concurrent-queue",
|
||||
"derive_more",
|
||||
"disqualified",
|
||||
"fixedbitset",
|
||||
"indexmap",
|
||||
"log",
|
||||
"nonmax",
|
||||
"serde",
|
||||
"smallvec",
|
||||
"thiserror",
|
||||
"variadics_please",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_ecs_macros"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38748d6f3339175c582d751f410fb60a93baf2286c3deb7efebb0878dce7f413"
|
||||
dependencies = [
|
||||
"bevy_macro_utils",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_macro_utils"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "052eeebcb8e7e072beea5031b227d9a290f8a7fbbb947573ab6ec81df0fb94be"
|
||||
dependencies = [
|
||||
"parking_lot",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"toml_edit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_platform"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7573dc824a1b08b4c93fdbe421c53e1e8188e9ca1dd74a414455fe571facb47"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"critical-section",
|
||||
"foldhash",
|
||||
"hashbrown",
|
||||
"portable-atomic",
|
||||
"portable-atomic-util",
|
||||
"serde",
|
||||
"spin",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_ptr"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df7370d0e46b60e071917711d0860721f5347bc958bf325975ae6913a5dfcf01"
|
||||
|
||||
[[package]]
|
||||
name = "bevy_reflect"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daeb91a63a1a4df00aa58da8cc4ddbd4b9f16ab8bb647c5553eb156ce36fa8c2"
|
||||
dependencies = [
|
||||
"assert_type_match",
|
||||
"bevy_platform",
|
||||
"bevy_ptr",
|
||||
"bevy_reflect_derive",
|
||||
"bevy_utils",
|
||||
"derive_more",
|
||||
"disqualified",
|
||||
"downcast-rs",
|
||||
"erased-serde",
|
||||
"foldhash",
|
||||
"glam 0.29.3",
|
||||
"serde",
|
||||
"smallvec",
|
||||
"smol_str",
|
||||
"thiserror",
|
||||
"uuid",
|
||||
"variadics_please",
|
||||
"wgpu-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_reflect_derive"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40ddadc55fe16b45faaa54ab2f9cb00548013c74812e8b018aa172387103cce6"
|
||||
dependencies = [
|
||||
"bevy_macro_utils",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_tasks"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b674242641cab680688fc3b850243b351c1af49d4f3417a576debd6cca8dcf5"
|
||||
dependencies = [
|
||||
"async-executor",
|
||||
"async-task",
|
||||
"atomic-waker",
|
||||
"bevy_platform",
|
||||
"cfg-if",
|
||||
"crossbeam-queue",
|
||||
"derive_more",
|
||||
"futures-lite",
|
||||
"heapless",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bevy_utils"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94f7a8905a125d2017e8561beefb7f2f5e67e93ff6324f072ad87c5fd6ec3b99"
|
||||
dependencies = [
|
||||
"bevy_platform",
|
||||
"thread_local",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
@@ -34,6 +224,21 @@ name = "bitflags"
|
||||
version = "2.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bumpalo"
|
||||
version = "3.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "c_vec"
|
||||
@@ -47,6 +252,37 @@ version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "concurrent-queue"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "critical-section"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-queue"
|
||||
version = "0.3.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f58bbc28f91df819d0aa2a2c00cd19754769c2fad90579b3592b1c9ba7a3115"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
|
||||
|
||||
[[package]]
|
||||
name = "deprecate-until"
|
||||
version = "0.1.1"
|
||||
@@ -59,12 +295,98 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05"
|
||||
dependencies = [
|
||||
"derive_more-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more-impl"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "disqualified"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c9c272297e804878a2a4b707cfcfc6d2328b5bb936944613b4fdf2b9269afdfd"
|
||||
|
||||
[[package]]
|
||||
name = "downcast-rs"
|
||||
version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ea8a8b81cacc08888170eef4d13b775126db426d0b348bee9d18c2c1eaf123cf"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "erased-serde"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e004d887f51fcb9fef17317a2f3525c887d8aa3f4f50fed920816a688284a5b7"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"typeid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.5.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||
|
||||
[[package]]
|
||||
name = "futures-io"
|
||||
version = "0.3.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||
|
||||
[[package]]
|
||||
name = "futures-lite"
|
||||
version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f78e10609fe0e0b3f4157ffab1876319b5b0db102a2c60dc4626306dc46b44ad"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"parking",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.3.3"
|
||||
@@ -77,17 +399,50 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.29.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8babf46d4c1c9d92deac9f7be466f76dfc4482b6452fc5024b5e8daf6ffeb3ee"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "glam"
|
||||
version = "0.30.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2d1aab06663bdce00d6ca5e5ed586ec8d18033a771906c993a1e3755b368d85"
|
||||
|
||||
[[package]]
|
||||
name = "hash32"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5971ac85611da7067dbfcabef3c70ebb5606018acd9e2a3903a0da507521e0d5"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heapless"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad"
|
||||
dependencies = [
|
||||
"hash32",
|
||||
"portable-atomic",
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
@@ -120,6 +475,16 @@ version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
|
||||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
@@ -132,6 +497,16 @@ version = "0.2.175"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96936507f153605bddfcda068dd804796c84324ed2510809e5b2a624c81da765"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
@@ -153,6 +528,12 @@ version = "2.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a282da65faaf38286cf3be983213fcf1d2e2a58700e808f83f4ea9a4804bc0"
|
||||
|
||||
[[package]]
|
||||
name = "nonmax"
|
||||
version = "0.5.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "610a5acd306ec67f907abe5567859a3c693fb9886eb1f012ab8f2a47bef3db51"
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.46.0"
|
||||
@@ -189,7 +570,8 @@ name = "pacman"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"glam",
|
||||
"bevy_ecs",
|
||||
"glam 0.30.5",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"once_cell",
|
||||
@@ -210,6 +592,35 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking"
|
||||
version = "2.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70d58bf43669b5795d1576d0641cfb6fbb2057bf629506267a92807158584a13"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc838d2a56b5b1a6c25f55575dfc605fabb63bb2365f6c2353ef9159aa69e4a5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pathfinding"
|
||||
version = "4.14.0"
|
||||
@@ -272,6 +683,21 @@ version = "0.2.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic"
|
||||
version = "1.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f84267b20a16ea918e43c6a88433c2d54fa145c92a811b5b047ccbe153674483"
|
||||
|
||||
[[package]]
|
||||
name = "portable-atomic-util"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.95"
|
||||
@@ -329,6 +755,15 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.11.1"
|
||||
@@ -379,12 +814,24 @@ version = "2.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d"
|
||||
|
||||
[[package]]
|
||||
name = "rustversion"
|
||||
version = "1.0.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "sdl2"
|
||||
version = "0.38.0"
|
||||
@@ -463,12 +910,36 @@ version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589"
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.15.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03"
|
||||
|
||||
[[package]]
|
||||
name = "smol_str"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd538fb6910ac1099850255cf94a94df6551fbdd602454387d0adb2d1ca6dead"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
dependencies = [
|
||||
"portable-atomic",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin_sleep"
|
||||
version = "1.3.2"
|
||||
@@ -478,6 +949,12 @@ dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "stable_deref_trait"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.27.2"
|
||||
@@ -537,6 +1014,23 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"toml_datetime",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing"
|
||||
version = "0.1.41"
|
||||
@@ -608,18 +1102,53 @@ dependencies = [
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typeid"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.18.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f33196643e165781c20a5ead5582283a7dacbb87855d867fbc2df3f81eddc1be"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
|
||||
|
||||
[[package]]
|
||||
name = "variadics_please"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41b6d82be61465f97d42bd1d15bf20f3b0a3a0905018f38f9d6f6962055b0b5c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
@@ -641,6 +1170,87 @@ dependencies = [
|
||||
"wit-bindgen-rt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"once_cell",
|
||||
"rustversion",
|
||||
"wasm-bindgen-macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"wasm-bindgen-backend",
|
||||
"wasm-bindgen-shared",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.100"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.77"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wgpu-types"
|
||||
version = "24.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50ac044c0e76c03a0378e7786ac505d010a873665e2d51383dcff8dd227dc69c"
|
||||
dependencies = [
|
||||
"bitflags 2.9.1",
|
||||
"js-sys",
|
||||
"log",
|
||||
"serde",
|
||||
"web-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
@@ -669,7 +1279,23 @@ version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
"windows-targets 0.53.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.52.6",
|
||||
"windows_aarch64_msvc 0.52.6",
|
||||
"windows_i686_gnu 0.52.6",
|
||||
"windows_i686_gnullvm 0.52.6",
|
||||
"windows_i686_msvc 0.52.6",
|
||||
"windows_x86_64_gnu 0.52.6",
|
||||
"windows_x86_64_gnullvm 0.52.6",
|
||||
"windows_x86_64_msvc 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -678,64 +1304,121 @@ version = "0.53.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c66f69fcc9ce11da9966ddb31a40968cad001c5bedeb5c2b82ede4253ab48aef"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
"windows_aarch64_gnullvm 0.53.0",
|
||||
"windows_aarch64_msvc 0.53.0",
|
||||
"windows_i686_gnu 0.53.0",
|
||||
"windows_i686_gnullvm 0.53.0",
|
||||
"windows_i686_msvc 0.53.0",
|
||||
"windows_x86_64_gnu 0.53.0",
|
||||
"windows_x86_64_gnullvm 0.53.0",
|
||||
"windows_x86_64_msvc 0.53.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86b8d5f90ddd19cb4a147a5fa63ca848db3df085e25fee3cc10b39b6eebae764"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7651a1f62a11b8cbd5e0d42526e55f2c99886c77e007179efff86c2b137e66c"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1dc67659d35f387f5f6c479dc4e28f1d4bb90ddd1a5d3da2e5d97b42d6272c3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ce6ccbdedbf6d6354471319e781c0dfef054c81fbc7cf83f338a4296c0cae11"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "581fee95406bb13382d2f65cd4a908ca7b1e4c2f1917f143ba16efe98a589b5d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e55b5ac9ea33f2fc1716d1742db15574fd6fc8dadc51caab1c16a3d3b4190ba"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a6e035dd0599267ce1ee132e51c27dd29437f63325753051e71dd9e42406c57"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.53.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3edebf492c8125044983378ecb5766203ad3b4c2f7a922bd7dd207f6d443e95"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "wit-bindgen-rt"
|
||||
version = "0.39.0"
|
||||
|
||||
@@ -24,6 +24,7 @@ smallvec = "1.15.1"
|
||||
strum = "0.27.2"
|
||||
strum_macros = "0.27.2"
|
||||
phf = { version = "0.11", features = ["macros"] }
|
||||
bevy_ecs = "0.16.1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# Pac-Man
|
||||
|
||||
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![Code Coverage][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
||||
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![If you're seeing this, Coveralls.io is broken again and it's not my fault.][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
||||
|
||||
[badge-test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml/badge.svg
|
||||
[badge-build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml/badge.svg
|
||||
|
||||
144
src/app.rs
144
src/app.rs
@@ -1,13 +1,11 @@
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use glam::Vec2;
|
||||
use sdl2::event::{Event, WindowEvent};
|
||||
use sdl2::keyboard::Keycode;
|
||||
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
|
||||
use sdl2::render::TextureCreator;
|
||||
use sdl2::ttf::Sdl2TtfContext;
|
||||
use sdl2::video::{Window, WindowContext};
|
||||
use sdl2::video::WindowContext;
|
||||
use sdl2::{AudioSubsystem, EventPump, Sdl, VideoSubsystem};
|
||||
use tracing::{error, event};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::error::{GameError, GameResult};
|
||||
|
||||
@@ -16,13 +14,10 @@ use crate::game::Game;
|
||||
use crate::platform::get_platform;
|
||||
|
||||
pub struct App {
|
||||
game: Game,
|
||||
canvas: Canvas<Window>,
|
||||
event_pump: &'static mut EventPump,
|
||||
backbuffer: Texture<'static>,
|
||||
paused: bool,
|
||||
pub game: Game,
|
||||
last_tick: Instant,
|
||||
cursor_pos: Vec2,
|
||||
focused: bool,
|
||||
_cursor_pos: Vec2,
|
||||
}
|
||||
|
||||
impl App {
|
||||
@@ -51,41 +46,35 @@ impl App {
|
||||
.build()
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
|
||||
let mut canvas = window
|
||||
let canvas = Box::leak(Box::new(
|
||||
window
|
||||
.into_canvas()
|
||||
.accelerated()
|
||||
.present_vsync()
|
||||
.build()
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?,
|
||||
));
|
||||
|
||||
canvas
|
||||
.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
|
||||
let texture_creator: &'static TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
||||
let texture_creator: &'static mut TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
||||
|
||||
let mut game = Game::new(texture_creator)?;
|
||||
let game = Game::new(canvas, texture_creator, event_pump)?;
|
||||
// game.audio.set_mute(cfg!(debug_assertions));
|
||||
|
||||
let mut backbuffer = texture_creator
|
||||
.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
||||
|
||||
// Initial draw
|
||||
game.draw(&mut canvas, &mut backbuffer)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
game.present_backbuffer(&mut canvas, &backbuffer, glam::Vec2::ZERO)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
// game.draw(&mut canvas, &mut backbuffer)
|
||||
// .map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
// game.present_backbuffer(&mut canvas, &backbuffer, glam::Vec2::ZERO)
|
||||
// .map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
|
||||
Ok(Self {
|
||||
Ok(App {
|
||||
game,
|
||||
canvas,
|
||||
event_pump,
|
||||
backbuffer,
|
||||
paused: false,
|
||||
focused: true,
|
||||
last_tick: Instant::now(),
|
||||
cursor_pos: Vec2::ZERO,
|
||||
_cursor_pos: Vec2::ZERO,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -93,78 +82,51 @@ impl App {
|
||||
{
|
||||
let start = Instant::now();
|
||||
|
||||
for event in self.event_pump.poll_iter() {
|
||||
match event {
|
||||
Event::Window { win_event, .. } => match win_event {
|
||||
WindowEvent::Hidden => {
|
||||
event!(tracing::Level::DEBUG, "Window hidden");
|
||||
}
|
||||
WindowEvent::Shown => {
|
||||
event!(tracing::Level::DEBUG, "Window shown");
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
// It doesn't really make sense to have this available in the browser
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
Event::Quit { .. }
|
||||
| Event::KeyDown {
|
||||
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
|
||||
..
|
||||
} => {
|
||||
event!(tracing::Level::INFO, "Exit requested. Exiting...");
|
||||
return false;
|
||||
}
|
||||
Event::KeyDown {
|
||||
keycode: Some(Keycode::P),
|
||||
..
|
||||
} => {
|
||||
self.paused = !self.paused;
|
||||
event!(tracing::Level::INFO, "{}", if self.paused { "Paused" } else { "Unpaused" });
|
||||
}
|
||||
Event::KeyDown {
|
||||
keycode: Some(Keycode::Space),
|
||||
..
|
||||
} => {
|
||||
self.game.toggle_debug_mode();
|
||||
}
|
||||
Event::KeyDown { keycode: Some(key), .. } => {
|
||||
self.game.keyboard_event(key);
|
||||
}
|
||||
Event::MouseMotion { x, y, .. } => {
|
||||
// Convert window coordinates to logical coordinates
|
||||
self.cursor_pos = Vec2::new(x as f32, y as f32);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
// for event in self
|
||||
// .game
|
||||
// .world
|
||||
// .get_non_send_resource_mut::<&'static mut EventPump>()
|
||||
// .unwrap()
|
||||
// .poll_iter()
|
||||
// {
|
||||
// match event {
|
||||
// Event::Window { win_event, .. } => match win_event {
|
||||
// WindowEvent::FocusGained => {
|
||||
// self.focused = true;
|
||||
// }
|
||||
// WindowEvent::FocusLost => {
|
||||
// self.focused = false;
|
||||
// }
|
||||
// _ => {}
|
||||
// },
|
||||
// Event::MouseMotion { x, y, .. } => {
|
||||
// // Convert window coordinates to logical coordinates
|
||||
// self.cursor_pos = Vec2::new(x as f32, y as f32);
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
|
||||
let dt = self.last_tick.elapsed().as_secs_f32();
|
||||
self.last_tick = Instant::now();
|
||||
|
||||
if !self.paused {
|
||||
self.game.tick(dt);
|
||||
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
||||
error!("Failed to draw game: {}", e);
|
||||
}
|
||||
if let Err(e) = self
|
||||
.game
|
||||
.present_backbuffer(&mut self.canvas, &self.backbuffer, self.cursor_pos)
|
||||
{
|
||||
error!("Failed to present backbuffer: {}", e);
|
||||
}
|
||||
let exit = self.game.tick(dt);
|
||||
|
||||
if exit {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
||||
// error!("Failed to draw game: {}", e);
|
||||
// }
|
||||
|
||||
if start.elapsed() < LOOP_TIME {
|
||||
let time = LOOP_TIME.saturating_sub(start.elapsed());
|
||||
if time != Duration::ZERO {
|
||||
get_platform().sleep(time);
|
||||
get_platform().sleep(time, self.focused);
|
||||
}
|
||||
} else {
|
||||
event!(
|
||||
tracing::Level::WARN,
|
||||
"Game loop behind schedule by: {:?}",
|
||||
start.elapsed() - LOOP_TIME
|
||||
);
|
||||
warn!("Game loop behind schedule by: {:?}", start.elapsed() - LOOP_TIME);
|
||||
}
|
||||
|
||||
true
|
||||
|
||||
@@ -1,128 +1,128 @@
|
||||
use smallvec::SmallVec;
|
||||
use std::collections::HashMap;
|
||||
// use smallvec::SmallVec;
|
||||
// use std::collections::HashMap;
|
||||
|
||||
use crate::entity::{graph::NodeId, traversal::Position};
|
||||
// use crate::entity::{graph::NodeId, traversal::Position};
|
||||
|
||||
/// Trait for entities that can participate in collision detection.
|
||||
pub trait Collidable {
|
||||
/// Returns the current position of this entity.
|
||||
fn position(&self) -> Position;
|
||||
// /// Trait for entities that can participate in collision detection.
|
||||
// pub trait Collidable {
|
||||
// /// Returns the current position of this entity.
|
||||
// fn position(&self) -> Position;
|
||||
|
||||
/// Checks if this entity is colliding with another entity.
|
||||
#[allow(dead_code)]
|
||||
fn is_colliding_with(&self, other: &dyn Collidable) -> bool {
|
||||
positions_overlap(&self.position(), &other.position())
|
||||
}
|
||||
}
|
||||
// /// Checks if this entity is colliding with another entity.
|
||||
// #[allow(dead_code)]
|
||||
// fn is_colliding_with(&self, other: &dyn Collidable) -> bool {
|
||||
// positions_overlap(&self.position(), &other.position())
|
||||
// }
|
||||
// }
|
||||
|
||||
/// System for tracking entities by their positions for efficient collision detection.
|
||||
#[derive(Default)]
|
||||
pub struct CollisionSystem {
|
||||
/// Maps node IDs to lists of entity IDs that are at that node
|
||||
node_entities: HashMap<NodeId, Vec<EntityId>>,
|
||||
/// Maps entity IDs to their current positions
|
||||
entity_positions: HashMap<EntityId, Position>,
|
||||
/// Next available entity ID
|
||||
next_id: EntityId,
|
||||
}
|
||||
// /// System for tracking entities by their positions for efficient collision detection.
|
||||
// #[derive(Default)]
|
||||
// pub struct CollisionSystem {
|
||||
// /// Maps node IDs to lists of entity IDs that are at that node
|
||||
// node_entities: HashMap<NodeId, Vec<EntityId>>,
|
||||
// /// Maps entity IDs to their current positions
|
||||
// entity_positions: HashMap<EntityId, Position>,
|
||||
// /// Next available entity ID
|
||||
// next_id: EntityId,
|
||||
// }
|
||||
|
||||
/// Unique identifier for an entity in the collision system
|
||||
pub type EntityId = u32;
|
||||
// /// Unique identifier for an entity in the collision system
|
||||
// pub type EntityId = u32;
|
||||
|
||||
impl CollisionSystem {
|
||||
/// Registers an entity with the collision system and returns its ID
|
||||
pub fn register_entity(&mut self, position: Position) -> EntityId {
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
// impl CollisionSystem {
|
||||
// /// Registers an entity with the collision system and returns its ID
|
||||
// pub fn register_entity(&mut self, position: Position) -> EntityId {
|
||||
// let id = self.next_id;
|
||||
// self.next_id += 1;
|
||||
|
||||
self.entity_positions.insert(id, position);
|
||||
self.update_node_entities(id, position);
|
||||
// self.entity_positions.insert(id, position);
|
||||
// self.update_node_entities(id, position);
|
||||
|
||||
id
|
||||
}
|
||||
// id
|
||||
// }
|
||||
|
||||
/// Updates an entity's position
|
||||
pub fn update_position(&mut self, entity_id: EntityId, new_position: Position) {
|
||||
if let Some(old_position) = self.entity_positions.get(&entity_id) {
|
||||
// Remove from old nodes
|
||||
self.remove_from_nodes(entity_id, *old_position);
|
||||
}
|
||||
// /// Updates an entity's position
|
||||
// pub fn update_position(&mut self, entity_id: EntityId, new_position: Position) {
|
||||
// if let Some(old_position) = self.entity_positions.get(&entity_id) {
|
||||
// // Remove from old nodes
|
||||
// self.remove_from_nodes(entity_id, *old_position);
|
||||
// }
|
||||
|
||||
// Update position and add to new nodes
|
||||
self.entity_positions.insert(entity_id, new_position);
|
||||
self.update_node_entities(entity_id, new_position);
|
||||
}
|
||||
// // Update position and add to new nodes
|
||||
// self.entity_positions.insert(entity_id, new_position);
|
||||
// self.update_node_entities(entity_id, new_position);
|
||||
// }
|
||||
|
||||
/// Removes an entity from the collision system
|
||||
#[allow(dead_code)]
|
||||
pub fn remove_entity(&mut self, entity_id: EntityId) {
|
||||
if let Some(position) = self.entity_positions.remove(&entity_id) {
|
||||
self.remove_from_nodes(entity_id, position);
|
||||
}
|
||||
}
|
||||
// /// Removes an entity from the collision system
|
||||
// #[allow(dead_code)]
|
||||
// pub fn remove_entity(&mut self, entity_id: EntityId) {
|
||||
// if let Some(position) = self.entity_positions.remove(&entity_id) {
|
||||
// self.remove_from_nodes(entity_id, position);
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Gets all entity IDs at a specific node
|
||||
pub fn entities_at_node(&self, node: NodeId) -> &[EntityId] {
|
||||
self.node_entities.get(&node).map(|v| v.as_slice()).unwrap_or(&[])
|
||||
}
|
||||
// /// Gets all entity IDs at a specific node
|
||||
// pub fn entities_at_node(&self, node: NodeId) -> &[EntityId] {
|
||||
// self.node_entities.get(&node).map(|v| v.as_slice()).unwrap_or(&[])
|
||||
// }
|
||||
|
||||
/// Gets all entity IDs that could collide with an entity at the given position
|
||||
pub fn potential_collisions(&self, position: &Position) -> Vec<EntityId> {
|
||||
let mut collisions = Vec::new();
|
||||
let nodes = get_nodes(position);
|
||||
// /// Gets all entity IDs that could collide with an entity at the given position
|
||||
// pub fn potential_collisions(&self, position: &Position) -> Vec<EntityId> {
|
||||
// let mut collisions = Vec::new();
|
||||
// let nodes = get_nodes(position);
|
||||
|
||||
for node in nodes {
|
||||
collisions.extend(self.entities_at_node(node));
|
||||
}
|
||||
// for node in nodes {
|
||||
// collisions.extend(self.entities_at_node(node));
|
||||
// }
|
||||
|
||||
// Remove duplicates
|
||||
collisions.sort_unstable();
|
||||
collisions.dedup();
|
||||
collisions
|
||||
}
|
||||
// // Remove duplicates
|
||||
// collisions.sort_unstable();
|
||||
// collisions.dedup();
|
||||
// collisions
|
||||
// }
|
||||
|
||||
/// Updates the node_entities map when an entity's position changes
|
||||
fn update_node_entities(&mut self, entity_id: EntityId, position: Position) {
|
||||
let nodes = get_nodes(&position);
|
||||
for node in nodes {
|
||||
self.node_entities.entry(node).or_default().push(entity_id);
|
||||
}
|
||||
}
|
||||
// /// Updates the node_entities map when an entity's position changes
|
||||
// fn update_node_entities(&mut self, entity_id: EntityId, position: Position) {
|
||||
// let nodes = get_nodes(&position);
|
||||
// for node in nodes {
|
||||
// self.node_entities.entry(node).or_default().push(entity_id);
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Removes an entity from all nodes it was previously at
|
||||
fn remove_from_nodes(&mut self, entity_id: EntityId, position: Position) {
|
||||
let nodes = get_nodes(&position);
|
||||
for node in nodes {
|
||||
if let Some(entities) = self.node_entities.get_mut(&node) {
|
||||
entities.retain(|&id| id != entity_id);
|
||||
if entities.is_empty() {
|
||||
self.node_entities.remove(&node);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// Removes an entity from all nodes it was previously at
|
||||
// fn remove_from_nodes(&mut self, entity_id: EntityId, position: Position) {
|
||||
// let nodes = get_nodes(&position);
|
||||
// for node in nodes {
|
||||
// if let Some(entities) = self.node_entities.get_mut(&node) {
|
||||
// entities.retain(|&id| id != entity_id);
|
||||
// if entities.is_empty() {
|
||||
// self.node_entities.remove(&node);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Checks if two positions overlap (entities are at the same location).
|
||||
fn positions_overlap(a: &Position, b: &Position) -> bool {
|
||||
let a_nodes = get_nodes(a);
|
||||
let b_nodes = get_nodes(b);
|
||||
// /// Checks if two positions overlap (entities are at the same location).
|
||||
// fn positions_overlap(a: &Position, b: &Position) -> bool {
|
||||
// let a_nodes = get_nodes(a);
|
||||
// let b_nodes = get_nodes(b);
|
||||
|
||||
// Check if any nodes overlap
|
||||
a_nodes.iter().any(|a_node| b_nodes.contains(a_node))
|
||||
// // Check if any nodes overlap
|
||||
// a_nodes.iter().any(|a_node| b_nodes.contains(a_node))
|
||||
|
||||
// TODO: More complex overlap detection, the above is a simple check, but it could become an early filter for more precise calculations later
|
||||
}
|
||||
// // TODO: More complex overlap detection, the above is a simple check, but it could become an early filter for more precise calculations later
|
||||
// }
|
||||
|
||||
/// Gets all nodes that an entity is currently at or between.
|
||||
fn get_nodes(pos: &Position) -> SmallVec<[NodeId; 2]> {
|
||||
let mut nodes = SmallVec::new();
|
||||
match pos {
|
||||
Position::AtNode(node) => nodes.push(*node),
|
||||
Position::BetweenNodes { from, to, .. } => {
|
||||
nodes.push(*from);
|
||||
nodes.push(*to);
|
||||
}
|
||||
}
|
||||
nodes
|
||||
}
|
||||
// /// Gets all nodes that an entity is currently at or between.
|
||||
// fn get_nodes(pos: &Position) -> SmallVec<[NodeId; 2]> {
|
||||
// let mut nodes = SmallVec::new();
|
||||
// match pos {
|
||||
// Position::AtNode(node) => nodes.push(*node),
|
||||
// Position::BetweenNodes { from, to, .. } => {
|
||||
// nodes.push(*from);
|
||||
// nodes.push(*to);
|
||||
// }
|
||||
// }
|
||||
// nodes
|
||||
// }
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use glam::IVec2;
|
||||
|
||||
/// The four cardinal directions.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
|
||||
#[repr(usize)]
|
||||
pub enum Direction {
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
#[default]
|
||||
Right,
|
||||
}
|
||||
|
||||
|
||||
@@ -1,254 +1,254 @@
|
||||
//! Ghost entity implementation.
|
||||
//!
|
||||
//! This module contains the ghost character logic, including movement,
|
||||
//! animation, and rendering. Ghosts move through the game graph using
|
||||
//! a traverser and display directional animated textures.
|
||||
// //! Ghost entity implementation.
|
||||
// //!
|
||||
// //! This module contains the ghost character logic, including movement,
|
||||
// //! animation, and rendering. Ghosts move through the game graph using
|
||||
// //! a traverser and display directional animated textures.
|
||||
|
||||
use pathfinding::prelude::dijkstra;
|
||||
use rand::prelude::*;
|
||||
use smallvec::SmallVec;
|
||||
use tracing::error;
|
||||
// use pathfinding::prelude::dijkstra;
|
||||
// use rand::prelude::*;
|
||||
// use smallvec::SmallVec;
|
||||
// use tracing::error;
|
||||
|
||||
use crate::entity::{
|
||||
collision::Collidable,
|
||||
direction::Direction,
|
||||
graph::{Edge, EdgePermissions, Graph, NodeId},
|
||||
r#trait::Entity,
|
||||
traversal::Traverser,
|
||||
};
|
||||
use crate::texture::animated::AnimatedTexture;
|
||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
// use crate::entity::{
|
||||
// collision::Collidable,
|
||||
// direction::Direction,
|
||||
// graph::{Edge, EdgePermissions, Graph, NodeId},
|
||||
// r#trait::Entity,
|
||||
// traversal::Traverser,
|
||||
// };
|
||||
// use crate::texture::animated::AnimatedTexture;
|
||||
// use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
// use crate::texture::sprite::SpriteAtlas;
|
||||
|
||||
use crate::error::{EntityError, GameError, GameResult, TextureError};
|
||||
// use crate::error::{EntityError, GameError, GameResult, TextureError};
|
||||
|
||||
/// Determines if a ghost can traverse a given edge.
|
||||
///
|
||||
/// Ghosts can move through edges that allow all entities or ghost-only edges.
|
||||
fn can_ghost_traverse(edge: Edge) -> bool {
|
||||
matches!(edge.permissions, EdgePermissions::All | EdgePermissions::GhostsOnly)
|
||||
}
|
||||
// /// Determines if a ghost can traverse a given edge.
|
||||
// ///
|
||||
// /// Ghosts can move through edges that allow all entities or ghost-only edges.
|
||||
// fn can_ghost_traverse(edge: Edge) -> bool {
|
||||
// matches!(edge.permissions, EdgePermissions::All | EdgePermissions::GhostsOnly)
|
||||
// }
|
||||
|
||||
/// The four classic ghost types.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum GhostType {
|
||||
Blinky,
|
||||
Pinky,
|
||||
Inky,
|
||||
Clyde,
|
||||
}
|
||||
// /// The four classic ghost types.
|
||||
// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
// pub enum GhostType {
|
||||
// Blinky,
|
||||
// Pinky,
|
||||
// Inky,
|
||||
// Clyde,
|
||||
// }
|
||||
|
||||
impl GhostType {
|
||||
/// Returns the ghost type name for atlas lookups.
|
||||
pub fn as_str(self) -> &'static str {
|
||||
match self {
|
||||
GhostType::Blinky => "blinky",
|
||||
GhostType::Pinky => "pinky",
|
||||
GhostType::Inky => "inky",
|
||||
GhostType::Clyde => "clyde",
|
||||
}
|
||||
}
|
||||
// impl GhostType {
|
||||
// /// Returns the ghost type name for atlas lookups.
|
||||
// pub fn as_str(self) -> &'static str {
|
||||
// match self {
|
||||
// GhostType::Blinky => "blinky",
|
||||
// GhostType::Pinky => "pinky",
|
||||
// GhostType::Inky => "inky",
|
||||
// GhostType::Clyde => "clyde",
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Returns the base movement speed for this ghost type.
|
||||
pub fn base_speed(self) -> f32 {
|
||||
match self {
|
||||
GhostType::Blinky => 1.0,
|
||||
GhostType::Pinky => 0.95,
|
||||
GhostType::Inky => 0.9,
|
||||
GhostType::Clyde => 0.85,
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// Returns the base movement speed for this ghost type.
|
||||
// pub fn base_speed(self) -> f32 {
|
||||
// match self {
|
||||
// GhostType::Blinky => 1.0,
|
||||
// GhostType::Pinky => 0.95,
|
||||
// GhostType::Inky => 0.9,
|
||||
// GhostType::Clyde => 0.85,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// A ghost entity that roams the game world.
|
||||
///
|
||||
/// Ghosts move through the game world using a graph-based navigation system
|
||||
/// and display directional animated sprites. They randomly choose directions
|
||||
/// at each intersection.
|
||||
pub struct Ghost {
|
||||
/// Handles movement through the game graph
|
||||
pub traverser: Traverser,
|
||||
/// The type of ghost (affects appearance and speed)
|
||||
pub ghost_type: GhostType,
|
||||
/// Manages directional animated textures for different movement states
|
||||
texture: DirectionalAnimatedTexture,
|
||||
/// Current movement speed
|
||||
speed: f32,
|
||||
}
|
||||
// /// A ghost entity that roams the game world.
|
||||
// ///
|
||||
// /// Ghosts move through the game world using a graph-based navigation system
|
||||
// /// and display directional animated sprites. They randomly choose directions
|
||||
// /// at each intersection.
|
||||
// pub struct Ghost {
|
||||
// /// Handles movement through the game graph
|
||||
// pub traverser: Traverser,
|
||||
// /// The type of ghost (affects appearance and speed)
|
||||
// pub ghost_type: GhostType,
|
||||
// /// Manages directional animated textures for different movement states
|
||||
// texture: DirectionalAnimatedTexture,
|
||||
// /// Current movement speed
|
||||
// speed: f32,
|
||||
// }
|
||||
|
||||
impl Entity for Ghost {
|
||||
fn traverser(&self) -> &Traverser {
|
||||
&self.traverser
|
||||
}
|
||||
// impl Entity for Ghost {
|
||||
// fn traverser(&self) -> &Traverser {
|
||||
// &self.traverser
|
||||
// }
|
||||
|
||||
fn traverser_mut(&mut self) -> &mut Traverser {
|
||||
&mut self.traverser
|
||||
}
|
||||
// fn traverser_mut(&mut self) -> &mut Traverser {
|
||||
// &mut self.traverser
|
||||
// }
|
||||
|
||||
fn texture(&self) -> &DirectionalAnimatedTexture {
|
||||
&self.texture
|
||||
}
|
||||
// fn texture(&self) -> &DirectionalAnimatedTexture {
|
||||
// &self.texture
|
||||
// }
|
||||
|
||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
||||
&mut self.texture
|
||||
}
|
||||
// fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
||||
// &mut self.texture
|
||||
// }
|
||||
|
||||
fn speed(&self) -> f32 {
|
||||
self.speed
|
||||
}
|
||||
// fn speed(&self) -> f32 {
|
||||
// self.speed
|
||||
// }
|
||||
|
||||
fn can_traverse(&self, edge: Edge) -> bool {
|
||||
can_ghost_traverse(edge)
|
||||
}
|
||||
// fn can_traverse(&self, edge: Edge) -> bool {
|
||||
// can_ghost_traverse(edge)
|
||||
// }
|
||||
|
||||
fn tick(&mut self, dt: f32, graph: &Graph) {
|
||||
// Choose random direction when at a node
|
||||
if self.traverser.position.is_at_node() {
|
||||
self.choose_random_direction(graph);
|
||||
}
|
||||
// fn tick(&mut self, dt: f32, graph: &Graph) {
|
||||
// // Choose random direction when at a node
|
||||
// if self.traverser.position.is_at_node() {
|
||||
// self.choose_random_direction(graph);
|
||||
// }
|
||||
|
||||
if let Err(e) = self.traverser.advance(graph, dt * 60.0 * self.speed, &can_ghost_traverse) {
|
||||
error!("Ghost movement error: {}", e);
|
||||
}
|
||||
self.texture.tick(dt);
|
||||
}
|
||||
}
|
||||
// if let Err(e) = self.traverser.advance(graph, dt * 60.0 * self.speed, &can_ghost_traverse) {
|
||||
// error!("Ghost movement error: {}", e);
|
||||
// }
|
||||
// self.texture.tick(dt);
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Ghost {
|
||||
/// Creates a new ghost instance at the specified starting node.
|
||||
///
|
||||
/// Sets up animated textures for all four directions with moving and stopped states.
|
||||
/// The moving animation cycles through two sprite variants.
|
||||
pub fn new(graph: &Graph, start_node: NodeId, ghost_type: GhostType, atlas: &SpriteAtlas) -> GameResult<Self> {
|
||||
let mut textures = [None, None, None, None];
|
||||
let mut stopped_textures = [None, None, None, None];
|
||||
// impl Ghost {
|
||||
// /// Creates a new ghost instance at the specified starting node.
|
||||
// ///
|
||||
// /// Sets up animated textures for all four directions with moving and stopped states.
|
||||
// /// The moving animation cycles through two sprite variants.
|
||||
// pub fn new(graph: &Graph, start_node: NodeId, ghost_type: GhostType, atlas: &SpriteAtlas) -> GameResult<Self> {
|
||||
// let mut textures = [None, None, None, None];
|
||||
// let mut stopped_textures = [None, None, None, None];
|
||||
|
||||
for direction in Direction::DIRECTIONS {
|
||||
let moving_prefix = match direction {
|
||||
Direction::Up => "up",
|
||||
Direction::Down => "down",
|
||||
Direction::Left => "left",
|
||||
Direction::Right => "right",
|
||||
};
|
||||
let moving_tiles = vec![
|
||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
||||
.ok_or_else(|| {
|
||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
"ghost/{}/{}_{}.png",
|
||||
ghost_type.as_str(),
|
||||
moving_prefix,
|
||||
"a"
|
||||
)))
|
||||
})?,
|
||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "b"))
|
||||
.ok_or_else(|| {
|
||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
"ghost/{}/{}_{}.png",
|
||||
ghost_type.as_str(),
|
||||
moving_prefix,
|
||||
"b"
|
||||
)))
|
||||
})?,
|
||||
];
|
||||
// for direction in Direction::DIRECTIONS {
|
||||
// let moving_prefix = match direction {
|
||||
// Direction::Up => "up",
|
||||
// Direction::Down => "down",
|
||||
// Direction::Left => "left",
|
||||
// Direction::Right => "right",
|
||||
// };
|
||||
// let moving_tiles = vec![
|
||||
// SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
||||
// .ok_or_else(|| {
|
||||
// GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
// "ghost/{}/{}_{}.png",
|
||||
// ghost_type.as_str(),
|
||||
// moving_prefix,
|
||||
// "a"
|
||||
// )))
|
||||
// })?,
|
||||
// SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "b"))
|
||||
// .ok_or_else(|| {
|
||||
// GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
// "ghost/{}/{}_{}.png",
|
||||
// ghost_type.as_str(),
|
||||
// moving_prefix,
|
||||
// "b"
|
||||
// )))
|
||||
// })?,
|
||||
// ];
|
||||
|
||||
let stopped_tiles =
|
||||
vec![
|
||||
SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
||||
.ok_or_else(|| {
|
||||
GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
"ghost/{}/{}_{}.png",
|
||||
ghost_type.as_str(),
|
||||
moving_prefix,
|
||||
"a"
|
||||
)))
|
||||
})?,
|
||||
];
|
||||
// let stopped_tiles =
|
||||
// vec![
|
||||
// SpriteAtlas::get_tile(atlas, &format!("ghost/{}/{}_{}.png", ghost_type.as_str(), moving_prefix, "a"))
|
||||
// .ok_or_else(|| {
|
||||
// GameError::Texture(TextureError::AtlasTileNotFound(format!(
|
||||
// "ghost/{}/{}_{}.png",
|
||||
// ghost_type.as_str(),
|
||||
// moving_prefix,
|
||||
// "a"
|
||||
// )))
|
||||
// })?,
|
||||
// ];
|
||||
|
||||
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.2)?);
|
||||
stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
||||
}
|
||||
// textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.2)?);
|
||||
// stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
||||
// }
|
||||
|
||||
Ok(Self {
|
||||
traverser: Traverser::new(graph, start_node, Direction::Left, &can_ghost_traverse),
|
||||
ghost_type,
|
||||
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
||||
speed: ghost_type.base_speed(),
|
||||
})
|
||||
}
|
||||
// Ok(Self {
|
||||
// traverser: Traverser::new(graph, start_node, Direction::Left, &can_ghost_traverse),
|
||||
// ghost_type,
|
||||
// texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
||||
// speed: ghost_type.base_speed(),
|
||||
// })
|
||||
// }
|
||||
|
||||
/// Chooses a random available direction at the current intersection.
|
||||
fn choose_random_direction(&mut self, graph: &Graph) {
|
||||
let current_node = self.traverser.position.from_node_id();
|
||||
let intersection = &graph.adjacency_list[current_node];
|
||||
// /// Chooses a random available direction at the current intersection.
|
||||
// fn choose_random_direction(&mut self, graph: &Graph) {
|
||||
// let current_node = self.traverser.position.from_node_id();
|
||||
// let intersection = &graph.adjacency_list[current_node];
|
||||
|
||||
// Collect all available directions
|
||||
let mut available_directions = SmallVec::<[_; 4]>::new();
|
||||
for direction in Direction::DIRECTIONS {
|
||||
if let Some(edge) = intersection.get(direction) {
|
||||
if can_ghost_traverse(edge) {
|
||||
available_directions.push(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Choose a random direction (avoid reversing unless necessary)
|
||||
if !available_directions.is_empty() {
|
||||
let mut rng = SmallRng::from_os_rng();
|
||||
// // Collect all available directions
|
||||
// let mut available_directions = SmallVec::<[_; 4]>::new();
|
||||
// for direction in Direction::DIRECTIONS {
|
||||
// if let Some(edge) = intersection.get(direction) {
|
||||
// if can_ghost_traverse(edge) {
|
||||
// available_directions.push(direction);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// // Choose a random direction (avoid reversing unless necessary)
|
||||
// if !available_directions.is_empty() {
|
||||
// let mut rng = SmallRng::from_os_rng();
|
||||
|
||||
// Filter out the opposite direction if possible, but allow it if we have limited options
|
||||
let opposite = self.traverser.direction.opposite();
|
||||
let filtered_directions: Vec<_> = available_directions
|
||||
.iter()
|
||||
.filter(|&&dir| dir != opposite || available_directions.len() <= 2)
|
||||
.collect();
|
||||
// // Filter out the opposite direction if possible, but allow it if we have limited options
|
||||
// let opposite = self.traverser.direction.opposite();
|
||||
// let filtered_directions: Vec<_> = available_directions
|
||||
// .iter()
|
||||
// .filter(|&&dir| dir != opposite || available_directions.len() <= 2)
|
||||
// .collect();
|
||||
|
||||
if let Some(&random_direction) = filtered_directions.choose(&mut rng) {
|
||||
self.traverser.set_next_direction(*random_direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
// if let Some(&random_direction) = filtered_directions.choose(&mut rng) {
|
||||
// self.traverser.set_next_direction(*random_direction);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Calculates the shortest path from the ghost's current position to a target node using Dijkstra's algorithm.
|
||||
///
|
||||
/// Returns a vector of NodeIds representing the path, or an error if pathfinding fails.
|
||||
/// The path includes the current node and the target node.
|
||||
pub fn calculate_path_to_target(&self, graph: &Graph, target: NodeId) -> GameResult<Vec<NodeId>> {
|
||||
let start_node = self.traverser.position.from_node_id();
|
||||
// /// Calculates the shortest path from the ghost's current position to a target node using Dijkstra's algorithm.
|
||||
// ///
|
||||
// /// Returns a vector of NodeIds representing the path, or an error if pathfinding fails.
|
||||
// /// The path includes the current node and the target node.
|
||||
// pub fn calculate_path_to_target(&self, graph: &Graph, target: NodeId) -> GameResult<Vec<NodeId>> {
|
||||
// let start_node = self.traverser.position.from_node_id();
|
||||
|
||||
// Use Dijkstra's algorithm to find the shortest path
|
||||
let result = dijkstra(
|
||||
&start_node,
|
||||
|&node_id| {
|
||||
// Get all edges from the current node
|
||||
graph.adjacency_list[node_id]
|
||||
.edges()
|
||||
.filter(|edge| can_ghost_traverse(*edge))
|
||||
.map(|edge| (edge.target, (edge.distance * 100.0) as u32))
|
||||
.collect::<Vec<_>>()
|
||||
},
|
||||
|&node_id| node_id == target,
|
||||
);
|
||||
// // Use Dijkstra's algorithm to find the shortest path
|
||||
// let result = dijkstra(
|
||||
// &start_node,
|
||||
// |&node_id| {
|
||||
// // Get all edges from the current node
|
||||
// graph.adjacency_list[node_id]
|
||||
// .edges()
|
||||
// .filter(|edge| can_ghost_traverse(*edge))
|
||||
// .map(|edge| (edge.target, (edge.distance * 100.0) as u32))
|
||||
// .collect::<Vec<_>>()
|
||||
// },
|
||||
// |&node_id| node_id == target,
|
||||
// );
|
||||
|
||||
result.map(|(path, _cost)| path).ok_or_else(|| {
|
||||
GameError::Entity(EntityError::PathfindingFailed(format!(
|
||||
"No path found from node {} to target {}",
|
||||
start_node, target
|
||||
)))
|
||||
})
|
||||
}
|
||||
// result.map(|(path, _cost)| path).ok_or_else(|| {
|
||||
// GameError::Entity(EntityError::PathfindingFailed(format!(
|
||||
// "No path found from node {} to target {}",
|
||||
// start_node, target
|
||||
// )))
|
||||
// })
|
||||
// }
|
||||
|
||||
/// Returns the ghost's color for debug rendering.
|
||||
pub fn debug_color(&self) -> sdl2::pixels::Color {
|
||||
match self.ghost_type {
|
||||
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
|
||||
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
|
||||
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
|
||||
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
|
||||
}
|
||||
}
|
||||
}
|
||||
// /// Returns the ghost's color for debug rendering.
|
||||
// pub fn debug_color(&self) -> sdl2::pixels::Color {
|
||||
// match self.ghost_type {
|
||||
// GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
|
||||
// GhostType::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
|
||||
// GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
|
||||
// GhostType::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Collidable for Ghost {
|
||||
fn position(&self) -> crate::entity::traversal::Position {
|
||||
self.traverser.position
|
||||
}
|
||||
}
|
||||
// impl Collidable for Ghost {
|
||||
// fn position(&self) -> crate::entity::traversal::Position {
|
||||
// self.traverser.position
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
use glam::Vec2;
|
||||
|
||||
use super::direction::Direction;
|
||||
use crate::systems::components::NodeId;
|
||||
|
||||
/// A unique identifier for a node, represented by its index in the graph's storage.
|
||||
pub type NodeId = usize;
|
||||
use super::direction::Direction;
|
||||
|
||||
/// Defines who can traverse a given edge.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
|
||||
@@ -1,117 +1,117 @@
|
||||
use crate::{
|
||||
constants,
|
||||
entity::{collision::Collidable, graph::Graph},
|
||||
error::{EntityError, GameResult},
|
||||
texture::sprite::{Sprite, SpriteAtlas},
|
||||
};
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
use strum_macros::{EnumCount, EnumIter};
|
||||
// use crate::{
|
||||
// constants,
|
||||
// entity::{collision::Collidable, graph::Graph},
|
||||
// error::{EntityError, GameResult},
|
||||
// texture::sprite::{Sprite, SpriteAtlas},
|
||||
// };
|
||||
// use sdl2::render::{Canvas, RenderTarget};
|
||||
// use strum_macros::{EnumCount, EnumIter};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ItemType {
|
||||
Pellet,
|
||||
Energizer,
|
||||
#[allow(dead_code)]
|
||||
Fruit {
|
||||
kind: FruitKind,
|
||||
},
|
||||
}
|
||||
// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
// pub enum ItemType {
|
||||
// Pellet,
|
||||
// Energizer,
|
||||
// #[allow(dead_code)]
|
||||
// Fruit {
|
||||
// kind: FruitKind,
|
||||
// },
|
||||
// }
|
||||
|
||||
impl ItemType {
|
||||
pub fn get_score(self) -> u32 {
|
||||
match self {
|
||||
ItemType::Pellet => 10,
|
||||
ItemType::Energizer => 50,
|
||||
ItemType::Fruit { kind } => kind.get_score(),
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl ItemType {
|
||||
// pub fn get_score(self) -> u32 {
|
||||
// match self {
|
||||
// ItemType::Pellet => 10,
|
||||
// ItemType::Energizer => 50,
|
||||
// ItemType::Fruit { kind } => kind.get_score(),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, EnumCount)]
|
||||
#[allow(dead_code)]
|
||||
pub enum FruitKind {
|
||||
Apple,
|
||||
Strawberry,
|
||||
Orange,
|
||||
Melon,
|
||||
Bell,
|
||||
Key,
|
||||
Galaxian,
|
||||
}
|
||||
// #[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, EnumCount)]
|
||||
// #[allow(dead_code)]
|
||||
// pub enum FruitKind {
|
||||
// Apple,
|
||||
// Strawberry,
|
||||
// Orange,
|
||||
// Melon,
|
||||
// Bell,
|
||||
// Key,
|
||||
// Galaxian,
|
||||
// }
|
||||
|
||||
impl FruitKind {
|
||||
#[allow(dead_code)]
|
||||
pub fn index(self) -> u8 {
|
||||
match self {
|
||||
FruitKind::Apple => 0,
|
||||
FruitKind::Strawberry => 1,
|
||||
FruitKind::Orange => 2,
|
||||
FruitKind::Melon => 3,
|
||||
FruitKind::Bell => 4,
|
||||
FruitKind::Key => 5,
|
||||
FruitKind::Galaxian => 6,
|
||||
}
|
||||
}
|
||||
// impl FruitKind {
|
||||
// #[allow(dead_code)]
|
||||
// pub fn index(self) -> u8 {
|
||||
// match self {
|
||||
// FruitKind::Apple => 0,
|
||||
// FruitKind::Strawberry => 1,
|
||||
// FruitKind::Orange => 2,
|
||||
// FruitKind::Melon => 3,
|
||||
// FruitKind::Bell => 4,
|
||||
// FruitKind::Key => 5,
|
||||
// FruitKind::Galaxian => 6,
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn get_score(self) -> u32 {
|
||||
match self {
|
||||
FruitKind::Apple => 100,
|
||||
FruitKind::Strawberry => 300,
|
||||
FruitKind::Orange => 500,
|
||||
FruitKind::Melon => 700,
|
||||
FruitKind::Bell => 1000,
|
||||
FruitKind::Key => 2000,
|
||||
FruitKind::Galaxian => 3000,
|
||||
}
|
||||
}
|
||||
}
|
||||
// pub fn get_score(self) -> u32 {
|
||||
// match self {
|
||||
// FruitKind::Apple => 100,
|
||||
// FruitKind::Strawberry => 300,
|
||||
// FruitKind::Orange => 500,
|
||||
// FruitKind::Melon => 700,
|
||||
// FruitKind::Bell => 1000,
|
||||
// FruitKind::Key => 2000,
|
||||
// FruitKind::Galaxian => 3000,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
pub struct Item {
|
||||
pub node_index: usize,
|
||||
pub item_type: ItemType,
|
||||
pub sprite: Sprite,
|
||||
pub collected: bool,
|
||||
}
|
||||
// pub struct Item {
|
||||
// pub node_index: usize,
|
||||
// pub item_type: ItemType,
|
||||
// pub sprite: Sprite,
|
||||
// pub collected: bool,
|
||||
// }
|
||||
|
||||
impl Item {
|
||||
pub fn new(node_index: usize, item_type: ItemType, sprite: Sprite) -> Self {
|
||||
Self {
|
||||
node_index,
|
||||
item_type,
|
||||
sprite,
|
||||
collected: false,
|
||||
}
|
||||
}
|
||||
// impl Item {
|
||||
// pub fn new(node_index: usize, item_type: ItemType, sprite: Sprite) -> Self {
|
||||
// Self {
|
||||
// node_index,
|
||||
// item_type,
|
||||
// sprite,
|
||||
// collected: false,
|
||||
// }
|
||||
// }
|
||||
|
||||
pub fn is_collected(&self) -> bool {
|
||||
self.collected
|
||||
}
|
||||
// pub fn is_collected(&self) -> bool {
|
||||
// self.collected
|
||||
// }
|
||||
|
||||
pub fn collect(&mut self) {
|
||||
self.collected = true;
|
||||
}
|
||||
// pub fn collect(&mut self) {
|
||||
// self.collected = true;
|
||||
// }
|
||||
|
||||
pub fn get_score(&self) -> u32 {
|
||||
self.item_type.get_score()
|
||||
}
|
||||
// pub fn get_score(&self) -> u32 {
|
||||
// self.item_type.get_score()
|
||||
// }
|
||||
|
||||
pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
||||
if self.collected {
|
||||
return Ok(());
|
||||
}
|
||||
// pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
||||
// if self.collected {
|
||||
// return Ok(());
|
||||
// }
|
||||
|
||||
let node = graph
|
||||
.get_node(self.node_index)
|
||||
.ok_or(EntityError::NodeNotFound(self.node_index))?;
|
||||
let position = node.position + constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||
// let node = graph
|
||||
// .get_node(self.node_index)
|
||||
// .ok_or(EntityError::NodeNotFound(self.node_index))?;
|
||||
// let position = node.position + constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||
|
||||
self.sprite.render(canvas, atlas, position)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
// self.sprite.render(canvas, atlas, position)?;
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Collidable for Item {
|
||||
fn position(&self) -> crate::entity::traversal::Position {
|
||||
crate::entity::traversal::Position::AtNode(self.node_index)
|
||||
}
|
||||
}
|
||||
// impl Collidable for Item {
|
||||
// fn position(&self) -> crate::entity::traversal::Position {
|
||||
// crate::entity::traversal::Position::AtNode(self.node_index)
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -5,4 +5,3 @@ pub mod graph;
|
||||
pub mod item;
|
||||
pub mod pacman;
|
||||
pub mod r#trait;
|
||||
pub mod traversal;
|
||||
|
||||
@@ -1,134 +1,115 @@
|
||||
//! Pac-Man entity implementation.
|
||||
//!
|
||||
//! This module contains the main player character logic, including movement,
|
||||
//! animation, and rendering. Pac-Man moves through the game graph using
|
||||
//! a traverser and displays directional animated textures.
|
||||
// //! Pac-Man entity implementation.
|
||||
// //!
|
||||
// //! This module contains the main player character logic, including movement,
|
||||
// //! animation, and rendering. Pac-Man moves through the game graph using
|
||||
// //! a traverser and displays directional animated textures.
|
||||
|
||||
use crate::entity::{
|
||||
collision::Collidable,
|
||||
direction::Direction,
|
||||
graph::{Edge, EdgePermissions, Graph, NodeId},
|
||||
r#trait::Entity,
|
||||
traversal::Traverser,
|
||||
};
|
||||
use crate::texture::animated::AnimatedTexture;
|
||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
use sdl2::keyboard::Keycode;
|
||||
use tracing::error;
|
||||
// use crate::entity::{
|
||||
// collision::Collidable,
|
||||
// direction::Direction,
|
||||
// graph::{Edge, EdgePermissions, Graph, NodeId},
|
||||
// r#trait::Entity,
|
||||
// traversal::Traverser,
|
||||
// };
|
||||
// use crate::texture::animated::AnimatedTexture;
|
||||
// use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
// use crate::texture::sprite::SpriteAtlas;
|
||||
// use tracing::error;
|
||||
|
||||
use crate::error::{GameError, GameResult, TextureError};
|
||||
// use crate::error::{GameError, GameResult, TextureError};
|
||||
|
||||
/// Determines if Pac-Man can traverse a given edge.
|
||||
///
|
||||
/// Pac-Man can only move through edges that allow all entities.
|
||||
fn can_pacman_traverse(edge: Edge) -> bool {
|
||||
matches!(edge.permissions, EdgePermissions::All)
|
||||
}
|
||||
// /// Determines if Pac-Man can traverse a given edge.
|
||||
// ///
|
||||
// /// Pac-Man can only move through edges that allow all entities.
|
||||
// fn can_pacman_traverse(edge: Edge) -> bool {
|
||||
// matches!(edge.permissions, EdgePermissions::All)
|
||||
// }
|
||||
|
||||
/// The main player character entity.
|
||||
///
|
||||
/// Pac-Man moves through the game world using a graph-based navigation system
|
||||
/// and displays directional animated sprites based on movement state.
|
||||
pub struct Pacman {
|
||||
/// Handles movement through the game graph
|
||||
pub traverser: Traverser,
|
||||
/// Manages directional animated textures for different movement states
|
||||
texture: DirectionalAnimatedTexture,
|
||||
}
|
||||
// /// The main player character entity.
|
||||
// ///
|
||||
// /// Pac-Man moves through the game world using a graph-based navigation system
|
||||
// /// and displays directional animated sprites based on movement state.
|
||||
// pub struct Pacman {
|
||||
// /// Handles movement through the game graph
|
||||
// pub traverser: Traverser,
|
||||
// /// Manages directional animated textures for different movement states
|
||||
// texture: DirectionalAnimatedTexture,
|
||||
// }
|
||||
|
||||
impl Entity for Pacman {
|
||||
fn traverser(&self) -> &Traverser {
|
||||
&self.traverser
|
||||
}
|
||||
// impl Entity for Pacman {
|
||||
// fn traverser(&self) -> &Traverser {
|
||||
// &self.traverser
|
||||
// }
|
||||
|
||||
fn traverser_mut(&mut self) -> &mut Traverser {
|
||||
&mut self.traverser
|
||||
}
|
||||
// fn traverser_mut(&mut self) -> &mut Traverser {
|
||||
// &mut self.traverser
|
||||
// }
|
||||
|
||||
fn texture(&self) -> &DirectionalAnimatedTexture {
|
||||
&self.texture
|
||||
}
|
||||
// fn texture(&self) -> &DirectionalAnimatedTexture {
|
||||
// &self.texture
|
||||
// }
|
||||
|
||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
||||
&mut self.texture
|
||||
}
|
||||
// fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture {
|
||||
// &mut self.texture
|
||||
// }
|
||||
|
||||
fn speed(&self) -> f32 {
|
||||
1.125
|
||||
}
|
||||
// fn speed(&self) -> f32 {
|
||||
// 1.125
|
||||
// }
|
||||
|
||||
fn can_traverse(&self, edge: Edge) -> bool {
|
||||
can_pacman_traverse(edge)
|
||||
}
|
||||
// fn can_traverse(&self, edge: Edge) -> bool {
|
||||
// can_pacman_traverse(edge)
|
||||
// }
|
||||
|
||||
fn tick(&mut self, dt: f32, graph: &Graph) {
|
||||
if let Err(e) = self.traverser.advance(graph, dt * 60.0 * 1.125, &can_pacman_traverse) {
|
||||
error!("Pac-Man movement error: {}", e);
|
||||
}
|
||||
self.texture.tick(dt);
|
||||
}
|
||||
}
|
||||
// fn tick(&mut self, dt: f32, graph: &Graph) {
|
||||
// if let Err(e) = self.traverser.advance(graph, dt * 60.0 * 1.125, &can_pacman_traverse) {
|
||||
// error!("Pac-Man movement error: {}", e);
|
||||
// }
|
||||
// self.texture.tick(dt);
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Pacman {
|
||||
/// Creates a new Pac-Man instance at the specified starting node.
|
||||
///
|
||||
/// Sets up animated textures for all four directions with moving and stopped states.
|
||||
/// The moving animation cycles through open mouth, closed mouth, and full sprites.
|
||||
pub fn new(graph: &Graph, start_node: NodeId, atlas: &SpriteAtlas) -> GameResult<Self> {
|
||||
let mut textures = [None, None, None, None];
|
||||
let mut stopped_textures = [None, None, None, None];
|
||||
// impl Pacman {
|
||||
// /// Creates a new Pac-Man instance at the specified starting node.
|
||||
// ///
|
||||
// /// Sets up animated textures for all four directions with moving and stopped states.
|
||||
// /// The moving animation cycles through open mouth, closed mouth, and full sprites.
|
||||
// pub fn new(graph: &Graph, start_node: NodeId, atlas: &SpriteAtlas) -> GameResult<Self> {
|
||||
// let mut textures = [None, None, None, None];
|
||||
// let mut stopped_textures = [None, None, None, None];
|
||||
|
||||
for direction in Direction::DIRECTIONS {
|
||||
let moving_prefix = match direction {
|
||||
Direction::Up => "pacman/up",
|
||||
Direction::Down => "pacman/down",
|
||||
Direction::Left => "pacman/left",
|
||||
Direction::Right => "pacman/right",
|
||||
};
|
||||
let moving_tiles = vec![
|
||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_a.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_a.png"))))?,
|
||||
SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?,
|
||||
SpriteAtlas::get_tile(atlas, "pacman/full.png")
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
||||
];
|
||||
// for direction in Direction::DIRECTIONS {
|
||||
// let moving_prefix = match direction {
|
||||
// Direction::Up => "pacman/up",
|
||||
// Direction::Down => "pacman/down",
|
||||
// Direction::Left => "pacman/left",
|
||||
// Direction::Right => "pacman/right",
|
||||
// };
|
||||
// let moving_tiles = vec![
|
||||
// SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_a.png"))
|
||||
// .ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_a.png"))))?,
|
||||
// SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
||||
// .ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?,
|
||||
// SpriteAtlas::get_tile(atlas, "pacman/full.png")
|
||||
// .ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
||||
// ];
|
||||
|
||||
let stopped_tiles = vec![SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?];
|
||||
// let stopped_tiles = vec![SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png"))
|
||||
// .ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?];
|
||||
|
||||
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.08)?);
|
||||
stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
||||
}
|
||||
// textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.08)?);
|
||||
// stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
||||
// }
|
||||
|
||||
Ok(Self {
|
||||
traverser: Traverser::new(graph, start_node, Direction::Left, &can_pacman_traverse),
|
||||
texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
||||
})
|
||||
}
|
||||
// Ok(Self {
|
||||
// traverser: Traverser::new(graph, start_node, Direction::Left, &can_pacman_traverse),
|
||||
// texture: DirectionalAnimatedTexture::new(textures, stopped_textures),
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Handles keyboard input to change Pac-Man's direction.
|
||||
///
|
||||
/// Maps arrow keys to directions and queues the direction change
|
||||
/// for the next valid intersection.
|
||||
pub fn handle_key(&mut self, keycode: Keycode) {
|
||||
let direction = match keycode {
|
||||
Keycode::Up => Some(Direction::Up),
|
||||
Keycode::Down => Some(Direction::Down),
|
||||
Keycode::Left => Some(Direction::Left),
|
||||
Keycode::Right => Some(Direction::Right),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(direction) = direction {
|
||||
self.traverser.set_next_direction(direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Collidable for Pacman {
|
||||
fn position(&self) -> crate::entity::traversal::Position {
|
||||
self.traverser.position
|
||||
}
|
||||
}
|
||||
// impl Collidable for Pacman {
|
||||
// fn position(&self) -> crate::entity::traversal::Position {
|
||||
// self.traverser.position
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,114 +1,114 @@
|
||||
//! Entity trait for common movement and rendering functionality.
|
||||
//!
|
||||
//! This module defines a trait that captures the shared behavior between
|
||||
//! different game entities like Ghosts and Pac-Man, including movement,
|
||||
//! rendering, and position calculations.
|
||||
// //! Entity trait for common movement and rendering functionality.
|
||||
// //!
|
||||
// //! This module defines a trait that captures the shared behavior between
|
||||
// //! different game entities like Ghosts and Pac-Man, including movement,
|
||||
// //! rendering, and position calculations.
|
||||
|
||||
use glam::Vec2;
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
// use glam::Vec2;
|
||||
// use sdl2::render::{Canvas, RenderTarget};
|
||||
|
||||
use crate::entity::direction::Direction;
|
||||
use crate::entity::graph::{Edge, Graph, NodeId};
|
||||
use crate::entity::traversal::{Position, Traverser};
|
||||
use crate::error::{EntityError, GameError, GameResult, TextureError};
|
||||
use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
// use crate::entity::direction::Direction;
|
||||
// use crate::entity::graph::{Edge, Graph, NodeId};
|
||||
// use crate::entity::traversal::{Position, Traverser};
|
||||
// use crate::error::{EntityError, GameError, GameResult, TextureError};
|
||||
// use crate::texture::directional::DirectionalAnimatedTexture;
|
||||
// use crate::texture::sprite::SpriteAtlas;
|
||||
|
||||
/// Trait defining common functionality for game entities that move through the graph.
|
||||
///
|
||||
/// This trait provides a unified interface for entities that:
|
||||
/// - Move through the game graph using a traverser
|
||||
/// - Render using directional animated textures
|
||||
/// - Have position calculations and movement speed
|
||||
#[allow(dead_code)]
|
||||
pub trait Entity {
|
||||
/// Returns a reference to the entity's traverser for movement control.
|
||||
fn traverser(&self) -> &Traverser;
|
||||
// /// Trait defining common functionality for game entities that move through the graph.
|
||||
// ///
|
||||
// /// This trait provides a unified interface for entities that:
|
||||
// /// - Move through the game graph using a traverser
|
||||
// /// - Render using directional animated textures
|
||||
// /// - Have position calculations and movement speed
|
||||
// #[allow(dead_code)]
|
||||
// pub trait Entity {
|
||||
// /// Returns a reference to the entity's traverser for movement control.
|
||||
// fn traverser(&self) -> &Traverser;
|
||||
|
||||
/// Returns a mutable reference to the entity's traverser for movement control.
|
||||
fn traverser_mut(&mut self) -> &mut Traverser;
|
||||
// /// Returns a mutable reference to the entity's traverser for movement control.
|
||||
// fn traverser_mut(&mut self) -> &mut Traverser;
|
||||
|
||||
/// Returns a reference to the entity's directional animated texture.
|
||||
fn texture(&self) -> &DirectionalAnimatedTexture;
|
||||
// /// Returns a reference to the entity's directional animated texture.
|
||||
// fn texture(&self) -> &DirectionalAnimatedTexture;
|
||||
|
||||
/// Returns a mutable reference to the entity's directional animated texture.
|
||||
fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture;
|
||||
// /// Returns a mutable reference to the entity's directional animated texture.
|
||||
// fn texture_mut(&mut self) -> &mut DirectionalAnimatedTexture;
|
||||
|
||||
/// Returns the movement speed multiplier for this entity.
|
||||
fn speed(&self) -> f32;
|
||||
// /// Returns the movement speed multiplier for this entity.
|
||||
// fn speed(&self) -> f32;
|
||||
|
||||
/// Determines if this entity can traverse a given edge.
|
||||
fn can_traverse(&self, edge: Edge) -> bool;
|
||||
// /// Determines if this entity can traverse a given edge.
|
||||
// fn can_traverse(&self, edge: Edge) -> bool;
|
||||
|
||||
/// Updates the entity's position and animation state.
|
||||
///
|
||||
/// This method advances movement through the graph and updates texture animation.
|
||||
fn tick(&mut self, dt: f32, graph: &Graph);
|
||||
// /// Updates the entity's position and animation state.
|
||||
// ///
|
||||
// /// This method advances movement through the graph and updates texture animation.
|
||||
// fn tick(&mut self, dt: f32, graph: &Graph);
|
||||
|
||||
/// Calculates the current pixel position in the game world.
|
||||
///
|
||||
/// Converts the graph position to screen coordinates, accounting for
|
||||
/// the board offset and centering the sprite.
|
||||
fn get_pixel_pos(&self, graph: &Graph) -> GameResult<Vec2> {
|
||||
let pos = match self.traverser().position {
|
||||
Position::AtNode(node_id) => {
|
||||
let node = graph.get_node(node_id).ok_or(EntityError::NodeNotFound(node_id))?;
|
||||
node.position
|
||||
}
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
let from_node = graph.get_node(from).ok_or(EntityError::NodeNotFound(from))?;
|
||||
let to_node = graph.get_node(to).ok_or(EntityError::NodeNotFound(to))?;
|
||||
let edge = graph.find_edge(from, to).ok_or(EntityError::EdgeNotFound { from, to })?;
|
||||
from_node.position + (to_node.position - from_node.position) * (traversed / edge.distance)
|
||||
}
|
||||
};
|
||||
// /// Calculates the current pixel position in the game world.
|
||||
// ///
|
||||
// /// Converts the graph position to screen coordinates, accounting for
|
||||
// /// the board offset and centering the sprite.
|
||||
// fn get_pixel_pos(&self, graph: &Graph) -> GameResult<Vec2> {
|
||||
// let pos = match self.traverser().position {
|
||||
// Position::AtNode(node_id) => {
|
||||
// let node = graph.get_node(node_id).ok_or(EntityError::NodeNotFound(node_id))?;
|
||||
// node.position
|
||||
// }
|
||||
// Position::BetweenNodes { from, to, traversed } => {
|
||||
// let from_node = graph.get_node(from).ok_or(EntityError::NodeNotFound(from))?;
|
||||
// let to_node = graph.get_node(to).ok_or(EntityError::NodeNotFound(to))?;
|
||||
// let edge = graph.find_edge(from, to).ok_or(EntityError::EdgeNotFound { from, to })?;
|
||||
// from_node.position + (to_node.position - from_node.position) * (traversed / edge.distance)
|
||||
// }
|
||||
// };
|
||||
|
||||
Ok(Vec2::new(
|
||||
pos.x + crate::constants::BOARD_PIXEL_OFFSET.x as f32,
|
||||
pos.y + crate::constants::BOARD_PIXEL_OFFSET.y as f32,
|
||||
))
|
||||
}
|
||||
// Ok(Vec2::new(
|
||||
// pos.x + crate::constants::BOARD_PIXEL_OFFSET.x as f32,
|
||||
// pos.y + crate::constants::BOARD_PIXEL_OFFSET.y as f32,
|
||||
// ))
|
||||
// }
|
||||
|
||||
/// Returns the current node ID that the entity is at or moving towards.
|
||||
///
|
||||
/// If the entity is at a node, returns that node ID.
|
||||
/// If the entity is between nodes, returns the node it's moving towards.
|
||||
fn current_node_id(&self) -> NodeId {
|
||||
match self.traverser().position {
|
||||
Position::AtNode(node_id) => node_id,
|
||||
Position::BetweenNodes { to, .. } => to,
|
||||
}
|
||||
}
|
||||
// /// Returns the current node ID that the entity is at or moving towards.
|
||||
// ///
|
||||
// /// If the entity is at a node, returns that node ID.
|
||||
// /// If the entity is between nodes, returns the node it's moving towards.
|
||||
// fn current_node_id(&self) -> NodeId {
|
||||
// match self.traverser().position {
|
||||
// Position::AtNode(node_id) => node_id,
|
||||
// Position::BetweenNodes { to, .. } => to,
|
||||
// }
|
||||
// }
|
||||
|
||||
/// Sets the next direction for the entity to take.
|
||||
///
|
||||
/// The direction is buffered and will be applied at the next opportunity,
|
||||
/// typically when the entity reaches a new node.
|
||||
fn set_next_direction(&mut self, direction: Direction) {
|
||||
self.traverser_mut().set_next_direction(direction);
|
||||
}
|
||||
// /// Sets the next direction for the entity to take.
|
||||
// ///
|
||||
// /// The direction is buffered and will be applied at the next opportunity,
|
||||
// /// typically when the entity reaches a new node.
|
||||
// fn set_next_direction(&mut self, direction: Direction) {
|
||||
// self.traverser_mut().set_next_direction(direction);
|
||||
// }
|
||||
|
||||
/// Renders the entity at its current position.
|
||||
///
|
||||
/// Draws the appropriate directional sprite based on the entity's
|
||||
/// current movement state and direction.
|
||||
fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
||||
let pixel_pos = self.get_pixel_pos(graph)?;
|
||||
let dest = crate::helpers::centered_with_size(
|
||||
glam::IVec2::new(pixel_pos.x as i32, pixel_pos.y as i32),
|
||||
glam::UVec2::new(16, 16),
|
||||
);
|
||||
// /// Renders the entity at its current position.
|
||||
// ///
|
||||
// /// Draws the appropriate directional sprite based on the entity's
|
||||
// /// current movement state and direction.
|
||||
// fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, graph: &Graph) -> GameResult<()> {
|
||||
// let pixel_pos = self.get_pixel_pos(graph)?;
|
||||
// let dest = crate::helpers::centered_with_size(
|
||||
// glam::IVec2::new(pixel_pos.x as i32, pixel_pos.y as i32),
|
||||
// glam::UVec2::new(16, 16),
|
||||
// );
|
||||
|
||||
if self.traverser().position.is_stopped() {
|
||||
self.texture()
|
||||
.render_stopped(canvas, atlas, dest, self.traverser().direction)
|
||||
.map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
||||
} else {
|
||||
self.texture()
|
||||
.render(canvas, atlas, dest, self.traverser().direction)
|
||||
.map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
||||
}
|
||||
// if self.traverser().position.is_stopped() {
|
||||
// self.texture()
|
||||
// .render_stopped(canvas, atlas, dest, self.traverser().direction)
|
||||
// .map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
||||
// } else {
|
||||
// self.texture()
|
||||
// .render(canvas, atlas, dest, self.traverser().direction)
|
||||
// .map_err(|e| GameError::Texture(TextureError::RenderFailed(e.to_string())))?;
|
||||
// }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
// Ok(())
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,229 +0,0 @@
|
||||
use tracing::error;
|
||||
|
||||
use crate::error::GameResult;
|
||||
|
||||
use super::direction::Direction;
|
||||
use super::graph::{Edge, Graph, NodeId};
|
||||
|
||||
/// Represents the current position of an entity traversing the graph.
|
||||
///
|
||||
/// This enum allows for precise tracking of whether an entity is exactly at a node
|
||||
/// or moving along an edge between two nodes.
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum Position {
|
||||
/// The traverser is located exactly at a node.
|
||||
AtNode(NodeId),
|
||||
/// The traverser is on an edge between two nodes.
|
||||
BetweenNodes {
|
||||
from: NodeId,
|
||||
to: NodeId,
|
||||
/// The floating-point distance traversed along the edge from the `from` node.
|
||||
traversed: f32,
|
||||
},
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Position {
|
||||
/// Returns `true` if the position is exactly at a node.
|
||||
pub fn is_at_node(&self) -> bool {
|
||||
matches!(self, Position::AtNode(_))
|
||||
}
|
||||
|
||||
/// Returns the `NodeId` of the current or most recently departed node.
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn from_node_id(&self) -> NodeId {
|
||||
match self {
|
||||
Position::AtNode(id) => *id,
|
||||
Position::BetweenNodes { from, .. } => *from,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `NodeId` of the destination node, if currently on an edge.
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn to_node_id(&self) -> Option<NodeId> {
|
||||
match self {
|
||||
Position::AtNode(_) => None,
|
||||
Position::BetweenNodes { to, .. } => Some(*to),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the traverser is stopped at a node.
|
||||
pub fn is_stopped(&self) -> bool {
|
||||
matches!(self, Position::AtNode(_))
|
||||
}
|
||||
}
|
||||
|
||||
/// Manages an entity's movement through the graph.
|
||||
///
|
||||
/// A `Traverser` encapsulates the state of an entity's position and direction,
|
||||
/// providing a way to advance along the graph's paths based on a given distance.
|
||||
/// It also handles direction changes, buffering the next intended direction.
|
||||
pub struct Traverser {
|
||||
/// The current position of the traverser in the graph.
|
||||
pub position: Position,
|
||||
/// The current direction of movement.
|
||||
pub direction: Direction,
|
||||
/// Buffered direction change with remaining frame count for timing.
|
||||
///
|
||||
/// The `u8` value represents the number of frames remaining before
|
||||
/// the buffered direction expires. This allows for responsive controls
|
||||
/// by storing direction changes for a limited time.
|
||||
pub next_direction: Option<(Direction, u8)>,
|
||||
}
|
||||
|
||||
impl Traverser {
|
||||
/// Creates a new traverser starting at the given node ID.
|
||||
///
|
||||
/// The traverser will immediately attempt to start moving in the initial direction.
|
||||
pub fn new<F>(graph: &Graph, start_node: NodeId, initial_direction: Direction, can_traverse: &F) -> Self
|
||||
where
|
||||
F: Fn(Edge) -> bool,
|
||||
{
|
||||
let mut traverser = Traverser {
|
||||
position: Position::AtNode(start_node),
|
||||
direction: initial_direction,
|
||||
next_direction: Some((initial_direction, 1)),
|
||||
};
|
||||
|
||||
// This will kickstart the traverser into motion
|
||||
if let Err(e) = traverser.advance(graph, 0.0, can_traverse) {
|
||||
error!("Traverser initialization error: {}", e);
|
||||
}
|
||||
|
||||
traverser
|
||||
}
|
||||
|
||||
/// Sets the next direction for the traverser to take.
|
||||
///
|
||||
/// The direction is buffered and will be applied at the next opportunity,
|
||||
/// typically when the traverser reaches a new node. This allows for responsive
|
||||
/// controls, as the new direction is stored for a limited time.
|
||||
pub fn set_next_direction(&mut self, new_direction: Direction) {
|
||||
if self.direction != new_direction {
|
||||
self.next_direction = Some((new_direction, 30));
|
||||
}
|
||||
}
|
||||
|
||||
/// Advances the traverser along the graph by a specified distance.
|
||||
///
|
||||
/// This method updates the traverser's position based on its current state
|
||||
/// and the distance to travel.
|
||||
///
|
||||
/// - If at a node, it checks for a buffered direction to start moving.
|
||||
/// - If between nodes, it moves along the current edge.
|
||||
/// - If it reaches a node, it attempts to transition to a new edge based on
|
||||
/// the buffered direction or by continuing straight.
|
||||
/// - If no valid move is possible, it stops at the node.
|
||||
///
|
||||
/// Returns an error if the movement is invalid (e.g., trying to move in an impossible direction).
|
||||
pub fn advance<F>(&mut self, graph: &Graph, distance: f32, can_traverse: &F) -> GameResult<()>
|
||||
where
|
||||
F: Fn(Edge) -> bool,
|
||||
{
|
||||
// Decrement the remaining frames for the next direction
|
||||
if let Some((direction, remaining)) = self.next_direction {
|
||||
if remaining > 0 {
|
||||
self.next_direction = Some((direction, remaining - 1));
|
||||
} else {
|
||||
self.next_direction = None;
|
||||
}
|
||||
}
|
||||
|
||||
match self.position {
|
||||
Position::AtNode(node_id) => {
|
||||
// We're not moving, but a buffered direction is available.
|
||||
if let Some((next_direction, _)) = self.next_direction {
|
||||
if let Some(edge) = graph.find_edge_in_direction(node_id, next_direction) {
|
||||
if can_traverse(edge) {
|
||||
// Start moving in that direction
|
||||
self.position = Position::BetweenNodes {
|
||||
from: node_id,
|
||||
to: edge.target,
|
||||
traversed: distance.max(0.0),
|
||||
};
|
||||
self.direction = next_direction;
|
||||
} else {
|
||||
return Err(crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(
|
||||
format!(
|
||||
"Cannot traverse edge from {} to {} in direction {:?}",
|
||||
node_id, edge.target, next_direction
|
||||
),
|
||||
)));
|
||||
}
|
||||
} else {
|
||||
return Err(crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(
|
||||
format!("No edge found in direction {:?} from node {}", next_direction, node_id),
|
||||
)));
|
||||
}
|
||||
|
||||
self.next_direction = None; // Consume the buffered direction regardless of whether we started moving with it
|
||||
}
|
||||
}
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
// There is no point in any of the next logic if we don't travel at all
|
||||
if distance <= 0.0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let edge = graph.find_edge(from, to).ok_or_else(|| {
|
||||
crate::error::GameError::Entity(crate::error::EntityError::InvalidMovement(format!(
|
||||
"Inconsistent state: Traverser is on a non-existent edge from {} to {}.",
|
||||
from, to
|
||||
)))
|
||||
})?;
|
||||
|
||||
let new_traversed = traversed + distance;
|
||||
|
||||
if new_traversed < edge.distance {
|
||||
// Still on the same edge, just update the distance.
|
||||
self.position = Position::BetweenNodes {
|
||||
from,
|
||||
to,
|
||||
traversed: new_traversed,
|
||||
};
|
||||
} else {
|
||||
let overflow = new_traversed - edge.distance;
|
||||
let mut moved = false;
|
||||
|
||||
// If we buffered a direction, try to find an edge in that direction
|
||||
if let Some((next_dir, _)) = self.next_direction {
|
||||
if let Some(edge) = graph.find_edge_in_direction(to, next_dir) {
|
||||
if can_traverse(edge) {
|
||||
self.position = Position::BetweenNodes {
|
||||
from: to,
|
||||
to: edge.target,
|
||||
traversed: overflow,
|
||||
};
|
||||
|
||||
self.direction = next_dir; // Remember our new direction
|
||||
self.next_direction = None; // Consume the buffered direction
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we didn't move, try to continue in the current direction
|
||||
if !moved {
|
||||
if let Some(edge) = graph.find_edge_in_direction(to, self.direction) {
|
||||
if can_traverse(edge) {
|
||||
self.position = Position::BetweenNodes {
|
||||
from: to,
|
||||
to: edge.target,
|
||||
traversed: overflow,
|
||||
};
|
||||
} else {
|
||||
self.position = Position::AtNode(to);
|
||||
self.next_direction = None;
|
||||
}
|
||||
} else {
|
||||
self.position = Position::AtNode(to);
|
||||
self.next_direction = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,13 @@
|
||||
|
||||
use std::io;
|
||||
|
||||
use bevy_ecs::event::Event;
|
||||
|
||||
/// Main error type for the Pac-Man game.
|
||||
///
|
||||
/// This is the primary error type that should be used in public APIs.
|
||||
/// It can represent any error that can occur during game operation.
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
#[derive(thiserror::Error, Debug, Event)]
|
||||
pub enum GameError {
|
||||
#[error("Asset error: {0}")]
|
||||
Asset(#[from] AssetError),
|
||||
|
||||
14
src/events.rs
Normal file
14
src/events.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
use bevy_ecs::event::Event;
|
||||
|
||||
use crate::systems::input::GameCommand;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Event)]
|
||||
pub enum GameEvent {
|
||||
Command(GameCommand),
|
||||
}
|
||||
|
||||
impl From<GameCommand> for GameEvent {
|
||||
fn from(command: GameCommand) -> Self {
|
||||
GameEvent::Command(command)
|
||||
}
|
||||
}
|
||||
777
src/game/mod.rs
777
src/game/mod.rs
@@ -1,353 +1,526 @@
|
||||
//! This module contains the main game logic and state.
|
||||
|
||||
use glam::{UVec2, Vec2};
|
||||
use rand::{rngs::SmallRng, Rng, SeedableRng};
|
||||
use sdl2::{
|
||||
keyboard::Keycode,
|
||||
pixels::Color,
|
||||
render::{Canvas, RenderTarget, Texture, TextureCreator},
|
||||
video::WindowContext,
|
||||
};
|
||||
|
||||
use crate::entity::r#trait::Entity;
|
||||
use crate::error::{EntityError, GameError, GameResult};
|
||||
|
||||
use crate::entity::{
|
||||
collision::{Collidable, CollisionSystem, EntityId},
|
||||
ghost::{Ghost, GhostType},
|
||||
pacman::Pacman,
|
||||
include!(concat!(env!("OUT_DIR"), "/atlas_data.rs"));
|
||||
|
||||
use crate::constants::CANVAS_SIZE;
|
||||
use crate::entity::direction::Direction;
|
||||
use crate::error::{GameError, GameResult, TextureError};
|
||||
use crate::events::GameEvent;
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::components::{
|
||||
DeltaTime, DirectionalAnimated, EntityType, GlobalState, PlayerBundle, PlayerControlled, Position, Renderable, Velocity,
|
||||
};
|
||||
use crate::systems::control::player_system;
|
||||
use crate::systems::movement::movement_system;
|
||||
use crate::systems::render::{directional_render_system, render_system, BackbufferResource, MapTextureResource};
|
||||
use crate::texture::animated::AnimatedTexture;
|
||||
use bevy_ecs::event::EventRegistry;
|
||||
use bevy_ecs::observer::Trigger;
|
||||
use bevy_ecs::schedule::IntoScheduleConfigs;
|
||||
use bevy_ecs::system::ResMut;
|
||||
use bevy_ecs::{schedule::Schedule, world::World};
|
||||
use sdl2::image::LoadTexture;
|
||||
use sdl2::render::{Canvas, ScaleMode, TextureCreator};
|
||||
use sdl2::video::{Window, WindowContext};
|
||||
use sdl2::EventPump;
|
||||
|
||||
use crate::asset::{get_asset_bytes, Asset};
|
||||
use crate::map::render::MapRenderer;
|
||||
use crate::{constants, texture::sprite::SpriteAtlas};
|
||||
use crate::systems::input::{input_system, Bindings, GameCommand};
|
||||
use crate::{
|
||||
constants,
|
||||
texture::sprite::{AtlasMapper, SpriteAtlas},
|
||||
};
|
||||
|
||||
pub mod state;
|
||||
use state::GameState;
|
||||
|
||||
/// The `Game` struct is the main entry point for the game.
|
||||
///
|
||||
/// It contains the game's state and logic, and is responsible for
|
||||
/// handling user input, updating the game state, and rendering the game.
|
||||
pub struct Game {
|
||||
state: GameState,
|
||||
pub world: World,
|
||||
pub schedule: Schedule,
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub fn new(texture_creator: &'static TextureCreator<WindowContext>) -> GameResult<Game> {
|
||||
let state = GameState::new(texture_creator)?;
|
||||
pub fn new(
|
||||
canvas: &'static mut Canvas<Window>,
|
||||
texture_creator: &'static mut TextureCreator<WindowContext>,
|
||||
event_pump: &'static mut EventPump,
|
||||
) -> GameResult<Game> {
|
||||
let mut world = World::default();
|
||||
let mut schedule = Schedule::default();
|
||||
|
||||
Ok(Game { state })
|
||||
}
|
||||
EventRegistry::register_event::<GameError>(&mut world);
|
||||
EventRegistry::register_event::<GameEvent>(&mut world);
|
||||
|
||||
pub fn keyboard_event(&mut self, keycode: Keycode) {
|
||||
self.state.pacman.handle_key(keycode);
|
||||
|
||||
if keycode == Keycode::M {
|
||||
self.state.audio.set_mute(!self.state.audio.is_muted());
|
||||
}
|
||||
|
||||
if keycode == Keycode::R {
|
||||
if let Err(e) = self.reset_game_state() {
|
||||
tracing::error!("Failed to reset game state: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Resets the game state, randomizing ghost positions and resetting Pac-Man
|
||||
fn reset_game_state(&mut self) -> GameResult<()> {
|
||||
let pacman_start_node = self.state.map.start_positions.pacman;
|
||||
self.state.pacman = Pacman::new(&self.state.map.graph, pacman_start_node, &self.state.atlas)?;
|
||||
|
||||
// Reset items
|
||||
self.state.items = self.state.map.generate_items(&self.state.atlas)?;
|
||||
|
||||
// Randomize ghost positions
|
||||
let ghost_types = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde];
|
||||
let mut rng = SmallRng::from_os_rng();
|
||||
|
||||
for (i, ghost) in self.state.ghosts.iter_mut().enumerate() {
|
||||
let random_node = rng.random_range(0..self.state.map.graph.node_count());
|
||||
*ghost = Ghost::new(&self.state.map.graph, random_node, ghost_types[i], &self.state.atlas)?;
|
||||
}
|
||||
|
||||
// Reset collision system
|
||||
self.state.collision_system = CollisionSystem::default();
|
||||
|
||||
// Re-register Pac-Man
|
||||
self.state.pacman_id = self.state.collision_system.register_entity(self.state.pacman.position());
|
||||
|
||||
// Re-register items
|
||||
self.state.item_ids.clear();
|
||||
for item in &self.state.items {
|
||||
let item_id = self.state.collision_system.register_entity(item.position());
|
||||
self.state.item_ids.push(item_id);
|
||||
}
|
||||
|
||||
// Re-register ghosts
|
||||
self.state.ghost_ids.clear();
|
||||
for ghost in &self.state.ghosts {
|
||||
let ghost_id = self.state.collision_system.register_entity(ghost.position());
|
||||
self.state.ghost_ids.push(ghost_id);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn tick(&mut self, dt: f32) {
|
||||
self.state.pacman.tick(dt, &self.state.map.graph);
|
||||
|
||||
// Update all ghosts
|
||||
for ghost in &mut self.state.ghosts {
|
||||
ghost.tick(dt, &self.state.map.graph);
|
||||
}
|
||||
|
||||
// Update collision system positions
|
||||
self.update_collision_positions();
|
||||
|
||||
// Check for collisions
|
||||
self.check_collisions();
|
||||
}
|
||||
|
||||
/// Toggles the debug mode on and off.
|
||||
///
|
||||
/// When debug mode is enabled, the game will render additional information
|
||||
/// that is useful for debugging, such as the collision grid and entity paths.
|
||||
pub fn toggle_debug_mode(&mut self) {
|
||||
self.state.debug_mode = !self.state.debug_mode;
|
||||
}
|
||||
|
||||
fn update_collision_positions(&mut self) {
|
||||
// Update Pac-Man's position
|
||||
self.state
|
||||
.collision_system
|
||||
.update_position(self.state.pacman_id, self.state.pacman.position());
|
||||
|
||||
// Update ghost positions
|
||||
for (ghost, &ghost_id) in self.state.ghosts.iter().zip(&self.state.ghost_ids) {
|
||||
self.state.collision_system.update_position(ghost_id, ghost.position());
|
||||
}
|
||||
}
|
||||
|
||||
fn check_collisions(&mut self) {
|
||||
// Check Pac-Man vs Items
|
||||
let potential_collisions = self
|
||||
.state
|
||||
.collision_system
|
||||
.potential_collisions(&self.state.pacman.position());
|
||||
|
||||
for entity_id in potential_collisions {
|
||||
if entity_id != self.state.pacman_id {
|
||||
// Check if this is an item collision
|
||||
if let Some(item_index) = self.find_item_by_id(entity_id) {
|
||||
let item = &mut self.state.items[item_index];
|
||||
if !item.is_collected() {
|
||||
item.collect();
|
||||
self.state.score += item.get_score();
|
||||
self.state.audio.eat();
|
||||
|
||||
// Handle energizer effects
|
||||
if matches!(item.item_type, crate::entity::item::ItemType::Energizer) {
|
||||
// TODO: Make ghosts frightened
|
||||
tracing::info!("Energizer collected! Ghosts should become frightened.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this is a ghost collision
|
||||
if let Some(_ghost_index) = self.find_ghost_by_id(entity_id) {
|
||||
// TODO: Handle Pac-Man being eaten by ghost
|
||||
tracing::info!("Pac-Man collided with ghost!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_item_by_id(&self, entity_id: EntityId) -> Option<usize> {
|
||||
self.state.item_ids.iter().position(|&id| id == entity_id)
|
||||
}
|
||||
|
||||
fn find_ghost_by_id(&self, entity_id: EntityId) -> Option<usize> {
|
||||
self.state.ghost_ids.iter().position(|&id| id == entity_id)
|
||||
}
|
||||
|
||||
pub fn draw<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> GameResult<()> {
|
||||
// Only render the map texture once and cache it
|
||||
if !self.state.map_rendered {
|
||||
let mut map_texture = self
|
||||
.state
|
||||
.texture_creator
|
||||
.create_texture_target(None, constants::CANVAS_SIZE.x, constants::CANVAS_SIZE.y)
|
||||
let mut backbuffer = texture_creator
|
||||
.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
||||
|
||||
canvas
|
||||
.with_texture_canvas(&mut map_texture, |map_canvas| {
|
||||
let mut map_texture = texture_creator
|
||||
.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
map_texture.set_scale_mode(ScaleMode::Nearest);
|
||||
|
||||
// Load atlas and create map texture
|
||||
let atlas_bytes = get_asset_bytes(Asset::Atlas)?;
|
||||
let atlas_texture = texture_creator.load_texture_bytes(&atlas_bytes).map_err(|e| {
|
||||
if e.to_string().contains("format") || e.to_string().contains("unsupported") {
|
||||
GameError::Texture(crate::error::TextureError::InvalidFormat(format!(
|
||||
"Unsupported texture format: {e}"
|
||||
)))
|
||||
} else {
|
||||
GameError::Texture(crate::error::TextureError::LoadFailed(e.to_string()))
|
||||
}
|
||||
})?;
|
||||
|
||||
let atlas_mapper = AtlasMapper {
|
||||
frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(),
|
||||
};
|
||||
let mut atlas = SpriteAtlas::new(atlas_texture, atlas_mapper);
|
||||
|
||||
// Create map tiles
|
||||
let mut map_tiles = Vec::with_capacity(35);
|
||||
for i in 0..35 {
|
||||
let tile_name = format!("maze/tiles/{}.png", i);
|
||||
let tile = SpriteAtlas::get_tile(&self.state.atlas, &tile_name).unwrap();
|
||||
let tile = atlas.get_tile(&tile_name).unwrap();
|
||||
map_tiles.push(tile);
|
||||
}
|
||||
MapRenderer::render_map(map_canvas, &mut self.state.atlas, &mut map_tiles);
|
||||
})
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
self.state.map_texture = Some(map_texture);
|
||||
self.state.map_rendered = true;
|
||||
}
|
||||
|
||||
// Render map to texture
|
||||
canvas
|
||||
.with_texture_canvas(backbuffer, |canvas| {
|
||||
canvas.set_draw_color(Color::BLACK);
|
||||
canvas.clear();
|
||||
if let Some(ref map_texture) = self.state.map_texture {
|
||||
canvas.copy(map_texture, None, None).unwrap();
|
||||
}
|
||||
|
||||
// Render all items
|
||||
for item in &self.state.items {
|
||||
if let Err(e) = item.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
tracing::error!("Failed to render item: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Render all ghosts
|
||||
for ghost in &self.state.ghosts {
|
||||
if let Err(e) = ghost.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
tracing::error!("Failed to render ghost: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = self.state.pacman.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
tracing::error!("Failed to render pacman: {}", e);
|
||||
}
|
||||
.with_texture_canvas(&mut map_texture, |map_canvas| {
|
||||
MapRenderer::render_map(map_canvas, &mut atlas, &mut map_tiles);
|
||||
})
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
|
||||
Ok(())
|
||||
let map = Map::new(constants::RAW_BOARD)?;
|
||||
let pacman_start_node = map.start_positions.pacman;
|
||||
|
||||
let mut textures = [None, None, None, None];
|
||||
let mut stopped_textures = [None, None, None, None];
|
||||
|
||||
for direction in Direction::DIRECTIONS {
|
||||
let moving_prefix = match direction {
|
||||
Direction::Up => "pacman/up",
|
||||
Direction::Down => "pacman/down",
|
||||
Direction::Left => "pacman/left",
|
||||
Direction::Right => "pacman/right",
|
||||
};
|
||||
let moving_tiles = vec![
|
||||
SpriteAtlas::get_tile(&atlas, &format!("{moving_prefix}_a.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_a.png"))))?,
|
||||
SpriteAtlas::get_tile(&atlas, &format!("{moving_prefix}_b.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?,
|
||||
SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
||||
];
|
||||
|
||||
let stopped_tiles = vec![SpriteAtlas::get_tile(&atlas, &format!("{moving_prefix}_b.png"))
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound(format!("{moving_prefix}_b.png"))))?];
|
||||
|
||||
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.08)?);
|
||||
stopped_textures[direction.as_usize()] = Some(AnimatedTexture::new(stopped_tiles, 0.1)?);
|
||||
}
|
||||
|
||||
pub fn present_backbuffer<T: RenderTarget>(
|
||||
&mut self,
|
||||
canvas: &mut Canvas<T>,
|
||||
backbuffer: &Texture,
|
||||
cursor_pos: glam::Vec2,
|
||||
) -> GameResult<()> {
|
||||
canvas
|
||||
.copy(backbuffer, None, None)
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
if self.state.debug_mode {
|
||||
if let Err(e) =
|
||||
self.state
|
||||
.map
|
||||
.debug_render_with_cursor(canvas, &mut self.state.text_texture, &mut self.state.atlas, cursor_pos)
|
||||
{
|
||||
tracing::error!("Failed to render debug cursor: {}", e);
|
||||
let player = PlayerBundle {
|
||||
player: PlayerControlled,
|
||||
position: Position::AtNode(pacman_start_node),
|
||||
velocity: Velocity {
|
||||
direction: Direction::Up,
|
||||
next_direction: None,
|
||||
speed: 1.125,
|
||||
},
|
||||
sprite: Renderable {
|
||||
sprite: SpriteAtlas::get_tile(&atlas, "pacman/full.png")
|
||||
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
|
||||
layer: 0,
|
||||
},
|
||||
directional_animated: DirectionalAnimated {
|
||||
textures,
|
||||
stopped_textures,
|
||||
},
|
||||
entity_type: EntityType::Player,
|
||||
};
|
||||
|
||||
world.insert_non_send_resource(atlas);
|
||||
world.insert_non_send_resource(event_pump);
|
||||
world.insert_non_send_resource(canvas);
|
||||
world.insert_non_send_resource(BackbufferResource(backbuffer));
|
||||
world.insert_non_send_resource(MapTextureResource(map_texture));
|
||||
|
||||
world.insert_resource(map);
|
||||
world.insert_resource(GlobalState { exit: false });
|
||||
world.insert_resource(Bindings::default());
|
||||
world.insert_resource(DeltaTime(0f32));
|
||||
|
||||
world.add_observer(|event: Trigger<GameEvent>, mut state: ResMut<GlobalState>| match *event {
|
||||
GameEvent::Command(command) => match command {
|
||||
GameCommand::Exit => {
|
||||
state.exit = true;
|
||||
}
|
||||
self.render_pathfinding_debug(canvas)?;
|
||||
}
|
||||
self.draw_hud(canvas)?;
|
||||
canvas.present();
|
||||
Ok(())
|
||||
_ => {}
|
||||
},
|
||||
});
|
||||
|
||||
schedule.add_systems(
|
||||
(
|
||||
input_system,
|
||||
player_system,
|
||||
movement_system,
|
||||
directional_render_system,
|
||||
render_system,
|
||||
)
|
||||
.chain(),
|
||||
);
|
||||
|
||||
// Spawn player
|
||||
world.spawn(player);
|
||||
|
||||
Ok(Game { world, schedule })
|
||||
}
|
||||
|
||||
/// Renders pathfinding debug lines from each ghost to Pac-Man.
|
||||
// fn handle_command(&mut self, command: crate::input::commands::GameCommand) {
|
||||
// use crate::input::commands::GameCommand;
|
||||
// match command {
|
||||
// GameCommand::MovePlayer(direction) => {
|
||||
// self.state.pacman.set_next_direction(direction);
|
||||
// }
|
||||
// GameCommand::ToggleDebug => {
|
||||
// self.toggle_debug_mode();
|
||||
// }
|
||||
// GameCommand::MuteAudio => {
|
||||
// let is_muted = self.state.audio.is_muted();
|
||||
// self.state.audio.set_mute(!is_muted);
|
||||
// }
|
||||
// GameCommand::ResetLevel => {
|
||||
// if let Err(e) = self.reset_game_state() {
|
||||
// tracing::error!("Failed to reset game state: {}", e);
|
||||
// }
|
||||
// }
|
||||
// GameCommand::TogglePause => {
|
||||
// self.state.paused = !self.state.paused;
|
||||
// }
|
||||
// GameCommand::Exit => {}
|
||||
// }
|
||||
// }
|
||||
|
||||
// fn process_events(&mut self) {
|
||||
// while let Some(event) = self.state.event_queue.pop_front() {
|
||||
// match event {
|
||||
// GameEvent::Command(command) => self.handle_command(command),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// Resets the game state, randomizing ghost positions and resetting Pac-Man
|
||||
// fn reset_game_state(&mut self) -> GameResult<()> {
|
||||
// let pacman_start_node = self.state.map.start_positions.pacman;
|
||||
// self.state.pacman = Pacman::new(&self.state.map.graph, pacman_start_node, &self.state.atlas)?;
|
||||
|
||||
// // Reset items
|
||||
// self.state.items = self.state.map.generate_items(&self.state.atlas)?;
|
||||
|
||||
// // Randomize ghost positions
|
||||
// let ghost_types = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde];
|
||||
// let mut rng = SmallRng::from_os_rng();
|
||||
|
||||
// for (i, ghost) in self.state.ghosts.iter_mut().enumerate() {
|
||||
// let random_node = rng.random_range(0..self.state.map.graph.node_count());
|
||||
// *ghost = Ghost::new(&self.state.map.graph, random_node, ghost_types[i], &self.state.atlas)?;
|
||||
// }
|
||||
|
||||
// // Reset collision system
|
||||
// self.state.collision_system = CollisionSystem::default();
|
||||
|
||||
// // Re-register Pac-Man
|
||||
// self.state.pacman_id = self.state.collision_system.register_entity(self.state.pacman.position());
|
||||
|
||||
// // Re-register items
|
||||
// self.state.item_ids.clear();
|
||||
// for item in &self.state.items {
|
||||
// let item_id = self.state.collision_system.register_entity(item.position());
|
||||
// self.state.item_ids.push(item_id);
|
||||
// }
|
||||
|
||||
// // Re-register ghosts
|
||||
// self.state.ghost_ids.clear();
|
||||
// for ghost in &self.state.ghosts {
|
||||
// let ghost_id = self.state.collision_system.register_entity(ghost.position());
|
||||
// self.state.ghost_ids.push(ghost_id);
|
||||
// }
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
/// Ticks the game state.
|
||||
///
|
||||
/// Each ghost's path is drawn in its respective color with a small offset
|
||||
/// to prevent overlapping lines.
|
||||
fn render_pathfinding_debug<T: RenderTarget>(&self, canvas: &mut Canvas<T>) -> GameResult<()> {
|
||||
let pacman_node = self.state.pacman.current_node_id();
|
||||
/// Returns true if the game should exit.
|
||||
pub fn tick(&mut self, dt: f32) -> bool {
|
||||
self.world.insert_resource(DeltaTime(dt));
|
||||
|
||||
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
|
||||
// Run all systems
|
||||
self.schedule.run(&mut self.world);
|
||||
|
||||
let state = self
|
||||
.world
|
||||
.get_resource::<GlobalState>()
|
||||
.expect("GlobalState could not be acquired");
|
||||
|
||||
return state.exit;
|
||||
|
||||
// // Process any events that have been posted (such as unpausing)
|
||||
// self.process_events();
|
||||
|
||||
// // If the game is paused, we don't need to do anything beyond returning
|
||||
// if self.state.paused {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// self.schedule.run(&mut self.world);
|
||||
|
||||
// self.state.pacman.tick(dt, &self.state.map.graph);
|
||||
|
||||
// // Update all ghosts
|
||||
// for ghost in &mut self.state.ghosts {
|
||||
// ghost.tick(dt, &self.state.map.graph);
|
||||
// }
|
||||
|
||||
// // Update collision system positions
|
||||
// self.update_collision_positions();
|
||||
|
||||
// // Check for collisions
|
||||
// self.check_collisions();
|
||||
}
|
||||
|
||||
// Set the ghost's color
|
||||
canvas.set_draw_color(ghost.debug_color());
|
||||
// /// Toggles the debug mode on and off.
|
||||
// ///
|
||||
// /// When debug mode is enabled, the game will render additional information
|
||||
// /// that is useful for debugging, such as the collision grid and entity paths.
|
||||
// pub fn toggle_debug_mode(&mut self) {
|
||||
// self.state.debug_mode = !self.state.debug_mode;
|
||||
// }
|
||||
|
||||
// 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
|
||||
// fn update_collision_positions(&mut self) {
|
||||
// // Update Pac-Man's position
|
||||
// self.state
|
||||
// .collision_system
|
||||
// .update_position(self.state.pacman_id, self.state.pacman.position());
|
||||
|
||||
// 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();
|
||||
// // Update ghost positions
|
||||
// for (ghost, &ghost_id) in self.state.ghosts.iter().zip(&self.state.ghost_ids) {
|
||||
// self.state.collision_system.update_position(ghost_id, ghost.position());
|
||||
// }
|
||||
// }
|
||||
|
||||
// Use the overall direction from start to end to determine the perpendicular offset
|
||||
let offset = match ghost.ghost_type {
|
||||
GhostType::Blinky => Vec2::new(0.25, 0.5),
|
||||
GhostType::Pinky => Vec2::new(-0.25, -0.25),
|
||||
GhostType::Inky => Vec2::new(0.5, -0.5),
|
||||
GhostType::Clyde => Vec2::new(-0.5, 0.25),
|
||||
} * 5.0;
|
||||
// fn check_collisions(&mut self) {
|
||||
// // Check Pac-Man vs Items
|
||||
// let potential_collisions = self
|
||||
// .state
|
||||
// .collision_system
|
||||
// .potential_collisions(&self.state.pacman.position());
|
||||
|
||||
// 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(GameError::Entity(EntityError::NodeNotFound(node_id)))?;
|
||||
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
|
||||
offset_positions.push(pos + offset);
|
||||
}
|
||||
// for entity_id in potential_collisions {
|
||||
// if entity_id != self.state.pacman_id {
|
||||
// // Check if this is an item collision
|
||||
// if let Some(item_index) = self.find_item_by_id(entity_id) {
|
||||
// let item = &mut self.state.items[item_index];
|
||||
// if !item.is_collected() {
|
||||
// item.collect();
|
||||
// self.state.score += item.get_score();
|
||||
// self.state.audio.eat();
|
||||
|
||||
// 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;
|
||||
}
|
||||
// // Handle energizer effects
|
||||
// if matches!(item.item_type, crate::entity::item::ItemType::Energizer) {
|
||||
// // TODO: Make ghosts frightened
|
||||
// tracing::info!("Energizer collected! Ghosts should become frightened.");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// Draw the line
|
||||
canvas
|
||||
.draw_line((from.x as i32, from.y as i32), (to.x as i32, to.y as i32))
|
||||
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Check if this is a ghost collision
|
||||
// if let Some(_ghost_index) = self.find_ghost_by_id(entity_id) {
|
||||
// // TODO: Handle Pac-Man being eaten by ghost
|
||||
// tracing::info!("Pac-Man collided with ghost!");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// fn find_item_by_id(&self, entity_id: EntityId) -> Option<usize> {
|
||||
// self.state.item_ids.iter().position(|&id| id == entity_id)
|
||||
// }
|
||||
|
||||
fn draw_hud<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> GameResult<()> {
|
||||
let lives = 3;
|
||||
let score_text = format!("{:02}", self.state.score);
|
||||
let x_offset = 4;
|
||||
let y_offset = 2;
|
||||
let lives_offset = 3;
|
||||
let score_offset = 7 - (score_text.len() as i32);
|
||||
self.state.text_texture.set_scale(1.0);
|
||||
if let Err(e) = self.state.text_texture.render(
|
||||
canvas,
|
||||
&mut self.state.atlas,
|
||||
&format!("{lives}UP HIGH SCORE "),
|
||||
UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
|
||||
) {
|
||||
tracing::error!("Failed to render HUD text: {}", e);
|
||||
}
|
||||
if let Err(e) = self.state.text_texture.render(
|
||||
canvas,
|
||||
&mut self.state.atlas,
|
||||
&score_text,
|
||||
UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
|
||||
) {
|
||||
tracing::error!("Failed to render score text: {}", e);
|
||||
}
|
||||
// fn find_ghost_by_id(&self, entity_id: EntityId) -> Option<usize> {
|
||||
// self.state.ghost_ids.iter().position(|&id| id == entity_id)
|
||||
// }
|
||||
|
||||
// Display FPS information in top-left corner
|
||||
// let fps_text = format!("FPS: {:.1} (1s) / {:.1} (10s)", self.fps_1s, self.fps_10s);
|
||||
// self.render_text_on(
|
||||
// pub fn draw<T: sdl2::render::RenderTarget>(&mut self, canvas: &mut Canvas<T>, backbuffer: &mut Texture) -> GameResult<()> {
|
||||
// // Only render the map texture once and cache it
|
||||
// if !self.state.map_rendered {
|
||||
// let mut map_texture = self
|
||||
// .state
|
||||
// .texture_creator
|
||||
// .create_texture_target(None, constants::CANVAS_SIZE.x, constants::CANVAS_SIZE.y)
|
||||
// .map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
|
||||
|
||||
// canvas
|
||||
// .with_texture_canvas(&mut map_texture, |map_canvas| {
|
||||
// let mut map_tiles = Vec::with_capacity(35);
|
||||
// for i in 0..35 {
|
||||
// let tile_name = format!("maze/tiles/{}.png", i);
|
||||
// let tile = SpriteAtlas::get_tile(&self.state.atlas, &tile_name).unwrap();
|
||||
// map_tiles.push(tile);
|
||||
// }
|
||||
// MapRenderer::render_map(map_canvas, &mut self.state.atlas, &mut map_tiles);
|
||||
// })
|
||||
// .map_err(|e| crate::error::GameError::Sdl(e.to_string()))?;
|
||||
// self.state.map_texture = Some(map_texture);
|
||||
// self.state.map_rendered = true;
|
||||
// }
|
||||
|
||||
// canvas.set_draw_color(Color::BLACK);
|
||||
// canvas.clear();
|
||||
// if let Some(ref map_texture) = self.state.map_texture {
|
||||
// canvas.copy(map_texture, None, None).unwrap();
|
||||
// }
|
||||
|
||||
// // Render all items
|
||||
// for item in &self.state.items {
|
||||
// if let Err(e) = item.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
// tracing::error!("Failed to render item: {}", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Render all ghosts
|
||||
// for ghost in &self.state.ghosts {
|
||||
// if let Err(e) = ghost.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
// tracing::error!("Failed to render ghost: {}", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if let Err(e) = self.state.pacman.render(canvas, &mut self.state.atlas, &self.state.map.graph) {
|
||||
// tracing::error!("Failed to render pacman: {}", e);
|
||||
// }
|
||||
|
||||
// if self.state.debug_mode {
|
||||
// if let Err(e) =
|
||||
// self.state
|
||||
// .map
|
||||
// .debug_render_with_cursor(canvas, &mut self.state.text_texture, &mut self.state.atlas, cursor_pos)
|
||||
// {
|
||||
// tracing::error!("Failed to render debug cursor: {}", e);
|
||||
// }
|
||||
// self.render_pathfinding_debug(canvas)?;
|
||||
// }
|
||||
// self.draw_hud(canvas)?;
|
||||
// canvas.present();
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
|
||||
// /// 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(())
|
||||
// }
|
||||
|
||||
// fn draw_hud<T: sdl2::render::RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> GameResult<()> {
|
||||
// let lives = 3;
|
||||
// let score_text = format!("{:02}", self.state.score);
|
||||
// let x_offset = 4;
|
||||
// let y_offset = 2;
|
||||
// let lives_offset = 3;
|
||||
// let score_offset = 7 - (score_text.len() as i32);
|
||||
// self.state.text_texture.set_scale(1.0);
|
||||
// if let Err(e) = self.state.text_texture.render(
|
||||
// canvas,
|
||||
// &*texture_creator,
|
||||
// &fps_text,
|
||||
// IVec2::new(10, 10),
|
||||
// Color::RGB(255, 255, 0), // Yellow color for FPS display
|
||||
// );
|
||||
// &mut self.state.atlas,
|
||||
// &format!("{lives}UP HIGH SCORE "),
|
||||
// glam::UVec2::new(8 * lives_offset as u32 + x_offset, y_offset),
|
||||
// ) {
|
||||
// tracing::error!("Failed to render HUD text: {}", e);
|
||||
// }
|
||||
// if let Err(e) = self.state.text_texture.render(
|
||||
// canvas,
|
||||
// &mut self.state.atlas,
|
||||
// &score_text,
|
||||
// glam::UVec2::new(8 * score_offset as u32 + x_offset, 8 + y_offset),
|
||||
// ) {
|
||||
// tracing::error!("Failed to render score text: {}", e);
|
||||
// }
|
||||
|
||||
Ok(())
|
||||
}
|
||||
// // Display FPS information in top-left corner
|
||||
// // let fps_text = format!("FPS: {:.1} (1s) / {:.1} (10s)", self.fps_1s, self.fps_10s);
|
||||
// // self.render_text_on(
|
||||
// // canvas,
|
||||
// // &*texture_creator,
|
||||
// // &fps_text,
|
||||
// // IVec2::new(10, 10),
|
||||
// // Color::RGB(255, 255, 0), // Yellow color for FPS display
|
||||
// // );
|
||||
|
||||
// Ok(())
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,146 +1,153 @@
|
||||
use sdl2::{
|
||||
image::LoadTexture,
|
||||
render::{Texture, TextureCreator},
|
||||
video::WindowContext,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
// use std::collections::VecDeque;
|
||||
|
||||
use crate::{
|
||||
asset::{get_asset_bytes, Asset},
|
||||
audio::Audio,
|
||||
constants::RAW_BOARD,
|
||||
entity::{
|
||||
collision::{Collidable, CollisionSystem},
|
||||
ghost::{Ghost, GhostType},
|
||||
item::Item,
|
||||
pacman::Pacman,
|
||||
},
|
||||
error::{GameError, GameResult, TextureError},
|
||||
game::EntityId,
|
||||
map::Map,
|
||||
texture::{
|
||||
sprite::{AtlasMapper, SpriteAtlas},
|
||||
text::TextTexture,
|
||||
},
|
||||
};
|
||||
// use sdl2::{
|
||||
// image::LoadTexture,
|
||||
// render::{Texture, TextureCreator},
|
||||
// video::WindowContext,
|
||||
// };
|
||||
// use smallvec::SmallVec;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/atlas_data.rs"));
|
||||
// use crate::{
|
||||
// asset::{get_asset_bytes, Asset},
|
||||
// audio::Audio,
|
||||
// constants::RAW_BOARD,
|
||||
// entity::{
|
||||
// collision::{Collidable, CollisionSystem, EntityId},
|
||||
// ghost::{Ghost, GhostType},
|
||||
// item::Item,
|
||||
// pacman::Pacman,
|
||||
// },
|
||||
// error::{GameError, GameResult, TextureError},
|
||||
// game::events::GameEvent,
|
||||
// map::builder::Map,
|
||||
// texture::{
|
||||
// sprite::{AtlasMapper, SpriteAtlas},
|
||||
// text::TextTexture,
|
||||
// },
|
||||
// };
|
||||
|
||||
/// The `GameState` struct holds all the essential data for the game.
|
||||
///
|
||||
/// This includes the score, map, entities (Pac-Man, ghosts, items),
|
||||
/// collision system, and rendering resources. By centralizing the game's state,
|
||||
/// we can cleanly separate it from the game's logic, making it easier to manage
|
||||
/// and reason about.
|
||||
pub struct GameState {
|
||||
pub score: u32,
|
||||
pub map: Map,
|
||||
pub pacman: Pacman,
|
||||
pub pacman_id: EntityId,
|
||||
pub ghosts: SmallVec<[Ghost; 4]>,
|
||||
pub ghost_ids: SmallVec<[EntityId; 4]>,
|
||||
pub items: Vec<Item>,
|
||||
pub item_ids: Vec<EntityId>,
|
||||
pub debug_mode: bool,
|
||||
// include!(concat!(env!("OUT_DIR"), "/atlas_data.rs"));
|
||||
|
||||
// Collision system
|
||||
pub(crate) collision_system: CollisionSystem,
|
||||
// /// The `GameState` struct holds all the essential data for the game.
|
||||
// ///
|
||||
// /// This includes the score, map, entities (Pac-Man, ghosts, items),
|
||||
// /// collision system, and rendering resources. By centralizing the game's state,
|
||||
// /// we can cleanly separate it from the game's logic, making it easier to manage
|
||||
// /// and reason about.
|
||||
// pub struct GameState {
|
||||
// pub paused: bool,
|
||||
|
||||
// Rendering resources
|
||||
pub(crate) atlas: SpriteAtlas,
|
||||
pub(crate) text_texture: TextTexture,
|
||||
// pub score: u32,
|
||||
// pub map: Map,
|
||||
// pub pacman: Pacman,
|
||||
// pub pacman_id: EntityId,
|
||||
// pub ghosts: SmallVec<[Ghost; 4]>,
|
||||
// pub ghost_ids: SmallVec<[EntityId; 4]>,
|
||||
// pub items: Vec<Item>,
|
||||
// pub item_ids: Vec<EntityId>,
|
||||
// pub debug_mode: bool,
|
||||
// pub event_queue: VecDeque<GameEvent>,
|
||||
|
||||
// Audio
|
||||
pub audio: Audio,
|
||||
// // Collision system
|
||||
// pub(crate) collision_system: CollisionSystem,
|
||||
|
||||
// Map texture pre-rendering
|
||||
pub(crate) map_texture: Option<Texture<'static>>,
|
||||
pub(crate) map_rendered: bool,
|
||||
pub(crate) texture_creator: &'static TextureCreator<WindowContext>,
|
||||
}
|
||||
// // Rendering resources
|
||||
// pub(crate) atlas: SpriteAtlas,
|
||||
// pub(crate) text_texture: TextTexture,
|
||||
|
||||
impl GameState {
|
||||
/// Creates a new `GameState` by initializing all the game's data.
|
||||
///
|
||||
/// This function sets up the map, Pac-Man, ghosts, items, collision system,
|
||||
/// and all rendering resources required to start the game. It returns a `GameResult`
|
||||
/// to handle any potential errors during initialization.
|
||||
pub fn new(texture_creator: &'static TextureCreator<WindowContext>) -> GameResult<Self> {
|
||||
let map = Map::new(RAW_BOARD)?;
|
||||
// // Audio
|
||||
// pub audio: Audio,
|
||||
|
||||
let start_node = map.start_positions.pacman;
|
||||
// // Map texture pre-rendering
|
||||
// pub(crate) map_texture: Option<Texture<'static>>,
|
||||
// pub(crate) map_rendered: bool,
|
||||
// pub(crate) texture_creator: &'static TextureCreator<WindowContext>,
|
||||
// }
|
||||
|
||||
let atlas_bytes = get_asset_bytes(Asset::Atlas)?;
|
||||
let atlas_texture = texture_creator.load_texture_bytes(&atlas_bytes).map_err(|e| {
|
||||
if e.to_string().contains("format") || e.to_string().contains("unsupported") {
|
||||
GameError::Texture(TextureError::InvalidFormat(format!("Unsupported texture format: {e}")))
|
||||
} else {
|
||||
GameError::Texture(TextureError::LoadFailed(e.to_string()))
|
||||
}
|
||||
})?;
|
||||
// impl GameState {
|
||||
// /// Creates a new `GameState` by initializing all the game's data.
|
||||
// ///
|
||||
// /// This function sets up the map, Pac-Man, ghosts, items, collision system,
|
||||
// /// and all rendering resources required to start the game. It returns a `GameResult`
|
||||
// /// to handle any potential errors during initialization.
|
||||
// pub fn new(texture_creator: &'static TextureCreator<WindowContext>) -> GameResult<Self> {
|
||||
// let map = Map::new(RAW_BOARD)?;
|
||||
|
||||
let atlas_mapper = AtlasMapper {
|
||||
frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(),
|
||||
};
|
||||
let atlas = SpriteAtlas::new(atlas_texture, atlas_mapper);
|
||||
// let start_node = map.start_positions.pacman;
|
||||
|
||||
let text_texture = TextTexture::new(1.0);
|
||||
let audio = Audio::new();
|
||||
let pacman = Pacman::new(&map.graph, start_node, &atlas)?;
|
||||
// let atlas_bytes = get_asset_bytes(Asset::Atlas)?;
|
||||
// let atlas_texture = texture_creator.load_texture_bytes(&atlas_bytes).map_err(|e| {
|
||||
// if e.to_string().contains("format") || e.to_string().contains("unsupported") {
|
||||
// GameError::Texture(TextureError::InvalidFormat(format!("Unsupported texture format: {e}")))
|
||||
// } else {
|
||||
// GameError::Texture(TextureError::LoadFailed(e.to_string()))
|
||||
// }
|
||||
// })?;
|
||||
|
||||
// Generate items (pellets and energizers)
|
||||
let items = map.generate_items(&atlas)?;
|
||||
// let atlas_mapper = AtlasMapper {
|
||||
// frames: ATLAS_FRAMES.into_iter().map(|(k, v)| (k.to_string(), *v)).collect(),
|
||||
// };
|
||||
// let atlas = SpriteAtlas::new(atlas_texture, atlas_mapper);
|
||||
|
||||
// Initialize collision system
|
||||
let mut collision_system = CollisionSystem::default();
|
||||
// let text_texture = TextTexture::new(1.0);
|
||||
// let audio = Audio::new();
|
||||
// let pacman = Pacman::new(&map.graph, start_node, &atlas)?;
|
||||
|
||||
// Register Pac-Man
|
||||
let pacman_id = collision_system.register_entity(pacman.position());
|
||||
// // Generate items (pellets and energizers)
|
||||
// let items = map.generate_items(&atlas)?;
|
||||
|
||||
// Register items
|
||||
let item_ids = items
|
||||
.iter()
|
||||
.map(|item| collision_system.register_entity(item.position()))
|
||||
.collect();
|
||||
// // Initialize collision system
|
||||
// let mut collision_system = CollisionSystem::default();
|
||||
|
||||
// Create and register ghosts
|
||||
let ghosts = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde]
|
||||
.iter()
|
||||
.zip(
|
||||
[
|
||||
map.start_positions.blinky,
|
||||
map.start_positions.pinky,
|
||||
map.start_positions.inky,
|
||||
map.start_positions.clyde,
|
||||
]
|
||||
.iter(),
|
||||
)
|
||||
.map(|(ghost_type, start_node)| Ghost::new(&map.graph, *start_node, *ghost_type, &atlas))
|
||||
.collect::<GameResult<SmallVec<[_; 4]>>>()?;
|
||||
// // Register Pac-Man
|
||||
// let pacman_id = collision_system.register_entity(pacman.position());
|
||||
|
||||
// Register ghosts
|
||||
let ghost_ids = ghosts
|
||||
.iter()
|
||||
.map(|ghost| collision_system.register_entity(ghost.position()))
|
||||
.collect();
|
||||
// // Register items
|
||||
// let item_ids = items
|
||||
// .iter()
|
||||
// .map(|item| collision_system.register_entity(item.position()))
|
||||
// .collect();
|
||||
|
||||
Ok(Self {
|
||||
map,
|
||||
atlas,
|
||||
pacman,
|
||||
pacman_id,
|
||||
ghosts,
|
||||
ghost_ids,
|
||||
items,
|
||||
item_ids,
|
||||
text_texture,
|
||||
audio,
|
||||
score: 0,
|
||||
debug_mode: false,
|
||||
collision_system,
|
||||
map_texture: None,
|
||||
map_rendered: false,
|
||||
texture_creator,
|
||||
})
|
||||
}
|
||||
}
|
||||
// // Create and register ghosts
|
||||
// let ghosts = [GhostType::Blinky, GhostType::Pinky, GhostType::Inky, GhostType::Clyde]
|
||||
// .iter()
|
||||
// .zip(
|
||||
// [
|
||||
// map.start_positions.blinky,
|
||||
// map.start_positions.pinky,
|
||||
// map.start_positions.inky,
|
||||
// map.start_positions.clyde,
|
||||
// ]
|
||||
// .iter(),
|
||||
// )
|
||||
// .map(|(ghost_type, start_node)| Ghost::new(&map.graph, *start_node, *ghost_type, &atlas))
|
||||
// .collect::<GameResult<SmallVec<[_; 4]>>>()?;
|
||||
|
||||
// // Register ghosts
|
||||
// let ghost_ids = ghosts
|
||||
// .iter()
|
||||
// .map(|ghost| collision_system.register_entity(ghost.position()))
|
||||
// .collect();
|
||||
|
||||
// Ok(Self {
|
||||
// paused: false,
|
||||
// map,
|
||||
// atlas,
|
||||
// pacman,
|
||||
// pacman_id,
|
||||
// ghosts,
|
||||
// ghost_ids,
|
||||
// items,
|
||||
// item_ids,
|
||||
// text_texture,
|
||||
// audio,
|
||||
// score: 0,
|
||||
// debug_mode: false,
|
||||
// collision_system,
|
||||
// map_texture: None,
|
||||
// map_rendered: false,
|
||||
// texture_creator,
|
||||
// event_queue: VecDeque::new(),
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -6,8 +6,10 @@ pub mod audio;
|
||||
pub mod constants;
|
||||
pub mod entity;
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
pub mod game;
|
||||
pub mod helpers;
|
||||
pub mod map;
|
||||
pub mod platform;
|
||||
pub mod systems;
|
||||
pub mod texture;
|
||||
|
||||
@@ -12,10 +12,12 @@ mod constants;
|
||||
|
||||
mod entity;
|
||||
mod error;
|
||||
mod events;
|
||||
mod game;
|
||||
mod helpers;
|
||||
mod map;
|
||||
mod platform;
|
||||
mod systems;
|
||||
mod texture;
|
||||
|
||||
/// The main entry point of the application.
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
//! Map construction and building functionality.
|
||||
|
||||
use crate::constants::{MapTile, BOARD_CELL_SIZE, CELL_SIZE, RAW_BOARD};
|
||||
use crate::constants::{MapTile, BOARD_CELL_SIZE, CELL_SIZE};
|
||||
use crate::entity::direction::Direction;
|
||||
use crate::entity::graph::{EdgePermissions, Graph, Node, NodeId};
|
||||
use crate::entity::item::{Item, ItemType};
|
||||
use crate::entity::graph::{EdgePermissions, Graph, Node};
|
||||
use crate::map::parser::MapTileParser;
|
||||
use crate::map::render::MapRenderer;
|
||||
use crate::texture::sprite::{Sprite, SpriteAtlas};
|
||||
use crate::systems::components::NodeId;
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
use bevy_ecs::resource::Resource;
|
||||
use glam::{IVec2, Vec2};
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
@@ -24,6 +25,7 @@ pub struct NodePositions {
|
||||
}
|
||||
|
||||
/// The main map structure containing the game board and navigation graph.
|
||||
#[derive(Resource)]
|
||||
pub struct Map {
|
||||
/// The node map for entity movement.
|
||||
pub graph: Graph,
|
||||
@@ -155,42 +157,42 @@ impl Map {
|
||||
}
|
||||
|
||||
/// Generates Item entities for pellets and energizers from the parsed map.
|
||||
pub fn generate_items(&self, atlas: &SpriteAtlas) -> GameResult<Vec<Item>> {
|
||||
// Pre-load sprites to avoid repeated texture lookups
|
||||
let pellet_sprite = SpriteAtlas::get_tile(atlas, "maze/pellet.png")
|
||||
.ok_or_else(|| MapError::InvalidConfig("Pellet texture not found".to_string()))?;
|
||||
let energizer_sprite = SpriteAtlas::get_tile(atlas, "maze/energizer.png")
|
||||
.ok_or_else(|| MapError::InvalidConfig("Energizer texture not found".to_string()))?;
|
||||
// pub fn generate_items(&self, atlas: &SpriteAtlas) -> GameResult<Vec<Item>> {
|
||||
// // Pre-load sprites to avoid repeated texture lookups
|
||||
// let pellet_sprite = SpriteAtlas::get_tile(atlas, "maze/pellet.png")
|
||||
// .ok_or_else(|| MapError::InvalidConfig("Pellet texture not found".to_string()))?;
|
||||
// let energizer_sprite = SpriteAtlas::get_tile(atlas, "maze/energizer.png")
|
||||
// .ok_or_else(|| MapError::InvalidConfig("Energizer texture not found".to_string()))?;
|
||||
|
||||
// Pre-allocate with estimated capacity (typical Pac-Man maps have ~240 pellets + 4 energizers)
|
||||
let mut items = Vec::with_capacity(250);
|
||||
// // Pre-allocate with estimated capacity (typical Pac-Man maps have ~240 pellets + 4 energizers)
|
||||
// let mut items = Vec::with_capacity(250);
|
||||
|
||||
// Parse the raw board once
|
||||
let parsed_map = MapTileParser::parse_board(RAW_BOARD)?;
|
||||
let map = parsed_map.tiles;
|
||||
// // Parse the raw board once
|
||||
// let parsed_map = MapTileParser::parse_board(RAW_BOARD)?;
|
||||
// let map = parsed_map.tiles;
|
||||
|
||||
// Iterate through the map and collect items more efficiently
|
||||
for (x, row) in map.iter().enumerate() {
|
||||
for (y, tile) in row.iter().enumerate() {
|
||||
match tile {
|
||||
MapTile::Pellet | MapTile::PowerPellet => {
|
||||
let grid_pos = IVec2::new(x as i32, y as i32);
|
||||
if let Some(&node_id) = self.grid_to_node.get(&grid_pos) {
|
||||
let (item_type, sprite) = match tile {
|
||||
MapTile::Pellet => (ItemType::Pellet, Sprite::new(pellet_sprite)),
|
||||
MapTile::PowerPellet => (ItemType::Energizer, Sprite::new(energizer_sprite)),
|
||||
_ => unreachable!(), // We already filtered for these types
|
||||
};
|
||||
items.push(Item::new(node_id, item_type, sprite));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
// // Iterate through the map and collect items more efficiently
|
||||
// for (x, row) in map.iter().enumerate() {
|
||||
// for (y, tile) in row.iter().enumerate() {
|
||||
// match tile {
|
||||
// MapTile::Pellet | MapTile::PowerPellet => {
|
||||
// let grid_pos = IVec2::new(x as i32, y as i32);
|
||||
// if let Some(&node_id) = self.grid_to_node.get(&grid_pos) {
|
||||
// let (item_type, sprite) = match tile {
|
||||
// MapTile::Pellet => (ItemType::Pellet, Sprite::new(pellet_sprite)),
|
||||
// MapTile::PowerPellet => (ItemType::Energizer, Sprite::new(energizer_sprite)),
|
||||
// _ => unreachable!(), // We already filtered for these types
|
||||
// };
|
||||
// items.push(Item::new(node_id, item_type, sprite));
|
||||
// }
|
||||
// }
|
||||
// _ => {}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Ok(items)
|
||||
}
|
||||
// Ok(items)
|
||||
// }
|
||||
|
||||
/// Renders a debug visualization with cursor-based highlighting.
|
||||
///
|
||||
|
||||
@@ -4,6 +4,3 @@ pub mod builder;
|
||||
pub mod layout;
|
||||
pub mod parser;
|
||||
pub mod render;
|
||||
|
||||
// Re-export main types for convenience
|
||||
pub use builder::Map;
|
||||
|
||||
@@ -11,8 +11,12 @@ use crate::platform::Platform;
|
||||
pub struct DesktopPlatform;
|
||||
|
||||
impl Platform for DesktopPlatform {
|
||||
fn sleep(&self, duration: Duration) {
|
||||
fn sleep(&self, duration: Duration, focused: bool) {
|
||||
if focused {
|
||||
spin_sleep::sleep(duration);
|
||||
} else {
|
||||
std::thread::sleep(duration);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_time(&self) -> f64 {
|
||||
|
||||
@@ -11,7 +11,7 @@ use crate::platform::Platform;
|
||||
pub struct EmscriptenPlatform;
|
||||
|
||||
impl Platform for EmscriptenPlatform {
|
||||
fn sleep(&self, duration: Duration) {
|
||||
fn sleep(&self, duration: Duration, _focused: bool) {
|
||||
unsafe {
|
||||
emscripten_sleep(duration.as_millis() as u32);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ pub mod emscripten;
|
||||
/// Platform abstraction trait that defines cross-platform functionality.
|
||||
pub trait Platform {
|
||||
/// Sleep for the specified duration using platform-appropriate method.
|
||||
fn sleep(&self, duration: Duration);
|
||||
fn sleep(&self, duration: Duration, focused: bool);
|
||||
|
||||
/// Get the current time in seconds since some reference point.
|
||||
/// This is available for future use in timing and performance monitoring.
|
||||
|
||||
149
src/systems/components.rs
Normal file
149
src/systems/components.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
use bevy_ecs::{bundle::Bundle, component::Component, resource::Resource};
|
||||
use glam::Vec2;
|
||||
|
||||
use crate::{
|
||||
entity::{direction::Direction, graph::Graph},
|
||||
error::{EntityError, GameResult},
|
||||
texture::{animated::AnimatedTexture, sprite::AtlasTile},
|
||||
};
|
||||
|
||||
/// A tag component for entities that are controlled by the player.
|
||||
#[derive(Default, Component)]
|
||||
pub struct PlayerControlled;
|
||||
|
||||
/// A tag component denoting the type of entity.
|
||||
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum EntityType {
|
||||
Player,
|
||||
Ghost,
|
||||
Pellet,
|
||||
PowerPellet,
|
||||
Wall,
|
||||
}
|
||||
|
||||
/// A component for entities that have a sprite, with a layer for ordering.
|
||||
///
|
||||
/// This is intended to be modified by other entities allowing animation.
|
||||
#[derive(Component)]
|
||||
pub struct Renderable {
|
||||
pub sprite: AtlasTile,
|
||||
pub layer: u8,
|
||||
}
|
||||
|
||||
/// A component for entities that have a directional animated texture.
|
||||
#[derive(Component)]
|
||||
pub struct DirectionalAnimated {
|
||||
pub textures: [Option<AnimatedTexture>; 4],
|
||||
pub stopped_textures: [Option<AnimatedTexture>; 4],
|
||||
}
|
||||
|
||||
/// A unique identifier for a node, represented by its index in the graph's storage.
|
||||
pub type NodeId = usize;
|
||||
|
||||
/// Represents the current position of an entity traversing the graph.
|
||||
///
|
||||
/// This enum allows for precise tracking of whether an entity is exactly at a node
|
||||
/// or moving along an edge between two nodes.
|
||||
#[derive(Component, Debug, Copy, Clone, PartialEq)]
|
||||
pub enum Position {
|
||||
/// The traverser is located exactly at a node.
|
||||
AtNode(NodeId),
|
||||
/// The traverser is on an edge between two nodes.
|
||||
BetweenNodes {
|
||||
from: NodeId,
|
||||
to: NodeId,
|
||||
/// The floating-point distance traversed along the edge from the `from` node.
|
||||
traversed: f32,
|
||||
},
|
||||
}
|
||||
|
||||
impl Position {
|
||||
/// Calculates the current pixel position in the game world.
|
||||
///
|
||||
/// Converts the graph position to screen coordinates, accounting for
|
||||
/// the board offset and centering the sprite.
|
||||
pub fn get_pixel_pos(&self, graph: &Graph) -> GameResult<Vec2> {
|
||||
let pos = match self {
|
||||
Position::AtNode(node_id) => {
|
||||
let node = graph.get_node(*node_id).ok_or(EntityError::NodeNotFound(*node_id))?;
|
||||
node.position
|
||||
}
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
let from_node = graph.get_node(*from).ok_or(EntityError::NodeNotFound(*from))?;
|
||||
let to_node = graph.get_node(*to).ok_or(EntityError::NodeNotFound(*to))?;
|
||||
let edge = graph
|
||||
.find_edge(*from, *to)
|
||||
.ok_or(EntityError::EdgeNotFound { from: *from, to: *to })?;
|
||||
from_node.position + (to_node.position - from_node.position) * (traversed / edge.distance)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Vec2::new(
|
||||
pos.x + crate::constants::BOARD_PIXEL_OFFSET.x as f32,
|
||||
pos.y + crate::constants::BOARD_PIXEL_OFFSET.y as f32,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Position {
|
||||
fn default() -> Self {
|
||||
Position::AtNode(0)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Position {
|
||||
/// Returns `true` if the position is exactly at a node.
|
||||
pub fn is_at_node(&self) -> bool {
|
||||
matches!(self, Position::AtNode(_))
|
||||
}
|
||||
|
||||
/// Returns the `NodeId` of the current or most recently departed node.
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn from_node_id(&self) -> NodeId {
|
||||
match self {
|
||||
Position::AtNode(id) => *id,
|
||||
Position::BetweenNodes { from, .. } => *from,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the `NodeId` of the destination node, if currently on an edge.
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub fn to_node_id(&self) -> Option<NodeId> {
|
||||
match self {
|
||||
Position::AtNode(_) => None,
|
||||
Position::BetweenNodes { to, .. } => Some(*to),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the traverser is stopped at a node.
|
||||
pub fn is_stopped(&self) -> bool {
|
||||
matches!(self, Position::AtNode(_))
|
||||
}
|
||||
}
|
||||
|
||||
/// A component for entities that have a velocity, with a direction and speed.
|
||||
#[derive(Default, Component)]
|
||||
pub struct Velocity {
|
||||
pub direction: Direction,
|
||||
pub next_direction: Option<(Direction, u8)>,
|
||||
pub speed: f32,
|
||||
}
|
||||
|
||||
#[derive(Bundle)]
|
||||
pub struct PlayerBundle {
|
||||
pub player: PlayerControlled,
|
||||
pub position: Position,
|
||||
pub velocity: Velocity,
|
||||
pub sprite: Renderable,
|
||||
pub directional_animated: DirectionalAnimated,
|
||||
pub entity_type: EntityType,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct GlobalState {
|
||||
pub exit: bool,
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct DeltaTime(pub f32);
|
||||
45
src/systems/control.rs
Normal file
45
src/systems/control.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use bevy_ecs::{
|
||||
event::{EventReader, EventWriter},
|
||||
system::{Query, ResMut},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
error::GameError,
|
||||
events::GameEvent,
|
||||
systems::{
|
||||
components::{GlobalState, PlayerControlled, Velocity},
|
||||
input::GameCommand,
|
||||
},
|
||||
};
|
||||
|
||||
// Handles
|
||||
pub fn player_system(
|
||||
mut events: EventReader<GameEvent>,
|
||||
mut state: ResMut<GlobalState>,
|
||||
mut players: Query<(&PlayerControlled, &mut Velocity)>,
|
||||
mut errors: EventWriter<GameError>,
|
||||
) {
|
||||
// Get the player's velocity (handling to ensure there is only one player)
|
||||
let mut velocity = match players.single_mut() {
|
||||
Ok((_, velocity)) => velocity,
|
||||
Err(e) => {
|
||||
errors.write(GameError::InvalidState(format!("Player not found: {}", e)).into());
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
// Handle events
|
||||
for event in events.read() {
|
||||
match event {
|
||||
GameEvent::Command(command) => match command {
|
||||
GameCommand::MovePlayer(direction) => {
|
||||
velocity.next_direction = Some((*direction, 90));
|
||||
}
|
||||
GameCommand::Exit => {
|
||||
state.exit = true;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
69
src/systems/input.rs
Normal file
69
src/systems/input.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use bevy_ecs::{
|
||||
event::EventWriter,
|
||||
resource::Resource,
|
||||
system::{NonSendMut, Res},
|
||||
};
|
||||
use sdl2::{event::Event, keyboard::Keycode, EventPump};
|
||||
|
||||
use crate::{entity::direction::Direction, events::GameEvent};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum GameCommand {
|
||||
MovePlayer(Direction),
|
||||
Exit,
|
||||
TogglePause,
|
||||
ToggleDebug,
|
||||
MuteAudio,
|
||||
ResetLevel,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Resource)]
|
||||
pub struct Bindings {
|
||||
key_bindings: HashMap<Keycode, GameCommand>,
|
||||
}
|
||||
|
||||
impl Default for Bindings {
|
||||
fn default() -> Self {
|
||||
let mut key_bindings = HashMap::new();
|
||||
|
||||
// Player movement
|
||||
key_bindings.insert(Keycode::Up, GameCommand::MovePlayer(Direction::Up));
|
||||
key_bindings.insert(Keycode::W, GameCommand::MovePlayer(Direction::Up));
|
||||
key_bindings.insert(Keycode::Down, GameCommand::MovePlayer(Direction::Down));
|
||||
key_bindings.insert(Keycode::S, GameCommand::MovePlayer(Direction::Down));
|
||||
key_bindings.insert(Keycode::Left, GameCommand::MovePlayer(Direction::Left));
|
||||
key_bindings.insert(Keycode::A, GameCommand::MovePlayer(Direction::Left));
|
||||
key_bindings.insert(Keycode::Right, GameCommand::MovePlayer(Direction::Right));
|
||||
key_bindings.insert(Keycode::D, GameCommand::MovePlayer(Direction::Right));
|
||||
|
||||
// Game actions
|
||||
key_bindings.insert(Keycode::P, GameCommand::TogglePause);
|
||||
key_bindings.insert(Keycode::Space, GameCommand::ToggleDebug);
|
||||
key_bindings.insert(Keycode::M, GameCommand::MuteAudio);
|
||||
key_bindings.insert(Keycode::R, GameCommand::ResetLevel);
|
||||
key_bindings.insert(Keycode::Escape, GameCommand::Exit);
|
||||
key_bindings.insert(Keycode::Q, GameCommand::Exit);
|
||||
|
||||
Self { key_bindings }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn input_system(bindings: Res<Bindings>, mut writer: EventWriter<GameEvent>, mut pump: NonSendMut<&'static mut EventPump>) {
|
||||
for event in pump.poll_iter() {
|
||||
match event {
|
||||
Event::Quit { .. } => {
|
||||
writer.write(GameEvent::Command(GameCommand::Exit));
|
||||
}
|
||||
Event::KeyDown { keycode: Some(key), .. } => {
|
||||
let command = bindings.key_bindings.get(&key).copied();
|
||||
if let Some(command) = command {
|
||||
tracing::info!("triggering command: {:?}", command);
|
||||
writer.write(GameEvent::Command(command));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/systems/mod.rs
Normal file
10
src/systems/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
//! The Entity-Component-System (ECS) module.
|
||||
//!
|
||||
//! This module contains all the ECS-related logic, including components, systems,
|
||||
//! and resources.
|
||||
|
||||
pub mod components;
|
||||
pub mod control;
|
||||
pub mod input;
|
||||
pub mod movement;
|
||||
pub mod render;
|
||||
133
src/systems/movement.rs
Normal file
133
src/systems/movement.rs
Normal file
@@ -0,0 +1,133 @@
|
||||
use crate::entity::graph::{Edge, EdgePermissions, Graph};
|
||||
use crate::error::{EntityError, GameError};
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::components::{DeltaTime, EntityType, Position, Velocity};
|
||||
use bevy_ecs::event::EventWriter;
|
||||
use bevy_ecs::system::{Query, Res};
|
||||
|
||||
fn can_traverse(entity_type: EntityType, edge: Edge) -> bool {
|
||||
match entity_type {
|
||||
EntityType::Player => matches!(edge.permissions, EdgePermissions::All),
|
||||
EntityType::Ghost => matches!(edge.permissions, EdgePermissions::All | EdgePermissions::GhostsOnly),
|
||||
_ => matches!(edge.permissions, EdgePermissions::All),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn movement_system(
|
||||
map: Res<Map>,
|
||||
delta_time: Res<DeltaTime>,
|
||||
mut entities: Query<(&mut Velocity, &mut Position, &EntityType)>,
|
||||
mut errors: EventWriter<GameError>,
|
||||
) {
|
||||
for (mut velocity, mut position, entity_type) in entities.iter_mut() {
|
||||
let distance = velocity.speed * 60.0 * delta_time.0;
|
||||
|
||||
// Decrement the remaining frames for the next direction
|
||||
if let Some((direction, remaining)) = velocity.next_direction {
|
||||
if remaining > 0 {
|
||||
velocity.next_direction = Some((direction, remaining - 1));
|
||||
} else {
|
||||
velocity.next_direction = None;
|
||||
}
|
||||
}
|
||||
|
||||
match *position {
|
||||
Position::AtNode(node_id) => {
|
||||
// We're not moving, but a buffered direction is available.
|
||||
if let Some((next_direction, _)) = velocity.next_direction {
|
||||
if let Some(edge) = map.graph.find_edge_in_direction(node_id, next_direction) {
|
||||
if can_traverse(*entity_type, edge) {
|
||||
// Start moving in that direction
|
||||
*position = Position::BetweenNodes {
|
||||
from: node_id,
|
||||
to: edge.target,
|
||||
traversed: distance,
|
||||
};
|
||||
velocity.direction = next_direction;
|
||||
velocity.next_direction = None;
|
||||
}
|
||||
} else {
|
||||
errors.write(
|
||||
EntityError::InvalidMovement(format!(
|
||||
"No edge found in direction {:?} from node {}",
|
||||
next_direction, node_id
|
||||
))
|
||||
.into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
// There is no point in any of the next logic if we don't travel at all
|
||||
if distance <= 0.0 {
|
||||
return;
|
||||
}
|
||||
|
||||
let edge = map
|
||||
.graph
|
||||
.find_edge(from, to)
|
||||
.ok_or_else(|| {
|
||||
errors.write(
|
||||
EntityError::InvalidMovement(format!(
|
||||
"Inconsistent state: Traverser is on a non-existent edge from {} to {}.",
|
||||
from, to
|
||||
))
|
||||
.into(),
|
||||
);
|
||||
return;
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let new_traversed = traversed + distance;
|
||||
|
||||
if new_traversed < edge.distance {
|
||||
// Still on the same edge, just update the distance.
|
||||
*position = Position::BetweenNodes {
|
||||
from,
|
||||
to,
|
||||
traversed: new_traversed,
|
||||
};
|
||||
} else {
|
||||
let overflow = new_traversed - edge.distance;
|
||||
let mut moved = false;
|
||||
|
||||
// If we buffered a direction, try to find an edge in that direction
|
||||
if let Some((next_dir, _)) = velocity.next_direction {
|
||||
if let Some(edge) = map.graph.find_edge_in_direction(to, next_dir) {
|
||||
if can_traverse(*entity_type, edge) {
|
||||
*position = Position::BetweenNodes {
|
||||
from: to,
|
||||
to: edge.target,
|
||||
traversed: overflow,
|
||||
};
|
||||
|
||||
velocity.direction = next_dir; // Remember our new direction
|
||||
velocity.next_direction = None; // Consume the buffered direction
|
||||
moved = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we didn't move, try to continue in the current direction
|
||||
if !moved {
|
||||
if let Some(edge) = map.graph.find_edge_in_direction(to, velocity.direction) {
|
||||
if can_traverse(*entity_type, edge) {
|
||||
*position = Position::BetweenNodes {
|
||||
from: to,
|
||||
to: edge.target,
|
||||
traversed: overflow,
|
||||
};
|
||||
} else {
|
||||
*position = Position::AtNode(to);
|
||||
velocity.next_direction = None;
|
||||
}
|
||||
} else {
|
||||
*position = Position::AtNode(to);
|
||||
velocity.next_direction = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
107
src/systems/render.rs
Normal file
107
src/systems/render.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use crate::error::{GameError, TextureError};
|
||||
use crate::map::builder::Map;
|
||||
use crate::systems::components::{DeltaTime, DirectionalAnimated, Position, Renderable, Velocity};
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
use bevy_ecs::entity::Entity;
|
||||
use bevy_ecs::event::EventWriter;
|
||||
use bevy_ecs::system::{NonSendMut, Query, Res};
|
||||
use sdl2::render::{Canvas, Texture};
|
||||
use sdl2::video::Window;
|
||||
|
||||
/// Updates the directional animated texture of an entity.
|
||||
///
|
||||
/// This runs before the render system so it can update the sprite based on the current direction of travel, as well as whether the entity is moving.
|
||||
pub fn directional_render_system(
|
||||
dt: Res<DeltaTime>,
|
||||
mut renderables: Query<(&Velocity, &mut DirectionalAnimated, &mut Renderable, &Position)>,
|
||||
mut errors: EventWriter<GameError>,
|
||||
) {
|
||||
for (velocity, mut texture, mut renderable, position) in renderables.iter_mut() {
|
||||
let stopped = matches!(position, Position::AtNode(_));
|
||||
let texture = if stopped {
|
||||
texture.stopped_textures[velocity.direction.as_usize()].as_mut()
|
||||
} else {
|
||||
texture.textures[velocity.direction.as_usize()].as_mut()
|
||||
};
|
||||
|
||||
if let Some(texture) = texture {
|
||||
if !stopped {
|
||||
texture.tick(dt.0);
|
||||
}
|
||||
renderable.sprite = *texture.current_tile();
|
||||
} else {
|
||||
errors.write(TextureError::RenderFailed(format!("Entity has no texture")).into());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A non-send resource for the map texture. This just wraps the texture with a type so it can be differentiated when exposed as a resource.
|
||||
pub struct MapTextureResource(pub Texture<'static>);
|
||||
|
||||
/// A non-send resource for the backbuffer texture. This just wraps the texture with a type so it can be differentiated when exposed as a resource.
|
||||
pub struct BackbufferResource(pub Texture<'static>);
|
||||
|
||||
pub fn render_system(
|
||||
mut canvas: NonSendMut<&mut Canvas<Window>>,
|
||||
map_texture: NonSendMut<MapTextureResource>,
|
||||
mut backbuffer: NonSendMut<BackbufferResource>,
|
||||
mut atlas: NonSendMut<SpriteAtlas>,
|
||||
map: Res<Map>,
|
||||
renderables: Query<(Entity, &mut Renderable, &Position)>,
|
||||
mut errors: EventWriter<GameError>,
|
||||
) {
|
||||
// Clear the main canvas first
|
||||
canvas.set_draw_color(sdl2::pixels::Color::BLACK);
|
||||
canvas.clear();
|
||||
|
||||
// Render to backbuffer
|
||||
canvas
|
||||
.with_texture_canvas(&mut backbuffer.0, |backbuffer_canvas| {
|
||||
// Clear the backbuffer
|
||||
backbuffer_canvas.set_draw_color(sdl2::pixels::Color::BLACK);
|
||||
backbuffer_canvas.clear();
|
||||
|
||||
// Copy the pre-rendered map texture to the backbuffer
|
||||
backbuffer_canvas
|
||||
.copy(&map_texture.0, None, None)
|
||||
.err()
|
||||
.map(|e| errors.write(TextureError::RenderFailed(e.to_string()).into()));
|
||||
|
||||
// Render all entities to the backbuffer
|
||||
for (_, mut renderable, position) in renderables
|
||||
// .iter_mut()
|
||||
// .sort_by_key::<&mut Renderable, _, _>(|(renderable, renderable, renderable)| renderable.layer)
|
||||
// .collect()
|
||||
{
|
||||
let pos = position.get_pixel_pos(&map.graph);
|
||||
match pos {
|
||||
Ok(pos) => {
|
||||
let dest = crate::helpers::centered_with_size(
|
||||
glam::IVec2::new(pos.x as i32, pos.y as i32),
|
||||
glam::UVec2::new(renderable.sprite.size.x as u32, renderable.sprite.size.y as u32),
|
||||
);
|
||||
|
||||
renderable
|
||||
.sprite
|
||||
.render(backbuffer_canvas, &mut atlas, dest)
|
||||
.err()
|
||||
.map(|e| errors.write(TextureError::RenderFailed(e.to_string()).into()));
|
||||
}
|
||||
Err(e) => {
|
||||
errors.write(e.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.err()
|
||||
.map(|e| errors.write(TextureError::RenderFailed(e.to_string()).into()));
|
||||
|
||||
// Copy backbuffer to main canvas and present
|
||||
canvas
|
||||
.copy(&backbuffer.0, None, None)
|
||||
.err()
|
||||
.map(|e| errors.write(TextureError::RenderFailed(e.to_string()).into()));
|
||||
|
||||
canvas.present();
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
use sdl2::rect::Rect;
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
|
||||
use crate::error::{AnimatedTextureError, GameError, GameResult, TextureError};
|
||||
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
||||
use crate::texture::sprite::AtlasTile;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AnimatedTexture {
|
||||
@@ -40,12 +37,6 @@ impl AnimatedTexture {
|
||||
&self.tiles[self.current_frame]
|
||||
}
|
||||
|
||||
pub fn render<T: RenderTarget>(&self, canvas: &mut Canvas<T>, atlas: &mut SpriteAtlas, dest: Rect) -> GameResult<()> {
|
||||
let mut tile = *self.current_tile();
|
||||
tile.render(canvas, atlas, dest)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the current frame index.
|
||||
#[allow(dead_code)]
|
||||
pub fn current_frame(&self) -> usize {
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
use sdl2::rect::Rect;
|
||||
use sdl2::render::{Canvas, RenderTarget};
|
||||
|
||||
use crate::entity::direction::Direction;
|
||||
use crate::error::GameResult;
|
||||
use crate::texture::animated::AnimatedTexture;
|
||||
use crate::texture::sprite::SpriteAtlas;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DirectionalAnimatedTexture {
|
||||
textures: [Option<AnimatedTexture>; 4],
|
||||
stopped_textures: [Option<AnimatedTexture>; 4],
|
||||
}
|
||||
|
||||
impl DirectionalAnimatedTexture {
|
||||
pub fn new(textures: [Option<AnimatedTexture>; 4], stopped_textures: [Option<AnimatedTexture>; 4]) -> Self {
|
||||
Self {
|
||||
textures,
|
||||
stopped_textures,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tick(&mut self, dt: f32) {
|
||||
for texture in self.textures.iter_mut().flatten() {
|
||||
texture.tick(dt);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render<T: RenderTarget>(
|
||||
&self,
|
||||
canvas: &mut Canvas<T>,
|
||||
atlas: &mut SpriteAtlas,
|
||||
dest: Rect,
|
||||
direction: Direction,
|
||||
) -> GameResult<()> {
|
||||
if let Some(texture) = &self.textures[direction.as_usize()] {
|
||||
texture.render(canvas, atlas, dest)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_stopped<T: RenderTarget>(
|
||||
&self,
|
||||
canvas: &mut Canvas<T>,
|
||||
atlas: &mut SpriteAtlas,
|
||||
dest: Rect,
|
||||
direction: Direction,
|
||||
) -> GameResult<()> {
|
||||
if let Some(texture) = &self.stopped_textures[direction.as_usize()] {
|
||||
texture.render(canvas, atlas, dest)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the texture has a direction.
|
||||
#[allow(dead_code)]
|
||||
pub fn has_direction(&self, direction: Direction) -> bool {
|
||||
self.textures[direction.as_usize()].is_some()
|
||||
}
|
||||
|
||||
/// Returns true if the texture has a stopped direction.
|
||||
#[allow(dead_code)]
|
||||
pub fn has_stopped_direction(&self, direction: Direction) -> bool {
|
||||
self.stopped_textures[direction.as_usize()].is_some()
|
||||
}
|
||||
|
||||
/// Returns the number of textures.
|
||||
#[allow(dead_code)]
|
||||
pub fn texture_count(&self) -> usize {
|
||||
self.textures.iter().filter(|t| t.is_some()).count()
|
||||
}
|
||||
|
||||
/// Returns the number of stopped textures.
|
||||
#[allow(dead_code)]
|
||||
pub fn stopped_texture_count(&self) -> usize {
|
||||
self.stopped_textures.iter().filter(|t| t.is_some()).count()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
pub mod animated;
|
||||
pub mod blinking;
|
||||
pub mod directional;
|
||||
pub mod sprite;
|
||||
pub mod text;
|
||||
|
||||
@@ -8,33 +8,6 @@ use std::collections::HashMap;
|
||||
|
||||
use crate::error::TextureError;
|
||||
|
||||
/// A simple sprite for stationary items like pellets and energizers.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Sprite {
|
||||
pub atlas_tile: AtlasTile,
|
||||
}
|
||||
|
||||
impl Sprite {
|
||||
pub fn new(atlas_tile: AtlasTile) -> Self {
|
||||
Self { atlas_tile }
|
||||
}
|
||||
|
||||
pub fn render<C: RenderTarget>(
|
||||
&self,
|
||||
canvas: &mut Canvas<C>,
|
||||
atlas: &mut SpriteAtlas,
|
||||
position: glam::Vec2,
|
||||
) -> Result<(), TextureError> {
|
||||
let dest = crate::helpers::centered_with_size(
|
||||
glam::IVec2::new(position.x as i32, position.y as i32),
|
||||
glam::UVec2::new(self.atlas_tile.size.x as u32, self.atlas_tile.size.y as u32),
|
||||
);
|
||||
let mut tile = self.atlas_tile;
|
||||
tile.render(canvas, atlas, dest)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
pub struct AtlasMapper {
|
||||
pub frames: HashMap<String, MapperFrame>,
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
use pacman::entity::collision::{Collidable, CollisionSystem};
|
||||
use pacman::entity::traversal::Position;
|
||||
|
||||
struct MockCollidable {
|
||||
pos: Position,
|
||||
}
|
||||
|
||||
impl Collidable for MockCollidable {
|
||||
fn position(&self) -> Position {
|
||||
self.pos
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_colliding_with() {
|
||||
let entity1 = MockCollidable {
|
||||
pos: Position::AtNode(1),
|
||||
};
|
||||
let entity2 = MockCollidable {
|
||||
pos: Position::AtNode(1),
|
||||
};
|
||||
let entity3 = MockCollidable {
|
||||
pos: Position::AtNode(2),
|
||||
};
|
||||
let entity4 = MockCollidable {
|
||||
pos: Position::BetweenNodes {
|
||||
from: 1,
|
||||
to: 2,
|
||||
traversed: 0.5,
|
||||
},
|
||||
};
|
||||
|
||||
assert!(entity1.is_colliding_with(&entity2));
|
||||
assert!(!entity1.is_colliding_with(&entity3));
|
||||
assert!(entity1.is_colliding_with(&entity4));
|
||||
assert!(entity3.is_colliding_with(&entity4));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collision_system_register_and_query() {
|
||||
let mut collision_system = CollisionSystem::default();
|
||||
|
||||
let pos1 = Position::AtNode(1);
|
||||
let entity1 = collision_system.register_entity(pos1);
|
||||
|
||||
let pos2 = Position::BetweenNodes {
|
||||
from: 1,
|
||||
to: 2,
|
||||
traversed: 0.5,
|
||||
};
|
||||
let entity2 = collision_system.register_entity(pos2);
|
||||
|
||||
let pos3 = Position::AtNode(3);
|
||||
let entity3 = collision_system.register_entity(pos3);
|
||||
|
||||
// Test entities_at_node
|
||||
assert_eq!(collision_system.entities_at_node(1), &[entity1, entity2]);
|
||||
assert_eq!(collision_system.entities_at_node(2), &[entity2]);
|
||||
assert_eq!(collision_system.entities_at_node(3), &[entity3]);
|
||||
assert_eq!(collision_system.entities_at_node(4), &[] as &[u32]);
|
||||
|
||||
// Test potential_collisions
|
||||
let mut collisions1 = collision_system.potential_collisions(&pos1);
|
||||
collisions1.sort_unstable();
|
||||
assert_eq!(collisions1, vec![entity1, entity2]);
|
||||
|
||||
let mut collisions2 = collision_system.potential_collisions(&pos2);
|
||||
collisions2.sort_unstable();
|
||||
assert_eq!(collisions2, vec![entity1, entity2]);
|
||||
|
||||
let mut collisions3 = collision_system.potential_collisions(&pos3);
|
||||
collisions3.sort_unstable();
|
||||
assert_eq!(collisions3, vec![entity3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collision_system_update() {
|
||||
let mut collision_system = CollisionSystem::default();
|
||||
|
||||
let entity1 = collision_system.register_entity(Position::AtNode(1));
|
||||
|
||||
assert_eq!(collision_system.entities_at_node(1), &[entity1]);
|
||||
assert_eq!(collision_system.entities_at_node(2), &[] as &[u32]);
|
||||
|
||||
collision_system.update_position(entity1, Position::AtNode(2));
|
||||
|
||||
assert_eq!(collision_system.entities_at_node(1), &[] as &[u32]);
|
||||
assert_eq!(collision_system.entities_at_node(2), &[entity1]);
|
||||
|
||||
collision_system.update_position(
|
||||
entity1,
|
||||
Position::BetweenNodes {
|
||||
from: 2,
|
||||
to: 3,
|
||||
traversed: 0.1,
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(collision_system.entities_at_node(1), &[] as &[u32]);
|
||||
assert_eq!(collision_system.entities_at_node(2), &[entity1]);
|
||||
assert_eq!(collision_system.entities_at_node(3), &[entity1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_collision_system_remove() {
|
||||
let mut collision_system = CollisionSystem::default();
|
||||
|
||||
let entity1 = collision_system.register_entity(Position::AtNode(1));
|
||||
let entity2 = collision_system.register_entity(Position::AtNode(1));
|
||||
|
||||
assert_eq!(collision_system.entities_at_node(1), &[entity1, entity2]);
|
||||
|
||||
collision_system.remove_entity(entity1);
|
||||
|
||||
assert_eq!(collision_system.entities_at_node(1), &[entity2]);
|
||||
|
||||
collision_system.remove_entity(entity2);
|
||||
assert_eq!(collision_system.entities_at_node(1), &[] as &[u32]);
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use pacman::{
|
||||
asset::{get_asset_bytes, Asset},
|
||||
game::state::ATLAS_FRAMES,
|
||||
game::ATLAS_FRAMES,
|
||||
texture::sprite::{AtlasMapper, SpriteAtlas},
|
||||
};
|
||||
use sdl2::{
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
use glam::U16Vec2;
|
||||
use pacman::entity::direction::Direction;
|
||||
use pacman::texture::animated::AnimatedTexture;
|
||||
use pacman::texture::directional::DirectionalAnimatedTexture;
|
||||
use pacman::texture::sprite::AtlasTile;
|
||||
use sdl2::pixels::Color;
|
||||
|
||||
fn mock_atlas_tile(id: u32) -> AtlasTile {
|
||||
AtlasTile {
|
||||
pos: U16Vec2::new(0, 0),
|
||||
size: U16Vec2::new(16, 16),
|
||||
color: Some(Color::RGB(id as u8, 0, 0)),
|
||||
}
|
||||
}
|
||||
|
||||
fn mock_animated_texture(id: u32) -> AnimatedTexture {
|
||||
AnimatedTexture::new(vec![mock_atlas_tile(id)], 0.1).expect("Invalid frame duration")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_directional_texture_partial_directions() {
|
||||
let mut textures = [None, None, None, None];
|
||||
textures[Direction::Up.as_usize()] = Some(mock_animated_texture(1));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, [None, None, None, None]);
|
||||
|
||||
assert_eq!(texture.texture_count(), 1);
|
||||
assert!(texture.has_direction(Direction::Up));
|
||||
assert!(!texture.has_direction(Direction::Down));
|
||||
assert!(!texture.has_direction(Direction::Left));
|
||||
assert!(!texture.has_direction(Direction::Right));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_directional_texture_all_directions() {
|
||||
let mut textures = [None, None, None, None];
|
||||
let directions = [
|
||||
(Direction::Up, 1),
|
||||
(Direction::Down, 2),
|
||||
(Direction::Left, 3),
|
||||
(Direction::Right, 4),
|
||||
];
|
||||
|
||||
for (direction, id) in directions {
|
||||
textures[direction.as_usize()] = Some(mock_animated_texture(id));
|
||||
}
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new(textures, [None, None, None, None]);
|
||||
|
||||
assert_eq!(texture.texture_count(), 4);
|
||||
for direction in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
|
||||
assert!(texture.has_direction(*direction));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_directional_texture_stopped() {
|
||||
let mut stopped_textures = [None, None, None, None];
|
||||
stopped_textures[Direction::Up.as_usize()] = Some(mock_animated_texture(1));
|
||||
|
||||
let texture = DirectionalAnimatedTexture::new([None, None, None, None], stopped_textures);
|
||||
|
||||
assert_eq!(texture.stopped_texture_count(), 1);
|
||||
assert!(texture.has_stopped_direction(Direction::Up));
|
||||
assert!(!texture.has_stopped_direction(Direction::Down));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_directional_texture_tick() {
|
||||
let mut textures = [None, None, None, None];
|
||||
textures[Direction::Up.as_usize()] = Some(mock_animated_texture(1));
|
||||
let mut texture = DirectionalAnimatedTexture::new(textures, [None, None, None, None]);
|
||||
|
||||
// This is a bit of a placeholder, since we can't inspect the inner state easily.
|
||||
// We're just ensuring the tick method runs without panicking.
|
||||
texture.tick(0.1);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
use pacman::constants::RAW_BOARD;
|
||||
use pacman::map::Map;
|
||||
use pacman::map::builder::Map;
|
||||
|
||||
mod collision;
|
||||
mod item;
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
use pacman::entity::ghost::{Ghost, GhostType};
|
||||
use pacman::entity::graph::Graph;
|
||||
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn create_test_atlas() -> SpriteAtlas {
|
||||
let mut frames = HashMap::new();
|
||||
let directions = ["up", "down", "left", "right"];
|
||||
let ghost_types = ["blinky", "pinky", "inky", "clyde"];
|
||||
|
||||
for ghost_type in &ghost_types {
|
||||
for (i, dir) in directions.iter().enumerate() {
|
||||
frames.insert(
|
||||
format!("ghost/{}/{}_{}.png", ghost_type, dir, "a"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 0,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
format!("ghost/{}/{}_{}.png", ghost_type, dir, "b"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 16,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||
SpriteAtlas::new(dummy_texture, mapper)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghost_creation() {
|
||||
let graph = Graph::new();
|
||||
let atlas = create_test_atlas();
|
||||
|
||||
let ghost = Ghost::new(&graph, 0, GhostType::Blinky, &atlas).unwrap();
|
||||
|
||||
assert_eq!(ghost.ghost_type, GhostType::Blinky);
|
||||
assert_eq!(ghost.traverser.position.from_node_id(), 0);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
use pacman::entity::direction::Direction;
|
||||
use pacman::entity::graph::{EdgePermissions, Graph, Node};
|
||||
use pacman::entity::traversal::{Position, Traverser};
|
||||
|
||||
fn create_test_graph() -> Graph {
|
||||
let mut graph = Graph::new();
|
||||
@@ -150,68 +149,3 @@ fn should_find_edge_between_nodes() {
|
||||
let non_existent_edge = graph.find_edge(0, 99);
|
||||
assert!(non_existent_edge.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_traverser_basic() {
|
||||
let graph = create_test_graph();
|
||||
let mut traverser = Traverser::new(&graph, 0, Direction::Left, &|_| true);
|
||||
|
||||
traverser.set_next_direction(Direction::Up);
|
||||
assert!(traverser.next_direction.is_some());
|
||||
assert_eq!(traverser.next_direction.unwrap().0, Direction::Up);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_traverser_advance() {
|
||||
let graph = create_test_graph();
|
||||
let mut traverser = Traverser::new(&graph, 0, Direction::Right, &|_| true);
|
||||
|
||||
traverser.advance(&graph, 5.0, &|_| true).unwrap();
|
||||
|
||||
match traverser.position {
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
assert_eq!(from, 0);
|
||||
assert_eq!(to, 1);
|
||||
assert_eq!(traversed, 5.0);
|
||||
}
|
||||
_ => panic!("Expected to be between nodes"),
|
||||
}
|
||||
|
||||
traverser.advance(&graph, 3.0, &|_| true).unwrap();
|
||||
|
||||
match traverser.position {
|
||||
Position::BetweenNodes { from, to, traversed } => {
|
||||
assert_eq!(from, 0);
|
||||
assert_eq!(to, 1);
|
||||
assert_eq!(traversed, 8.0);
|
||||
}
|
||||
_ => panic!("Expected to be between nodes"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_traverser_with_permissions() {
|
||||
let mut graph = Graph::new();
|
||||
let node1 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node2 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(16.0, 0.0),
|
||||
});
|
||||
|
||||
graph
|
||||
.add_edge(node1, node2, false, None, Direction::Right, EdgePermissions::GhostsOnly)
|
||||
.unwrap();
|
||||
|
||||
// Pacman can't traverse ghost-only edges
|
||||
let mut traverser = Traverser::new(&graph, node1, Direction::Right, &|edge| {
|
||||
matches!(edge.permissions, EdgePermissions::All)
|
||||
});
|
||||
|
||||
traverser
|
||||
.advance(&graph, 5.0, &|edge| matches!(edge.permissions, EdgePermissions::All))
|
||||
.unwrap();
|
||||
|
||||
// Should still be at the node since it can't traverse
|
||||
assert!(traverser.position.is_at_node());
|
||||
}
|
||||
|
||||
@@ -1,53 +1,46 @@
|
||||
use glam::U16Vec2;
|
||||
use pacman::{
|
||||
entity::{
|
||||
collision::Collidable,
|
||||
item::{FruitKind, Item, ItemType},
|
||||
},
|
||||
texture::sprite::{AtlasTile, Sprite},
|
||||
};
|
||||
use strum::{EnumCount, IntoEnumIterator};
|
||||
// use glam::U16Vec2;
|
||||
// use pacman::texture::sprite::{AtlasTile, Sprite};
|
||||
|
||||
#[test]
|
||||
fn test_item_type_get_score() {
|
||||
assert_eq!(ItemType::Pellet.get_score(), 10);
|
||||
assert_eq!(ItemType::Energizer.get_score(), 50);
|
||||
// #[test]
|
||||
// fn test_item_type_get_score() {
|
||||
// assert_eq!(ItemType::Pellet.get_score(), 10);
|
||||
// assert_eq!(ItemType::Energizer.get_score(), 50);
|
||||
|
||||
let fruit = ItemType::Fruit { kind: FruitKind::Apple };
|
||||
assert_eq!(fruit.get_score(), 100);
|
||||
}
|
||||
// let fruit = ItemType::Fruit { kind: FruitKind::Apple };
|
||||
// assert_eq!(fruit.get_score(), 100);
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_fruit_kind_increasing_score() {
|
||||
// Build a list of fruit kinds, sorted by their index
|
||||
let mut kinds = FruitKind::iter()
|
||||
.map(|kind| (kind.index(), kind.get_score()))
|
||||
.collect::<Vec<_>>();
|
||||
kinds.sort_unstable_by_key(|(index, _)| *index);
|
||||
// #[test]
|
||||
// fn test_fruit_kind_increasing_score() {
|
||||
// // Build a list of fruit kinds, sorted by their index
|
||||
// let mut kinds = FruitKind::iter()
|
||||
// .map(|kind| (kind.index(), kind.get_score()))
|
||||
// .collect::<Vec<_>>();
|
||||
// kinds.sort_unstable_by_key(|(index, _)| *index);
|
||||
|
||||
assert_eq!(kinds.len(), FruitKind::COUNT);
|
||||
// assert_eq!(kinds.len(), FruitKind::COUNT);
|
||||
|
||||
// Check that the score increases as expected
|
||||
for window in kinds.windows(2) {
|
||||
let ((_, prev), (_, next)) = (window[0], window[1]);
|
||||
assert!(prev < next, "Fruits should have increasing scores, but {prev:?} < {next:?}");
|
||||
}
|
||||
}
|
||||
// // Check that the score increases as expected
|
||||
// for window in kinds.windows(2) {
|
||||
// let ((_, prev), (_, next)) = (window[0], window[1]);
|
||||
// assert!(prev < next, "Fruits should have increasing scores, but {prev:?} < {next:?}");
|
||||
// }
|
||||
// }
|
||||
|
||||
#[test]
|
||||
fn test_item_creation_and_collection() {
|
||||
let atlas_tile = AtlasTile {
|
||||
pos: U16Vec2::new(0, 0),
|
||||
size: U16Vec2::new(16, 16),
|
||||
color: None,
|
||||
};
|
||||
let sprite = Sprite::new(atlas_tile);
|
||||
let mut item = Item::new(0, ItemType::Pellet, sprite);
|
||||
// #[test]
|
||||
// fn test_item_creation_and_collection() {
|
||||
// let atlas_tile = AtlasTile {
|
||||
// pos: U16Vec2::new(0, 0),
|
||||
// size: U16Vec2::new(16, 16),
|
||||
// color: None,
|
||||
// };
|
||||
// let sprite = Sprite::new(atlas_tile);
|
||||
// let mut item = Item::new(0, ItemType::Pellet, sprite);
|
||||
|
||||
assert!(!item.is_collected());
|
||||
assert_eq!(item.get_score(), 10);
|
||||
assert_eq!(item.position().from_node_id(), 0);
|
||||
// assert!(!item.is_collected());
|
||||
// assert_eq!(item.get_score(), 10);
|
||||
// assert_eq!(item.position().from_node_id(), 0);
|
||||
|
||||
item.collect();
|
||||
assert!(item.is_collected());
|
||||
}
|
||||
// item.collect();
|
||||
// assert!(item.is_collected());
|
||||
// }
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
use glam::Vec2;
|
||||
use pacman::constants::{CELL_SIZE, RAW_BOARD};
|
||||
use pacman::map::Map;
|
||||
use sdl2::render::Texture;
|
||||
use pacman::map::builder::Map;
|
||||
|
||||
#[test]
|
||||
fn test_map_creation() {
|
||||
@@ -34,60 +33,60 @@ fn test_map_node_positions() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_items() {
|
||||
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
use std::collections::HashMap;
|
||||
// #[test]
|
||||
// fn test_generate_items() {
|
||||
// use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
// use std::collections::HashMap;
|
||||
|
||||
let map = Map::new(RAW_BOARD).unwrap();
|
||||
// let map = Map::new(RAW_BOARD).unwrap();
|
||||
|
||||
// Create a minimal atlas for testing
|
||||
let mut frames = HashMap::new();
|
||||
frames.insert(
|
||||
"maze/pellet.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
"maze/energizer.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 8,
|
||||
y: 0,
|
||||
width: 8,
|
||||
height: 8,
|
||||
},
|
||||
);
|
||||
// // Create a minimal atlas for testing
|
||||
// let mut frames = HashMap::new();
|
||||
// frames.insert(
|
||||
// "maze/pellet.png".to_string(),
|
||||
// MapperFrame {
|
||||
// x: 0,
|
||||
// y: 0,
|
||||
// width: 8,
|
||||
// height: 8,
|
||||
// },
|
||||
// );
|
||||
// frames.insert(
|
||||
// "maze/energizer.png".to_string(),
|
||||
// MapperFrame {
|
||||
// x: 8,
|
||||
// y: 0,
|
||||
// width: 8,
|
||||
// height: 8,
|
||||
// },
|
||||
// );
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let texture = unsafe { std::mem::transmute::<usize, Texture<'static>>(0usize) };
|
||||
let atlas = SpriteAtlas::new(texture, mapper);
|
||||
// let mapper = AtlasMapper { frames };
|
||||
// let texture = unsafe { std::mem::transmute::<usize, Texture<'static>>(0usize) };
|
||||
// let atlas = SpriteAtlas::new(texture, mapper);
|
||||
|
||||
let items = map.generate_items(&atlas).unwrap();
|
||||
// let items = map.generate_items(&atlas).unwrap();
|
||||
|
||||
// Verify we have items
|
||||
assert!(!items.is_empty());
|
||||
// // Verify we have items
|
||||
// assert!(!items.is_empty());
|
||||
|
||||
// Count different types
|
||||
let pellet_count = items
|
||||
.iter()
|
||||
.filter(|item| matches!(item.item_type, pacman::entity::item::ItemType::Pellet))
|
||||
.count();
|
||||
let energizer_count = items
|
||||
.iter()
|
||||
.filter(|item| matches!(item.item_type, pacman::entity::item::ItemType::Energizer))
|
||||
.count();
|
||||
// // Count different types
|
||||
// let pellet_count = items
|
||||
// .iter()
|
||||
// .filter(|item| matches!(item.item_type, pacman::entity::item::ItemType::Pellet))
|
||||
// .count();
|
||||
// let energizer_count = items
|
||||
// .iter()
|
||||
// .filter(|item| matches!(item.item_type, pacman::entity::item::ItemType::Energizer))
|
||||
// .count();
|
||||
|
||||
// Should have both types
|
||||
assert_eq!(pellet_count, 240);
|
||||
assert_eq!(energizer_count, 4);
|
||||
// // Should have both types
|
||||
// assert_eq!(pellet_count, 240);
|
||||
// assert_eq!(energizer_count, 4);
|
||||
|
||||
// All items should be uncollected initially
|
||||
assert!(items.iter().all(|item| !item.is_collected()));
|
||||
// // All items should be uncollected initially
|
||||
// assert!(items.iter().all(|item| !item.is_collected()));
|
||||
|
||||
// All items should have valid node indices
|
||||
assert!(items.iter().all(|item| item.node_index < map.graph.node_count()));
|
||||
}
|
||||
// // All items should have valid node indices
|
||||
// assert!(items.iter().all(|item| item.node_index < map.graph.node_count()));
|
||||
// }
|
||||
|
||||
107
tests/pacman.rs
107
tests/pacman.rs
@@ -1,107 +0,0 @@
|
||||
use pacman::entity::direction::Direction;
|
||||
use pacman::entity::graph::{Graph, Node};
|
||||
use pacman::entity::pacman::Pacman;
|
||||
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
use sdl2::keyboard::Keycode;
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn create_test_graph() -> Graph {
|
||||
let mut graph = Graph::new();
|
||||
let node1 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node2 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(16.0, 0.0),
|
||||
});
|
||||
let node3 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 16.0),
|
||||
});
|
||||
|
||||
graph.connect(node1, node2, false, None, Direction::Right).unwrap();
|
||||
graph.connect(node1, node3, false, None, Direction::Down).unwrap();
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
fn create_test_atlas() -> SpriteAtlas {
|
||||
let mut frames = HashMap::new();
|
||||
let directions = ["up", "down", "left", "right"];
|
||||
|
||||
for (i, dir) in directions.iter().enumerate() {
|
||||
frames.insert(
|
||||
format!("pacman/{dir}_a.png"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 0,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
format!("pacman/{dir}_b.png"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 16,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
frames.insert(
|
||||
"pacman/full.png".to_string(),
|
||||
MapperFrame {
|
||||
x: 64,
|
||||
y: 0,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||
SpriteAtlas::new(dummy_texture, mapper)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pacman_creation() {
|
||||
let graph = create_test_graph();
|
||||
let atlas = create_test_atlas();
|
||||
let pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||
|
||||
assert!(pacman.traverser.position.is_at_node());
|
||||
assert_eq!(pacman.traverser.direction, Direction::Left);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pacman_key_handling() {
|
||||
let graph = create_test_graph();
|
||||
let atlas = create_test_atlas();
|
||||
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||
|
||||
let test_cases = [
|
||||
(Keycode::Up, Direction::Up),
|
||||
(Keycode::Down, Direction::Down),
|
||||
(Keycode::Left, Direction::Left),
|
||||
(Keycode::Right, Direction::Right),
|
||||
];
|
||||
|
||||
for (key, expected_direction) in test_cases {
|
||||
pacman.handle_key(key);
|
||||
assert!(pacman.traverser.next_direction.is_some() || pacman.traverser.direction == expected_direction);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_pacman_invalid_key() {
|
||||
let graph = create_test_graph();
|
||||
let atlas = create_test_atlas();
|
||||
let mut pacman = Pacman::new(&graph, 0, &atlas).unwrap();
|
||||
|
||||
let original_direction = pacman.traverser.direction;
|
||||
let original_next_direction = pacman.traverser.next_direction;
|
||||
|
||||
pacman.handle_key(Keycode::Space);
|
||||
assert_eq!(pacman.traverser.direction, original_direction);
|
||||
assert_eq!(pacman.traverser.next_direction, original_next_direction);
|
||||
}
|
||||
@@ -1,120 +0,0 @@
|
||||
use pacman::entity::direction::Direction;
|
||||
use pacman::entity::ghost::{Ghost, GhostType};
|
||||
use pacman::entity::graph::{Graph, Node};
|
||||
use pacman::texture::sprite::{AtlasMapper, MapperFrame, SpriteAtlas};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn create_test_atlas() -> SpriteAtlas {
|
||||
let mut frames = HashMap::new();
|
||||
let directions = ["up", "down", "left", "right"];
|
||||
let ghost_types = ["blinky", "pinky", "inky", "clyde"];
|
||||
|
||||
for ghost_type in &ghost_types {
|
||||
for (i, dir) in directions.iter().enumerate() {
|
||||
frames.insert(
|
||||
format!("ghost/{}/{}_{}.png", ghost_type, dir, "a"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 0,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
frames.insert(
|
||||
format!("ghost/{}/{}_{}.png", ghost_type, dir, "b"),
|
||||
MapperFrame {
|
||||
x: i as u16 * 16,
|
||||
y: 16,
|
||||
width: 16,
|
||||
height: 16,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let mapper = AtlasMapper { frames };
|
||||
let dummy_texture = unsafe { std::mem::zeroed() };
|
||||
SpriteAtlas::new(dummy_texture, mapper)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghost_pathfinding() {
|
||||
// Create a simple test graph
|
||||
let mut graph = Graph::new();
|
||||
|
||||
// Add nodes in a simple line: 0 -> 1 -> 2
|
||||
let node0 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node1 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(10.0, 0.0),
|
||||
});
|
||||
let node2 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(20.0, 0.0),
|
||||
});
|
||||
|
||||
// Connect the nodes
|
||||
graph.connect(node0, node1, false, None, Direction::Right).unwrap();
|
||||
graph.connect(node1, node2, false, None, Direction::Right).unwrap();
|
||||
|
||||
// Create a test atlas for the ghost
|
||||
let atlas = create_test_atlas();
|
||||
|
||||
// Create a ghost at node 0
|
||||
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
|
||||
|
||||
// Test pathfinding from node 0 to node 2
|
||||
let path = ghost.calculate_path_to_target(&graph, node2);
|
||||
|
||||
assert!(path.is_ok());
|
||||
let path = path.unwrap();
|
||||
assert!(
|
||||
path == vec![node0, node1, node2] || path == vec![node2, node1, node0],
|
||||
"Path was not what was expected"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghost_pathfinding_no_path() {
|
||||
// Create a test graph with disconnected components
|
||||
let mut graph = Graph::new();
|
||||
|
||||
let node0 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node1 = graph.add_node(Node {
|
||||
position: glam::Vec2::new(10.0, 0.0),
|
||||
});
|
||||
|
||||
// Don't connect the nodes
|
||||
let atlas = create_test_atlas();
|
||||
let ghost = Ghost::new(&graph, node0, GhostType::Blinky, &atlas).unwrap();
|
||||
|
||||
// Test pathfinding when no path exists
|
||||
let path = ghost.calculate_path_to_target(&graph, node1);
|
||||
|
||||
assert!(path.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ghost_debug_colors() {
|
||||
let atlas = create_test_atlas();
|
||||
let mut graph = Graph::new();
|
||||
let node = graph.add_node(Node {
|
||||
position: glam::Vec2::new(0.0, 0.0),
|
||||
});
|
||||
|
||||
let blinky = Ghost::new(&graph, node, GhostType::Blinky, &atlas).unwrap();
|
||||
let pinky = Ghost::new(&graph, node, GhostType::Pinky, &atlas).unwrap();
|
||||
let inky = Ghost::new(&graph, node, GhostType::Inky, &atlas).unwrap();
|
||||
let clyde = Ghost::new(&graph, node, GhostType::Clyde, &atlas).unwrap();
|
||||
|
||||
// Test that each ghost has a different debug color
|
||||
let colors = std::collections::HashSet::from([
|
||||
blinky.debug_color(),
|
||||
pinky.debug_color(),
|
||||
inky.debug_color(),
|
||||
clyde.debug_color(),
|
||||
]);
|
||||
assert_eq!(colors.len(), 4, "All ghost colors should be unique");
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
use glam::U16Vec2;
|
||||
use pacman::texture::sprite::{AtlasMapper, AtlasTile, MapperFrame, Sprite, SpriteAtlas};
|
||||
use pacman::texture::sprite::{AtlasMapper, AtlasTile, MapperFrame, SpriteAtlas};
|
||||
use sdl2::pixels::Color;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@@ -92,12 +92,3 @@ fn test_atlas_tile_new_and_with_color() {
|
||||
let tile_with_color = tile.with_color(color);
|
||||
assert_eq!(tile_with_color.color, Some(color));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sprite_new() {
|
||||
let atlas_tile = AtlasTile::new(U16Vec2::new(0, 0), U16Vec2::new(16, 16), None);
|
||||
let sprite = Sprite::new(atlas_tile);
|
||||
|
||||
assert_eq!(sprite.atlas_tile.pos, atlas_tile.pos);
|
||||
assert_eq!(sprite.atlas_tile.size, atlas_tile.size);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user