The following code:
let mut world = World::new();
struct DeleteSys {
pub entity: Option<entity>,
};
impl System for DeleteSys {
type SystemData = Entities;
fn run(&mut self, entities: Self::SystemData) {
if let Some(entity) = self.entity {
if let Err(err) = entities.delete(entity) {
println!("Failed deleting entity: {}", err);
}
}
self.entity = None;
}
}
let mut delete = DeleteSys { entity: None };
struct PrintSys;
impl System for PrintSys {
type SystemData = (
Entities,
ReadStorage,
ReadStorage,
);
fn run(&mut self, (entities, ints, bools): Self::SystemData) {
println!("Without entities: {}", (&ints, &bools).join().count());
println!("With entities: {}", (&*entities, &ints, &bools).join().count());
}
}
let mut print = PrintSys;
System::setup(&mut print, &mut world.res);
let _e1 = world
.create_entity()
.with(CompInt(12))
.with(CompBool(true))
.build();
let e2 = world
.create_entity()
.with(CompInt(12))
.with(CompBool(true))
.build();
let _e3 = world
.create_entity()
.with(CompInt(12))
.with(CompBool(true))
.build();
world.maintain();
print.run_now(&world.res);
delete.entity = Some(e2);
delete.run_now(&world.res);
world.maintain();
print.run_now(&world.res);
</entity>
prints the following
Without entities: 3
With entities: 3
Without entities: 3
With entities: 2
该提问来源于开源项目:amethyst/specs