-
Notifications
You must be signed in to change notification settings - Fork 69
/
components.rs
373 lines (324 loc) · 12.1 KB
/
components.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
//! [Component]s and [Bundle]s used by the plugin.
pub use crate::ldtk::EntityInstance;
use crate::ldtk::{LayerInstance, Type};
use bevy::prelude::*;
use std::{
collections::HashSet,
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
};
#[allow(unused_imports)]
use crate::{
assets::LdtkLevel,
prelude::{LdtkEntity, LdtkIntCell},
resources::{LevelSelection, LevelSpawnBehavior},
utils::ldtk_grid_coords_to_grid_coords,
};
use bevy_ecs_tilemap::tiles::{TileBundle, TilePos};
/// [Component] added to any `IntGrid` tile by default.
///
/// When loading levels, you can flesh out `IntGrid` entities in your own system by querying for
/// `Added<IntGridCell>`.
/// Or, you can hook into the entity's spawning process using [LdtkIntCell].
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct IntGridCell {
pub value: i32,
}
/// [Component] that determines the desired levels to be loaded for an [LdtkWorldBundle].
///
/// There is an abstraction for this in the form of the [LevelSelection] resource.
/// This component does not respond to the
/// [LevelSpawnBehavior::UseWorldTranslation::load_level_neighbors] option at all, while the
/// [LevelSelection] does.
/// If a [LevelSelection] is inserted, the plugin will update this component based off its value.
/// If not, [LevelSet] allows you to have more direct control over the levels you spawn.
///
/// Changes to this component are idempotent, so levels won't be respawned greedily.
#[derive(Clone, Eq, PartialEq, Debug, Default, Component)]
pub struct LevelSet {
pub iids: HashSet<String>,
}
impl LevelSet {
pub fn from_iid<T: Into<String>>(iid: T) -> Self {
let mut iids = HashSet::default();
iids.insert(iid.into());
Self { iids }
}
}
/// [Component] that indicates that an ldtk entity should be a child of the world, not the level.
///
/// By default, [LdtkEntity]s are children of the level they spawn in.
/// This can be a problem if that entity is supposed to travel across multiple levels, since they
/// will despawn the moment the level they were born in despawns.
///
/// This component makes them children of the [LdtkWorldBundle] (after one update),
/// so they can traverse levels without despawning.
/// Furthermore, this component prevents respawns of the same entity if the level they were born in
/// despawns/respawns.
/// For this purpose, it uses the `iid` stored in this component to uniquely identify ldtk
/// entities.
///
/// Implements [LdtkEntity], and can be added to an [LdtkEntity] bundle with the `#[worldly]` field
/// attribute. See [LdtkEntity#worldly] for more details.
#[derive(Clone, Eq, PartialEq, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct Worldly {
pub entity_iid: String,
}
impl Worldly {
/// Creates a [Worldly] from the entity information available to the
/// [LdtkEntity::bundle_entity] method.
///
/// Used for the `#[worldly]` attribute macro for `#[derive(LdtkEntity)]`.
/// See [LdtkEntity#worldly] for more info.
pub fn from_entity_info(entity_instance: &EntityInstance) -> Worldly {
Worldly {
entity_iid: entity_instance.iid.clone(),
}
}
}
/// [Component] that stores grid-based coordinate information.
///
/// For Tile, AutoTile, and IntGrid layers, all tiles have this component by default.
///
/// Can be added to an [LdtkEntity] bundle with the `#[grid_coords]` attribute.
/// Then, it will be spawned with the initial grid-based position of the entity in LDtk.
/// See [LdtkEntity#grid_coords] for attribute macro usage.
///
/// Note that the plugin will not automatically update the entity's [Transform] when this component
/// is updated, nor visa versa.
/// This is left up to the user since there are plenty of scenarios where this behavior needs to be
/// custom.
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct GridCoords {
pub x: i32,
pub y: i32,
}
impl From<IVec2> for GridCoords {
fn from(i_vec_2: IVec2) -> Self {
GridCoords {
x: i_vec_2.x,
y: i_vec_2.y,
}
}
}
impl From<GridCoords> for IVec2 {
fn from(grid_coords: GridCoords) -> Self {
IVec2::new(grid_coords.x, grid_coords.y)
}
}
impl From<TilePos> for GridCoords {
fn from(tile_pos: TilePos) -> Self {
GridCoords {
x: tile_pos.x as i32,
y: tile_pos.y as i32,
}
}
}
impl From<GridCoords> for TilePos {
fn from(grid_coords: GridCoords) -> Self {
TilePos::new(grid_coords.x as u32, grid_coords.y as u32)
}
}
impl Add<GridCoords> for GridCoords {
type Output = GridCoords;
fn add(self, rhs: GridCoords) -> Self::Output {
GridCoords {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl AddAssign<GridCoords> for GridCoords {
fn add_assign(&mut self, rhs: GridCoords) {
self.x += rhs.x;
self.y += rhs.y;
}
}
impl Sub<GridCoords> for GridCoords {
type Output = GridCoords;
fn sub(self, rhs: GridCoords) -> Self::Output {
GridCoords {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
impl SubAssign<GridCoords> for GridCoords {
fn sub_assign(&mut self, rhs: GridCoords) {
self.x -= rhs.x;
self.y -= rhs.y;
}
}
impl Mul<GridCoords> for GridCoords {
type Output = GridCoords;
fn mul(self, rhs: GridCoords) -> Self::Output {
GridCoords {
x: self.x * rhs.x,
y: self.y * rhs.y,
}
}
}
impl MulAssign<GridCoords> for GridCoords {
fn mul_assign(&mut self, rhs: GridCoords) {
self.x *= rhs.x;
self.y *= rhs.y;
}
}
impl GridCoords {
pub fn new(x: i32, y: i32) -> GridCoords {
GridCoords { x, y }
}
/// Creates a [GridCoords] from the entity information available to the
/// [LdtkEntity::bundle_entity] method.
///
/// Used for the `#[grid_coords]` attribute macro for `#[derive(LdtkEntity)]`.
/// See [LdtkEntity#grid_coords] for more info.
pub fn from_entity_info(
entity_instance: &EntityInstance,
layer_instance: &LayerInstance,
) -> GridCoords {
ldtk_grid_coords_to_grid_coords(entity_instance.grid, layer_instance.c_hei)
}
}
/// [Component] for storing user-defined custom data for a paticular tile in an LDtk tileset
/// definition.
///
/// Automatically inserted on any tiles with metadata.
#[derive(Clone, Eq, PartialEq, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct TileMetadata {
pub data: String,
}
/// [Component] for storing user-defined, enum-based tags for a particular tile in an LDtk tileset
/// definition.
///
/// Automatically inserted on any tiles with enum tags.
#[derive(Clone, Eq, PartialEq, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct TileEnumTags {
pub tags: Vec<String>,
pub source_enum_uid: Option<i32>,
}
/// [Component] for storing some LDtk layer information on layer entities.
///
/// Based on [LayerInstance], but without the fields with tile and entity information.
///
/// Automatically inserted for IntGrid, AutoTile, and Tile layers.
#[derive(Clone, PartialEq, Debug, Default, Component, Reflect)]
#[reflect(Component)]
pub struct LayerMetadata {
/// Grid-based height
pub c_hei: i32,
/// Grid-based width
pub c_wid: i32,
/// Grid size
pub grid_size: i32,
/// Layer definition identifier
pub identifier: String,
/// Layer opacity as Float [0-1]
pub opacity: f32,
/// Total layer X pixel offset, including both instance and definition offsets.
pub px_total_offset_x: i32,
/// Total layer Y pixel offset, including both instance and definition offsets.
pub px_total_offset_y: i32,
/// The definition UID of corresponding Tileset, if any.
pub tileset_def_uid: Option<i32>,
/// The relative path to corresponding Tileset, if any.
pub tileset_rel_path: Option<String>,
/// Layer type (possible values: IntGrid, Entities, Tiles or AutoLayer)
pub layer_instance_type: Type,
/// Unique layer instance identifier
pub iid: String,
/// Reference the Layer definition UID
pub layer_def_uid: i32,
/// Reference to the UID of the level containing this layer instance
pub level_id: i32,
/// An Array containing the UIDs of optional rules that were enabled in this specific layer
/// instance.
pub optional_rules: Vec<i32>,
/// This layer can use another tileset by overriding the tileset UID here.
pub override_tileset_uid: Option<i32>,
/// X offset in pixels to render this layer, usually 0 (IMPORTANT: this should be added to
/// the `LayerDef` optional offset, see `__pxTotalOffsetX`)
pub px_offset_x: i32,
/// Y offset in pixels to render this layer, usually 0 (IMPORTANT: this should be added to
/// the `LayerDef` optional offset, see `__pxTotalOffsetY`)
pub px_offset_y: i32,
/// Random seed used for Auto-Layers rendering
pub seed: i32,
/// Layer instance visibility
pub visible: bool,
}
impl From<&LayerInstance> for LayerMetadata {
fn from(instance: &LayerInstance) -> Self {
LayerMetadata {
c_hei: instance.c_hei,
c_wid: instance.c_wid,
grid_size: instance.grid_size,
identifier: instance.identifier.clone(),
opacity: instance.opacity,
px_total_offset_x: instance.px_total_offset_x,
px_total_offset_y: instance.px_total_offset_y,
tileset_def_uid: instance.tileset_def_uid,
tileset_rel_path: instance.tileset_rel_path.clone(),
layer_instance_type: instance.layer_instance_type.clone(),
iid: instance.iid.clone(),
layer_def_uid: instance.layer_def_uid,
level_id: instance.level_id,
optional_rules: instance.optional_rules.clone(),
override_tileset_uid: instance.override_tileset_uid,
px_offset_x: instance.px_offset_x,
px_offset_y: instance.px_offset_y,
seed: instance.seed,
visible: instance.visible,
}
}
}
/// [Component] that indicates that an LDtk level or world should respawn.
///
/// Inserting this component on an entity with either `Handle<LdtkAsset>` or `Handle<LdtkLevel>`
/// components will cause it to respawn.
/// This can be used to implement a simple level-restart feature.
/// Internally, this is used to support the entire level spawning process
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Hash, Component, Reflect)]
#[reflect(Component)]
pub struct Respawn;
#[derive(Copy, Clone, Debug, Default, Bundle)]
pub(crate) struct TileGridBundle {
#[bundle]
pub tile_bundle: TileBundle,
pub grid_coords: GridCoords,
}
#[derive(Clone, Default, Bundle)]
pub(crate) struct IntGridCellBundle {
pub int_grid_cell: IntGridCell,
}
#[derive(Clone, Bundle, Default)]
pub(crate) struct EntityInstanceBundle {
pub entity_instance: EntityInstance,
}
/// [Bundle] for spawning LDtk worlds and their levels. The main bundle for using this plugin.
///
/// After the ldtk file is done loading, the levels you've chosen with [LevelSelection] or
/// [LevelSet] will begin to spawn.
/// Each level is its own entity, with the [LdtkWorldBundle] as its parent.
/// Each level has a `Handle<LdtkLevel>` component.
///
/// All non-Entity layers (IntGrid, Tile, and AutoTile) will also spawn as their own entities.
/// Each layer's parent will be the level entity.
/// Each layer will have a [LayerMetadata] component, and are [bevy_ecs_tilemap::TilemapBundle]s.
/// Each tile in these layers will have the layer entity as its parent.
///
/// For Entity layers, all LDtk entities in the level are spawned as children to the level entity,
/// unless marked by a [Worldly] component.
#[derive(Clone, Default, Bundle)]
pub struct LdtkWorldBundle {
pub ldtk_handle: Handle<crate::assets::LdtkAsset>,
pub level_set: LevelSet,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub computed_visibility: ComputedVisibility,
}