-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathevents.rs
More file actions
334 lines (279 loc) · 9.88 KB
/
Copy pathevents.rs
File metadata and controls
334 lines (279 loc) · 9.88 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// This example shows how to use events to be notified when animations reach points of interest.
//
// - We'll create a few animations for our character (run, shoot) in a setup system
//
// - We'll add markers on specific frames of our animations:
// - when a character's foot touches the ground
// - when the character shoots their gun
//
// - We'll setup a UI that shows all the animation events that exist.
// Events received at each update will be highlighted.
//
// - We'll spawn special effects when a marker is hit:
// - A bullet when the character shoots their gun
// - A shockwave when their feet hit the ground
use std::collections::HashSet;
use bevy::{color, prelude::*};
use bevy_spritesheet_animation::prelude::*;
fn main() {
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
))
.insert_resource(TimeScale(1.0))
.add_systems(Startup, (spawn_character, create_ui))
.add_systems(
Update,
(
show_triggered_events,
fade_triggered_events,
spawn_visual_effects,
animate_bullets,
animate_footsteps,
update_on_keypress,
),
)
.run();
}
// Let's use a custom resource to store our markers and access them across systems
#[derive(Resource)]
struct MyMarkers {
bullet_out: Marker,
foot_touches_ground: Marker,
}
fn spawn_character(
mut commands: Commands,
assets: Res<AssetServer>,
mut animations: ResMut<Assets<Animation>>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
commands.spawn(Camera2d);
// Create the animation
let image = assets.load("character.png");
let spritesheet = Spritesheet::new(&image, 8, 8);
let foot_touches_ground_marker = Marker::new();
let bullet_out_marker = Marker::new();
let animation = spritesheet
.create_animation()
.set_repetitions(AnimationRepeat::Times(3))
// Clip 1: run
//
// The character's foot touches the ground on frame 1 and then again on frame 5
.add_row(3)
.set_clip_repetitions(4)
.add_clip_marker(foot_touches_ground_marker, 1)
.add_clip_marker(foot_touches_ground_marker, 5)
// Clip 2: shoot
//
// The character shoots their gun on frame 1
.start_clip()
.add_horizontal_strip(0, 5, 5)
.add_clip_marker(bullet_out_marker, 1)
.build();
let animation_handle = animations.add(animation);
// Store the markers as a resource
commands.insert_resource(MyMarkers {
bullet_out: bullet_out_marker,
foot_touches_ground: foot_touches_ground_marker,
});
// Spawn the character
let sprite = spritesheet
.with_size_hint(768, 768)
.sprite(&mut atlas_layouts);
commands.spawn((sprite, SpritesheetAnimation::new(animation_handle)));
}
fn create_ui(mut commands: Commands) {
commands
// Full-screen container
.spawn(Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
..default()
})
.with_children(|parent| {
// Help text
parent
.spawn(Node {
margin: UiRect::all(Val::Px(10.0)),
..default()
})
.with_child((Text::new("SPACE: toggle slow motion\nR: reset animation"),));
// Events
let mut add_event = |event_type: EventType| {
parent
// Row
.spawn(Node {
margin: UiRect::all(Val::Px(10.0)),
align_items: AlignItems::Center,
..default()
})
.with_children(|parent| {
// Colored square
parent.spawn((
Node {
width: Val::Px(50.0),
height: Val::Px(50.0),
margin: UiRect::right(Val::Px(10.0)),
..default()
},
BorderColor::all(color::palettes::css::GRAY),
BackgroundColor(color::palettes::css::DEEP_PINK.with_alpha(0.0).into()),
event_type,
));
// Event name
parent.spawn((Text(format!("{event_type:?}")), Label));
});
};
add_event(EventType::MarkerHit);
add_event(EventType::ClipRepetitionEnd);
add_event(EventType::ClipEnd);
add_event(EventType::RepetitionEnd);
add_event(EventType::End);
});
}
// Component attached to a UI square to be highlighted when the given event type is received
#[derive(Debug, Component, Clone, Copy, PartialEq, Eq, Hash)]
enum EventType {
MarkerHit,
ClipRepetitionEnd,
ClipEnd,
RepetitionEnd,
End,
}
fn show_triggered_events(
mut marker_hit_messages: MessageReader<MarkerHit>,
mut clip_repitition_end_messages: MessageReader<ClipRepetitionEnd>,
mut clip_end_messages: MessageReader<ClipEnd>,
mut animation_repitition_end_messages: MessageReader<AnimationRepetitionEnd>,
mut animation_end_messages: MessageReader<AnimationEnd>,
mut squares: Query<(&mut BackgroundColor, &EventType)>,
) {
// Collect the events that were just received
let mut triggered_events: HashSet<EventType> = HashSet::new();
for _ in marker_hit_messages.read() {
triggered_events.insert(EventType::MarkerHit);
}
for _ in clip_repitition_end_messages.read() {
triggered_events.insert(EventType::ClipRepetitionEnd);
}
for _ in clip_end_messages.read() {
triggered_events.insert(EventType::ClipEnd);
}
for _ in animation_repitition_end_messages.read() {
triggered_events.insert(EventType::RepetitionEnd);
}
for _ in animation_end_messages.read() {
triggered_events.insert(EventType::End);
}
// Set full alpha for the squares which events were just received
for (mut color, event_type) in &mut squares {
if triggered_events.contains(event_type) {
color.0.set_alpha(1.0);
}
}
}
fn fade_triggered_events(time: Res<Time>, mut squares: Query<&mut BackgroundColor>) {
const FADE_SPEED: f32 = 3.0;
for mut color in &mut squares {
let new_alpha = color.0.alpha() - time.delta_secs() * FADE_SPEED;
color.0.set_alpha(new_alpha);
}
}
// Spawns footsteps & bullets when the marked frames are played
fn spawn_visual_effects(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut messages: MessageReader<MarkerHit>,
my_markers: Res<MyMarkers>,
) {
for MarkerHit { marker, .. } in messages.read() {
// Spawn a shockwave at each footstep
if marker == &my_markers.foot_touches_ground {
commands.spawn((
Mesh2d(meshes.add(Circle { radius: 1.0 })),
MeshMaterial2d(materials.add(ColorMaterial::default())),
Transform::from_xyz(0.0, -30.0, -1.0),
Footstep,
));
}
// Spawn a bullet when firing
else if marker == &my_markers.bullet_out {
commands.spawn((
Mesh2d(meshes.add(Circle { radius: 3.0 })),
MeshMaterial2d(materials.add(Color::from(color::palettes::css::YELLOW))),
Transform::from_xyz(50.0, 15.0, 0.0),
Bullet,
));
}
}
}
#[derive(Component)]
struct Bullet;
fn animate_bullets(
time: Res<Time>,
time_scale: Res<TimeScale>,
mut commands: Commands,
mut bullets: Query<(Entity, &mut Transform), With<Bullet>>,
) {
const MOVE_SPEED: f32 = 800.0;
for (entity, mut transform) in &mut bullets {
// Move horizontally
transform.translation.x += time.delta_secs() * time_scale.0 * MOVE_SPEED;
// Despawn when far away
if transform.translation.x > 5000.0 {
commands.entity(entity).despawn();
}
}
}
#[derive(Component)]
struct Footstep;
fn animate_footsteps(
time: Res<Time>,
time_scale: Res<TimeScale>,
mut commands: Commands,
mut materials: ResMut<Assets<ColorMaterial>>,
mut footsteps: Query<(Entity, &mut Transform, &MeshMaterial2d<ColorMaterial>), With<Footstep>>,
) {
const GROW_SPEED: f32 = 100.0;
const FADE_SPEED: f32 = 4.0;
for (entity, mut transform, material_handle) in &mut footsteps {
// Grow
transform.scale += time.delta_secs() * time_scale.0 * Vec3::splat(GROW_SPEED);
// Fade away
if let Some(material) = materials.get_mut(material_handle) {
material
.color
.set_alpha(material.color.alpha() - time.delta_secs() * time_scale.0 * FADE_SPEED);
// Despawn when transparent
if material.color.alpha() <= 0.0 {
commands.entity(entity).despawn();
}
}
}
}
#[derive(Resource)]
struct TimeScale(f32);
fn update_on_keypress(
keyboard: Res<ButtonInput<KeyCode>>,
mut time_scale: ResMut<TimeScale>,
mut sprite: Single<&mut SpritesheetAnimation>,
) {
// Space: toggle slomo
if keyboard.just_pressed(KeyCode::Space) {
// Update our global time scale (used by bullets and footsteps)
if time_scale.0 >= 1.0 {
time_scale.0 = 0.1;
} else {
time_scale.0 = 1.0;
}
// Update the sprite's playback speed
sprite.speed_factor = time_scale.0;
}
// R: reset the animation
if keyboard.just_pressed(KeyCode::KeyR) {
sprite.reset();
}
}