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); world.prepare(); for _ in 0..7 { world.emit(Update); world.perform_queued_actions(); } }