pipes move when batch changes!

main
Elijah Voigt 2 months ago
parent d1957652c5
commit 0800c56c12

@ -101,6 +101,7 @@ fn main() {
.add_observer(flap) .add_observer(flap)
.add_observer(populate_batch) .add_observer(populate_batch)
.add_observer(populate_pipe) .add_observer(populate_pipe)
.add_observer(move_pipe)
.add_observer(populate_ground) .add_observer(populate_ground)
.add_observer(populate_ceiling) .add_observer(populate_ceiling)
.add_observer(populate_hitbox) .add_observer(populate_hitbox)
@ -110,7 +111,7 @@ fn main() {
fn tweak_camera(camera: Single<(Entity, &mut Camera)>, mut commands: Commands) { fn tweak_camera(camera: Single<(Entity, &mut Camera)>, mut commands: Commands) {
debug!("Tweaking camera"); debug!("Tweaking camera");
let (e, mut c) = camera.into_inner(); let (e, mut c) = camera.into_inner();
c.clear_color = ClearColorConfig::Custom(DEEP_SKY_BLUE.into()); c.clear_color = ClearColorConfig::Custom(SKY_BLUE.into());
commands.entity(e).insert(ColorGrading { ..default() }); commands.entity(e).insert(ColorGrading { ..default() });
} }
@ -340,18 +341,49 @@ fn populate_ceiling(
)); ));
} }
fn move_pipe(
trigger: Trigger<OnInsert, Batch>,
mut pipes: Query<(&Batch, &Pipe, &mut Transform)>,
rand: Res<Rand>,
) {
if let Ok((Batch(id), pipe, mut pipe_t)) = pipes.get_mut(trigger.target()) {
*pipe_t = {
let offset = {
let val = rand.0.hash_one(id);
let option = val % 3;
match option {
0 => 100.0,
1 => 0.0,
2 => -100.0,
_ => panic!("Can only pick 1 of 3 pipe offsets"),
}
};
match pipe {
Pipe::Top => {
Transform::from_xyz(0.0, 300.0 + offset, -2.0).with_scale(Vec3::splat(100.0))
}
Pipe::Bottom => {
Transform::from_xyz(0.0, -300.0 + offset, -2.0).with_scale(Vec3::splat(100.0))
}
}
};
}
}
/// Based on if this is a Top or Bottom pipe the placement changes /// Based on if this is a Top or Bottom pipe the placement changes
/// Otherwise this just spawns in the center of the batch. /// Otherwise this just spawns in the center of the batch.
fn populate_pipe( fn populate_pipe(
trigger: Trigger<OnAdd, Pipe>, trigger: Trigger<OnAdd, Pipe>,
pipes: Query<(&Pipe, &Batch)>, pipes: Query<(&Batch, &Pipe)>,
pipe_assets: Res<PipeAssets>, pipe_assets: Res<PipeAssets>,
mut commands: Commands, mut commands: Commands,
rand: Res<Rand>, rand: Res<Rand>,
) { ) {
let pipe_t = { let pipe_t = {
let (pipe, Batch(id)) = pipes.get(trigger.target()).unwrap(); let (Batch(id), pipe) = pipes.get(trigger.target()).unwrap();
let offset = { let offset = {
let val = rand.0.hash_one(id); let val = rand.0.hash_one(id);
@ -374,6 +406,7 @@ fn populate_pipe(
} }
} }
}; };
commands.entity(trigger.target()).insert(( commands.entity(trigger.target()).insert((
pipe_t, pipe_t,
pipe_assets.material.clone(), pipe_assets.material.clone(),
@ -440,14 +473,14 @@ fn init_assets(
ground_assets.material = MeshMaterial2d(materials.add(ColorMaterial { ground_assets.material = MeshMaterial2d(materials.add(ColorMaterial {
texture: Some(server.load("flappy/ground.png")), texture: Some(server.load("flappy/ground.png")),
color: Srgba::new(0.9, 0.9, 0.9, 1.0).into(), color: Srgba::new(0.1, 0.1, 0.1, 1.0).into(),
..default() ..default()
})); }));
ground_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0))); ground_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
ceiling_assets.material = MeshMaterial2d(materials.add(ColorMaterial { ceiling_assets.material = MeshMaterial2d(materials.add(ColorMaterial {
texture: Some(server.load("flappy/ceiling.png")), texture: Some(server.load("flappy/ceiling.png")),
color: Srgba::new(0.9, 0.9, 0.9, 1.0).into(), color: Srgba::new(0.1, 0.1, 0.1, 1.0).into(),
..default() ..default()
})); }));
ceiling_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 0.777))); ceiling_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 0.777)));
@ -716,12 +749,12 @@ fn init_background(
let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0))); let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
{ {
let t = Transform::from_xyz(-325.0, 0.0, -32.0).with_scale(Vec3::splat(650.0)); let t = Transform::from_xyz(-325.0, 0.0, -32.0).with_scale(Vec3::splat(650.0));
commands.spawn((ParallaxDepth(4.0), mesh.clone(), material.clone(), t)); commands.spawn((ParallaxDepth(24.0), mesh.clone(), material.clone(), t));
} }
{ {
let t = Transform::from_xyz(325.0, 0.0, -32.0).with_scale(Vec3::splat(650.0)); let t = Transform::from_xyz(325.0, 0.0, -32.0).with_scale(Vec3::splat(650.0));
commands.spawn((ParallaxDepth(4.0), mesh.clone(), material.clone(), t)); commands.spawn((ParallaxDepth(24.0), mesh.clone(), material.clone(), t));
} }
} }
{ {
@ -734,11 +767,11 @@ fn init_background(
let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0))); let mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 1.0)));
{ {
let t = Transform::from_xyz(-325.0, 0.0, -64.0).with_scale(Vec3::splat(650.0)); let t = Transform::from_xyz(-325.0, 0.0, -64.0).with_scale(Vec3::splat(650.0));
commands.spawn((ParallaxDepth(8.0), mesh.clone(), material.clone(), t)); commands.spawn((ParallaxDepth(32.0), mesh.clone(), material.clone(), t));
} }
{ {
let t = Transform::from_xyz(325.0, 0.0, -64.0).with_scale(Vec3::splat(650.0)); let t = Transform::from_xyz(325.0, 0.0, -64.0).with_scale(Vec3::splat(650.0));
commands.spawn((ParallaxDepth(8.0), mesh.clone(), material.clone(), t)); commands.spawn((ParallaxDepth(32.0), mesh.clone(), material.clone(), t));
} }
} }
} }

@ -76,7 +76,7 @@ fn wrap_parallax_items(
// for each item in the paralax items // for each item in the paralax items
items.iter_mut().for_each(|(mut t, gt, v, Mesh2d(m))| { items.iter_mut().for_each(|(mut t, gt, v, Mesh2d(m))| {
if !v.get() { if !v.get() {
info!("Item is not visible"); debug!("Item is not visible");
// Get the total size (window + scale) // Get the total size (window + scale)
let half_extents = { let half_extents = {
let Aabb { half_extents, .. } = meshes.get(m).unwrap().compute_aabb().unwrap(); let Aabb { half_extents, .. } = meshes.get(m).unwrap().compute_aabb().unwrap();
@ -84,16 +84,16 @@ fn wrap_parallax_items(
}; };
let object_size = t.scale.truncate() * 2.0; let object_size = t.scale.truncate() * 2.0;
let total_size = window_size_in_world_space + (object_size * half_extents); let total_size = window_size_in_world_space + (object_size * half_extents);
info!("Sizes:\n\twindow {window_size_in_world_space}\n\tobject size: {object_size}\n\tAabb Half extents: {half_extents}\n\tTotal size: {total_size}"); debug!("Sizes:\n\twindow {window_size_in_world_space}\n\tobject size: {object_size}\n\tAabb Half extents: {half_extents}\n\tTotal size: {total_size}");
// Double check that item is out of bounds // Double check that item is out of bounds
let bounds_check = Vec2::abs(cam_gt.translation().truncate() - gt.translation().truncate()); let bounds_check = Vec2::abs(cam_gt.translation().truncate() - gt.translation().truncate());
let out_of_bounds = (total_size / 2.0) - bounds_check; let out_of_bounds = (total_size / 2.0) - bounds_check;
info!("Bounds check {bounds_check} | Out of bounds: {out_of_bounds:?}"); debug!("Bounds check {bounds_check} | Out of bounds: {out_of_bounds:?}");
info!("Starting position: {:?}", t.translation); debug!("Starting position: {:?}", t.translation);
if out_of_bounds.x < 0.0 { if out_of_bounds.x < 0.0 {
info!("Object is actually out of bounds horizontally"); debug!("Object is actually out of bounds horizontally");
// determine if should move to the left or right relative to camera // determine if should move to the left or right relative to camera
let move_right = cam_gt.translation().x > gt.translation().x; let move_right = cam_gt.translation().x > gt.translation().x;
@ -105,7 +105,7 @@ fn wrap_parallax_items(
} }
} }
if out_of_bounds.y < 0.0 { if out_of_bounds.y < 0.0 {
info!("Object is actually out of bounds vertically"); debug!("Object is actually out of bounds vertically");
let move_up = cam_gt.translation().y > gt.translation().y; let move_up = cam_gt.translation().y > gt.translation().y;
// move up or down // move up or down
@ -115,7 +115,7 @@ fn wrap_parallax_items(
t.translation.y -= total_size.y; t.translation.y -= total_size.y;
} }
} }
info!("Moved to {:?}", t.translation); debug!("Moved to {:?}", t.translation);
} }
}); });
} }

Loading…
Cancel
Save