use std::cell::{Ref, RefMut};
use crate::entity::{ArchetypeId, Entity, EntityRaw};
pub trait World: Sized {
#[inline(always)]
fn create<A: Archetype>(&mut self, components: A::Components) -> Entity<A>
where
Self: WorldHas<A>,
{
<Self as WorldHas<A>>::resolve_create(self, components)
}
#[inline(always)]
fn try_create<A: Archetype>(&mut self, components: A::Components) -> Option<Entity<A>>
where
Self: WorldHas<A>,
{
<Self as WorldHas<A>>::resolve_try_create(self, components)
}
#[inline(always)]
fn destroy<K: EntityKey>(&mut self, entity: K) -> K::DestroyOutput
where
Self: WorldCanResolve<K>,
{
<Self as WorldCanResolve<K>>::resolve_destroy(self, entity)
}
#[inline(always)]
fn archetype<A: Archetype>(&self) -> &A
where
Self: WorldHas<A>,
{
<Self as WorldHas<A>>::resolve_archetype(self)
}
#[inline(always)]
fn archetype_mut<A: Archetype>(&mut self) -> &mut A
where
Self: WorldHas<A>,
{
<Self as WorldHas<A>>::resolve_archetype_mut(self)
}
}
pub trait Archetype
where
Self: Sized,
for<'a> Self: ArchetypeCanResolve<'a, Self::View<'a>, Entity<Self>>,
for<'a> Self: ArchetypeCanResolve<'a, Self::View<'a>, EntityRaw<Self>>,
{
const ARCHETYPE_ID: ArchetypeId;
type Components;
type Slices<'a>;
type Borrow<'a>;
type View<'a>: View
where
Self: 'a;
#[doc(hidden)]
fn get_slice_entities(&self) -> &[Entity<Self>];
#[inline(always)]
fn view<'a, K: EntityKey>(&'a mut self, entity: K) -> Option<Self::View<'a>>
where
Self: ArchetypeCanResolve<'a, Self::View<'a>, K>,
{
<Self as ArchetypeCanResolve<Self::View<'a>, K>>::resolve_view(self, entity)
}
#[inline(always)]
fn get_slice<C>(&mut self) -> &[C]
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_get_slice(self)
}
#[inline(always)]
fn get_slice_mut<C>(&mut self) -> &mut [C]
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_get_slice_mut(self)
}
#[inline(always)]
fn borrow_slice<C>(&self) -> Ref<[C]>
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_borrow_slice(self)
}
#[inline(always)]
fn borrow_slice_mut<C>(&self) -> RefMut<[C]>
where
Self: ArchetypeHas<C>,
{
<Self as ArchetypeHas<C>>::resolve_borrow_slice_mut(self)
}
}
pub trait WorldHas<A: Archetype>: World {
#[doc(hidden)]
fn resolve_create(&mut self, data: A::Components) -> Entity<A>;
#[doc(hidden)]
fn resolve_try_create(&mut self, data: A::Components) -> Option<Entity<A>>;
#[doc(hidden)]
fn resolve_destroy(&mut self, entity: Entity<A>) -> Option<A::Components>;
#[doc(hidden)]
fn resolve_archetype(&self) -> &A;
#[doc(hidden)]
fn resolve_archetype_mut(&mut self) -> &mut A;
}
pub trait ArchetypeHas<C>: Archetype {
#[doc(hidden)]
fn resolve_get_slice(&mut self) -> &[C];
#[doc(hidden)]
fn resolve_get_slice_mut(&mut self) -> &mut [C];
#[doc(hidden)]
fn resolve_borrow_slice(&self) -> Ref<[C]>;
#[doc(hidden)]
fn resolve_borrow_slice_mut(&self) -> RefMut<[C]>;
#[doc(hidden)]
fn resolve_borrow<'a>(borrow: &'a Self::Borrow<'a>) -> Ref<'a, C>;
#[doc(hidden)]
fn resolve_borrow_mut<'a>(borrow: &'a Self::Borrow<'a>) -> RefMut<'a, C>;
}
pub trait View {
#[inline(always)]
fn component<C>(&self) -> &C
where
Self: ViewHas<C>,
{
<Self as ViewHas<C>>::resolve_component(self)
}
#[inline(always)]
fn component_mut<C>(&mut self) -> &mut C
where
Self: ViewHas<C>,
{
<Self as ViewHas<C>>::resolve_component_mut(self)
}
}
pub trait ViewHas<C>: View {
#[doc(hidden)]
fn resolve_component(&self) -> &C;
#[doc(hidden)]
fn resolve_component_mut(&mut self) -> &mut C;
}
pub trait WorldCanResolve<K: EntityKey> {
#[doc(hidden)]
fn resolve_destroy(&mut self, entity: K) -> K::DestroyOutput;
}
pub trait ArchetypeCanResolve<'a, View, K: EntityKey> {
#[doc(hidden)]
fn resolve_for(&self, entity: K) -> Option<usize>;
#[doc(hidden)]
fn resolve_view(&'a mut self, entity: K) -> Option<View>;
}
#[doc(hidden)]
pub trait EntityKey {
type DestroyOutput;
}
#[doc(hidden)]
pub trait StorageCanResolve<K: EntityKey> {
#[doc(hidden)]
fn resolve_for(&self, entity: K) -> Option<usize>;
}