From ca57bce787313c7ba57b96345590d49fb629c41b Mon Sep 17 00:00:00 2001 From: LunarAkai Date: Mon, 4 Aug 2025 01:47:13 +0200 Subject: [PATCH] ecs? --- Cargo.lock | 500 +----------------- crates/moonhare_ecs/Cargo.toml | 8 + crates/moonhare_ecs/src/generational_index.rs | 131 +++++ crates/moonhare_ecs/src/lib.rs | 15 + crates/moonhare_internal/Cargo.toml | 2 +- crates/moonhare_internal/src/lib.rs | 4 +- playground/src/main.rs | 1 - 7 files changed, 177 insertions(+), 484 deletions(-) create mode 100644 crates/moonhare_ecs/Cargo.toml create mode 100644 crates/moonhare_ecs/src/generational_index.rs create mode 100644 crates/moonhare_ecs/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index a09dad0..6cacabe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,7 +73,7 @@ dependencies = [ "ndk-context", "ndk-sys", "num_enum", - "thiserror 1.0.69", + "thiserror", ] [[package]] @@ -82,6 +82,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fc7eb209b1518d6bb87b283c20095f5228ecda460da70b44f0802523dea6da04" +[[package]] +name = "anymap" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344" + [[package]] name = "arrayref" version = "0.3.9" @@ -100,48 +106,11 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" -[[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" @@ -164,147 +133,6 @@ dependencies = [ "windows-targets 0.52.6", ] -[[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 2.0.12", - "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 2.0.1", - "erased-serde", - "foldhash", - "glam", - "serde", - "smallvec", - "smol_str", - "thiserror 2.0.12", - "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" @@ -316,9 +144,6 @@ name = "bitflags" version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" -dependencies = [ - "serde", -] [[package]] name = "block2" @@ -341,12 +166,6 @@ version = "1.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" -[[package]] -name = "byteorder" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" - [[package]] name = "bytes" version = "1.10.1" @@ -364,7 +183,7 @@ dependencies = [ "polling", "rustix 0.38.44", "slab", - "thiserror 1.0.69", + "thiserror", ] [[package]] @@ -453,7 +272,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ca0197aee26d1ae37445ee532fefce43251d24cc7c166799f4d46817f1d3973" dependencies = [ "crossbeam-utils", - "portable-atomic", ] [[package]] @@ -496,21 +314,6 @@ dependencies = [ "libc", ] -[[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" @@ -523,27 +326,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f" -[[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 = "dispatch" version = "0.2.0" @@ -560,12 +342,6 @@ dependencies = [ "objc2 0.6.1", ] -[[package]] -name = "disqualified" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9c272297e804878a2a4b707cfcfc6d2328b5bb936944613b4fdf2b9269afdfd" - [[package]] name = "dlib" version = "0.5.2" @@ -581,12 +357,6 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" -[[package]] -name = "downcast-rs" -version = "2.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea8a8b81cacc08888170eef4d13b775126db426d0b348bee9d18c2c1eaf123cf" - [[package]] name = "dpi" version = "0.1.2" @@ -599,16 +369,6 @@ 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 = "errno" version = "0.3.13" @@ -619,12 +379,6 @@ dependencies = [ "windows-sys 0.60.2", ] -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - [[package]] name = "fern" version = "0.7.1" @@ -635,24 +389,12 @@ dependencies = [ "log", ] -[[package]] -name = "fixedbitset" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" - [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "foldhash" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" - [[package]] name = "foreign-types" version = "0.5.0" @@ -680,31 +422,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" -[[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.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f5edaec856126859abb19ed65f39e90fea3a9574b9707f13539acf4abf7eb532" -dependencies = [ - "fastrand", - "futures-core", - "futures-io", - "parking", - "pin-project-lite", -] - [[package]] name = "generator" version = "0.7.5" @@ -757,15 +474,6 @@ dependencies = [ "xml-rs", ] -[[package]] -name = "glam" -version = "0.29.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8babf46d4c1c9d92deac9f7be466f76dfc4482b6452fc5024b5e8daf6ffeb3ee" -dependencies = [ - "serde", -] - [[package]] name = "glfw" version = "0.59.0" @@ -871,35 +579,11 @@ dependencies = [ "gl_generator", ] -[[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 = "hermit-abi" @@ -940,7 +624,7 @@ dependencies = [ "combine", "jni-sys", "log", - "thiserror 1.0.69", + "thiserror", "walkdir", "windows-sys 0.45.0", ] @@ -1022,16 +706,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd945864f07fe9f5371a27ad7b52a172b4b499999f1d97574c9fa68373937e12" -[[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.27" @@ -1095,6 +769,13 @@ dependencies = [ "adler2", ] +[[package]] +name = "moonhare_ecs" +version = "0.1.0" +dependencies = [ + "anymap", +] + [[package]] name = "moonhare_engine" version = "0.1.0" @@ -1129,7 +810,7 @@ dependencies = [ name = "moonhare_internal" version = "0.1.0" dependencies = [ - "bevy_ecs", + "moonhare_ecs", "moonhare_event", "moonhare_game", "moonhare_graphics", @@ -1167,7 +848,7 @@ dependencies = [ "ndk-sys", "num_enum", "raw-window-handle", - "thiserror 1.0.69", + "thiserror", ] [[package]] @@ -1185,12 +866,6 @@ dependencies = [ "jni-sys", ] -[[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" @@ -1508,35 +1183,6 @@ dependencies = [ "ttf-parser", ] -[[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 0.5.17", - "smallvec", - "windows-targets 0.52.6", -] - [[package]] name = "percent-encoding" version = "2.3.1" @@ -1596,21 +1242,6 @@ dependencies = [ "windows-sys 0.60.2", ] -[[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-macro-crate" version = "3.3.0" @@ -1780,12 +1411,6 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" -[[package]] -name = "scopeguard" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" - [[package]] name = "sctk-adwaita" version = "0.10.1" @@ -1872,7 +1497,7 @@ dependencies = [ "log", "memmap2", "rustix 0.38.44", - "thiserror 1.0.69", + "thiserror", "wayland-backend", "wayland-client", "wayland-csd-frame", @@ -1892,21 +1517,6 @@ 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 = "stable_deref_trait" -version = "1.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" - [[package]] name = "state" version = "0.6.0" @@ -1939,16 +1549,7 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl 1.0.69", -] - -[[package]] -name = "thiserror" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" -dependencies = [ - "thiserror-impl 2.0.12", + "thiserror-impl", ] [[package]] @@ -1962,17 +1563,6 @@ dependencies = [ "syn", ] -[[package]] -name = "thiserror-impl" -version = "2.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "thread_local" version = "1.1.9" @@ -2091,12 +1681,6 @@ version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2df906b07856748fa3f6e0ad0cbaa047052d4a7dd609e231c4f72cee8c36f31" -[[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.18" @@ -2109,41 +1693,12 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493" -[[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.17.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cf4199d1e5d15ddd86a694e4d0dffa9c323ce759fea589f00fef9d81cc1931d" -dependencies = [ - "getrandom", - "js-sys", - "serde", - "wasm-bindgen", -] - [[package]] name = "valuable" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" -[[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 = "version_check" version = "0.9.5" @@ -2247,7 +1802,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "673a33c33048a5ade91a6b139580fa174e19fb0d23f396dca9fa15f2e1e49b35" dependencies = [ "cc", - "downcast-rs 1.2.1", + "downcast-rs", "rustix 1.0.8", "scoped-tls", "smallvec", @@ -2369,19 +1924,6 @@ dependencies = [ "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" diff --git a/crates/moonhare_ecs/Cargo.toml b/crates/moonhare_ecs/Cargo.toml new file mode 100644 index 0000000..f02e3d3 --- /dev/null +++ b/crates/moonhare_ecs/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2024" +name = "moonhare_ecs" +version.workspace = true +authors.workspace = true + +[dependencies] +anymap = "*" \ No newline at end of file diff --git a/crates/moonhare_ecs/src/generational_index.rs b/crates/moonhare_ecs/src/generational_index.rs new file mode 100644 index 0000000..2def46f --- /dev/null +++ b/crates/moonhare_ecs/src/generational_index.rs @@ -0,0 +1,131 @@ +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +pub struct GenerationalIndex { + index: usize, + generation: u64, +} + +impl GenerationalIndex { + pub fn index(&self) -> usize { + self.index + } + + pub fn generation(&self) -> u64 { + self.generation + } +} + +struct AllocatorEntry { + is_live: bool, + generation: u64, +} + +pub struct GenerationalIndexAllocator { + entries: Vec, + free: Vec, +} + +impl GenerationalIndexAllocator { + pub fn allocate(&mut self) -> GenerationalIndex { + match self.free.pop() { + Some(index) =>{ + self.entries[index].generation += 1; + self.entries[index].is_live = true; + + GenerationalIndex { + index, + generation: self.entries[index].generation, + } + }, + None => { + self.entries.push(AllocatorEntry { + is_live: true, + generation: 0, + }); + + GenerationalIndex { + index: self.entries.len() -1, + generation: 0, + } + } + } + } + + // Returns true if the index was allocated before and is now deallocated + pub fn deallocate(&mut self, index: GenerationalIndex) -> bool { + if self.is_live(index) { + self.entries[index.index()].is_live = false; + self.free.push(index.index()); + true + } else { + false + } + } + + pub fn is_live(&self, index: GenerationalIndex) -> bool { + index.index() < self.entries.len() && self.entries[index.index()].generation == index.generation && self.entries[index.index()].is_live + } +} + +struct ArrayEntry { + value: T, + generation: u64, +} + +pub struct GenerationalIndexArray(Vec>>); + +impl GenerationalIndexArray { + pub fn set(&mut self, index: GenerationalIndex, value: T) { + while self.0.len() <= index.index() { + self.0.push(None); + } + + let previous_generation = match &self.0[index.index()] { + Some(entry) => entry.generation, + None => 0, + }; + + if previous_generation > index.generation() { + panic!("write an index from previous gen"); + } + + self.0[index.index()] = Some(ArrayEntry { + value, + generation: index.generation(), + }); + } + + pub fn remove(&mut self, index: GenerationalIndex) { + if index.index() < self.0.len() { + self.0[index.index()] = None; + } + } + + pub fn get(&self, index: GenerationalIndex) -> Option<&T> { + if index.index() >= self.0.len() { + return None; + } + + match &self.0[index.index()] { + Some(entry) => if entry.generation == index.generation() { + Some(&entry.value) + } else { + None + }, + None => None + } + } + pub fn get_mut(&mut self, index: GenerationalIndex) -> Option<&mut T> { + if index.index() >= self.0.len() { + return None; + } + + match &mut self.0[index.index()] { + Some(entry) => if entry.generation == index.generation() { + Some(&mut entry.value) + } else { + None + }, + None => None + } + } +} diff --git a/crates/moonhare_ecs/src/lib.rs b/crates/moonhare_ecs/src/lib.rs new file mode 100644 index 0000000..6a9fc59 --- /dev/null +++ b/crates/moonhare_ecs/src/lib.rs @@ -0,0 +1,15 @@ +use anymap::AnyMap; + +use crate::generational_index::{GenerationalIndex, GenerationalIndexAllocator, GenerationalIndexArray}; + +pub mod generational_index; + +pub type Entity = GenerationalIndex; +pub type EntityMap = GenerationalIndexArray; + +// based on: https://kyren.github.io/2018/09/14/rustconf-talk.html +pub struct ECS { + pub entity_allocator: GenerationalIndexAllocator, + pub entity_components: AnyMap, + pub resources: AnyMap +} \ No newline at end of file diff --git a/crates/moonhare_internal/Cargo.toml b/crates/moonhare_internal/Cargo.toml index 5d267e6..7262a33 100644 --- a/crates/moonhare_internal/Cargo.toml +++ b/crates/moonhare_internal/Cargo.toml @@ -5,8 +5,8 @@ version.workspace = true authors.workspace = true [dependencies] -bevy_ecs = "0.16.1" +moonhare_ecs = { path = "../moonhare_ecs" } moonhare_event = { path = "../moonhare_event" } moonhare_game = { path = "../moonhare_game" } moonhare_graphics = { path = "../moonhare_graphics" } diff --git a/crates/moonhare_internal/src/lib.rs b/crates/moonhare_internal/src/lib.rs index 080e4fa..3cc5570 100644 --- a/crates/moonhare_internal/src/lib.rs +++ b/crates/moonhare_internal/src/lib.rs @@ -1,6 +1,4 @@ -// Writing an ecs from scratch seems hard, might do it in the future though -pub use bevy_ecs as ecs; - +pub use moonhare_ecs as ecs; pub use moonhare_event as event; pub use moonhare_game as game; pub use moonhare_graphics as graphics; diff --git a/playground/src/main.rs b/playground/src/main.rs index d195a49..3fdd057 100644 --- a/playground/src/main.rs +++ b/playground/src/main.rs @@ -2,7 +2,6 @@ use std::fs::File; use moonhare_engine::{game::Game, log}; - fn main() { let _ = log::configere_logger(); log::info("test");