|
|
|
@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
use bevy::render::primitives::Aabb;
|
|
|
|
|
|
|
|
use bevy::render::mesh::MeshAabb;
|
|
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ParallaxPlugin;
|
|
|
|
pub struct ParallaxPlugin;
|
|
|
|
@ -54,9 +57,10 @@ fn move_parallax_items(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn wrap_parallax_items(
|
|
|
|
fn wrap_parallax_items(
|
|
|
|
mut items: Query<(&mut Transform, &GlobalTransform, &ViewVisibility), (With<ParallaxDepth>, Without<Camera2d>, Changed<ViewVisibility>)>,
|
|
|
|
mut items: Query<(&mut Transform, &GlobalTransform, &ViewVisibility, &Mesh2d), (With<ParallaxDepth>, Without<Camera2d>, Changed<ViewVisibility>)>,
|
|
|
|
camera: Single<(&Camera, &GlobalTransform), With<Camera2d>>,
|
|
|
|
camera: Single<(&Camera, &GlobalTransform), With<Camera2d>>,
|
|
|
|
window: Single<&Window>,
|
|
|
|
window: Single<&Window>,
|
|
|
|
|
|
|
|
meshes: Res<Assets<Mesh>>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
if !items.is_empty() {
|
|
|
|
if !items.is_empty() {
|
|
|
|
let (cam, cam_gt) = *camera;
|
|
|
|
let (cam, cam_gt) = *camera;
|
|
|
|
@ -68,26 +72,28 @@ fn wrap_parallax_items(
|
|
|
|
|
|
|
|
|
|
|
|
Vec2::abs(Vec2::new(bottom_right.x - top_left.x, bottom_right.y - top_left.y))
|
|
|
|
Vec2::abs(Vec2::new(bottom_right.x - top_left.x, bottom_right.y - top_left.y))
|
|
|
|
};
|
|
|
|
};
|
|
|
|
debug!("Window size: {:?}", window_size_in_world_space);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// for each item in the paralax items
|
|
|
|
// for each item in the paralax items
|
|
|
|
items.iter_mut().for_each(|(mut t, gt, v)| {
|
|
|
|
items.iter_mut().for_each(|(mut t, gt, v, Mesh2d(m))| {
|
|
|
|
if !v.get() {
|
|
|
|
if !v.get() {
|
|
|
|
debug!("The item is not visible");
|
|
|
|
info!("Item is not visible");
|
|
|
|
|
|
|
|
|
|
|
|
// Get the total size (window + scale)
|
|
|
|
// Get the total size (window + scale)
|
|
|
|
let total_size = window_size_in_world_space + (t.scale.truncate() * 2.0);
|
|
|
|
let half_extents = {
|
|
|
|
debug!("Window size: {:?} | Object size: {:?}", window_size_in_world_space, t.scale.truncate());
|
|
|
|
let Aabb { half_extents, .. } = meshes.get(m).unwrap().compute_aabb().unwrap();
|
|
|
|
debug!("Total size: {:?}", total_size);
|
|
|
|
half_extents.truncate()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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}");
|
|
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
debug!("Bounds check {:?}", bounds_check);
|
|
|
|
info!("Bounds check {bounds_check} | Out of bounds: {out_of_bounds:?}");
|
|
|
|
debug!("Out of bounds: {:?}", out_of_bounds);
|
|
|
|
|
|
|
|
debug!("Starting position: {:?}", t.translation);
|
|
|
|
info!("Starting position: {:?}", t.translation);
|
|
|
|
if out_of_bounds.x < 0.0 {
|
|
|
|
if out_of_bounds.x < 0.0 {
|
|
|
|
debug!("Object is actually out of bounds horizontally");
|
|
|
|
info!("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;
|
|
|
|
|
|
|
|
|
|
|
|
@ -99,7 +105,7 @@ fn wrap_parallax_items(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if out_of_bounds.y < 0.0 {
|
|
|
|
if out_of_bounds.y < 0.0 {
|
|
|
|
debug!("Object is actually out of bounds vertically");
|
|
|
|
info!("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
|
|
|
|
@ -109,7 +115,7 @@ fn wrap_parallax_items(
|
|
|
|
t.translation.y -= total_size.y;
|
|
|
|
t.translation.y -= total_size.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("Moved to {:?}", t.translation);
|
|
|
|
info!("Moved to {:?}", t.translation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|