use std::cell::RefCell;
use std::marker::PhantomData;
use crate::entity::{self, ealloc};
use crate::world::offline;
use crate::{comp, Archetype};
pub struct EntityCreator<'t, A: Archetype> {
buffer: &'t RefCell<&'t mut offline::BufferShard>,
ealloc: ealloc::BorrowedShard<'t, A>,
}
impl<'t, A: Archetype> EntityCreator<'t, A> {
pub fn new(
buffer: &'t RefCell<&'t mut offline::BufferShard>,
ealloc: ealloc::BorrowedShard<'t, A>,
) -> Self {
Self { buffer, ealloc }
}
pub fn create(&mut self, comps: comp::Map<A>) -> entity::Entity<A> {
self.with_hint(comps, Default::default())
}
pub fn with_hint(
&mut self,
comps: comp::Map<A>,
hint: <A::Ealloc as entity::Ealloc>::AllocHint,
) -> entity::Entity<A> {
let mut buffer = self.buffer.borrow_mut();
let ealloc = &mut *self.ealloc;
buffer.create_entity_with_hint_and_shard(comps, &mut *ealloc, hint)
}
}
pub struct EntityDeleter<'t, A: Archetype> {
buffer: &'t RefCell<&'t mut offline::BufferShard>,
_ph: PhantomData<A>,
}
impl<'t, A: Archetype> EntityDeleter<'t, A> {
pub fn new(buffer: &'t RefCell<&'t mut offline::BufferShard>) -> Self {
Self { buffer, _ph: PhantomData }
}
pub fn queue<E: entity::Ref<Archetype = A>>(&mut self, entity: E) {
let mut buffer = self.buffer.borrow_mut();
buffer.delete_entity::<A, E>(entity);
}
}
#[cfg(test)]
mod tests;