use sky_ecs::{CommandBuffer, World};
#[derive(Clone, Copy, Debug)]
struct Position {
x: f32,
y: f32,
}
#[derive(Clone, Copy, Debug)]
struct Health(f32);
#[derive(Clone, Copy, Debug)]
#[allow(dead_code)]
struct Poison {
dps: f32,
}
#[derive(Clone, Copy, Debug)]
struct Shield;
fn main() {
let mut world = World::new();
let warrior = world.spawn((Position { x: 0.0, y: 0.0 }, Health(100.0)));
let mage = world.spawn((Position { x: 5.0, y: 0.0 }, Health(60.0)));
let rogue = world.spawn((Position { x: 10.0, y: 0.0 }, Health(80.0)));
println!("=== Initial state ===");
print_entities(&world);
let mut cmds = CommandBuffer::new();
cmds.insert(warrior, Shield);
cmds.insert(mage, Poison { dps: 15.0 });
cmds.spawn((Position { x: -5.0, y: 0.0 }, Health(50.0)));
cmds.despawn(rogue);
cmds.apply(&mut world);
println!("\n=== After commands ===");
print_entities(&world);
println!("\nWarrior has Shield? {}", world.has::<Shield>(warrior));
println!("Mage has Poison? {}", world.has::<Poison>(mage));
println!("Rogue alive? {}", world.contains(rogue));
let mut cmds = CommandBuffer::new();
cmds.remove::<Poison>(mage);
cmds.apply(&mut world);
println!("\n=== After removing poison ===");
println!("Mage has Poison? {}", world.has::<Poison>(mage));
println!("Mage health: {:?}", world.get::<Health>(mage));
}
fn print_entities(world: &World) {
let q = world.query::<(&Position, &Health)>();
q.for_each_with_entity(|entity, (pos, hp)| {
println!(
" {:?}: pos=({:.0}, {:.0}) hp={:.0}",
entity, pos.x, pos.y, hp.0
);
});
println!(" Total entities: {}", world.entity_count());
}