use std::{fmt::Debug, ops::Deref};
use amethyst_assets::PrefabData;
use amethyst_core::ecs::{
storage::MaskedStorage, world::EntitiesRes, Component, DenseVecStorage, Entity, Join, Storage,
WriteStorage,
};
use amethyst_derive::PrefabData;
use amethyst_error::Error;
use log::error;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PrefabData)]
#[prefab(Component)]
pub struct Removal<I>
where
I: Debug + Clone + Send + Sync + 'static,
{
id: I,
}
impl<I> Removal<I>
where
I: Debug + Clone + Send + Sync + 'static,
{
pub fn new(id: I) -> Self {
Removal { id }
}
}
impl<I> Component for Removal<I>
where
I: Debug + Clone + Send + Sync + 'static,
{
type Storage = DenseVecStorage<Self>;
}
pub fn exec_removal<I, D>(
entities: &EntitiesRes,
removal_storage: &Storage<'_, Removal<I>, D>,
removal_id: I,
) where
I: Debug + Clone + PartialEq + Send + Sync + 'static,
D: Deref<Target = MaskedStorage<Removal<I>>>,
{
for (e, _) in (&*entities, removal_storage)
.join()
.filter(|(_, r)| r.id == removal_id)
{
if let Err(err) = entities.delete(e) {
error!("Failed to delete entity during exec_removal: {:?}", err);
}
}
}
pub fn add_removal_to_entity<T: PartialEq + Clone + Debug + Send + Sync + 'static>(
entity: Entity,
id: T,
storage: &mut WriteStorage<'_, Removal<T>>,
) {
storage
.insert(entity, Removal::new(id))
.unwrap_or_else(|_| {
panic!(
"Failed to insert `Removal` component id to entity {:?}.",
entity,
)
});
}