diff options
author | HampusM <hampus@hampusmat.com> | 2024-02-21 23:54:37 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-02-22 19:27:52 +0100 |
commit | 9f65dba3afd4e8f20881914fc86fa997cb64a13d (patch) | |
tree | d760f8f478af6591648ef3f53d4e0cb34ee76908 /ecs/examples | |
parent | b0ef7f2cf8787c5732e0eb5554161ad75179a4b3 (diff) |
feat(ecs): add support for system local components
Diffstat (limited to 'ecs/examples')
-rw-r--r-- | ecs/examples/with_local.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/ecs/examples/with_local.rs b/ecs/examples/with_local.rs new file mode 100644 index 0000000..d7af0e0 --- /dev/null +++ b/ecs/examples/with_local.rs @@ -0,0 +1,76 @@ +use ecs::component::Local; +use ecs::system::{Into, System}; +use ecs::{Query, World}; + +struct SomeData +{ + num: u64, +} + +struct Name +{ + name: String, +} + +struct SayHelloState +{ + cnt: usize, +} + +fn say_hello(mut query: Query<(SomeData, String)>, mut state: Local<SayHelloState>) +{ + for (data, text) in query.iter_mut() { + println!("Hello there. Count {}: {}: {}", state.cnt, text, data.num); + + state.cnt += 1; + } +} + +fn say_whats_up(mut query: Query<(SomeData, Name)>, mut state: Local<SayHelloState>) +{ + for (data, name) in query.iter_mut() { + println!( + "Whats up, {}. Number is {}. Count {}", + name.name, data.num, state.cnt + ); + + state.cnt += 1; + } +} + +#[derive(Debug, PartialEq, Eq, Hash)] +enum Event +{ + Update, +} + +fn main() +{ + let mut world = World::<Event>::new(); + + world.register_system( + Event::Update, + say_hello.into_system().initialize(SayHelloState { cnt: 0 }), + ); + + world.register_system( + Event::Update, + say_whats_up + .into_system() + .initialize(SayHelloState { cnt: 0 }), + ); + + world.create_entity(( + SomeData { num: 987_654 }, + "Yoo".to_string(), + Name { name: "Bob".to_string() }, + )); + + world.create_entity((SomeData { num: 345 }, "Haha".to_string())); + + world.emit(&Event::Update); + + println!("Haha"); + + world.emit(&Event::Update); +} |