extern crate specs;
use specs::prelude::*;
use crate::{simulation::Plugin, integrator::{INTEGRATE_POSITION_SYSTEM_NAME}};
pub struct DeleteToBeDestroyedEntitiesSystem;
impl<'a> System<'a> for DeleteToBeDestroyedEntitiesSystem {
type SystemData = (Entities<'a>, ReadStorage<'a, ToBeDestroyed>);
fn run(&mut self, (ents, des): Self::SystemData) {
for (entity, _) in (&ents, &des).join() {
ents.delete(entity).expect("Could not delete entity");
}
}
}
pub struct DestroyAtomsPlugin;
impl Plugin for DestroyAtomsPlugin {
fn build(&self, builder: &mut crate::simulation::SimulationBuilder) {
builder.dispatcher_builder.add(
DeleteToBeDestroyedEntitiesSystem,
"",
&[INTEGRATE_POSITION_SYSTEM_NAME],
);
}
fn deps(&self) -> Vec::<Box<dyn Plugin>> {
Vec::new()
}
}
#[derive(Default)]
pub struct ToBeDestroyed;
impl Component for ToBeDestroyed {
type Storage = NullStorage<Self>;
}
pub mod tests {
#[allow(unused_imports)]
use super::*;
extern crate specs;
#[allow(unused_imports)]
use specs::{Builder, Entity, RunNow, World};
extern crate nalgebra;
#[allow(unused_imports)]
use crate::atom::Position;
#[allow(unused_imports)]
use nalgebra::Vector3;
#[test]
fn test_to_be_destroyed_system() {
let mut test_world = World::new();
test_world.register::<ToBeDestroyed>();
test_world.register::<Position>();
let test_entity1 = test_world.create_entity().with(Position::new()).build();
let test_entity2 = test_world
.create_entity()
.with(Position::new())
.with(ToBeDestroyed)
.build();
let mut system = DeleteToBeDestroyedEntitiesSystem;
system.run_now(&test_world);
test_world.maintain();
let positions = test_world.read_storage::<Position>();
assert!(positions.get(test_entity1).is_some());
assert!(positions.get(test_entity2).is_none());
}
}