-
Notifications
You must be signed in to change notification settings - Fork 0
/
full.rs
197 lines (169 loc) · 5.39 KB
/
full.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
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
#![allow(dead_code)]
use ecs::prelude::*;
#[derive(Clone)] // `StructOf` implements Clone if possible
struct GameWorld {
units: StructOf<Vec<Unit>>, // UnitStructOf<VecFamily>,
corpses: StructOf<Vec<Corpse>>, // CorpseStructOf<VecFamily>,
particles: StructOf<Vec<Particle>>, // ParticleStructOf<VecFamily>,
}
#[derive(SplitFields, Debug, Clone)]
#[split(debug)] // derive `Debug` for the `UnitRef` generated struct
struct Unit {
// id: Id,
pos: (f32, f32),
health: f32,
tick: usize,
damage: Option<f32>,
}
#[derive(SplitFields, Debug)]
struct Corpse {
// Nest `Unit` to efficiently store the fields and to refer to them directly in the queries.
// But you can still access the whole `Unit` as a single component.
#[split(nested)]
unit: Unit,
time: f32,
}
#[derive(SplitFields, Debug)]
#[split(clone)] // implement clone method for the `ParticleRef` generated struct to clone the data into a `Particle`
struct Particle {
pos: (f32, f32),
time: f32,
}
fn main() {
println!("Hello, example!");
let mut world = GameWorld {
units: Default::default(),
corpses: Default::default(),
particles: Default::default(),
};
let player_id = world.units.insert(Unit {
pos: (0.0, 0.0),
health: 10.0,
tick: 7,
damage: None,
});
world.units.insert(Unit {
pos: (1.0, -2.0),
health: 15.0,
tick: 3,
damage: Some(1.5),
});
world.corpses.insert(Corpse {
unit: Unit {
pos: (-4.0, 3.0),
health: 0.0,
tick: 10,
damage: None,
},
time: 1.0,
});
for _ in 0..3 {
world.particles.insert(Particle {
pos: (1.0, -0.5),
time: 1.0,
});
}
// Iterate over all fields of all units
println!("Units:");
for unit in world.units.iter() {
println!("{unit:?}");
}
// Iterate over all fields of all particles
println!("\nParticles:");
for (_, particle) in world.particles.iter() {
let particle_cloned: Particle = particle.clone();
println!("{particle_cloned:?}");
}
// Query fields
{
println!("\nPosition with tick:");
// Declare a view struct to query into
#[derive(Debug)]
struct UnitRef<'a> {
pos: &'a (f32, f32),
tick: &'a usize,
}
println!("\nQuerying into a struct:");
for unit in query!(world.units, UnitRef { pos, tick }) {
println!("{:?}", unit);
}
// Or just query into a tuple
println!("\nQuerying into a tuple:");
for unit in query!(world.units, (&pos, &tick)) {
println!("{:?}", unit);
}
// Query a single entity
println!("\nSingle entity:");
if let Some(player) = get!(world.units, player_id, (&pos, &tick)) {
println!("{:?}", player);
}
}
// Query an optional field
{
println!("\nHealth with damage:");
for unit in query!(world.units, (&health, &damage.Get.Some)) {
// Now we get access to units which have health *and* damage
println!("{:?}", unit);
}
}
// Splitting mutable access to components
{
// Iterate mutably over all units' healths
println!("\nHealths:");
let ids = world.units.ids();
for &id in &ids {
// Sadly you cant `query!` mutably, so you have to manually iterate over id's and `get!` each entity
let (health,) = get!(world.units, id, (&mut health)).unwrap();
println!("Updating {health:?}");
// Iterate mutably over all units' ticks
println!(" Inner query over ticks:");
for &id in &ids {
let (tick,) = get!(world.units, id, (&mut tick)).unwrap();
println!(" Incrementing {tick:?}");
*tick += 1;
}
*health -= 5.0;
}
// Iterate over all units' healths again
println!("\nUpdated healths");
for id in ids {
let (health,) = get!(world.units, id, (&health)).unwrap();
println!("{:?}", health);
}
}
// Query from a nested storage
{
println!("\nTicks inside units inside corpses:");
// `tick` is located inside `unit`
for corpse in query!(world.corpses, (&unit.tick)) {
println!("{:?}", corpse);
}
}
// Query multiple entity types at the same time
{
// Declare a view struct to have the same access to both entities
#[derive(Debug)]
struct PosRef<'a> {
pos: &'a (f32, f32),
}
println!("\nPositions of units and corpses:");
let units = query!(world.units, PosRef { pos });
// And we can have different access patterns for each entity
// so we can access the position of the nested unit of the corpse
let particles = query!(world.corpses, PosRef { pos: &unit.pos });
for item in units.chain(particles) {
println!("{:?}", item);
}
}
// Query the whole nested storage
{
println!("\nNested units:");
for item in query!(world.corpses, (&unit)) {
println!("{:?}", item);
}
}
println!("\nTaking back ownership of all units:");
for unit in world.units.into_iter() {
println!("{unit:?}");
}
}