|
|
|
|
@ -20,6 +20,7 @@ fn main() {
|
|
|
|
|
.init_resource::<BirdAssets>()
|
|
|
|
|
.init_resource::<PipeAssets>()
|
|
|
|
|
.init_resource::<GroundAssets>()
|
|
|
|
|
.init_resource::<CeilingAssets>()
|
|
|
|
|
.init_resource::<Score>()
|
|
|
|
|
.init_resource::<RewindFrames>()
|
|
|
|
|
.init_resource::<Flaps>()
|
|
|
|
|
@ -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<OnAdd, Ground>,
|
|
|
|
|
grounds: Query<&Ground>,
|
|
|
|
|
@ -256,6 +264,24 @@ fn populate_ground(
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn populate_ceiling(
|
|
|
|
|
trigger: Trigger<OnAdd, Ceiling>,
|
|
|
|
|
ceiling: Query<&Ceiling>,
|
|
|
|
|
ceiling_assets: Res<CeilingAssets>,
|
|
|
|
|
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<ColorMaterial>,
|
|
|
|
|
mesh: Mesh2d,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
struct PipeAssets {
|
|
|
|
|
material: MeshMaterial2d<ColorMaterial>,
|
|
|
|
|
@ -338,6 +370,7 @@ fn init_assets(
|
|
|
|
|
mut bird_assets: ResMut<BirdAssets>,
|
|
|
|
|
mut pipe_assets: ResMut<PipeAssets>,
|
|
|
|
|
mut ground_assets: ResMut<GroundAssets>,
|
|
|
|
|
mut ceiling_assets: ResMut<CeilingAssets>,
|
|
|
|
|
server: Res<AssetServer>,
|
|
|
|
|
) {
|
|
|
|
|
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<State<PlayerState>>,
|
|
|
|
|
bird: Single<&ColliderAabb, With<Bird>>,
|
|
|
|
|
obstacles: Query<&ColliderAabb, Or<(With<Ground>, With<Pipe>)>>,
|
|
|
|
|
obstacles: Query<&ColliderAabb, Or<(With<Ground>, With<Pipe>, With<Ceiling>)>>,
|
|
|
|
|
mut next: ResMut<NextState<PlayerState>>,
|
|
|
|
|
) {
|
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
|
|