diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 878df8f026..428b0e359f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -274,13 +274,13 @@ jobs: toolchain: stable - name: Build bevy run: | - cargo build --no-default-features --features "bevy_dynamic_plugin,bevy_gilrs,bevy_gltf,bevy_winit,render,png,hdr,x11,bevy_ci_testing,trace,trace_chrome" + cargo build --no-default-features --features "bevy_dynamic_plugin,bevy_gilrs,bevy_gltf,bevy_winit,render,png,hdr,x11,bevy_ci_testing,trace,trace_chrome,bevy_audio,vorbis" - name: Run examples run: | for example in .github/example-run/*.ron; do example_name=`basename $example .ron` echo "running $example_name - "`date` - time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features "bevy_dynamic_plugin,bevy_gilrs,bevy_gltf,bevy_winit,render,png,hdr,x11,bevy_ci_testing,trace,trace_chrome" + time CI_TESTING_CONFIG=$example xvfb-run cargo run --example $example_name --no-default-features --features "bevy_dynamic_plugin,bevy_gilrs,bevy_gltf,bevy_winit,render,png,hdr,x11,bevy_ci_testing,trace,trace_chrome,bevy_audio,vorbis" sleep 10 done zip traces.zip trace*.json diff --git a/assets/sounds/breakout_collision.ogg b/assets/sounds/breakout_collision.ogg new file mode 100644 index 0000000000..0211d70cfb Binary files /dev/null and b/assets/sounds/breakout_collision.ogg differ diff --git a/examples/games/breakout.rs b/examples/games/breakout.rs index d00e5200df..d2ee2e2f71 100644 --- a/examples/games/breakout.rs +++ b/examples/games/breakout.rs @@ -58,12 +58,14 @@ fn main() { .insert_resource(Scoreboard { score: 0 }) .insert_resource(ClearColor(BACKGROUND_COLOR)) .add_startup_system(setup) + .add_event::() .add_system_set( SystemSet::new() .with_run_criteria(FixedTimestep::step(TIME_STEP as f64)) .with_system(check_for_collisions) .with_system(move_paddle.before(check_for_collisions)) - .with_system(apply_velocity.before(check_for_collisions)), + .with_system(apply_velocity.before(check_for_collisions)) + .with_system(play_collision_sound.after(check_for_collisions)), ) .add_system(update_scoreboard) .add_system(bevy::input::system::exit_on_esc_system) @@ -82,9 +84,14 @@ struct Velocity(Vec2); #[derive(Component)] struct Collider; +#[derive(Default)] +struct CollisionEvent; + #[derive(Component)] struct Brick; +struct CollisionSound(Handle); + // This bundle is a collection of the components that define a "wall" in our game #[derive(Bundle)] struct WallBundle { @@ -167,6 +174,10 @@ fn setup(mut commands: Commands, asset_server: Res) { commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); + // Sound + let ball_collision_sound = asset_server.load("sounds/breakout_collision.ogg"); + commands.insert_resource(CollisionSound(ball_collision_sound)); + // Paddle let paddle_y = BOTTOM_WALL + GAP_BETWEEN_PADDLE_AND_FLOOR; @@ -268,7 +279,7 @@ fn setup(mut commands: Commands, asset_server: Res) { // the space on the top and sides of the bricks only captures a lower bound, not an exact value let center_of_bricks = (LEFT_WALL + RIGHT_WALL) / 2.0; let left_edge_of_bricks = center_of_bricks - // Space taken up by the bricks + // Space taken up by the bricks - (n_columns as f32 / 2.0 * BRICK_SIZE.x) // Space taken up by the gaps - n_vertical_gaps as f32 / 2.0 * GAP_BETWEEN_BRICKS; @@ -349,6 +360,7 @@ fn check_for_collisions( mut scoreboard: ResMut, mut ball_query: Query<(&mut Velocity, &Transform), With>, collider_query: Query<(Entity, &Transform, Option<&Brick>), With>, + mut collision_events: EventWriter, ) { let (mut ball_velocity, ball_transform) = ball_query.single_mut(); let ball_size = ball_transform.scale.truncate(); @@ -362,6 +374,9 @@ fn check_for_collisions( transform.scale.truncate(), ); if let Some(collision) = collision { + // Sends a collision event so that other systems can react to the collision + collision_events.send_default(); + // Bricks should be despawned and increment the scoreboard on collision if maybe_brick.is_some() { scoreboard.score += 1; @@ -394,3 +409,15 @@ fn check_for_collisions( } } } + +fn play_collision_sound( + mut collision_events: EventReader, + audio: Res