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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
mod add_component;
mod entity_id;
use crate::error;
use crate::sparse_set::ViewAddEntity;
use crate::unknown_storage::UnknownStorage;
use add_component::AddComponent;
use alloc::vec::Vec;
use core::any::{Any, TypeId};
pub use entity_id::EntityId;
pub struct EntitiesMut;
pub struct Entities {
data: Vec<EntityId>,
list: Option<(usize, usize)>,
}
impl Default for Entities {
fn default() -> Self {
Entities {
data: Vec::new(),
list: None,
}
}
}
impl Entities {
pub(super) fn delete(&mut self, entity: EntityId) -> bool {
self.delete_unchecked(entity)
}
pub fn is_alive(&self, entity: EntityId) -> bool {
entity.uindex() < self.data.len()
&& entity == unsafe { *self.data.get_unchecked(entity.uindex()) }
}
pub fn try_add_component<C, S: AddComponent<C>>(
&self,
storages: S,
component: C,
entity: EntityId,
) -> Result<(), error::AddComponent> {
storages.try_add_component(component, entity, &self)
}
pub fn add_component<C, S: AddComponent<C>>(
&self,
storages: S,
component: C,
entity: EntityId,
) {
storages
.try_add_component(component, entity, &self)
.unwrap()
}
pub(super) fn generate(&mut self) -> EntityId {
let index = self.list.map(|(_, old)| old);
if let Some((new, ref mut old)) = self.list {
if new == *old {
self.list = None;
} else {
*old = unsafe { self.data.get_unchecked(*old).uindex() };
}
}
if let Some(index) = index {
unsafe { self.data.get_unchecked_mut(index).set_index(index as u64) };
unsafe { *self.data.get_unchecked(index) }
} else {
let entity_id = EntityId::new(self.data.len() as u64);
self.data.push(entity_id);
entity_id
}
}
pub fn delete_unchecked(&mut self, entity_id: EntityId) -> bool {
if self.is_alive(entity_id) {
if unsafe {
self.data
.get_unchecked_mut(entity_id.uindex())
.bump_version()
.is_ok()
} {
if let Some((ref mut new, _)) = self.list {
unsafe {
self.data
.get_unchecked_mut(*new)
.set_index(entity_id.index())
};
*new = entity_id.uindex();
} else {
self.list = Some((entity_id.uindex(), entity_id.uindex()));
}
}
true
} else {
false
}
}
pub fn add_entity<T: ViewAddEntity>(
&mut self,
storages: T,
component: T::Component,
) -> EntityId {
let entity_id = self.generate();
storages.add_entity(component, entity_id);
entity_id
}
}
impl UnknownStorage for Entities {
fn delete(&mut self, _entity: EntityId, _: &mut Vec<TypeId>) {}
fn clear(&mut self) {
if self.data.is_empty() {
return;
}
let mut last_alive = self.data.len() as u64 - 1;
for (i, id) in self.data.iter_mut().enumerate().rev() {
let target = last_alive;
if id.bump_version().is_ok() {
last_alive = i as u64;
}
id.set_index(target);
}
let begin = self
.data
.iter()
.position(|id| id.version() < ((1u64 << (EntityId::VERSION_LEN + 1)) - 1))
.unwrap();
let end = self
.data
.iter()
.rev()
.position(|id| id.version() < ((1u64 << (EntityId::VERSION_LEN + 1)) - 1))
.unwrap();
self.list = Some((self.data.len() - end - 1, begin));
}
fn unpack(&mut self, _entity: EntityId) {}
fn any(&self) -> &dyn Any {
self
}
fn any_mut(&mut self) -> &mut dyn Any {
self
}
}
#[test]
fn entities() {
use core::num::NonZeroU64;
let mut entities = Entities::default();
let key00 = entities.generate();
let key10 = entities.generate();
assert_eq!(key00.index(), 0);
assert_eq!(key00.version(), 0);
assert_eq!(key10.index(), 1);
assert_eq!(key10.version(), 0);
assert!(entities.delete_unchecked(key00));
assert!(!entities.delete_unchecked(key00));
let key01 = entities.generate();
assert_eq!(key01.index(), 0);
assert_eq!(key01.version(), 1);
assert!(entities.delete_unchecked(key10));
assert!(entities.delete_unchecked(key01));
let key11 = entities.generate();
let key02 = entities.generate();
assert_eq!(key11.index(), 1);
assert_eq!(key11.version(), 1);
assert_eq!(key02.index(), 0);
assert_eq!(key02.version(), 2);
let last_key = EntityId(NonZeroU64::new(!(!0 >> 15) + 1).unwrap());
entities.data[0] = last_key;
assert!(entities.delete_unchecked(last_key));
assert_eq!(entities.list, None);
let dead = entities.generate();
assert_eq!(dead.index(), 2);
assert_eq!(dead.version(), 0);
}