-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathocclusion.rs
166 lines (145 loc) · 4.23 KB
/
occlusion.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use bevy::{
color::palettes::css::{BLUE, YELLOW},
input::mouse::MouseWheel,
prelude::*,
};
use bevy_light_2d::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, Light2dPlugin))
.add_systems(Startup, setup)
.add_systems(
Update,
(move_lights, control_camera_movement, control_camera_zoom),
)
.run();
}
#[derive(Component)]
struct YellowLight;
#[derive(Component)]
struct BlueLight;
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn((
PointLight2d {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
color: Color::Srgba(YELLOW),
},
Transform::from_translation(Vec3::new(0.0, 200.0, 0.0)),
YellowLight,
));
commands.spawn((
PointLight2d {
intensity: 20.0,
radius: 1000.0,
falloff: 10.0,
cast_shadows: true,
color: Color::Srgba(BLUE),
},
Transform::from_translation(Vec3::new(0.0, -200.0, 0.0)),
BlueLight,
));
commands.spawn((
LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
Transform::from_xyz(-400.0, 0., 0.0),
));
commands.spawn((
LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
Transform::from_xyz(-200.0, 0.0, 0.0),
));
commands.spawn((
LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
Transform::from_xyz(0.0, 0.0, 0.0),
));
commands.spawn((
LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
Transform::from_xyz(200.0, 0.0, 0.0),
));
commands.spawn((
LightOccluder2d {
shape: LightOccluder2dShape::Rectangle {
half_size: Vec2::splat(25.0),
},
},
Transform::from_xyz(400.0, 0.0, 0.0),
));
}
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_secs().sin() * 500.
}
for mut light_transform in &mut blue_query {
light_transform.translation.x = time.elapsed_secs().cos() * 500.
}
}
const CAMERA_SPEED: f32 = 10.0;
fn control_camera_movement(
mut camera_current: Local<Vec2>,
mut camera_target: Local<Vec2>,
mut query_cameras: Query<&mut Transform, With<Camera>>,
keyboard: Res<ButtonInput<KeyCode>>,
) {
if keyboard.pressed(KeyCode::KeyW) {
camera_target.y += CAMERA_SPEED;
}
if keyboard.pressed(KeyCode::KeyS) {
camera_target.y -= CAMERA_SPEED;
}
if keyboard.pressed(KeyCode::KeyA) {
camera_target.x -= CAMERA_SPEED;
}
if keyboard.pressed(KeyCode::KeyD) {
camera_target.x += CAMERA_SPEED;
}
// Smooth camera.
let blend_ratio = 0.2;
let movement = *camera_target - *camera_current;
*camera_current += movement * blend_ratio;
// Update all sprite cameras.
for mut camera_transform in query_cameras.iter_mut() {
camera_transform.translation.x = camera_current.x;
camera_transform.translation.y = camera_current.y;
}
}
const MIN_CAMERA_SCALE: f32 = 1.;
const MAX_CAMERA_SCALE: f32 = 20.;
fn control_camera_zoom(
mut cameras: Query<&mut OrthographicProjection, With<Camera>>,
time: Res<Time>,
mut scroll_event_reader: EventReader<MouseWheel>,
) {
let mut projection_delta = 0.;
for event in scroll_event_reader.read() {
projection_delta += event.y * 3.;
}
if projection_delta == 0. {
return;
}
for mut camera in cameras.iter_mut() {
camera.scale = (camera.scale - projection_delta * time.delta_secs())
.clamp(MIN_CAMERA_SCALE, MAX_CAMERA_SCALE);
}
}