-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathchange.rs
More file actions
54 lines (46 loc) · 1.52 KB
/
change.rs
File metadata and controls
54 lines (46 loc) · 1.52 KB
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
use std::{any::TypeId, collections::VecDeque};
use ahash::AHashMap;
use hecs::{Component, DynamicBundle, Entity};
/// Tracks changes made to certain components.
#[derive(Default)]
pub struct ChangeTracker {
registries: AHashMap<TypeId, ChangeRegistry>,
}
impl ChangeTracker {
pub fn track_component<T: Component>(&mut self) {
self.registries
.insert(TypeId::of::<T>(), ChangeRegistry::default());
}
pub fn on_insert(&mut self, entity: Entity, components: &impl DynamicBundle) {
components.with_ids(|typs| {
for ty in typs {
let registry = self.registries.get_mut(ty);
if let Some(registry) = registry {
registry.mark_changed(entity);
}
}
});
}
pub fn iter_changed<T: Component>(&self) -> impl Iterator<Item = Entity> + '_ {
self.registries.get(&TypeId::of::<T>())
.unwrap_or_else(|| panic!("Components of type {} are not tracked for changes. Call `Ecs::track_component` to enable change tracking.", std::any::type_name::<T>()))
.changed_entities
.iter()
.copied()
}
}
#[derive(Default)]
struct ChangeRegistry {
changed_entities: VecDeque<Entity>,
}
impl ChangeRegistry {
pub fn mark_changed(&mut self, entity: Entity) {
if !self.changed_entities.contains(&entity) {
self.changed_entities.push_back(entity);
}
}
#[allow(unused)]
pub fn pop(&mut self) {
self.changed_entities.pop_front();
}
}