Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions examples/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn control_character(
Option<&Shooting>,
)>,
my_animations: Res<MyAnimations>,
mut messages: MessageReader<AnimationEvent>,
mut messages: MessageReader<AnimationRepetitionEnd>,
) {
// Control the character with the keyboard

Expand Down Expand Up @@ -156,12 +156,11 @@ fn control_character(
// We use animation events to detect when this happens.
// Check out the `events` examples for more details.

for event in messages.read() {
if let AnimationEvent::AnimationRepetitionEnd {
entity, animation, ..
} = event
&& animation == &my_animations.shoot
{
for AnimationRepetitionEnd {
entity, animation, ..
} in messages.read()
{
if animation == &my_animations.shoot {
commands.entity(*entity).remove::<Shooting>();
}
}
Expand Down
78 changes: 38 additions & 40 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,31 @@ enum EventType {
}

fn show_triggered_events(
mut messages: MessageReader<AnimationEvent>,
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 event in messages.read() {
match event {
AnimationEvent::MarkerHit { .. } => {
triggered_events.insert(EventType::MarkerHit);
}
AnimationEvent::ClipRepetitionEnd { .. } => {
triggered_events.insert(EventType::ClipRepetitionEnd);
}
AnimationEvent::ClipEnd { .. } => {
triggered_events.insert(EventType::ClipEnd);
}
AnimationEvent::AnimationRepetitionEnd { .. } => {
triggered_events.insert(EventType::RepetitionEnd);
}
AnimationEvent::AnimationEnd { .. } => {
triggered_events.insert(EventType::End);
}
}
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
Expand All @@ -219,29 +219,27 @@ fn spawn_visual_effects(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut messages: MessageReader<AnimationEvent>,
mut messages: MessageReader<MarkerHit>,
my_markers: Res<MyMarkers>,
) {
for event in messages.read() {
if let AnimationEvent::MarkerHit { marker, .. } = event {
// 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,
));
}
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,
));
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions examples/headless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,26 @@ fn spawn_animation(mut commands: Commands, mut animations: ResMut<Assets<Animati
commands.spawn(SpritesheetAnimation::new(animation_handle));
}

fn log_animations_events(mut events: MessageReader<AnimationEvent>) {
for event in events.read() {
println!("{:?}", event);
fn log_animations_events(
mut marker_hit_reader: MessageReader<MarkerHit>,
mut clip_repitition_end_reader: MessageReader<ClipRepetitionEnd>,
mut clip_end_reader: MessageReader<ClipEnd>,
mut animation_repitition_end_reader: MessageReader<AnimationRepetitionEnd>,
mut animation_end_reader: MessageReader<AnimationEnd>,
) {
for marker_hit in marker_hit_reader.read() {
println!("{:?}", marker_hit);
}
for clip_repitition_end in clip_repitition_end_reader.read() {
println!("{:?}", clip_repitition_end);
}
for clip_end in clip_end_reader.read() {
println!("{:?}", clip_end);
}
for animation_repitition_end in animation_repitition_end_reader.read() {
println!("{:?}", animation_repitition_end);
}
for animation_end in animation_end_reader.read() {
println!("{:?}", animation_end);
}
}
Loading