From ef7b76ff39d501028852835649f618fcbe17a003 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 13 Apr 2024 17:38:45 +0200 Subject: feat(ecs): add extensions --- ecs/examples/extension.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 ecs/examples/extension.rs (limited to 'ecs/examples') diff --git a/ecs/examples/extension.rs b/ecs/examples/extension.rs new file mode 100644 index 0000000..2022b05 --- /dev/null +++ b/ecs/examples/extension.rs @@ -0,0 +1,77 @@ +use ecs::actions::Actions; +use ecs::event::Event; +use ecs::extension::{Collector as ExtensionCollector, Extension}; +use ecs::{Component, Query, World}; + +#[derive(Debug, Component)] +struct Position +{ + x: u32, + y: u32, +} + +#[derive(Debug, Component)] +struct EnemySpawnSource; + +#[derive(Debug, Component)] +enum EvilnessLevel +{ + Medium, +} + +#[derive(Debug)] +struct Update; + +impl Event for Update {} + +fn spawn_enemies( + spawner_query: Query<(EnemySpawnSource, Position)>, + enemies_query: Query<(EvilnessLevel,)>, + mut actions: Actions, +) +{ + let Some((_, enemy_spawner_position)) = spawner_query.iter().next() else { + return; + }; + + let enemy_cnt = enemies_query.iter().count(); + + if enemy_cnt > 3 { + return; + } + + actions.spawn(( + EvilnessLevel::Medium, + Position { + x: enemy_spawner_position.x * enemy_cnt as u32, + y: enemy_spawner_position.y, + }, + )); + + println!("Spawned enemy with medium evilness and 45 strength"); +} + +struct EnemySpawningExtension; + +impl Extension for EnemySpawningExtension +{ + fn collect(self, mut collector: ExtensionCollector<'_>) + { + collector.add_system(Update, spawn_enemies); + + collector.add_entity((Position { x: 187, y: 30 }, EnemySpawnSource)); + } +} + +fn main() +{ + let mut world = World::new(); + + world.add_extension(EnemySpawningExtension); + + for _ in 0..7 { + world.emit(Update); + + world.perform_queued_actions(); + } +} -- cgit v1.2.3-18-g5258