forked from amethyst/specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.rs
109 lines (88 loc) · 2.91 KB
/
track.rs
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
extern crate hibitset;
extern crate shrev;
extern crate specs;
use specs::prelude::*;
struct TrackedComponent(u64);
impl Component for TrackedComponent {
type Storage = FlaggedStorage<Self>;
}
#[derive(Default)]
struct SysA {
reader_id: Option<ReaderId<ComponentEvent>>,
inserted: BitSet,
modified: BitSet,
removed: BitSet,
}
impl<'a> System<'a> for SysA {
type SystemData = (Entities<'a>, ReadStorage<'a, TrackedComponent>);
fn setup(&mut self, res: &mut World) {
Self::SystemData::setup(res);
self.reader_id = Some(WriteStorage::<TrackedComponent>::fetch(&res).register_reader());
}
fn run(&mut self, (entities, tracked): Self::SystemData) {
self.modified.clear();
self.inserted.clear();
self.removed.clear();
let events = tracked
.channel()
.read(self.reader_id.as_mut().expect("ReaderId not found"));
for event in events {
match event {
ComponentEvent::Modified(id) => {
self.modified.add(*id);
}
ComponentEvent::Inserted(id) => {
self.inserted.add(*id);
}
ComponentEvent::Removed(id) => {
self.removed.add(*id);
}
}
}
for (entity, _tracked, _) in (&entities, &tracked, &self.modified).join() {
println!("modified: {:?}", entity);
}
for (entity, _tracked, _) in (&entities, &tracked, &self.inserted).join() {
println!("inserted: {:?}", entity);
}
for (entity, _tracked, _) in (&entities, &tracked, &self.removed).join() {
println!("removed: {:?}", entity);
}
}
}
#[derive(Default)]
struct SysB;
impl<'a> System<'a> for SysB {
type SystemData = (Entities<'a>, WriteStorage<'a, TrackedComponent>);
fn run(&mut self, (entities, mut tracked): Self::SystemData) {
for (entity, mut restricted) in (&entities, &mut tracked.restrict_mut()).join() {
if entity.id() % 2 == 0 {
let mut comp = restricted.get_mut_unchecked();
comp.0 += 1;
}
}
}
}
fn main() {
let mut world = World::new();
let mut dispatcher = DispatcherBuilder::new()
.with(SysA::default(), "sys_a", &[])
.with(SysB::default(), "sys_b", &[])
.build();
dispatcher.setup(&mut world);
for _ in 0..50 {
world.create_entity().with(TrackedComponent(0)).build();
}
dispatcher.dispatch(&mut world);
world.maintain();
let entities = (&world.entities(), &world.read_storage::<TrackedComponent>())
.join()
.map(|(e, _)| e)
.collect::<Vec<Entity>>();
world.delete_entities(&entities).unwrap();
for _ in 0..50 {
world.create_entity().with(TrackedComponent(0)).build();
}
dispatcher.dispatch(&mut world);
world.maintain();
}