use ecs::actions::Actions; use ecs::extension::{Collector as ExtensionCollector, Extension}; use ecs::phase::UPDATE as UPDATE_PHASE; 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, } 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_PHASE, 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.step(); } }