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
use super::Component;
use super::ComponentStorage;
use super::EntityHandle;
use crate::systems;
use systems::World;
type OnEntitiesFreeCb = fn(&World, &[EntityHandle]);
pub struct RegisteredComponent {
on_entities_free_cb: OnEntitiesFreeCb,
}
impl RegisteredComponent {
fn new(on_entities_free_cb: OnEntitiesFreeCb) -> Self {
RegisteredComponent {
on_entities_free_cb,
}
}
fn on_entities_free(&self, world: &World, entity_handles: &[EntityHandle]) {
(self.on_entities_free_cb)(world, entity_handles);
}
}
pub struct ComponentRegistry {
registered_components: Vec<RegisteredComponent>,
}
impl ComponentRegistry {
pub fn new() -> Self {
ComponentRegistry {
registered_components: vec![],
}
}
pub fn register_component<T: Component + 'static>(&mut self) {
let callback = |world: &World, entity_handles: &[EntityHandle]| {
let mut storage = world.fetch_mut::<T::Storage>();
for entity_handle in entity_handles {
storage.free_if_exists(entity_handle);
}
};
self.registered_components
.push(RegisteredComponent::new(callback));
}
pub fn on_entities_free(&self, world: &World, entity_handles: &[EntityHandle]) {
println!("on_entities_free {:?}", entity_handles);
for rc in &self.registered_components {
rc.on_entities_free(world, entity_handles);
}
}
}