diff --git a/examples/game2d.rs b/examples/game2d.rs index 41ace00..c833c87 100644 --- a/examples/game2d.rs +++ b/examples/game2d.rs @@ -2,12 +2,12 @@ use games::*; fn main() { App::new() - .add_plugins(( - BaseGamePlugin { - name: "2d game example".into(), - game_type: GameType::Two, - }, - )).add_systems(Startup, spawn_guy).run(); + .add_plugins((BaseGamePlugin { + name: "2d game example".into(), + game_type: GameType::Two, + },)) + .add_systems(Startup, spawn_guy) + .run(); } fn spawn_guy( @@ -29,4 +29,3 @@ fn spawn_guy( commands.spawn((name, material, mesh, t)); } - diff --git a/src/base_game.rs b/src/base_game.rs index 6b084f8..e2cf794 100644 --- a/src/base_game.rs +++ b/src/base_game.rs @@ -22,14 +22,18 @@ impl Default for BaseGamePlugin { impl Plugin for BaseGamePlugin { fn build(&self, app: &mut App) { - app.add_plugins(DefaultPlugins.set(WindowPlugin { - primary_window: Some(Window { - fit_canvas_to_parent: true, - canvas: Some(format!("#{}-canvas", self.name)), - ..default() - }), - ..default() - }).set(ImagePlugin::default_nearest())) + app.add_plugins( + DefaultPlugins + .set(WindowPlugin { + primary_window: Some(Window { + fit_canvas_to_parent: true, + canvas: Some(format!("#{}-canvas", self.name)), + ..default() + }), + ..default() + }) + .set(ImagePlugin::default_nearest()), + ) .add_plugins(DebuggingPlugin) .add_plugins(MeshPickingPlugin) .add_plugins(LoadingPlugin) diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index 23d0d26..c7a3178 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -75,7 +75,7 @@ fn main() { sync_resource_to_ui::.run_if(resource_changed::), sync_resource_to_ui::.run_if(resource_changed::), ), - scoring.run_if(on_event::) + scoring.run_if(on_event::), ), ) .add_observer(flap) @@ -137,7 +137,16 @@ fn init_bird( let tape = Tape::default(); - commands.spawn((name, mesh, material, physics, t, Bird, tape, CollisionEventsEnabled)); + commands.spawn(( + name, + mesh, + material, + physics, + t, + Bird, + tape, + CollisionEventsEnabled, + )); } #[derive(Component, Clone)] @@ -187,14 +196,15 @@ fn init_obstacles( ) }; let hitbox = { - let physics = (RigidBody::Static, Collider::rectangle(1.0, 10.0), Sensor, CollisionEventsEnabled); + let physics = ( + RigidBody::Static, + Collider::rectangle(1.0, 10.0), + Sensor, + CollisionEventsEnabled, + ); let name = Name::new("hitbox"); - ( - name.clone(), - physics.clone(), - Hitbox, - ) + (name.clone(), physics.clone(), Hitbox) }; // TODO: Instead of spawning infinite floor/pipes, we should instead spawn enough for 1-3 a few @@ -203,16 +213,20 @@ fn init_obstacles( // overhead fairly static. (1..10).for_each(|i| { // TODO: Jitter up/down/close/far of pipes for challenge - let above = Transform::from_xyz(300.0 * i as f32, 300.0, -1.0).with_scale(Vec3::splat(100.0)); + let above = + Transform::from_xyz(300.0 * i as f32, 300.0, -1.0).with_scale(Vec3::splat(100.0)); commands.spawn((pipe.clone(), above)); - let below = Transform::from_xyz(300.0 * i as f32, -200.0, -1.0).with_scale(Vec3::splat(100.0)); + let below = + Transform::from_xyz(300.0 * i as f32, -200.0, -1.0).with_scale(Vec3::splat(100.0)); commands.spawn((pipe.clone(), below)); - let hitbox_pos = Transform::from_xyz(300.0 * i as f32, 0.0, 10.0).with_scale(Vec3::splat(100.0)); + let hitbox_pos = + Transform::from_xyz(300.0 * i as f32, 0.0, 10.0).with_scale(Vec3::splat(100.0)); commands.spawn((hitbox_pos, hitbox.clone())); - let floor = Transform::from_xyz(300.0 * i as f32, -300.0, 1.0).with_scale(Vec3::splat(100.0)); + let floor = + Transform::from_xyz(300.0 * i as f32, -300.0, 1.0).with_scale(Vec3::splat(100.0)); commands.spawn((ground.clone(), floor)); }); } @@ -227,12 +241,34 @@ fn init_ui(mut commands: Commands) { }, PlayerState::Stasis, children![ - (Text::new("You Died"), TextLayout::new_with_justify(JustifyText::Center)), - (SyncResource::::default(), Text::default(), TextLayout::new_with_justify(JustifyText::Center)), - (SyncResource::::default(), Text::default(), TextLayout::new_with_justify(JustifyText::Center)), - (SyncResource::::default(), Text::default(), TextLayout::new_with_justify(JustifyText::Center)), - (SyncResource::::default(), Text::default(), TextLayout::new_with_justify(JustifyText::Center)), - (Text::new("Press R to Rewind"), TextLayout::new_with_justify(JustifyText::Center)), + ( + Text::new("You Died"), + TextLayout::new_with_justify(JustifyText::Center) + ), + ( + SyncResource::::default(), + Text::default(), + TextLayout::new_with_justify(JustifyText::Center) + ), + ( + SyncResource::::default(), + Text::default(), + TextLayout::new_with_justify(JustifyText::Center) + ), + ( + SyncResource::::default(), + Text::default(), + TextLayout::new_with_justify(JustifyText::Center) + ), + ( + SyncResource::::default(), + Text::default(), + TextLayout::new_with_justify(JustifyText::Center) + ), + ( + Text::new("Press R to Rewind"), + TextLayout::new_with_justify(JustifyText::Center) + ), ], )); @@ -255,35 +291,27 @@ fn init_ui(mut commands: Commands) { )) .observe(start_game); - commands.spawn( - Node { + commands + .spawn(Node { align_self: AlignSelf::End, justify_self: JustifySelf::Center, flex_direction: FlexDirection::Row, ..default() - }, - ).with_children(|parent| { - parent - .spawn(( - Node { - ..default() - }, - Button, - children![Text::new("Rewind!"),], - )) - .observe(start_rewind) - .observe(end_rewind); - - parent - .spawn(( - Node { - ..default() - }, - Button, - children![Text::new("Flap!"),], - )) - .observe(flap_button); - }); + }) + .with_children(|parent| { + parent + .spawn(( + Node { ..default() }, + Button, + children![Text::new("Rewind!"),], + )) + .observe(start_rewind) + .observe(end_rewind); + + parent + .spawn((Node { ..default() }, Button, children![Text::new("Flap!"),])) + .observe(flap_button); + }); commands.spawn(( Node { @@ -294,7 +322,7 @@ fn init_ui(mut commands: Commands) { BackgroundColor(WHITE.into()), SyncResource::::default(), Text::default(), - TextLayout::new_with_justify(JustifyText::Center) + TextLayout::new_with_justify(JustifyText::Center), )); } @@ -306,7 +334,11 @@ fn end_rewind(_trigger: Trigger>, mut next: ResMut>, mut commands: Commands, bird: Single>) { +fn flap_button( + trigger: Trigger>, + mut commands: Commands, + bird: Single>, +) { let e = *bird; info!("Flapping {:?}", e); commands.trigger_targets(Flap, e); @@ -328,10 +360,11 @@ fn un_pause_game(mut next: ResMut>) { struct Flap; // Observer for flapping -fn flap(trigger: Trigger, +fn flap( + trigger: Trigger, mut bird: Query<&mut ExternalImpulse, With>, mut flaps: ResMut, - ) { +) { info!("real flap for {:?}", trigger.target()); // Increment flap stat flaps.0 += 1; @@ -387,17 +420,27 @@ fn record( "Only record in the alive state" ); - birds.iter_mut().for_each(|(transform, linear_velocity, angular_velocity, mut tape)| { - tape.translations.push(transform.translation); - tape.rotations.push(transform.rotation); - tape.linear_velocities.push(*linear_velocity); - tape.angular_velocities.push(*angular_velocity); - }); + birds + .iter_mut() + .for_each(|(transform, linear_velocity, angular_velocity, mut tape)| { + tape.translations.push(transform.translation); + tape.rotations.push(transform.rotation); + tape.linear_velocities.push(*linear_velocity); + tape.angular_velocities.push(*angular_velocity); + }); } fn rewind( #[cfg(debug_assertions)] state: Res>, - mut birds: Query<(&mut Transform, &mut AngularVelocity, &mut LinearVelocity, &mut Tape), With>, + mut birds: Query< + ( + &mut Transform, + &mut AngularVelocity, + &mut LinearVelocity, + &mut Tape, + ), + With, + >, mut frames: ResMut, ) { debug_assert!( @@ -405,24 +448,26 @@ fn rewind( "Only rewind in the rewinding state" ); - birds.iter_mut().for_each(|(mut transform, mut angular_velocity, mut linear_velocity, mut tape)| { - match (tape.translations.pop(), tape.rotations.pop()) { - (Some(t), Some(r)) => { - transform.translation = t; - transform.rotation = r; - frames.0 += 1; - }, - (None, None) => (), - _ => panic!("Translations and rotations are out of sync!"), - } - // TODO: Only need to set {angular|linear}_velocity at end of Rewind - if let Some(av) = tape.angular_velocities.pop() { - *angular_velocity = av; - } - if let Some(lv) = tape.linear_velocities.pop() { - *linear_velocity = lv; - } - }); + birds.iter_mut().for_each( + |(mut transform, mut angular_velocity, mut linear_velocity, mut tape)| { + match (tape.translations.pop(), tape.rotations.pop()) { + (Some(t), Some(r)) => { + transform.translation = t; + transform.rotation = r; + frames.0 += 1; + } + (None, None) => (), + _ => panic!("Translations and rotations are out of sync!"), + } + // TODO: Only need to set {angular|linear}_velocity at end of Rewind + if let Some(av) = tape.angular_velocities.pop() { + *angular_velocity = av; + } + if let Some(lv) = tape.linear_velocities.pop() { + *linear_velocity = lv; + } + }, + ); } // PERF: May run more than necessary, should be event-driven on aabb intersection @@ -523,15 +568,15 @@ fn scoring( hitboxes: Query>, mut score: ResMut, ) { - events.read().filter(|CollisionEnded(a, b)| { - bird.contains(*a) && hitboxes.contains(*b) - }).for_each(|_| { - info!("Hit event while {:?}", state.get()); - match state.get() { - PlayerState::Alive => score.0 = score.0.saturating_add(1), - PlayerState::Rewind => score.0 = score.0.saturating_sub(1), - PlayerState::Pause | PlayerState::Stasis => (), - } - }) - + events + .read() + .filter(|CollisionEnded(a, b)| bird.contains(*a) && hitboxes.contains(*b)) + .for_each(|_| { + info!("Hit event while {:?}", state.get()); + match state.get() { + PlayerState::Alive => score.0 = score.0.saturating_add(1), + PlayerState::Rewind => score.0 = score.0.saturating_sub(1), + PlayerState::Pause | PlayerState::Stasis => (), + } + }) } diff --git a/src/physics2d.rs b/src/physics2d.rs index cf35573..fabf8d3 100644 --- a/src/physics2d.rs +++ b/src/physics2d.rs @@ -6,10 +6,11 @@ pub struct Physics2dPlugin; impl Plugin for Physics2dPlugin { fn build(&self, app: &mut App) { - app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())).add_systems( - Update, - toggle_physics_debug_render.run_if(state_changed::), - ); + app.add_plugins((PhysicsPlugins::default(), PhysicsDebugPlugin::default())) + .add_systems( + Update, + toggle_physics_debug_render.run_if(state_changed::), + ); } }