Skip to content

Commit

Permalink
Pretty up light occlusion example
Browse files Browse the repository at this point in the history
  • Loading branch information
jgayfer committed Aug 6, 2024
1 parent 1f100c6 commit 8548a55
Showing 1 changed file with 55 additions and 18 deletions.
73 changes: 55 additions & 18 deletions examples/occlusion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::prelude::*;
use bevy::{
color::palettes::css::{BLUE, YELLOW},
prelude::*,
};
use bevy_light_2d::prelude::*;

fn main() {
Expand All @@ -9,31 +12,58 @@ fn main() {
.run();
}

#[derive(Component)]
struct YellowLight;

#[derive(Component)]
struct BlueLight;

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());

commands.spawn(PointLight2dBundle {
point_light: PointLight2d {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
commands.spawn((
PointLight2dBundle {
point_light: PointLight2d {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
color: Color::Srgba(YELLOW),
},
transform: Transform {
translation: Vec3::new(0.0, 200.0, 0.0),
..default()
},
..default()
},
transform: Transform {
translation: Vec3::new(0.0, 100.0, 0.0),
YellowLight,
));

commands.spawn((
PointLight2dBundle {
point_light: PointLight2d {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
color: Color::Srgba(BLUE),
},
transform: Transform {
translation: Vec3::new(0.0, -200.0, 0.0),
..default()
},
..default()
},
..default()
});
BlueLight,
));

commands.spawn(LightOccluder2dBundle {
light_occluder: LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(-400.0, -50.0, 0.0),
transform: Transform::from_xyz(-400.0, 0., 0.0),
..default()
});

Expand All @@ -43,7 +73,7 @@ fn setup(mut commands: Commands) {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(-200.0, -50.0, 0.0),
transform: Transform::from_xyz(-200.0, 0.0, 0.0),
..default()
});

Expand All @@ -53,7 +83,7 @@ fn setup(mut commands: Commands) {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(0.0, -50.0, 0.0),
transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default()
});

Expand All @@ -63,7 +93,7 @@ fn setup(mut commands: Commands) {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(200.0, -50.0, 0.0),
transform: Transform::from_xyz(200.0, 0.0, 0.0),
..default()
});

Expand All @@ -73,13 +103,20 @@ fn setup(mut commands: Commands) {
half_size: Vec2::splat(25.0),
},
},
transform: Transform::from_xyz(400.0, -50.0, 0.0),
transform: Transform::from_xyz(400.0, 0.0, 0.0),
..default()
});
}

fn move_lights(mut query: Query<&mut Transform, With<PointLight2d>>, time: Res<Time>) {
for mut light_transform in &mut query {
fn move_lights(
mut yellow_query: Query<&mut Transform, (With<YellowLight>, Without<BlueLight>)>,
mut blue_query: Query<&mut Transform, (With<BlueLight>, Without<YellowLight>)>,
time: Res<Time>,
) {
for mut light_transform in &mut yellow_query {
light_transform.translation.x = time.elapsed_seconds().sin() * 500.
}
for mut light_transform in &mut blue_query {
light_transform.translation.x = time.elapsed_seconds().cos() * 500.
}
}

0 comments on commit 8548a55

Please sign in to comment.