forked from Jondolf/avian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.rs
248 lines (232 loc) · 7.85 KB
/
sensor.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use avian2d::{math::*, prelude::*};
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use examples_common_2d::ExampleCommonPlugin;
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
ExampleCommonPlugin,
// Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels.
// The unit allows the engine to tune its parameters for the scale of the world, improving stability.
PhysicsPlugins::default().with_length_unit(20.0),
))
.insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1)))
.insert_resource(Gravity(Vector::ZERO))
.add_event::<MovementAction>()
.add_systems(Startup, setup)
.add_systems(
Update,
(
keyboard_input1,
keyboard_input2,
movement.run_if(has_movement), // don't mutably access the character if there is no movement
apply_movement_damping,
apply_pressure_plate_colour,
update_velocity_text,
log_events,
)
.chain(),
)
.run();
}
#[derive(Component, Default, Reflect)]
struct Character;
#[derive(Component, Default, Reflect)]
struct PressurePlate;
#[derive(Component, Default, Reflect)]
struct CharacterVelocityText;
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
) {
commands.spawn((
MaterialMesh2dBundle {
mesh: meshes.add(Capsule2d::new(12.5, 20.0)).into(),
material: materials.add(Color::srgb(0.2, 0.7, 0.9)),
transform: Transform::from_xyz(0.0, -100.0, 1.0),
..default()
},
Character,
RigidBody::Dynamic,
Collider::capsule(12.5, 20.0),
Name::new("Character"),
));
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(0.0, 150.0, 0.0),
sprite: Sprite {
color: Color::WHITE,
custom_size: Some(Vec2::new(100.0, 100.0)),
..default()
},
..default()
},
PressurePlate,
Sensor,
RigidBody::Static,
Collider::rectangle(100.0, 100.0),
Name::new("Pressure Plate"),
));
commands.spawn((
TextBundle::from_section(
"Velocity: ",
TextStyle {
font_size: 16.0,
..default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
bottom: Val::Px(5.0),
left: Val::Px(5.0),
..default()
}),
CharacterVelocityText,
Name::new("Character Velocity Text"),
));
commands.spawn(Camera2dBundle::default());
}
#[derive(Event, Debug, Reflect)]
pub enum MovementAction {
Velocity(Vector),
Offset(Vector),
Stop,
}
// use velocity
fn keyboard_input1(
mut movement_event_writer: EventWriter<MovementAction>,
keyboard_input: Res<ButtonInput<KeyCode>>,
time: Res<Time<Physics>>,
) {
if time.is_paused() {
return;
}
let space = keyboard_input.pressed(KeyCode::Space);
if space {
movement_event_writer.send(MovementAction::Stop);
return;
}
let left = keyboard_input.any_pressed([KeyCode::KeyA]);
let right = keyboard_input.any_pressed([KeyCode::KeyD]);
let up = keyboard_input.any_pressed([KeyCode::KeyW]);
let down = keyboard_input.any_pressed([KeyCode::KeyS]);
let horizontal = right as i8 - left as i8;
let vertical = up as i8 - down as i8;
let direction = Vector::new(horizontal as Scalar, vertical as Scalar);
if direction != Vector::ZERO {
movement_event_writer.send(MovementAction::Velocity(direction));
}
}
// use position offset
fn keyboard_input2(
mut movement_event_writer: EventWriter<MovementAction>,
keyboard_input: Res<ButtonInput<KeyCode>>,
time: Res<Time<Physics>>,
) {
if time.is_paused() {
return;
}
let left = keyboard_input.any_pressed([KeyCode::ArrowLeft]);
let right = keyboard_input.any_pressed([KeyCode::ArrowRight]);
let up = keyboard_input.any_pressed([KeyCode::ArrowUp]);
let down = keyboard_input.any_pressed([KeyCode::ArrowDown]);
let horizontal = right as i8 - left as i8;
let vertical = up as i8 - down as i8;
let direction = Vector::new(horizontal as Scalar, vertical as Scalar);
if direction != Vector::ZERO {
movement_event_writer.send(MovementAction::Offset(direction));
}
}
fn has_movement(mut reader: EventReader<MovementAction>) -> bool {
reader.read().next().is_some()
}
fn movement(
time: Res<Time>,
mut movement_event_reader: EventReader<MovementAction>,
mut controllers: Query<(&mut LinearVelocity, &mut Position), With<Character>>,
) {
let delta_time = time.delta_seconds_f64().adjust_precision();
for event in movement_event_reader.read() {
for (mut linear_velocity, mut position) in &mut controllers {
match event {
MovementAction::Stop => {
linear_velocity.x = 0.0;
linear_velocity.y = 0.0;
}
MovementAction::Velocity(direction) => {
let movement_acceleration = 2000.0;
linear_velocity.x += direction.x * movement_acceleration * delta_time;
linear_velocity.y += direction.y * movement_acceleration * delta_time;
}
MovementAction::Offset(direction) => {
let speed = 100.0;
position.x += direction.x * speed * delta_time;
position.y += direction.y * speed * delta_time;
}
}
}
}
}
#[allow(clippy::type_complexity)]
fn apply_movement_damping(
mut query: Query<
(&mut LinearVelocity, &mut AngularVelocity),
(With<Character>, Without<Sleeping>),
>,
time: Res<Time<Physics>>,
) {
if time.is_paused() {
return;
}
let damping_factor = 0.95;
for (mut linear_velocity, mut angular_velocity) in &mut query {
linear_velocity.x *= damping_factor;
if linear_velocity.x.abs() < 0.001 {
linear_velocity.x = 0.0;
}
linear_velocity.y *= damping_factor;
if linear_velocity.y.abs() < 0.001 {
linear_velocity.y = 0.0;
}
angular_velocity.0 *= damping_factor;
if angular_velocity.0.abs() < 0.001 {
angular_velocity.0 = 0.0;
}
}
}
fn apply_pressure_plate_colour(
mut query: Query<(&mut Sprite, &CollidingEntities), With<PressurePlate>>,
) {
for (mut sprite, colliding_entities) in &mut query {
if colliding_entities.0.is_empty() {
sprite.color = Color::srgb(0.2, 0.7, 0.9);
} else {
sprite.color = Color::srgb(0.9, 0.7, 0.2);
}
}
}
fn update_velocity_text(
character_query: Query<(&LinearVelocity, Has<Sleeping>), With<Character>>,
pressure_plate_query: Query<Has<Sleeping>, With<PressurePlate>>,
mut text_query: Query<&mut Text, With<CharacterVelocityText>>,
) {
if let (Ok((velocity, character_sleeping)), Ok(pressure_plate_sleeping)) = (
character_query.get_single(),
pressure_plate_query.get_single(),
) {
text_query.single_mut().sections[0].value = format!(
"Velocity: {:.4}, {:.4}\nCharacter sleeping:{}\nPressure plate sleeping: {}",
velocity.x, velocity.y, character_sleeping, pressure_plate_sleeping
);
}
}
fn log_events(mut started: EventReader<CollisionStarted>, mut ended: EventReader<CollisionEnded>) {
// print out the started and ended events
for event in started.read() {
println!("CollisionStarted: {:?}", event);
}
for event in ended.read() {
println!("CollisionEnded: {:?}", event);
}
}