1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use crate::{prelude::*, query, storage::StorageGuard, StorageRef, StorageRefMut};
pub use hobo_derive::AsEntity;
use owning_ref::{OwningRef, OwningRefMut};
use std::any::type_name;

/// An opaque copyable identifier that is used to attach and fetch components
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Entity(pub(crate) u64);

impl Entity {
	pub(crate) fn root() -> Self { Self(0) }
}

pub trait AsEntity {
	fn as_entity(&self) -> Entity;
	#[inline] fn try_get_cmp<'a, C: 'static>(&self) -> Option<OwningRef<StorageRef<'a, C>, C>> where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let entity = self.as_entity();
		let storage = world.storage::<C>();
		if !storage.has(entity) {
			World::unmark_borrow_mut();
			return None;
		}
		let res = Some(OwningRef::new(storage).map(|x| x.get(entity).unwrap()));
		World::unmark_borrow_mut();
		res
	}
	#[inline] fn try_get_cmp_mut<'a, C: 'static>(&self) -> Option<OwningRefMut<StorageGuard<'a, C, StorageRefMut<'a, C>>, C>> where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let entity = self.as_entity();
		if !world.storage::<C>().has(entity) {
			World::unmark_borrow_mut();
			return None;
		}
		let res = Some(OwningRefMut::new(world.storage_mut::<C>()).map_mut(|x| x.get_mut(entity).unwrap()));
		World::unmark_borrow_mut();
		res
	}
	#[inline]
	#[track_caller]
	fn get_cmp<'a, C: 'static>(&self) -> OwningRef<StorageRef<'a, C>, C> where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = OwningRef::new(world.storage::<C>()).try_map(|x| x.get(self).ok_or_else(|| type_name::<C>())).expect("entity does not have component");
		World::unmark_borrow_mut();
		res
	}
	#[inline]
	#[track_caller]
	fn get_cmp_mut<'a, C: 'static>(&self) -> OwningRefMut<StorageGuard<'a, C, StorageRefMut<'a, C>>, C> where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = OwningRefMut::new(world.storage_mut::<C>()).try_map_mut(|x| x.get_mut(self).ok_or_else(|| type_name::<C>())).expect("entity does not have component");
		World::unmark_borrow_mut();
		res
	}
	#[inline] fn get_cmp_mut_or<'a, C: 'static>(&self, f: impl FnOnce() -> C) -> OwningRefMut<StorageGuard<'a, C, StorageRefMut<'a, C>>, C> where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = OwningRefMut::new(world.storage_mut::<C>()).map_mut(move |x| x.get_mut_or(self, f));
		World::unmark_borrow_mut();
		res
	}
	#[inline] fn get_cmp_mut_or_default<'a, C: 'static + Default>(&self) -> OwningRefMut<StorageGuard<'a, C, StorageRefMut<'a, C>>, C> where Self: Sized {
		self.get_cmp_mut_or(Default::default)
	}
	#[inline] fn remove_cmp<C: 'static>(&self) where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		world.storage_mut::<C>().remove(self);
		World::unmark_borrow_mut();
	}
	fn find_in_ancestors<Q: query::Query>(&self) -> Vec<Q::Fetch> {
		let mut entities = Some(Parent::ancestors(self.as_entity()).into_iter().collect());
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().map(|entity| Q::fetch(world, entity)).collect::<Vec<_>>();
		World::unmark_borrow_mut();
		res
	}
	fn try_find_first_in_ancestors<Q: query::Query>(&self) -> Option<Q::Fetch> {
		let mut entities = Some(Parent::ancestors(self.as_entity()).into_iter().collect());
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().next().map(|e| Q::fetch(world, e));
		World::unmark_borrow_mut();
		res
	}
	#[inline]
	#[track_caller]
	fn find_first_in_ancestors<Q: query::Query>(&self) -> Q::Fetch { self.try_find_first_in_ancestors::<Q>().expect("could not find query in ancestor") }
	fn find_in_descendants<Q: query::Query>(&self) -> Vec<Q::Fetch> {
		let mut entities = Some(Children::descendants(self.as_entity()).into_iter().collect());
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().map(|entity| Q::fetch(world, entity)).collect::<Vec<_>>();
		World::unmark_borrow_mut();
		res
	}
	fn find_in_children<Q: query::Query>(&self) -> Vec<Q::Fetch> {
		let mut entities = Some(self.as_entity().try_get_cmp::<Children>().map_or_else(default, |x| x.0.iter().copied().collect()));
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().map(|entity| Q::fetch(world, entity)).collect::<Vec<_>>();
		World::unmark_borrow_mut();
		res
	}
	fn try_find_first_in_descendants<Q: query::Query>(&self) -> Option<Q::Fetch> {
		let mut entities = Some(Children::descendants(self.as_entity()).into_iter().collect());
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().next().map(|e| Q::fetch(world, e));
		World::unmark_borrow_mut();
		res
	}
	fn try_find_first_in_children<Q: query::Query>(&self) -> Option<Q::Fetch> {
		let mut entities = Some(self.as_entity().try_get_cmp::<Children>().map_or_else(default, |x| x.0.iter().copied().collect()));
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		Q::filter(world, &mut entities);
		let res = entities.unwrap_or_default().into_iter().next().map(|e| Q::fetch(world, e));
		World::unmark_borrow_mut();
		res
	}
	#[inline]
	#[track_caller]
	fn find_first_in_descendants<Q: query::Query>(&self) -> Q::Fetch { self.try_find_first_in_descendants::<Q>().expect("could not find query in descendant") }
	#[inline]
	#[track_caller]
	fn find_first_in_children<Q: query::Query>(&self) -> Q::Fetch { self.try_find_first_in_children::<Q>().expect("could not find child") }
	#[inline] fn has_cmp<C: 'static>(&self) -> bool where Self: Sized {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = world.storage::<C>().has(self.as_entity());
		World::unmark_borrow_mut();
		res
	}

	#[inline] fn remove(&self) {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = world.remove_entity(self.as_entity());
		World::unmark_borrow_mut();
		res
	}
	#[inline] fn is_dead(&self)  -> bool {
		World::mark_borrow();
		let world = unsafe { &*WORLD.get() as &World };
		let res = world.is_dead(self.as_entity());
		World::unmark_borrow();
		res
	}
	#[inline] fn add_component<T: 'static>(&self, component: T) {
		World::mark_borrow_mut();
		let world = unsafe { &mut *WORLD.get() as &mut World };
		let res = world.storage_mut::<T>().add(self.as_entity(), component);
		World::unmark_borrow_mut();
		res
	}
	#[inline] fn component<T: 'static>(self, component: T) -> Self where Self: Sized { self.add_component(component); self }
}

impl AsEntity for Entity {
	fn as_entity(&self) -> Entity { *self }
}

impl<T: AsEntity> AsEntity for &T {
	fn as_entity(&self) -> Entity { T::as_entity(*self) }
}
impl<T: AsEntity> AsEntity for &mut T {
	fn as_entity(&self) -> Entity { T::as_entity(*self) }
}