diff options
author | HampusM <hampus@hampusmat.com> | 2024-04-13 17:38:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-04-13 17:38:45 +0200 |
commit | ef7b76ff39d501028852835649f618fcbe17a003 (patch) | |
tree | 00cf4966c1bcfa0812f436a793a1711867869601 /ecs/examples/extension.rs | |
parent | f2120591b0051d4239de73960709248456e884e7 (diff) |
feat(ecs): add extensions
Diffstat (limited to 'ecs/examples/extension.rs')
-rw-r--r-- | ecs/examples/extension.rs | 77 |
1 files changed, 77 insertions, 0 deletions
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(); + } +} |