-
Notifications
You must be signed in to change notification settings - Fork 69
/
resources.rs
132 lines (119 loc) · 4.71 KB
/
resources.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
//! Resources and events used by the plugin.
use crate::ldtk::Level;
use bevy::prelude::*;
#[allow(unused_imports)]
use crate::components::{LdtkWorldBundle, LevelSet};
/// Resource for choosing which level(s) to spawn.
///
/// Updating this will despawn the current level and spawn the new one (unless they are the same).
/// You can also load the selected level's neighbors using the [LevelSpawnBehavior] option.
///
/// This resource works by updating the [LdtkWorldBundle]'s [LevelSet] component.
/// If you need more control over the spawned levels than this resource provides,
/// you can choose not to insert this resource and interface with [LevelSet] directly instead.
#[derive(Clone, Eq, PartialEq, Debug, Resource)]
pub enum LevelSelection {
/// Spawn level with the given identifier.
Identifier(String),
/// Spawn level from its index in the LDtk file's list of levels.
Index(usize),
/// Spawn level with the given level `iid`.
Iid(String),
/// Spawn level with the given level `uid`.
Uid(i32),
}
impl Default for LevelSelection {
fn default() -> Self {
LevelSelection::Index(0)
}
}
impl LevelSelection {
pub fn is_match(&self, index: &usize, level: &Level) -> bool {
match self {
LevelSelection::Identifier(s) => *s == level.identifier,
LevelSelection::Index(i) => *i == *index,
LevelSelection::Iid(i) => *i == level.iid,
LevelSelection::Uid(u) => *u == level.uid,
}
}
}
/// Option in [LdtkSettings] that determines clear color behavior.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub enum SetClearColor {
/// Don't update the clear color at all
No,
/// Update the clear color to use the background color of the current level
/// (determined by [LevelSelection])
FromLevelBackground,
/// Update the clear color to use the entire editor's background color
FromEditorBackground,
}
impl Default for SetClearColor {
fn default() -> Self {
Self::No
}
}
/// Option in [LdtkSettings] that determines level spawn behavior.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum LevelSpawnBehavior {
/// Newly spawned levels will be spawned with a translation of zero relative to the
/// [LdtkWorldBundle].
#[default]
UseZeroTranslation,
/// Newly spawned levels will be spawned with translations like their location in the LDtk
/// world.
///
/// Useful for "2d free map" and "GridVania" layouts.
UseWorldTranslation {
/// When used with the [LevelSelection] resource, levels in the `__level_neighbors` list of
/// the selected level will be spawned in addition to the selected level.
load_level_neighbors: bool,
},
}
/// Option in [LdtkSettings] that determines the visual representation of IntGrid layers when they don't have AutoTile rules.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum IntGridRendering {
/// Renders the tile with its corresponding color in LDtk, so it appears like it does in LDtk
#[default]
Colorful,
/// Does not render the tile
Invisible,
}
/// Option in [LdtkSettings] that dictates how the plugin handles level backgrounds.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub enum LevelBackground {
/// The level background's color (and image, if it exists) are rendered.
/// The first layer of the level will be the background color.
#[default]
Rendered,
/// There will be no level backgrounds, not even an empty layer.
Nonexistent,
}
/// Settings resource for the plugin.
/// Check out the documentation for each field type to learn more.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Resource)]
pub struct LdtkSettings {
pub level_spawn_behavior: LevelSpawnBehavior,
pub set_clear_color: SetClearColor,
pub int_grid_rendering: IntGridRendering,
pub level_background: LevelBackground,
}
/// Events fired by the plugin related to level spawning/despawning.
///
/// Each variant stores the level's `iid` in LDtk.
#[derive(Clone, Eq, PartialEq, Debug, Hash)]
pub enum LevelEvent {
/// Indicates that a level has been triggered to spawn, but hasn't been spawned yet.
SpawnTriggered(String),
/// The level, with all of its layers, entities, etc., has spawned.
///
/// Note: due to the frame-delay of [GlobalTransform] being updated, this may not be the event
/// you want to listen for.
/// If your systems are [GlobalTransform]-dependent, see [LevelEvent::Transformed].
Spawned(String),
/// Occurs during the [CoreSet::PostUpdate] after the level has spawned, so all
/// [GlobalTransform]s of the level should be updated.
Transformed(String),
/// Indicates that a level has despawned.
Despawned(String),
}