-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathsystems.rs
418 lines (373 loc) · 16.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
//! System functions used by the plugin for processing ldtk files.
#[cfg(feature = "render")]
use crate::resources::SetClearColor;
use crate::{
app::{LdtkEntityMap, LdtkIntCellMap},
assets::{LdtkProject, LdtkProjectData, LevelMetadataAccessor},
components::*,
ldtk::{Level, TilesetDefinition},
level::spawn_level,
resources::{LdtkSettings, LevelEvent, LevelSelection, LevelSpawnBehavior},
utils::*,
};
#[cfg(feature = "external_levels")]
use crate::assets::LdtkExternalLevel;
use bevy::{ecs::system::SystemState, prelude::*};
use std::collections::{HashMap, HashSet};
/// Detects [LdtkProject] events and spawns levels as children of the [LdtkWorldBundle].
#[allow(clippy::too_many_arguments)]
pub fn process_ldtk_assets(
mut commands: Commands,
mut ldtk_project_events: EventReader<AssetEvent<LdtkProject>>,
ldtk_world_query: Query<(Entity, &Handle<LdtkProject>)>,
#[cfg(feature = "render")] ldtk_settings: Res<LdtkSettings>,
#[cfg(feature = "render")] mut clear_color: ResMut<ClearColor>,
#[cfg(feature = "render")] ldtk_project_assets: Res<Assets<LdtkProject>>,
) {
let mut ldtk_handles_to_respawn = HashSet::new();
let mut ldtk_handles_for_clear_color = HashSet::new();
for event in ldtk_project_events.iter() {
match event {
AssetEvent::Created { handle } => {
debug!("LDtk asset creation detected.");
ldtk_handles_for_clear_color.insert(handle);
}
AssetEvent::Modified { handle } => {
info!("LDtk asset modification detected.");
ldtk_handles_to_respawn.insert(handle);
ldtk_handles_for_clear_color.insert(handle);
}
AssetEvent::Removed { handle } => {
info!("LDtk asset removal detected.");
// if mesh was modified and removed in the same update, ignore the modification
// events are ordered so future modification events are ok
ldtk_handles_to_respawn.retain(|changed_handle| *changed_handle != handle);
}
}
}
#[cfg(feature = "render")]
if ldtk_settings.set_clear_color == SetClearColor::FromEditorBackground {
for handle in ldtk_handles_for_clear_color.iter() {
if let Some(project) = &ldtk_project_assets.get(handle) {
clear_color.0 = project.json_data().bg_color;
}
}
}
for (entity, handle) in ldtk_world_query.iter() {
if ldtk_handles_to_respawn.contains(handle) {
commands.entity(entity).insert(Respawn);
}
}
}
/// Updates all LevelSet components according to the LevelSelection
pub fn apply_level_selection(
level_selection: Option<Res<LevelSelection>>,
ldtk_settings: Res<LdtkSettings>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
mut level_set_query: Query<(&Handle<LdtkProject>, &mut LevelSet)>,
#[cfg(feature = "render")] mut clear_color: ResMut<ClearColor>,
) {
if let Some(level_selection) = level_selection {
for (ldtk_handle, mut level_set) in level_set_query.iter_mut() {
if let Some(project) = &ldtk_project_assets.get(ldtk_handle) {
if let Some(level) = project.find_raw_level_by_level_selection(&level_selection) {
let new_level_set = {
let mut iids = HashSet::new();
iids.insert(LevelIid::new(level.iid.clone()));
if let LevelSpawnBehavior::UseWorldTranslation {
load_level_neighbors,
} = ldtk_settings.level_spawn_behavior
{
if load_level_neighbors {
iids.extend(
level
.neighbours
.iter()
.map(|n| LevelIid::new(n.level_iid.clone())),
);
}
}
LevelSet { iids }
};
if *level_set != new_level_set {
*level_set = new_level_set;
#[cfg(feature = "render")]
if ldtk_settings.set_clear_color == SetClearColor::FromLevelBackground {
clear_color.0 = level.bg_color;
}
}
}
}
}
}
}
/// Triggers the spawning/despawning of levels according to `LevelSet` values.
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub fn apply_level_set(
mut commands: Commands,
ldtk_world_query: Query<(
Entity,
&LevelSet,
Option<&Children>,
&Handle<LdtkProject>,
Option<&Respawn>,
)>,
ldtk_level_query: Query<(&LevelIid, Entity)>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
ldtk_settings: Res<LdtkSettings>,
mut level_events: EventWriter<LevelEvent>,
) {
for (world_entity, level_set, children, ldtk_asset_handle, respawn) in ldtk_world_query.iter() {
// Only apply level set if the asset has finished loading
if let Some(project) = ldtk_project_assets.get(ldtk_asset_handle) {
// Determine what levels are currently spawned
let previous_level_maps = children
.into_iter()
.flat_map(|iterator| iterator.iter())
.filter_map(|child_entity| ldtk_level_query.get(*child_entity).ok())
.map(|(level_iid, entity)| (level_iid.clone(), entity))
.collect::<HashMap<_, _>>();
let previous_iids: HashSet<&LevelIid> = previous_level_maps.keys().collect();
let level_set_as_ref = level_set.iids.iter().collect::<HashSet<_>>();
// Spawn levels that should be spawned but aren't
let spawned_levels = level_set_as_ref
.difference(&previous_iids)
.filter_map(|&iid| project.get_raw_level_by_iid(iid.get()))
.map(|level| {
level_events.send(LevelEvent::SpawnTriggered(LevelIid::new(level.iid.clone())));
pre_spawn_level(&mut commands, level, &ldtk_settings)
})
.collect::<Vec<_>>();
commands.entity(world_entity).push_children(&spawned_levels);
// Despawn levels that shouldn't be spawned but are
for &iid in previous_iids.difference(&level_set_as_ref) {
let map_entity = previous_level_maps.get(iid).expect(
"The set of previous_iids and the keys in previous_level_maps should be the same.",
);
commands.entity(*map_entity).despawn_recursive();
level_events.send(LevelEvent::Despawned(iid.clone()));
}
// If the world was empty before but has now been populated, and this world was
// supposed to respawn, then this run of the system must have completed the "spawning"
// portion of said respawn.
// In that case, the respawn component needs to be removed so that the cleanup system
// doesn't start the process over again.
if previous_iids.is_empty() && !spawned_levels.is_empty() && respawn.is_some() {
commands.entity(world_entity).remove::<Respawn>();
}
}
}
}
fn pre_spawn_level(commands: &mut Commands, level: &Level, ldtk_settings: &LdtkSettings) -> Entity {
let mut translation = Vec3::ZERO;
if let LevelSpawnBehavior::UseWorldTranslation { .. } = ldtk_settings.level_spawn_behavior {
let level_coords = ldtk_pixel_coords_to_translation(
IVec2::new(level.world_x, level.world_y + level.px_hei),
0,
);
translation.x = level_coords.x;
translation.y = level_coords.y;
}
commands
.spawn(LevelIid::new(level.iid.clone()))
.insert(SpatialBundle {
transform: Transform::from_translation(translation),
..default()
})
.insert(Name::new(level.identifier.clone()))
.id()
}
/// Performs all the spawning of levels, layers, chunks, bundles, entities, tiles, etc. when a
/// LevelIid is added or respawned.
#[allow(clippy::too_many_arguments, clippy::type_complexity)]
pub fn process_ldtk_levels(
mut commands: Commands,
asset_server: Res<AssetServer>,
images: ResMut<Assets<Image>>,
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
ldtk_project_assets: Res<Assets<LdtkProject>>,
#[cfg(feature = "external_levels")] level_assets: Res<Assets<LdtkExternalLevel>>,
ldtk_entity_map: NonSend<LdtkEntityMap>,
ldtk_int_cell_map: NonSend<LdtkIntCellMap>,
ldtk_query: Query<&Handle<LdtkProject>>,
level_query: Query<
(
Entity,
&LevelIid,
&Parent,
Option<&Respawn>,
Option<&Children>,
),
Or<(Added<LevelIid>, With<Respawn>)>,
>,
worldly_query: Query<&Worldly>,
mut level_events: EventWriter<LevelEvent>,
ldtk_settings: Res<LdtkSettings>,
) {
for (ldtk_entity, level_iid, parent, respawn, children) in level_query.iter() {
// Checking if the level has any children is an okay method of checking whether it has
// already been processed.
// Users will most likely not be adding children to the level entity betwen its creation
// and its processing.
//
// Furthermore, there are no circumstances where an already-processed level entity needs to
// be processed again.
// In the case of respawning levels, the level entity will have its descendants *despawned*
// first, by a separate system.
let already_processed = matches!(children, Some(children) if !children.is_empty());
if !already_processed {
if let Ok(ldtk_handle) = ldtk_query.get(parent.get()) {
if let Some(ldtk_project) = ldtk_project_assets.get(ldtk_handle) {
// Commence the spawning
let tileset_definition_map: HashMap<i32, &TilesetDefinition> = ldtk_project
.json_data()
.defs
.tilesets
.iter()
.map(|t| (t.uid, t))
.collect();
let entity_definition_map =
create_entity_definition_map(&ldtk_project.json_data().defs.entities);
let layer_definition_map =
create_layer_definition_map(&ldtk_project.json_data().defs.layers);
let int_grid_image_handle = &ldtk_project.int_grid_image_handle();
let worldly_set = worldly_query.iter().cloned().collect();
let maybe_level_data = match ldtk_project.data() {
#[cfg(feature = "internal_levels")]
LdtkProjectData::Standalone(project) => project
.level_map()
.get(level_iid.get())
.and_then(|level_metadata| {
let loaded_level = project
.get_loaded_level_at_indices(level_metadata.indices())?;
Some((level_metadata, loaded_level))
}),
#[cfg(feature = "external_levels")]
LdtkProjectData::Parent(project) => project
.level_map()
.get(level_iid.get())
.and_then(|level_metadata| {
let loaded_level = project.get_external_level_at_indices(
&level_assets,
level_metadata.metadata().indices(),
)?;
Some((level_metadata.metadata(), loaded_level))
}),
};
if let Some((level_metadata, loaded_level)) = maybe_level_data {
spawn_level(
loaded_level,
level_metadata.bg_image(),
&mut commands,
&asset_server,
&images,
&mut texture_atlases,
&ldtk_entity_map,
&ldtk_int_cell_map,
&entity_definition_map,
&layer_definition_map,
ldtk_project.tileset_map(),
&tileset_definition_map,
int_grid_image_handle,
worldly_set,
ldtk_entity,
&ldtk_settings,
);
level_events.send(LevelEvent::Spawned(LevelIid::new(
loaded_level.iid().clone(),
)));
}
if respawn.is_some() {
commands.entity(ldtk_entity).remove::<Respawn>();
}
}
}
}
}
}
/// Performs the "despawning" portion of the respawn process for `Respawn` entities.
///
/// This is currently an exclusive system for scheduling purposes.
/// If we need to revert it to its non-exclusive form, copy it from commit
/// 90155a75acb6dea4c97bb92a724b741e693b100d
pub fn clean_respawn_entities(world: &mut World) {
#[allow(clippy::type_complexity)]
let mut system_state: SystemState<(
Query<&Children, (With<Handle<LdtkProject>>, With<Respawn>)>,
Query<(Entity, &LevelIid), With<Respawn>>,
Query<&LevelIid, Without<Respawn>>,
Query<Entity, With<Worldly>>,
EventWriter<LevelEvent>,
)> = SystemState::new(world);
let mut entities_to_despawn_recursively = Vec::new();
let mut entities_to_despawn_descendants = Vec::new();
{
let (
ldtk_worlds_to_clean,
ldtk_levels_to_clean,
other_ldtk_levels,
worldly_entities,
mut level_events,
) = system_state.get_mut(world);
for world_children in ldtk_worlds_to_clean.iter() {
for child in world_children
.iter()
.filter(|l| other_ldtk_levels.contains(**l) || worldly_entities.contains(**l))
{
entities_to_despawn_recursively.push(*child);
if let Ok(level_iid) = other_ldtk_levels.get(*child) {
level_events.send(LevelEvent::Despawned(level_iid.clone()));
}
}
}
for (level_entity, level_iid) in ldtk_levels_to_clean.iter() {
entities_to_despawn_descendants.push(level_entity);
level_events.send(LevelEvent::Despawned(level_iid.clone()));
}
}
for entity in entities_to_despawn_recursively {
world.entity_mut(entity).despawn_recursive();
}
for entity in entities_to_despawn_descendants {
world.entity_mut(entity).despawn_descendants();
}
}
/// Implements the functionality for `Worldly` components.
pub fn worldly_adoption(
mut commands: Commands,
mut worldly_query: Query<(&mut Transform, &Parent, Entity), Added<Worldly>>,
transform_query: Query<(&Transform, &Parent), Without<Worldly>>,
) {
for (mut transform, parent, entity) in worldly_query.iter_mut() {
if let Ok((level_transform, level_parent)) = transform_query.get(parent.get()) {
// Find the entity's world-relative transform, so it doesn't move when its parent changes
*transform = level_transform.mul_transform(*transform);
// Make it a child of the world
commands.entity(level_parent.get()).add_child(entity);
}
}
}
/// Returns the `iid`s of levels that have spawned in this update.
///
/// Mean to be used in a chain with [fire_level_transformed_events].
pub fn detect_level_spawned_events(mut reader: EventReader<LevelEvent>) -> Vec<LevelIid> {
let mut spawned_ids = Vec::new();
for event in reader.iter() {
if let LevelEvent::Spawned(id) = event {
spawned_ids.push(id.clone());
}
}
spawned_ids
}
/// Fires [LevelEvent::Transformed] events for all the entities that spawned in the previous
/// update.
///
/// Meant to be used in a chain with [detect_level_spawned_events].
pub fn fire_level_transformed_events(
In(spawned_ids): In<Vec<LevelIid>>,
mut writer: EventWriter<LevelEvent>,
) {
for id in spawned_ids {
writer.send(LevelEvent::Transformed(id));
}
}