forked from Trouv/bevy_ecs_ldtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systems.rs
490 lines (441 loc) · 18.7 KB
/
systems.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use crate::components::*;
use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*;
use std::collections::{HashMap, HashSet};
use bevy_rapier2d::prelude::*;
pub fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let camera = Camera2dBundle::default();
commands.spawn(camera);
let ldtk_handle = asset_server.load("Typical_2D_platformer_example.ldtk");
commands.spawn(LdtkWorldBundle {
ldtk_handle,
..Default::default()
});
}
pub fn dbg_player_items(
input: Res<Input<KeyCode>>,
mut query: Query<(&Items, &EntityInstance), With<Player>>,
) {
for (items, entity_instance) in &mut query {
if input.just_pressed(KeyCode::P) {
dbg!(&items);
dbg!(&entity_instance);
}
}
}
pub fn movement(
input: Res<Input<KeyCode>>,
mut query: Query<(&mut Velocity, &mut Climber, &GroundDetection), With<Player>>,
) {
for (mut velocity, mut climber, ground_detection) in &mut query {
let right = if input.pressed(KeyCode::D) { 1. } else { 0. };
let left = if input.pressed(KeyCode::A) { 1. } else { 0. };
velocity.linvel.x = (right - left) * 200.;
if climber.intersecting_climbables.is_empty() {
climber.climbing = false;
} else if input.just_pressed(KeyCode::W) || input.just_pressed(KeyCode::S) {
climber.climbing = true;
}
if climber.climbing {
let up = if input.pressed(KeyCode::W) { 1. } else { 0. };
let down = if input.pressed(KeyCode::S) { 1. } else { 0. };
velocity.linvel.y = (up - down) * 200.;
}
if input.just_pressed(KeyCode::Space) && (ground_detection.on_ground || climber.climbing) {
velocity.linvel.y = 500.;
climber.climbing = false;
}
}
}
/// Spawns heron collisions for the walls of a level
///
/// You could just insert a ColliderBundle in to the WallBundle,
/// but this spawns a different collider for EVERY wall tile.
/// This approach leads to bad performance.
///
/// Instead, by flagging the wall tiles and spawning the collisions later,
/// we can minimize the amount of colliding entities.
///
/// The algorithm used here is a nice compromise between simplicity, speed,
/// and a small number of rectangle colliders.
/// In basic terms, it will:
/// 1. consider where the walls are
/// 2. combine wall tiles into flat "plates" in each individual row
/// 3. combine the plates into rectangles across multiple rows wherever possible
/// 4. spawn colliders for each rectangle
pub fn spawn_wall_collision(
mut commands: Commands,
wall_query: Query<(&GridCoords, &Parent), Added<Wall>>,
parent_query: Query<&Parent, Without<Wall>>,
level_query: Query<(Entity, &Handle<LdtkLevel>)>,
levels: Res<Assets<LdtkLevel>>,
) {
/// Represents a wide wall that is 1 tile tall
/// Used to spawn wall collisions
#[derive(Clone, Eq, PartialEq, Debug, Default, Hash)]
struct Plate {
left: i32,
right: i32,
}
/// A simple rectangle type representing a wall of any size
struct Rect {
left: i32,
right: i32,
top: i32,
bottom: i32,
}
// Consider where the walls are
// storing them as GridCoords in a HashSet for quick, easy lookup
//
// The key of this map will be the entity of the level the wall belongs to.
// This has two consequences in the resulting collision entities:
// 1. it forces the walls to be split along level boundaries
// 2. it lets us easily add the collision entities as children of the appropriate level entity
let mut level_to_wall_locations: HashMap<Entity, HashSet<GridCoords>> = HashMap::new();
wall_query.for_each(|(&grid_coords, parent)| {
// An intgrid tile's direct parent will be a layer entity, not the level entity
// To get the level entity, you need the tile's grandparent.
// This is where parent_query comes in.
if let Ok(grandparent) = parent_query.get(parent.get()) {
level_to_wall_locations
.entry(grandparent.get())
.or_default()
.insert(grid_coords);
}
});
if !wall_query.is_empty() {
level_query.for_each(|(level_entity, level_handle)| {
if let Some(level_walls) = level_to_wall_locations.get(&level_entity) {
let level = levels
.get(level_handle)
.expect("Level should be loaded by this point");
let LayerInstance {
c_wid: width,
c_hei: height,
grid_size,
..
} = level
.level
.layer_instances
.clone()
.expect("Level asset should have layers")[0];
// combine wall tiles into flat "plates" in each individual row
let mut plate_stack: Vec<Vec<Plate>> = Vec::new();
for y in 0..height {
let mut row_plates: Vec<Plate> = Vec::new();
let mut plate_start = None;
// + 1 to the width so the algorithm "terminates" plates that touch the right edge
for x in 0..width + 1 {
match (plate_start, level_walls.contains(&GridCoords { x, y })) {
(Some(s), false) => {
row_plates.push(Plate {
left: s,
right: x - 1,
});
plate_start = None;
}
(None, true) => plate_start = Some(x),
_ => (),
}
}
plate_stack.push(row_plates);
}
// combine "plates" into rectangles across multiple rows
let mut rect_builder: HashMap<Plate, Rect> = HashMap::new();
let mut prev_row: Vec<Plate> = Vec::new();
let mut wall_rects: Vec<Rect> = Vec::new();
// an extra empty row so the algorithm "finishes" the rects that touch the top edge
plate_stack.push(Vec::new());
for (y, current_row) in plate_stack.into_iter().enumerate() {
for prev_plate in &prev_row {
if !current_row.contains(prev_plate) {
// remove the finished rect so that the same plate in the future starts a new rect
if let Some(rect) = rect_builder.remove(prev_plate) {
wall_rects.push(rect);
}
}
}
for plate in ¤t_row {
rect_builder
.entry(plate.clone())
.and_modify(|e| e.top += 1)
.or_insert(Rect {
bottom: y as i32,
top: y as i32,
left: plate.left,
right: plate.right,
});
}
prev_row = current_row;
}
commands.entity(level_entity).with_children(|level| {
// Spawn colliders for every rectangle..
// Making the collider a child of the level serves two purposes:
// 1. Adjusts the transforms to be relative to the level for free
// 2. the colliders will be despawned automatically when levels unload
for wall_rect in wall_rects {
level
.spawn_empty()
.insert(Collider::cuboid(
(wall_rect.right as f32 - wall_rect.left as f32 + 1.)
* grid_size as f32
/ 2.,
(wall_rect.top as f32 - wall_rect.bottom as f32 + 1.)
* grid_size as f32
/ 2.,
))
.insert(RigidBody::Fixed)
.insert(Friction::new(1.0))
.insert(Transform::from_xyz(
(wall_rect.left + wall_rect.right + 1) as f32 * grid_size as f32
/ 2.,
(wall_rect.bottom + wall_rect.top + 1) as f32 * grid_size as f32
/ 2.,
0.,
))
.insert(GlobalTransform::default());
}
});
}
});
}
}
pub fn detect_climb_range(
mut climbers: Query<&mut Climber>,
climbables: Query<Entity, With<Climbable>>,
mut collisions: EventReader<CollisionEvent>,
) {
for collision in collisions.iter() {
match collision {
CollisionEvent::Started(collider_a, collider_b, _) => {
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_a), climbables.get(*collider_b))
{
climber.intersecting_climbables.insert(climbable);
}
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_b), climbables.get(*collider_a))
{
climber.intersecting_climbables.insert(climbable);
};
}
CollisionEvent::Stopped(collider_a, collider_b, _) => {
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_a), climbables.get(*collider_b))
{
climber.intersecting_climbables.remove(&climbable);
}
if let (Ok(mut climber), Ok(climbable)) =
(climbers.get_mut(*collider_b), climbables.get(*collider_a))
{
climber.intersecting_climbables.remove(&climbable);
}
}
}
}
}
pub fn ignore_gravity_if_climbing(
mut query: Query<(&Climber, &mut GravityScale), Changed<Climber>>,
) {
for (climber, mut gravity_scale) in &mut query {
if climber.climbing {
gravity_scale.0 = 0.0;
} else {
gravity_scale.0 = 1.0;
}
}
}
pub fn patrol(mut query: Query<(&mut Transform, &mut Velocity, &mut Patrol)>) {
for (mut transform, mut velocity, mut patrol) in &mut query {
if patrol.points.len() <= 1 {
continue;
}
let mut new_velocity =
(patrol.points[patrol.index] - transform.translation.truncate()).normalize() * 75.;
if new_velocity.dot(velocity.linvel) < 0. {
if patrol.index == 0 {
patrol.forward = true;
} else if patrol.index == patrol.points.len() - 1 {
patrol.forward = false;
}
transform.translation.x = patrol.points[patrol.index].x;
transform.translation.y = patrol.points[patrol.index].y;
if patrol.forward {
patrol.index += 1;
} else {
patrol.index -= 1;
}
new_velocity =
(patrol.points[patrol.index] - transform.translation.truncate()).normalize() * 75.;
}
velocity.linvel = new_velocity;
}
}
const ASPECT_RATIO: f32 = 16. / 9.;
#[allow(clippy::type_complexity)]
pub fn camera_fit_inside_current_level(
mut camera_query: Query<
(
&mut bevy::render::camera::OrthographicProjection,
&mut Transform,
),
Without<Player>,
>,
player_query: Query<&Transform, With<Player>>,
level_query: Query<
(&Transform, &Handle<LdtkLevel>),
(Without<OrthographicProjection>, Without<Player>),
>,
level_selection: Res<LevelSelection>,
ldtk_levels: Res<Assets<LdtkLevel>>,
) {
if let Ok(Transform {
translation: player_translation,
..
}) = player_query.get_single()
{
let player_translation = *player_translation;
let (mut orthographic_projection, mut camera_transform) = camera_query.single_mut();
for (level_transform, level_handle) in &level_query {
if let Some(ldtk_level) = ldtk_levels.get(level_handle) {
let level = &ldtk_level.level;
if level_selection.is_match(&0, level) {
let level_ratio = level.px_wid as f32 / ldtk_level.level.px_hei as f32;
orthographic_projection.viewport_origin = Vec2::ZERO;
if level_ratio > ASPECT_RATIO {
// level is wider than the screen
let height = (level.px_hei as f32 / 9.).round() * 9.;
let width = height * ASPECT_RATIO;
orthographic_projection.scaling_mode =
bevy::render::camera::ScalingMode::Fixed { width, height };
camera_transform.translation.x =
(player_translation.x - level_transform.translation.x - width / 2.)
.clamp(0., level.px_wid as f32 - width);
camera_transform.translation.y = 0.;
} else {
// level is taller than the screen
let width = (level.px_wid as f32 / 16.).round() * 16.;
let height = width / ASPECT_RATIO;
orthographic_projection.scaling_mode =
bevy::render::camera::ScalingMode::Fixed { width, height };
camera_transform.translation.y =
(player_translation.y - level_transform.translation.y - height / 2.)
.clamp(0., level.px_hei as f32 - height);
camera_transform.translation.x = 0.;
}
camera_transform.translation.x += level_transform.translation.x;
camera_transform.translation.y += level_transform.translation.y;
}
}
}
}
}
pub fn update_level_selection(
level_query: Query<(&Handle<LdtkLevel>, &Transform), Without<Player>>,
player_query: Query<&Transform, With<Player>>,
mut level_selection: ResMut<LevelSelection>,
ldtk_levels: Res<Assets<LdtkLevel>>,
) {
for (level_handle, level_transform) in &level_query {
if let Some(ldtk_level) = ldtk_levels.get(level_handle) {
let level_bounds = Rect {
min: Vec2::new(level_transform.translation.x, level_transform.translation.y),
max: Vec2::new(
level_transform.translation.x + ldtk_level.level.px_wid as f32,
level_transform.translation.y + ldtk_level.level.px_hei as f32,
),
};
for player_transform in &player_query {
if player_transform.translation.x < level_bounds.max.x
&& player_transform.translation.x > level_bounds.min.x
&& player_transform.translation.y < level_bounds.max.y
&& player_transform.translation.y > level_bounds.min.y
&& !level_selection.is_match(&0, &ldtk_level.level)
{
*level_selection = LevelSelection::Iid(ldtk_level.level.iid.clone());
}
}
}
}
}
pub fn spawn_ground_sensor(
mut commands: Commands,
detect_ground_for: Query<(Entity, &Collider), Added<GroundDetection>>,
) {
for (entity, shape) in &detect_ground_for {
if let Some(cuboid) = shape.as_cuboid() {
let Vec2 {
x: half_extents_x,
y: half_extents_y,
} = cuboid.half_extents();
let detector_shape = Collider::cuboid(half_extents_x / 2.0, 2.);
let sensor_translation = Vec3::new(0., -half_extents_y, 0.);
commands.entity(entity).with_children(|builder| {
builder
.spawn_empty()
.insert(ActiveEvents::COLLISION_EVENTS)
.insert(detector_shape)
.insert(Sensor)
.insert(Transform::from_translation(sensor_translation))
.insert(GlobalTransform::default())
.insert(GroundSensor {
ground_detection_entity: entity,
intersecting_ground_entities: HashSet::new(),
});
});
}
}
}
pub fn ground_detection(
mut ground_sensors: Query<&mut GroundSensor>,
mut collisions: EventReader<CollisionEvent>,
collidables: Query<With<Collider>, Without<Sensor>>,
) {
for collision_event in collisions.iter() {
match collision_event {
CollisionEvent::Started(e1, e2, _) => {
if collidables.contains(*e1) {
if let Ok(mut sensor) = ground_sensors.get_mut(*e2) {
sensor.intersecting_ground_entities.insert(*e1);
}
} else if collidables.contains(*e2) {
if let Ok(mut sensor) = ground_sensors.get_mut(*e1) {
sensor.intersecting_ground_entities.insert(*e2);
}
}
}
CollisionEvent::Stopped(e1, e2, _) => {
if collidables.contains(*e1) {
if let Ok(mut sensor) = ground_sensors.get_mut(*e2) {
sensor.intersecting_ground_entities.remove(e1);
}
} else if collidables.contains(*e2) {
if let Ok(mut sensor) = ground_sensors.get_mut(*e1) {
sensor.intersecting_ground_entities.remove(e2);
}
}
}
}
}
}
pub fn update_on_ground(
mut ground_detectors: Query<&mut GroundDetection>,
ground_sensors: Query<&GroundSensor, Changed<GroundSensor>>,
) {
for sensor in &ground_sensors {
if let Ok(mut ground_detection) = ground_detectors.get_mut(sensor.ground_detection_entity) {
ground_detection.on_ground = !sensor.intersecting_ground_entities.is_empty();
}
}
}
pub fn restart_level(
mut commands: Commands,
level_query: Query<Entity, With<Handle<LdtkLevel>>>,
input: Res<Input<KeyCode>>,
) {
if input.just_pressed(KeyCode::R) {
for level_entity in &level_query {
commands.entity(level_entity).insert(Respawn);
}
}
}