use self::arch_storage::{ArchStorage, ArchStorageIndex};
use crate::{
archetype::Archetype,
entity::EntityId,
prelude::{Bundle, ComponentFactory, ComponentId},
};
use bevy_ptr::PtrMut;
use std::ops::Deref;
pub mod arch_storage;
pub mod storages;
pub mod tag_storage;
pub struct ArchEntityStorage {
arch_storage: ArchStorage,
entities: Vec<EntityId>,
}
impl Deref for ArchEntityStorage {
type Target = ArchStorage;
fn deref(&self) -> &Self::Target {
&self.arch_storage
}
}
impl ArchEntityStorage {
pub fn new<A: Archetype>(compf: &ComponentFactory) -> Option<Self> {
Some(Self {
arch_storage: ArchStorage::new::<A>(compf)?,
entities: Vec::new(),
})
}
pub fn next_index(&self) -> ArchStorageIndex {
ArchStorageIndex(self.len())
}
pub fn store_entity<B: Bundle + Archetype>(
&mut self,
entity_id: EntityId,
bundle: B,
compf: &ComponentFactory,
) -> Option<ArchStorageIndex> {
self.entities.push(entity_id);
self.arch_storage.store_bundle(compf, bundle)
}
pub fn get_component_mut(
&mut self,
index: ArchStorageIndex,
comp_id: ComponentId,
) -> Option<PtrMut<'_>> {
self.arch_storage.get_component_mut(index, comp_id)
}
pub unsafe fn get_component_mut_unchecked(
&mut self,
index: ArchStorageIndex,
comp_id: ComponentId,
) -> PtrMut<'_> { unsafe {
self.arch_storage
.get_component_mut_unchecked(index, comp_id)
}}
pub fn get_entity_at(&self, index: ArchStorageIndex) -> Option<EntityId> {
self.entities.get(index.0).copied()
}
pub unsafe fn get_entity_at_unchecked(&self, index: ArchStorageIndex) -> EntityId { unsafe {
*self.entities.get_unchecked(index.0)
}}
pub fn swap_remove(&mut self, index: ArchStorageIndex) -> Option<EntityId> {
self.entities.swap_remove(index.0);
unsafe { self.arch_storage.swap_remove_unchecked(index) }
self.get_entity_at(index) }
}