summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-04-10 20:40:26 +0200
committerHampusM <hampus@hampusmat.com>2024-04-10 20:40:26 +0200
commit50fb3122a2a141f1b78a874c139b097459a09408 (patch)
treed997f6ff189312717ffc01a6109f3a9cc991846f
parentd5379b32d73e6a1074c7ccc127846f5c89fbbfbd (diff)
docs(ecs): add example using Sole
-rw-r--r--ecs/examples/with_sole.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/ecs/examples/with_sole.rs b/ecs/examples/with_sole.rs
new file mode 100644
index 0000000..a387bea
--- /dev/null
+++ b/ecs/examples/with_sole.rs
@@ -0,0 +1,59 @@
+use ecs::event::Event;
+use ecs::sole::Single;
+use ecs::{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);
+}
+
+#[derive(Debug)]
+struct EventA;
+
+impl Event for EventA {}
+
+#[derive(Debug)]
+struct EventB;
+
+impl Event for EventB {}
+
+fn main()
+{
+ let mut world = World::new();
+
+ world.register_system(EventA, count_ammo);
+ world.register_system(EventB, 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.emit(EventA);
+
+ world.emit(EventB);
+}