-
Notifications
You must be signed in to change notification settings - Fork 221
/
cluster_bomb.rs
199 lines (175 loc) · 5.87 KB
/
cluster_bomb.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
198
199
use rand::prelude::*;
#[cfg(feature = "parallel")]
use rayon::iter::ParallelIterator;
use specs::{prelude::*, storage::HashMapStorage, WorldExt};
const TAU: f32 = 2. * std::f32::consts::PI;
#[derive(Debug)]
struct ClusterBomb {
fuse: usize,
}
impl Component for ClusterBomb {
// This uses `HashMapStorage`, because only some entities are cluster bombs.
type Storage = HashMapStorage<Self>;
}
#[derive(Debug)]
struct Shrapnel {
durability: usize,
}
impl Component for Shrapnel {
// This uses `HashMapStorage`, because only some entities are shrapnels.
type Storage = HashMapStorage<Self>;
}
#[derive(Debug, Clone)]
struct Pos(f32, f32);
impl Component for Pos {
// This uses `VecStorage`, because all entities have a position.
type Storage = VecStorage<Self>;
}
#[derive(Debug)]
struct Vel(f32, f32);
impl Component for Vel {
// This uses `DenseVecStorage`, because nearly all entities have a velocity.
type Storage = DenseVecStorage<Self>;
}
struct ClusterBombSystem;
impl<'a> System<'a> for ClusterBombSystem {
type SystemData = (
Entities<'a>,
WriteStorage<'a, ClusterBomb>,
ReadStorage<'a, Pos>,
// Allows lazily adding and removing components to entities
// or executing arbitrary code with world access lazily via `execute`.
Read<'a, LazyUpdate>,
);
fn run(&mut self, (entities, mut bombs, positions, updater): Self::SystemData) {
use rand::distributions::Uniform;
let durability_range = Uniform::new(10, 20);
let update_position = |(entity, bomb, position): (Entity, &mut ClusterBomb, &Pos)| {
let mut rng = rand::thread_rng();
if bomb.fuse == 0 {
let _ = entities.delete(entity);
for _ in 0..9 {
let shrapnel = entities.create();
updater.insert(
shrapnel,
Shrapnel {
durability: durability_range.sample(&mut rng),
},
);
updater.insert(shrapnel, position.clone());
let angle: f32 = rng.gen::<f32>() * TAU;
updater.insert(shrapnel, Vel(angle.sin(), angle.cos()));
}
} else {
bomb.fuse -= 1;
}
};
// Join components in potentially parallel way using rayon.
#[cfg(not(feature = "parallel"))]
{
(&entities, &mut bombs, &positions)
.join()
.for_each(update_position);
}
#[cfg(feature = "parallel")]
{
(&entities, &mut bombs, &positions)
.par_join()
.for_each(update_position);
}
}
}
struct PhysicsSystem;
impl<'a> System<'a> for PhysicsSystem {
type SystemData = (WriteStorage<'a, Pos>, ReadStorage<'a, Vel>);
fn run(&mut self, (mut pos, vel): Self::SystemData) {
#[cfg(not(feature = "parallel"))]
(&mut pos, &vel).join().for_each(|(pos, vel)| {
pos.0 += vel.0;
pos.1 += vel.1;
});
#[cfg(feature = "parallel")]
(&mut pos, &vel).par_join().for_each(|(pos, vel)| {
pos.0 += vel.0;
pos.1 += vel.1;
});
}
}
struct ShrapnelSystem;
impl<'a> System<'a> for ShrapnelSystem {
type SystemData = (Entities<'a>, WriteStorage<'a, Shrapnel>);
fn run(&mut self, (entities, mut shrapnels): Self::SystemData) {
#[cfg(not(feature = "parallel"))]
(&entities, &mut shrapnels)
.join()
.for_each(|(entity, shrapnel)| {
if shrapnel.durability == 0 {
let _ = entities.delete(entity);
} else {
shrapnel.durability -= 1;
}
});
#[cfg(feature = "parallel")]
(&entities, &mut shrapnels)
.par_join()
.for_each(|(entity, shrapnel)| {
if shrapnel.durability == 0 {
let _ = entities.delete(entity);
} else {
shrapnel.durability -= 1;
}
});
}
}
fn main() {
let mut world = World::new();
let mut dispatcher = DispatcherBuilder::new()
.with(PhysicsSystem, "physics", &[])
.with(ClusterBombSystem, "cluster_bombs", &[])
.with(ShrapnelSystem, "shrapnels", &[])
.build();
dispatcher.setup(&mut world);
world
.create_entity()
.with(Pos(0., 0.))
.with(ClusterBomb { fuse: 3 })
.build();
let mut step = 0;
loop {
step += 1;
let mut entities = 0;
{
// Simple console rendering
let positions = world.read_storage::<Pos>();
const WIDTH: usize = 10;
const HEIGHT: usize = 10;
const SCALE: f32 = 1. / 4.;
let mut screen = [[0; WIDTH]; HEIGHT];
for entity in world.entities().join() {
if let Some(pos) = positions.get(entity) {
let x = (pos.0 * SCALE + WIDTH as f32 / 2.).floor() as usize;
let y = (pos.1 * SCALE + HEIGHT as f32 / 2.).floor() as usize;
if x < WIDTH && y < HEIGHT {
screen[x][y] += 1;
}
}
entities += 1;
}
println!("Step: {}, Entities: {}", step, entities);
for row in &screen {
for cell in row {
print!("{}", cell);
}
println!();
}
println!();
}
if entities == 0 {
break;
}
dispatcher.dispatch(&world);
// Maintain dynamically added and removed entities in dispatch.
// This is what actually executes changes done by `LazyUpdate`.
world.maintain();
}
}