summaryrefslogtreecommitdiff
path: root/engine-ecs/examples/with_sole.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-05-21 17:55:20 +0200
committerHampusM <hampus@hampusmat.com>2026-05-21 17:55:20 +0200
commit8022e8998290b067b8aa0cb9cba8ba410826bdab (patch)
tree7171e79ce530e03079046ee8fd12167160c45480 /engine-ecs/examples/with_sole.rs
parent412cee02c252f91bcf0b70a3f5cc5fca6d2b4c62 (diff)
chore: rename ecs* crates to engine-ecs*HEADmaster
Diffstat (limited to 'engine-ecs/examples/with_sole.rs')
-rw-r--r--engine-ecs/examples/with_sole.rs61
1 files changed, 61 insertions, 0 deletions
diff --git a/engine-ecs/examples/with_sole.rs b/engine-ecs/examples/with_sole.rs
new file mode 100644
index 0000000..4b2fa44
--- /dev/null
+++ b/engine-ecs/examples/with_sole.rs
@@ -0,0 +1,61 @@
+use engine_ecs::pair::{ChildOf, Pair};
+use engine_ecs::phase::{Phase, UPDATE as UPDATE_PHASE};
+use engine_ecs::sole::Single;
+use engine_ecs::{declare_entity, Component, Query, Sole, World};
+
+#[derive(Component)]
+struct Ammo
+{
+ ammo_left: u32,
+}
+
+#[derive(Sole, Default)]
+struct AmmoCounter
+{
+ counter: u32,
+}
+
+fn count_ammo(query: Query<(&Ammo,)>, mut ammo_counter: Single<AmmoCounter>)
+{
+ for (ammo,) in &query {
+ println!("Found {} ammo", ammo.ammo_left);
+
+ ammo_counter.counter += ammo.ammo_left;
+ }
+}
+
+fn print_total_ammo_count(ammo_counter: Single<AmmoCounter>)
+{
+ println!("Total ammo count: {}", ammo_counter.counter);
+
+ assert_eq!(ammo_counter.counter, 19);
+}
+
+declare_entity!(
+ PRINT_AMMO_COUNT_PHASE,
+ (
+ Phase,
+ Pair::builder()
+ .relation::<ChildOf>()
+ .target_id(*UPDATE_PHASE)
+ .build()
+ )
+);
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.create_declared_entity(&PRINT_AMMO_COUNT_PHASE);
+
+ world.register_system(*UPDATE_PHASE, count_ammo);
+ world.register_system(*PRINT_AMMO_COUNT_PHASE, print_total_ammo_count);
+
+ world.create_entity((Ammo { ammo_left: 4 },));
+ world.create_entity((Ammo { ammo_left: 7 },));
+ world.create_entity((Ammo { ammo_left: 8 },));
+
+ world.add_sole(AmmoCounter::default()).unwrap();
+
+ world.step();
+}