use bevy::{
camera::{
primitives::Aabb,
visibility::{NoFrustumCulling, Visibility, VisibilityClass, add_visibility_class},
},
color::Color,
ecs::{
component::Component,
entity::Entity,
query::{Changed, Or, Without},
system::{Commands, Query},
},
math::Vec3A,
reflect::Reflect,
render::sync_world::SyncToRenderWorld,
transform::components::Transform,
};
#[derive(Component, Reflect, Clone, Copy)]
#[require(SyncToRenderWorld)]
pub struct AmbientLight2d {
pub color: Color,
pub intensity: f32,
}
impl Default for AmbientLight2d {
fn default() -> Self {
Self {
color: Color::WHITE,
intensity: 1.,
}
}
}
#[derive(Component, Reflect, Clone, Copy)]
#[require(SyncToRenderWorld, Transform, Visibility, VisibilityClass)]
#[component(on_add = add_visibility_class::<Self>)]
pub struct PointLight2d {
pub color: Color,
pub intensity: f32,
pub inner_radius: f32,
pub outer_radius: f32,
}
impl PointLight2d {
fn aabb(&self) -> Aabb {
Aabb {
center: Vec3A::ZERO,
half_extents: Vec3A::new(self.outer_radius, self.outer_radius, 0.),
}
}
}
impl Default for PointLight2d {
fn default() -> Self {
Self {
color: Color::WHITE,
intensity: 1.,
inner_radius: 0.,
outer_radius: 64.,
}
}
}
pub(super) fn update_point_light_bounds(
light_query: Query<
(Entity, &PointLight2d),
(
Or<(Changed<PointLight2d>, Without<Aabb>)>,
Without<NoFrustumCulling>,
),
>,
mut commands: Commands,
) {
for (entity, light) in light_query {
commands.entity(entity).insert(light.aabb());
}
}