1use std::any::TypeId;
4use std::collections::HashMap;
5
6use crate::entity::{Alloc, Entity};
7use crate::storage::{pair_iter, SparseSet, Storage};
8
9pub trait Component: std::any::Any + Send + Sync + 'static {}
14impl<T: std::any::Any + Send + Sync> Component for T {}
15
16#[derive(Default)]
18pub struct World {
19 alloc: Alloc,
20 storages: HashMap<TypeId, Box<dyn Storage>>,
21}
22
23impl World {
24 pub fn new() -> Self {
25 Self::default()
26 }
27
28 pub fn spawn(&mut self) -> Entity {
30 self.alloc.spawn()
31 }
32
33 pub fn despawn(&mut self, entity: Entity) -> bool {
36 if !self.alloc.alive(entity) {
37 return false;
38 }
39 for storage in self.storages.values_mut() {
40 storage.remove_entity(entity);
41 }
42 self.alloc.despawn(entity);
43 true
44 }
45
46 pub fn alive(&self, entity: Entity) -> bool {
48 self.alloc.alive(entity)
49 }
50
51 pub fn entities(&self) -> Vec<Entity> {
53 self.alloc.iter_live().collect()
54 }
55
56 pub fn len(&self) -> usize {
57 self.alloc.live_len()
58 }
59
60 pub fn is_empty(&self) -> bool {
61 self.len() == 0
62 }
63
64 fn storage_of<T: Component>(&self) -> Option<&SparseSet<T>> {
65 self.storages
66 .get(&TypeId::of::<T>())
67 .and_then(|s| s.as_any().downcast_ref::<SparseSet<T>>())
68 }
69
70 fn entry<T: Component>(&mut self) -> &mut SparseSet<T> {
71 self.storages
72 .entry(TypeId::of::<T>())
73 .or_insert_with(|| Box::new(SparseSet::<T>::new()))
74 .as_any_mut()
75 .downcast_mut::<SparseSet<T>>()
76 .expect("Speicher-Tabelle passt zum TypeId")
77 }
78
79 pub fn insert<T: Component>(&mut self, entity: Entity, component: T) -> Option<T> {
82 if !self.alloc.alive(entity) {
83 return None;
84 }
85 self.entry::<T>().insert(entity, component)
86 }
87
88 pub fn get<T: Component>(&self, entity: Entity) -> Option<&T> {
89 self.storage_of::<T>()?.get(entity)
90 }
91
92 pub fn get_mut<T: Component>(&mut self, entity: Entity) -> Option<&mut T> {
93 if !self.alloc.alive(entity) {
94 return None;
95 }
96 self.entry::<T>().get_mut(entity)
97 }
98
99 pub fn remove<T: Component>(&mut self, entity: Entity) -> Option<T> {
101 self.storages
102 .get_mut(&TypeId::of::<T>())?
103 .as_any_mut()
104 .downcast_mut::<SparseSet<T>>()?
105 .remove(entity)
106 }
107
108 pub fn has<T: Component>(&self, entity: Entity) -> bool {
109 self.storage_of::<T>().is_some_and(|s| s.contains(entity))
110 }
111
112 pub fn count<T: Component>(&self) -> usize {
114 self.storage_of::<T>().map(|s| s.len()).unwrap_or(0)
115 }
116
117 pub fn for_each<T: Component>(&self, f: impl FnMut(Entity, &T)) {
119 if let Some(set) = self.storage_of::<T>() {
120 set.for_each(f);
121 }
122 }
123
124 pub fn for_each_mut<T: Component>(&mut self, f: impl FnMut(Entity, &mut T)) {
126 if let Some(boxed) = self.storages.remove(&TypeId::of::<T>()) {
129 let mut boxed = boxed;
130 if let Some(set) = boxed.as_any_mut().downcast_mut::<SparseSet<T>>() {
131 set.for_each_mut(f);
132 }
133 self.storages.insert(TypeId::of::<T>(), boxed);
134 }
135 }
136
137 pub fn for_each_pair<A: Component, B: Component>(
141 &mut self,
142 f: impl FnMut(Entity, &mut A, &mut B),
143 ) {
144 debug_assert_ne!(
145 TypeId::of::<A>(),
146 TypeId::of::<B>(),
147 "for_each_pair braucht zwei verschiedene Komponenten-Typen"
148 );
149 let mut a = self.storages.remove(&TypeId::of::<A>());
150 let mut b = self.storages.remove(&TypeId::of::<B>());
151 if let (Some(a), Some(b)) = (&mut a, &mut b) {
152 if let (Some(a), Some(b)) = (
153 a.as_any_mut().downcast_mut::<SparseSet<A>>(),
154 b.as_any_mut().downcast_mut::<SparseSet<B>>(),
155 ) {
156 pair_iter(a, b, f);
157 }
158 }
159 if let Some(x) = a {
160 self.storages.insert(TypeId::of::<A>(), x);
161 }
162 if let Some(x) = b {
163 self.storages.insert(TypeId::of::<B>(), x);
164 }
165 }
166}