use bevy_ecs::{reflect::ReflectComponent, system::Query};
use bevy_math::{vec3, Vec2};
use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::camera::Camera;
use bevy_transform::components::{GlobalTransform, Transform};
#[derive(Debug, Default, Clone, TypeUuid, Reflect)]
#[reflect(Component)]
#[uuid = "0e436fcb-7b34-420c-92df-6fda230332d8"]
pub struct Parallax {
pub factor: Vec2,
pub transform: Transform,
}
pub fn parallax_transform_system(
cameras: Query<(&GlobalTransform, &Camera)>,
mut parallax: Query<(&mut Transform, &Parallax)>,
) {
if let Some((camera_transform, _camera)) = cameras.iter().next() {
let translation = camera_transform.translation;
for (mut transform, parallax) in parallax.iter_mut() {
transform.translation = parallax.transform.translation
+ translation * vec3(1.0, 1.0, 0.0)
- translation * parallax.factor.extend(0.0);
transform.rotation = parallax.transform.rotation;
transform.scale = parallax.transform.scale;
}
}
}
impl Parallax {
pub fn new(factor: Vec2, transform: Transform) -> Self {
Self { factor, transform }
}
}