diff --git a/assets/flappy/ceiling.png b/assets/flappy/ceiling.png new file mode 100644 index 0000000..e202302 --- /dev/null +++ b/assets/flappy/ceiling.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a786c7930eed28d37b1725e8fed1a63af60c2a3c886f3c9474979986badca594 +size 251 diff --git a/assets/flappy/pipe.png b/assets/flappy/pipe.png index 3c368b4..f8ca626 100644 --- a/assets/flappy/pipe.png +++ b/assets/flappy/pipe.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1f291a3023d0f6e69da445c20b233aeddb1bc1a2858d60b71081bf406a47df84 -size 243 +oid sha256:deffb8ad67214563e29be38699275173a969a9b06464837d9aa4b283c42dedba +size 242 diff --git a/src/bin/flappy/main.rs b/src/bin/flappy/main.rs index d13fa1a..e9c73f1 100644 --- a/src/bin/flappy/main.rs +++ b/src/bin/flappy/main.rs @@ -20,6 +20,7 @@ fn main() { .init_resource::() .init_resource::() .init_resource::() + .init_resource::() .init_resource::() .init_resource::() .init_resource::() @@ -97,6 +98,7 @@ fn main() { .add_observer(populate_batch) .add_observer(populate_pipe) .add_observer(populate_ground) + .add_observer(populate_ceiling) .add_observer(populate_hitbox) .run(); } @@ -169,6 +171,9 @@ fn init_bird( #[derive(Component, Clone)] struct Ground(isize); +#[derive(Component, Clone)] +struct Ceiling(isize); + #[derive(Component, Clone)] enum Pipe { Top, @@ -218,6 +223,11 @@ fn populate_batch( parent.spawn(Ground(0)); parent.spawn(Ground(1)); parent.spawn(Ground(2)); + parent.spawn(Ceiling(-2)); + parent.spawn(Ceiling(-1)); + parent.spawn(Ceiling(0)); + parent.spawn(Ceiling(1)); + parent.spawn(Ceiling(2)); if *batch_id > 0 { parent.spawn((Pipe::Top, Batch(*batch_id))); parent.spawn((Pipe::Bottom, Batch(*batch_id))); @@ -236,8 +246,6 @@ fn update_batch_position( }); } -/// The ground population spawns a center peace and two pieces of ground -/// to the left and right of the center. fn populate_ground( trigger: Trigger, grounds: Query<&Ground>, @@ -256,6 +264,24 @@ fn populate_ground( )); } +fn populate_ceiling( + trigger: Trigger, + ceiling: Query<&Ceiling>, + ceiling_assets: Res, + mut commands: Commands, +) { + let Ceiling(idx) = ceiling.get(trigger.target()).unwrap(); + debug!("populating ceiling{:?}", idx); + commands.entity(trigger.target()).insert(( + ceiling_assets.material.clone(), + ceiling_assets.mesh.clone(), + Name::new("ceiling"), + RigidBody::Static, + Collider::rectangle(1.0, 1.0), + Transform::from_xyz(100.0 * (*idx) as f32, 300.0, -3.0).with_scale(Vec3::splat(100.0)), + )); +} + /// Based on if this is a Top or Bottom pipe the placement changes /// Otherwise this just spawns in the center of the batch. fn populate_pipe( @@ -283,10 +309,10 @@ fn populate_pipe( match pipe { Pipe::Top => { - Transform::from_xyz(0.0, 400.0 + offset, -2.0).with_scale(Vec3::splat(100.0)) + Transform::from_xyz(0.0, 300.0 + offset, -2.0).with_scale(Vec3::splat(100.0)) } Pipe::Bottom => { - Transform::from_xyz(0.0, -200.0 + offset, -2.0).with_scale(Vec3::splat(100.0)) + Transform::from_xyz(0.0, -300.0 + offset, -2.0).with_scale(Vec3::splat(100.0)) } } }; @@ -325,6 +351,12 @@ struct GroundAssets { mesh: Mesh2d, } +#[derive(Resource, Default)] +struct CeilingAssets { + material: MeshMaterial2d, + mesh: Mesh2d, +} + #[derive(Resource, Default)] struct PipeAssets { material: MeshMaterial2d, @@ -338,6 +370,7 @@ fn init_assets( mut bird_assets: ResMut, mut pipe_assets: ResMut, mut ground_assets: ResMut, + mut ceiling_assets: ResMut, server: Res, ) { pipe_assets.material = MeshMaterial2d(materials.add(ColorMaterial { @@ -345,7 +378,7 @@ fn init_assets( color: GREEN.into(), ..default() })); - pipe_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 4.0))); + pipe_assets.mesh = Mesh2d(meshes.add(Rectangle::new(0.875, 4.0))); ground_assets.material = MeshMaterial2d(materials.add(ColorMaterial { texture: Some(server.load("flappy/ground.png")), @@ -354,6 +387,13 @@ fn init_assets( })); ground_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0))); + ceiling_assets.material = MeshMaterial2d(materials.add(ColorMaterial { + texture: Some(server.load("flappy/ceiling.png")), + color: BLACK.into(), + ..default() + })); + ceiling_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 0.777))); + bird_assets.material = MeshMaterial2d(materials.add(ColorMaterial { texture: Some(server.load("flappy/bird.png")), color: ORANGE.into(), @@ -704,7 +744,7 @@ fn rewind( fn detect_dead( #[cfg(debug_assertions)] state: Res>, bird: Single<&ColliderAabb, With>, - obstacles: Query<&ColliderAabb, Or<(With, With)>>, + obstacles: Query<&ColliderAabb, Or<(With, With, With)>>, mut next: ResMut>, ) { #[cfg(debug_assertions)]