diff options
| author | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2026-05-21 17:55:20 +0200 |
| commit | 8022e8998290b067b8aa0cb9cba8ba410826bdab (patch) | |
| tree | 7171e79ce530e03079046ee8fd12167160c45480 /engine-ecs/examples/with_local.rs | |
| parent | 412cee02c252f91bcf0b70a3f5cc5fca6d2b4c62 (diff) | |
Diffstat (limited to 'engine-ecs/examples/with_local.rs')
| -rw-r--r-- | engine-ecs/examples/with_local.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/engine-ecs/examples/with_local.rs b/engine-ecs/examples/with_local.rs new file mode 100644 index 0000000..2c04f26 --- /dev/null +++ b/engine-ecs/examples/with_local.rs @@ -0,0 +1,70 @@ +use engine_ecs::component::local::Local; +use engine_ecs::phase::UPDATE as UPDATE_PHASE; +use engine_ecs::system::initializable::Initializable; +use engine_ecs::system::Into; +use engine_ecs::{Component, Query, World}; + +#[derive(Component)] +struct SomeData +{ + num: u64, +} + +#[derive(Component)] +struct Name +{ + name: String, +} + +#[derive(Component)] +struct SayHelloState +{ + cnt: usize, +} + +fn say_hello(query: Query<(&SomeData,)>, mut state: Local<SayHelloState>) +{ + for (data,) in &query { + println!("Hello there. Count {}: {}", state.cnt, data.num); + + state.cnt += 1; + } +} + +fn say_whats_up(query: Query<(&SomeData, &Name)>, mut state: Local<SayHelloState>) +{ + for (data, name) in &query { + println!( + "Whats up, {}. Number is {}. Count {}", + name.name, data.num, state.cnt + ); + + state.cnt += 1; + } +} + +fn main() +{ + let mut world = World::new(); + + world.register_system( + *UPDATE_PHASE, + say_hello + .into_system() + .initialize((SayHelloState { cnt: 0 },)), + ); + + world.register_system( + *UPDATE_PHASE, + say_whats_up + .into_system() + .initialize((SayHelloState { cnt: 0 },)), + ); + + world.create_entity((SomeData { num: 987_654 }, Name { name: "Bob".to_string() })); + + world.create_entity((SomeData { num: 345 },)); + + world.step(); + world.step(); +} |
