Compare commits

...

1 Commits

Author SHA1 Message Date
Elijah Voigt 1293a03968 pipes move when batch changes! 2 months ago

@ -101,6 +101,7 @@ fn main() {
.add_observer(flap)
.add_observer(populate_batch)
.add_observer(populate_pipe)
.add_observer(move_pipe)
.add_observer(populate_ground)
.add_observer(populate_ceiling)
.add_observer(populate_hitbox)
@ -110,7 +111,7 @@ fn main() {
fn tweak_camera(camera: Single<(Entity, &mut Camera)>, mut commands: Commands) {
debug!("Tweaking camera");
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() });
}
@ -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
/// Otherwise this just spawns in the center of the batch.
fn populate_pipe(
trigger: Trigger<OnAdd, Pipe>,
pipes: Query<(&Pipe, &Batch)>,
pipes: Query<(&Batch, &Pipe)>,
pipe_assets: Res<PipeAssets>,
mut commands: Commands,
rand: Res<Rand>,
) {
let pipe_t = {
let (pipe, Batch(id)) = pipes.get(trigger.target()).unwrap();
let (Batch(id), pipe) = pipes.get(trigger.target()).unwrap();
let offset = {
let val = rand.0.hash_one(id);
@ -374,6 +406,7 @@ fn populate_pipe(
}
}
};
commands.entity(trigger.target()).insert((
pipe_t,
pipe_assets.material.clone(),
@ -440,14 +473,14 @@ fn init_assets(
ground_assets.material = MeshMaterial2d(materials.add(ColorMaterial {
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()
}));
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: Srgba::new(0.9, 0.9, 0.9, 1.0).into(),
color: Srgba::new(0.1, 0.1, 0.1, 1.0).into(),
..default()
}));
ceiling_assets.mesh = Mesh2d(meshes.add(Rectangle::new(1.0, 0.777)));

@ -76,7 +76,7 @@ fn wrap_parallax_items(
// for each item in the paralax items
items.iter_mut().for_each(|(mut t, gt, v, Mesh2d(m))| {
if !v.get() {
info!("Item is not visible");
debug!("Item is not visible");
// Get the total size (window + scale)
let half_extents = {
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 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
let bounds_check = Vec2::abs(cam_gt.translation().truncate() - gt.translation().truncate());
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 {
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
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 {
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;
// move up or down
@ -115,7 +115,7 @@ fn wrap_parallax_items(
t.translation.y -= total_size.y;
}
}
info!("Moved to {:?}", t.translation);
debug!("Moved to {:?}", t.translation);
}
});
}

Loading…
Cancel
Save