Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/ecs/observers.rs
6592 views
1
//! Demonstrates how to observe life-cycle triggers as well as define custom ones.
2
3
use bevy::{
4
platform::collections::{HashMap, HashSet},
5
prelude::*,
6
};
7
use rand::{Rng, SeedableRng};
8
use rand_chacha::ChaCha8Rng;
9
10
fn main() {
11
App::new()
12
.add_plugins(DefaultPlugins)
13
.init_resource::<SpatialIndex>()
14
.add_systems(Startup, setup)
15
.add_systems(Update, (draw_shapes, handle_click))
16
// Observers are systems that run when an event is "triggered". This observer runs whenever
17
// `ExplodeMines` is triggered.
18
.add_observer(
19
|event: On<ExplodeMines>,
20
mines: Query<&Mine>,
21
index: Res<SpatialIndex>,
22
mut commands: Commands| {
23
// Access resources
24
for e in index.get_nearby(event.pos) {
25
// Run queries
26
let mine = mines.get(e).unwrap();
27
if mine.pos.distance(event.pos) < mine.size + event.radius {
28
// And queue commands, including triggering additional events
29
// Here we trigger the `Explode` event for entity `e`
30
commands.trigger_targets(Explode, e);
31
}
32
}
33
},
34
)
35
// This observer runs whenever the `Mine` component is added to an entity, and places it in a simple spatial index.
36
.add_observer(on_add_mine)
37
// This observer runs whenever the `Mine` component is removed from an entity (including despawning it)
38
// and removes it from the spatial index.
39
.add_observer(on_remove_mine)
40
.run();
41
}
42
43
#[derive(Component)]
44
struct Mine {
45
pos: Vec2,
46
size: f32,
47
}
48
49
impl Mine {
50
fn random(rand: &mut ChaCha8Rng) -> Self {
51
Mine {
52
pos: Vec2::new(
53
(rand.random::<f32>() - 0.5) * 1200.0,
54
(rand.random::<f32>() - 0.5) * 600.0,
55
),
56
size: 4.0 + rand.random::<f32>() * 16.0,
57
}
58
}
59
}
60
61
#[derive(Event)]
62
struct ExplodeMines {
63
pos: Vec2,
64
radius: f32,
65
}
66
67
#[derive(EntityEvent)]
68
struct Explode;
69
70
fn setup(mut commands: Commands) {
71
commands.spawn(Camera2d);
72
commands.spawn((
73
Text::new(
74
"Click on a \"Mine\" to trigger it.\n\
75
When it explodes it will trigger all overlapping mines.",
76
),
77
Node {
78
position_type: PositionType::Absolute,
79
top: px(12),
80
left: px(12),
81
..default()
82
},
83
));
84
85
let mut rng = ChaCha8Rng::seed_from_u64(19878367467713);
86
87
commands
88
.spawn(Mine::random(&mut rng))
89
// Observers can watch for events targeting a specific entity.
90
// This will create a new observer that runs whenever the Explode event
91
// is triggered for this spawned entity.
92
.observe(explode_mine);
93
94
// We want to spawn a bunch of mines. We could just call the code above for each of them.
95
// That would create a new observer instance for every Mine entity. Having duplicate observers
96
// generally isn't worth worrying about as the overhead is low. But if you want to be maximally efficient,
97
// you can reuse observers across entities.
98
//
99
// First, observers are actually just entities with the Observer component! The `observe()` functions
100
// you've seen so far in this example are just shorthand for manually spawning an observer.
101
let mut observer = Observer::new(explode_mine);
102
103
// As we spawn entities, we can make this observer watch each of them:
104
for _ in 0..1000 {
105
let entity = commands.spawn(Mine::random(&mut rng)).id();
106
observer.watch_entity(entity);
107
}
108
109
// By spawning the Observer component, it becomes active!
110
commands.spawn(observer);
111
}
112
113
fn on_add_mine(event: On<Add, Mine>, query: Query<&Mine>, mut index: ResMut<SpatialIndex>) {
114
let mine = query.get(event.entity()).unwrap();
115
let tile = (
116
(mine.pos.x / CELL_SIZE).floor() as i32,
117
(mine.pos.y / CELL_SIZE).floor() as i32,
118
);
119
index.map.entry(tile).or_default().insert(event.entity());
120
}
121
122
// Remove despawned mines from our index
123
fn on_remove_mine(event: On<Remove, Mine>, query: Query<&Mine>, mut index: ResMut<SpatialIndex>) {
124
let mine = query.get(event.entity()).unwrap();
125
let tile = (
126
(mine.pos.x / CELL_SIZE).floor() as i32,
127
(mine.pos.y / CELL_SIZE).floor() as i32,
128
);
129
index.map.entry(tile).and_modify(|set| {
130
set.remove(&event.entity());
131
});
132
}
133
134
fn explode_mine(event: On<Explode>, query: Query<&Mine>, mut commands: Commands) {
135
// If a triggered event is targeting a specific entity you can access it with `.entity()`
136
let id = event.entity();
137
let Ok(mut entity) = commands.get_entity(id) else {
138
return;
139
};
140
info!("Boom! {} exploded.", id.index());
141
entity.despawn();
142
let mine = query.get(id).unwrap();
143
// Trigger another explosion cascade.
144
commands.trigger(ExplodeMines {
145
pos: mine.pos,
146
radius: mine.size,
147
});
148
}
149
150
// Draw a circle for each mine using `Gizmos`
151
fn draw_shapes(mut gizmos: Gizmos, mines: Query<&Mine>) {
152
for mine in &mines {
153
gizmos.circle_2d(
154
mine.pos,
155
mine.size,
156
Color::hsl((mine.size - 4.0) / 16.0 * 360.0, 1.0, 0.8),
157
);
158
}
159
}
160
161
// Trigger `ExplodeMines` at the position of a given click
162
fn handle_click(
163
mouse_button_input: Res<ButtonInput<MouseButton>>,
164
camera: Single<(&Camera, &GlobalTransform)>,
165
windows: Query<&Window>,
166
mut commands: Commands,
167
) {
168
let Ok(windows) = windows.single() else {
169
return;
170
};
171
172
let (camera, camera_transform) = *camera;
173
if let Some(pos) = windows
174
.cursor_position()
175
.and_then(|cursor| camera.viewport_to_world(camera_transform, cursor).ok())
176
.map(|ray| ray.origin.truncate())
177
&& mouse_button_input.just_pressed(MouseButton::Left)
178
{
179
commands.trigger(ExplodeMines { pos, radius: 1.0 });
180
}
181
}
182
183
#[derive(Resource, Default)]
184
struct SpatialIndex {
185
map: HashMap<(i32, i32), HashSet<Entity>>,
186
}
187
188
/// Cell size has to be bigger than any `TriggerMine::radius`
189
const CELL_SIZE: f32 = 64.0;
190
191
impl SpatialIndex {
192
// Lookup all entities within adjacent cells of our spatial index
193
fn get_nearby(&self, pos: Vec2) -> Vec<Entity> {
194
let tile = (
195
(pos.x / CELL_SIZE).floor() as i32,
196
(pos.y / CELL_SIZE).floor() as i32,
197
);
198
let mut nearby = Vec::new();
199
for x in -1..2 {
200
for y in -1..2 {
201
if let Some(mines) = self.map.get(&(tile.0 + x, tile.1 + y)) {
202
nearby.extend(mines.iter());
203
}
204
}
205
}
206
nearby
207
}
208
}
209
210