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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
use core::any::{type_name, TypeId};
use crate::{
action::LocalActionEncoder,
bundle::Bundle,
entity::{Entity, EntityRef, Location},
type_id, NoSuchEntity,
};
use super::{World, WorldLocal};
impl World {
/// Removes component from the specified entity and returns its value.
///
/// Returns `Ok(Some(comp))` if component was removed.
/// Returns `Ok(None)` if entity does not have component of this type.
/// Returns `Err(NoSuchEntity)` if entity is not alive.
#[inline(always)]
pub fn remove<T>(
&mut self,
entity: impl Entity,
) -> Result<(Option<T>, EntityRef<'_>), NoSuchEntity>
where
T: 'static,
{
self.maintenance();
let src_loc = entity.lookup(&self.entities).ok_or(NoSuchEntity)?;
debug_assert!(src_loc.arch < u32::MAX, "Allocated entities were spawned");
if !self.archetypes[src_loc.arch as usize].has_component(type_id::<T>()) {
// Safety: entity is not moved
// Reference is created with correct location of entity in this world.
let e = unsafe { EntityRef::from_parts(entity.id(), src_loc, self.local()) };
return Ok((None, e));
}
let dst_arch = self
.edges
.remove(&mut self.archetypes, src_loc.arch, type_id::<T>());
debug_assert_ne!(src_loc.arch, dst_arch);
let (before, after) = self
.archetypes
.split_at_mut(src_loc.arch.max(dst_arch) as usize);
let (src, dst) = match src_loc.arch < dst_arch {
true => (&mut before[src_loc.arch as usize], &mut after[0]),
false => (&mut after[0], &mut before[dst_arch as usize]),
};
let (dst_idx, opt_src_id, component) =
unsafe { src.remove::<T>(entity.id(), dst, src_loc.idx) };
let dst_loc = Location::new(dst_arch, dst_idx);
self.entities.set_location(entity.id(), dst_loc);
if let Some(src_id) = opt_src_id {
self.entities.set_location(src_id, src_loc);
}
// Safety: entity is moved
// Reference is created with correct location of entity in this world.
let e = unsafe { EntityRef::from_parts(entity.id(), dst_loc, self.local()) };
Ok((Some(component), e))
}
/// Drops component from the specified entity.
///
/// Returns `Err(NoSuchEntity)` if entity is not alive.
#[inline(always)]
pub fn drop<T>(&mut self, entity: impl Entity) -> Result<(), NoSuchEntity>
where
T: 'static,
{
self.drop_erased(entity, type_id::<T>())
}
/// Drops component from the specified entity.
///
/// Returns `Err(NoSuchEntity)` if entity is not alive.
#[inline(always)]
pub fn drop_erased(&mut self, entity: impl Entity, ty: TypeId) -> Result<(), NoSuchEntity> {
self.maintenance();
let src_loc = entity.lookup(&self.entities).ok_or(NoSuchEntity)?;
debug_assert!(src_loc.arch < u32::MAX, "Allocated entities were spawned");
if !self.archetypes[src_loc.arch as usize].has_component(ty) {
// Safety: entity is not moved
// Reference is created with correct location of entity in this world.
return Ok(());
}
let dst_arch = self.edges.remove(&mut self.archetypes, src_loc.arch, ty);
debug_assert_ne!(src_loc.arch, dst_arch);
let (before, after) = self
.archetypes
.split_at_mut(src_loc.arch.max(dst_arch) as usize);
let (src, dst) = match src_loc.arch < dst_arch {
true => (&mut before[src_loc.arch as usize], &mut after[0]),
false => (&mut after[0], &mut before[dst_arch as usize]),
};
let encoder = LocalActionEncoder::new(self.action_buffer.get_mut(), &self.entities);
let (dst_idx, opt_src_id) =
unsafe { src.drop_bundle(entity.id(), dst, src_loc.idx, encoder) };
let dst_loc = Location::new(dst_arch, dst_idx);
self.entities.set_location(entity.id(), dst_loc);
if let Some(src_id) = opt_src_id {
self.entities.set_location(src_id, src_loc);
}
self.execute_local_actions();
Ok(())
}
/// Drops entity's components that are found in the specified bundle.
///
/// If entity is not alive, fails with `Err(NoSuchEntity)`.
///
/// Unlike other methods that use `Bundle` trait, this method does not require
/// all components from bundle to be registered in the world.
/// Entity can't have components that are not registered in the world,
/// so no need to drop them.
///
/// For this reason there's no separate method that uses `ComponentBundle` trait.
///
/// # Example
///
/// ```
/// # use edict::{world::World, ExampleComponent};
///
/// struct OtherComponent;
///
/// let mut world = World::new();
/// let mut entity = world.spawn((ExampleComponent,)).id();
///
/// assert!(world.try_has_component::<ExampleComponent>(entity).unwrap());
/// world.drop_bundle::<(ExampleComponent, OtherComponent)>(entity).unwrap();
/// assert!(!world.try_has_component::<ExampleComponent>(entity).unwrap());
/// ```
#[inline(always)]
pub fn drop_bundle<B>(&mut self, entity: impl Entity) -> Result<(), NoSuchEntity>
where
B: Bundle,
{
if !B::static_valid() {
panic!(
"Specified bundle `{}` is not valid. Check for duplicate component types",
type_name::<B>()
);
}
self.maintenance();
let src_loc = entity.lookup(&self.entities).ok_or(NoSuchEntity)?;
debug_assert!(src_loc.arch < u32::MAX, "Allocated entities were spawned");
if B::static_with_ids(|ids| {
ids.iter()
.all(|&id| !self.archetypes[src_loc.arch as usize].has_component(id))
}) {
// Safety: entity is not moved
// Reference is created with correct location of entity in this world.
return Ok(());
}
let dst_arch = self
.edges
.remove_bundle::<B>(&mut self.archetypes, src_loc.arch);
debug_assert_ne!(src_loc.arch, dst_arch);
let (before, after) = self
.archetypes
.split_at_mut(src_loc.arch.max(dst_arch) as usize);
let (src, dst) = match src_loc.arch < dst_arch {
true => (&mut before[src_loc.arch as usize], &mut after[0]),
false => (&mut after[0], &mut before[dst_arch as usize]),
};
let encoder = LocalActionEncoder::new(self.action_buffer.get_mut(), &self.entities);
let (dst_idx, opt_src_id) =
unsafe { src.drop_bundle(entity.id(), dst, src_loc.idx, encoder) };
let dst_loc = Location::new(dst_arch, dst_idx);
self.entities.set_location(entity.id(), dst_loc);
if let Some(src_id) = opt_src_id {
self.entities.set_location(src_id, src_loc);
}
self.execute_local_actions();
Ok(())
}
}
impl WorldLocal {
/// Drops component from the specified entity.
///
/// Returns `Err(NoSuchEntity)` if entity is not alive.
///
/// This is deferred version of [`World::drop`].
/// It can be used on shared `WorldLocal` reference.
/// Operation is queued and executed on next call to [`World::run_deferred`]
/// or when mutable operation is performed on the world.
///
/// # Example
///
/// ```
/// # use edict::{world::WorldLocal, ExampleComponent};
///
/// let mut world = WorldLocal::new();
/// let mut entity = world.spawn((ExampleComponent,)).id();
///
/// assert!(world.try_has_component::<ExampleComponent>(entity).unwrap());
///
/// world.drop_defer::<ExampleComponent>(entity);
///
/// assert!(world.try_has_component::<ExampleComponent>(entity).unwrap());
///
/// world.run_deferred();
///
/// assert!(!world.try_has_component::<ExampleComponent>(entity).unwrap());
/// ```
#[inline(always)]
pub fn drop_defer<T>(&self, entity: impl Entity)
where
T: 'static,
{
self.drop_erased_defer(entity, type_id::<T>())
}
/// Drops component from the specified entity.
///
/// Returns `Err(NoSuchEntity)` if entity is not alive.
///
/// This is deferred version of [`World::drop_erased`].
/// It can be used on shared `WorldLocal` reference.
/// Operation is queued and executed on next call to [`World::run_deferred`]
/// or when mutable operation is performed on the world.
#[inline(always)]
pub fn drop_erased_defer(&self, entity: impl Entity, ty: TypeId) {
let id = entity.id();
self.defer(move |world| {
let _ = world.drop_erased(id, ty);
})
}
/// Drops entity's components that are found in the specified bundle.
///
/// If entity is not alive, fails with `Err(NoSuchEntity)`.
///
/// Unlike other methods that use `Bundle` trait, this method does not require
/// all components from bundle to be registered in the world.
/// Entity can't have components that are not registered in the world,
/// so no need to drop them.
///
/// For this reason there's no separate method that uses `ComponentBundle` trait.
///
/// This is deferred version of [`World::drop_bundle`].
/// It can be used on shared `WorldLocal` reference.
/// Operation is queued and executed on next call to [`World::run_deferred`]
/// or when mutable operation is performed on the world.
///
/// # Example
///
/// ```
/// # use edict::{world::WorldLocal, ExampleComponent};
///
/// struct OtherComponent;
///
/// let mut world = WorldLocal::new();
/// let mut entity = world.spawn((ExampleComponent,)).id();
///
/// assert!(world.try_has_component::<ExampleComponent>(entity).unwrap());
///
/// world.drop_bundle_defer::<(ExampleComponent, OtherComponent)>(entity);
///
/// assert!(world.try_has_component::<ExampleComponent>(entity).unwrap());
///
/// world.run_deferred();
///
/// assert!(!world.try_has_component::<ExampleComponent>(entity).unwrap());
/// ```
#[inline(always)]
pub fn drop_bundle_defer<B>(&self, entity: impl Entity)
where
B: Bundle,
{
let id = entity.id();
self.defer(move |world| {
let _ = world.drop_bundle::<B>(id);
});
}
}