entity_component/
components.rs1use crate::{create_bitset, Entity, BitSetVec, BitSet, ComponentIterator, ComponentIteratorMut, BITSET_SIZE};
2
3use std::collections::HashMap;
4use std::any::{TypeId, Any};
5use std::sync::Mutex;
6use atomic_refcell_try::AtomicRefMut;
7
8lazy_static::lazy_static! {
9 #[doc(hidden)]
10 pub static ref COMPONENT_REGISTRY: Mutex<HashMap<TypeId, Box<dyn Fn(AtomicRefMut<dyn Any+'static>, &[Entity]) + Send + Sync>>> = Mutex::new(HashMap::default());
11}
12
13pub struct Components<T> {
17 bitset: BitSetVec,
18 components: Vec<Option<T>>,
19}
20
21impl<T: 'static> Default for Components<T> {
22 fn default() -> Self {
23 COMPONENT_REGISTRY.lock().unwrap().insert(TypeId::of::<Self>(), Box::new(|any, entities| {
27 let mut me = AtomicRefMut::map(any, |j| j.downcast_mut::<Self>().unwrap());
28 for e in entities {
29 me.remove(*e);
30 }
31 }));
32 Self {
33 bitset: create_bitset(),
34 components: Vec::with_capacity(BITSET_SIZE >> 4),
36 }
37 }
38}
39
40impl<T> Components<T> {
41 pub fn insert(&mut self, entity: Entity, component: T) -> Option<T> {
44 let mut insertion = Some(component);
45 if self.bitset.bit_test(entity.index() as usize) {
46 std::mem::swap(
47 &mut insertion,
48 &mut self.components[entity.index() as usize],
49 );
50 insertion
51 } else {
52 self.allocate_enough(entity.index() as usize);
53 self.bitset.bit_set(entity.index() as usize);
54 self.components[entity.index() as usize] = insertion;
55 None
56 }
57 }
58 fn allocate_enough(&mut self, until: usize) {
61 if self.components.len() <= until {
62 let qty = (until - self.components.len()) + 1;
63 for _ in 0..qty {
64 self.components.push(None);
65 }
66 }
67 }
68 pub fn get(&self, entity: Entity) -> Option<&T> {
70 if self.bitset.bit_test(entity.index() as usize) {
71 self.components[entity.index() as usize].as_ref()
72 } else {
73 None
74 }
75 }
76 pub fn get_mut(&mut self, entity: Entity) -> Option<&mut T> {
78 if self.bitset.bit_test(entity.index() as usize) {
79 self.components[entity.index() as usize].as_mut()
80 } else {
81 None
82 }
83 }
84 pub fn remove(&mut self, entity: Entity) -> Option<T> {
88 let idx = entity.index() as usize;
89 if self.bitset.bit_test(idx) {
90 self.bitset.bit_reset(idx);
91 let mut ret = None;
92 std::mem::swap(&mut ret, &mut self.components[idx]);
93 ret
94 } else {
95 None
96 }
97 }
98 pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a T> {
101 self.components.iter().flatten()
102 }
103 pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> {
106 self.components.iter_mut().flatten()
107 }
108 pub fn iter_with_bitset<'a>(&'a self, bitset: std::rc::Rc<BitSetVec>) -> ComponentIterator<'a, T> {
112 ComponentIterator {
113 current_id: 0,
114 max_id: self.components.len(),
115 storage: &self.components,
116 bitset,
117 }
118 }
119 pub fn iter_mut_with_bitset<'a>(
123 &'a mut self,
124 bitset: std::rc::Rc<BitSetVec>,
125 ) -> ComponentIteratorMut<'a, T> {
126 ComponentIteratorMut {
127 current_id: 0,
128 max_id: self.components.len(),
129 storage: &mut self.components,
130 bitset,
131 }
132 }
133 pub fn bitset(&self) -> &BitSetVec {
143 &self.bitset
144 }
145}
146
147#[cfg(test)]
148mod tests {
149 use crate::*;
150 #[test]
151 fn create_remove_components() {
152 #[derive(Debug, Clone, PartialEq, Eq)]
153 struct A;
154
155 let mut entities = Entities::default();
156 let e1 = entities.create();
157 let e2 = entities.create();
158
159 let mut storage = Components::<A>::default();
160 storage.insert(e1, A);
161 storage.insert(e2, A);
162 assert!(storage.get(e1).is_some());
163 storage.remove(e1);
164 assert!(storage.get(e1).is_none());
165 assert_eq!(storage.iter().cloned().collect::<Vec<_>>(), vec![A])
166 }
167}
168
169