use std::marker::PhantomData;
use hecs::{
Bundle, CommandBuffer as CommandBufferInternal, Component, DynamicBundle, Entity, World,
};
pub trait WriteCmd: Component {
fn execute(&mut self, world: &mut World);
}
struct RemoveCmd<C: Component> {
entity: Entity,
marker: PhantomData<C>,
}
impl<C: Component> RemoveCmd<C> {
fn new(entity: Entity) -> Self {
Self {
entity,
marker: PhantomData,
}
}
}
impl<C: Component + Bundle> WriteCmd for RemoveCmd<C> {
fn execute(&mut self, world: &mut World) {
world
.remove::<C>(self.entity)
.expect("Failed to remove components from entity");
}
}
impl<F: FnMut(&mut World) + Component> WriteCmd for F {
fn execute(&mut self, world: &mut World) {
(self)(world)
}
}
#[derive(Default)]
pub struct CommandBuffer {
components: CommandBufferInternal,
despawns: Vec<Entity>,
writes: Vec<Box<dyn WriteCmd>>,
}
impl CommandBuffer {
pub fn new() -> Self {
Self::default()
}
pub fn insert(&mut self, entity: Entity, components: impl DynamicBundle) {
self.components.insert(entity, components)
}
pub fn insert_one(&mut self, entity: Entity, component: impl Component) {
self.components.insert(entity, (component,))
}
pub fn spawn(&mut self, components: impl DynamicBundle) {
self.components.spawn(components)
}
pub fn despawn(&mut self, entity: Entity) {
self.despawns.push(entity)
}
pub fn remove<C: Component + Bundle>(&mut self, entity: Entity) {
self.writes.push(Box::new(RemoveCmd::<C>::new(entity)))
}
pub fn remove_one<C: Component>(&mut self, entity: Entity) {
self.writes.push(Box::new(RemoveCmd::<(C,)>::new(entity)))
}
pub fn execute(&mut self, world: &mut World) {
self.components.run_on(world);
self.writes.drain(..).for_each(|mut cmd| cmd.execute(world));
self.despawns
.drain(..)
.for_each(|e| world.despawn(e).expect("Failed to despawn entity"));
}
pub fn write(&mut self, cmd: impl WriteCmd) {
self.writes.push(Box::new(cmd))
}
pub fn clear(&mut self) {
self.despawns.clear();
self.writes.clear();
self.components.clear();
}
}
impl WriteCmd for CommandBuffer {
fn execute(&mut self, world: &mut World) {
self.execute(world)
}
}