1use crate::entity::{EntityStore, EntityId};
2use crate::arch::{ArchStore, NewEntityResult};
3use crate::component::{Component, Flag};
4
5pub struct World {
7 entity_store: EntityStore,
8 arch_store: ArchStore,
9}
10
11impl World {
12 pub fn new() -> Self {
13 Self {
14 entity_store: EntityStore::new(),
15 arch_store: ArchStore::new(),
16 }
17 }
18
19 kiwi_internal_macros::gen_spawn_entity!();
20
21 pub fn kill(&mut self, ent_id: EntityId) {
28 let ent = &self.entity_store.entities()[ent_id as usize];
29 self.arch_store.remove_entity(ent);
30
31 self.entity_store.kill(ent_id);
32 }
33
34 pub fn kill_and_keep(&mut self, ent_id: EntityId) {
41 let ent = &self.entity_store.entities()[ent_id as usize];
42 self.arch_store.remove_entity(ent);
43
44 self.entity_store.kill(ent_id);
45 }
46
47 pub fn free_id(&mut self, ent_id: EntityId) {
52 self.entity_store.free_id(ent_id);
53 }
54
55 pub fn is_alive(&mut self, ent_id: EntityId) -> bool {
58 self.entity_store.is_alive(ent_id)
59 }
60
61 pub fn entity_count(&self) -> usize {
63 self.entity_store.entity_count()
64 }
65
66 pub fn get_component<T: Component + 'static>(&self, entity: EntityId) -> &T {
77 let entity = &self.entity_store.entities()[entity as usize];
78 unsafe { self.arch_store.get_archetype(entity.arch_id).get_component::<T>(entity.arch_row) }
79 }
80
81 pub fn get_component_mut<T: Component + 'static>(&mut self, entity: EntityId) -> &mut T {
86 let entity = &self.entity_store.entities()[entity as usize];
87 unsafe { self.arch_store.get_archetype_mut(entity.arch_id).get_component_mut::<T>(entity.arch_row) }
88 }
89
90 pub fn set_component<T: Component + 'static>(&mut self, entity: EntityId, comp: T) {
95 let entity = &self.entity_store.entities()[entity as usize];
96 self.arch_store.get_archetype_mut(entity.arch_id).set_component(entity.arch_row, comp);
97 }
98
99 pub fn has_component<C: Component>(&self, entity: EntityId) -> bool {
101 let entity = &self.entity_store.entities()[entity as usize];
102 self.arch_store.get_archetype(entity.arch_id).has_component(C::id())
103 }
104
105 pub fn has_flag<F: Flag>(&self, entity: EntityId, flag: F) -> bool {
107 self.entity_store.has_flag(entity, flag.into())
108 }
109
110 pub fn set_flag<F: Flag>(&mut self, entity: EntityId, flag: F) {
116 self.entity_store.set_flag(entity, flag.into())
117 }
118
119 pub fn unset_flag<F: Flag>(&mut self, entity: EntityId, flag: F) {
121 self.entity_store.unset_flag(entity, flag.into())
122 }
123}
124
125impl World {
127 pub fn query_ids<'a>(&'a self) -> impl std::iter::Iterator<Item = EntityId> + 'a {
129 self.entity_store.entities().iter()
130 .enumerate()
131 .map(|(id, _)| id as EntityId)
132 .filter(|id| {
133 self.entity_store.is_alive(*id)
134 })
135 }
136
137 #[inline]
138 #[doc(hidden)]
139 pub fn query_ids0<'a>(&'a self) -> impl std::iter::Iterator<Item = EntityId> + 'a {
140 self.query_ids()
141 }
142
143 kiwi_internal_macros::gen_query!();
144}