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
use crate::;
use ;
use ;
/// [LdtkEntityAppExt]: super::LdtkEntityAppExt
/// [Bundle]: bevy::prelude::Bundle
/// [App]: bevy::prelude::App
/// [Component]: bevy::prelude::Component
/// [Sprite]: bevy::prelude::Sprite
/// [TextureAtlasLayout]: bevy::prelude::TextureAtlasLayout
///
/// Provides a constructor which can be used for spawning entities from an LDtk file.
///
/// After implementing this trait on a [Bundle], you can register it to spawn automatically for a
/// given identifier via [LdtkEntityAppExt] functions on your [App].
///
/// For common use cases, you'll want to use derive-macro `#[derive(LdtkEntity)]`, but you can also
/// provide a custom implementation.
///
/// You can also implement this trait on non-[Bundle] types, but only [Bundle]s can be registered.
///
/// If there is an entity in the LDtk file that is NOT registered, an entity will be spawned with
/// an [EntityInstance] component, allowing you to flesh it out in your own system.
///
/// *Derive macro requires the "derive" feature, which is enabled by default*
///
/// ## Derive macro usage
/// Using `#[derive(LdtkEntity)]` on a [Bundle] struct will allow the type to be registered to the
/// [App] via [LdtkEntityAppExt] functions:
/// ```no_run
/// use bevy::prelude::*;
/// use bevy_ecs_ldtk::prelude::*;
///
/// fn main() {
/// App::empty()
/// .add_plugins(LdtkPlugin)
/// .register_ldtk_entity::<MyBundle>("my_entity_identifier")
/// // add other systems, plugins, resources...
/// .run();
/// }
///
/// # #[derive(Component, Default)]
/// # struct ComponentA;
/// # #[derive(Component, Default)]
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
/// c: ComponentC,
/// }
/// ```
/// Now, when loading your ldtk file, any entities with the entity identifier
/// "my_entity_identifier" will be spawned as `MyBundle`s.
///
/// By default, each component or nested bundle in the bundle will be consumed from bundle's
/// [Default] implementation, which means that deriving (or implementing manually) [Default]
/// is required (unless all fields are overriden, see below).
/// However, this behavior can be overridden with some field attribute macros...
///
/// ### `#[sprite...]`
/// Indicates that a [Sprite] field should be created with an actual material/image.
/// There are two forms for this attribute:
/// - `#[sprite("path/to/asset.png")]` will create the field using the image at the provided
/// path in the assets folder.
/// - `#[sprite]` will create the field using its Editor Visual image in LDtk, if it has one.
///
/// Note that if your editor visual is part of a tilemap, you should use `#[sprite_sheet]` instead.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Sellable;
/// # #[derive(Component, Default)]
/// # struct PlayerComponent;
/// # #[derive(Component, Default)]
/// # struct Health;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Gem {
/// #[sprite("textures/gem.png")]
/// sprite: Sprite,
/// sellable: Sellable,
/// }
///
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Player {
/// player: PlayerComponent,
/// health: Health,
/// #[sprite] // Uses the Editor Visual sprite in LDtk
/// sprite: Sprite,
/// }
/// ```
///
/// ### `#[sprite_sheet...]`
/// Similar to `#[sprite...]`, indicates that a [Sprite] field should be created
/// with an actual material/image.
/// There are two forms for this attribute:
/// - `#[sprite_sheet("path/to/asset.png", tile_width, tile_height, columns, rows, padding,
/// offset, index)]` will create the field using all of the information provided.
/// Similar to using [TextureAtlasLayout::from_grid()].
/// - `#[sprite_sheet]` will create the field using information from the LDtk Editor visual,
/// if it has one.
/// - `#[sprite_sheet(no_grid)]` will create the field using information from the LDtk
/// Editor visual, if it has one, but without using a grid. Instead a single texture will be used.
/// This may be useful if the LDtk entity's visual uses a rectangle of tiles from its tileset,
/// but will prevent using the generated [TextureAtlasLayout] for animation purposes.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Damage;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Sword {
/// #[sprite_sheet("weapons.png", 32, 32, 4, 5, 5, 1, 17)]
/// sprite_sheet: Sprite,
/// damage: Damage,
/// }
///
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Dagger {
/// damage: Damage,
/// bleed_damage: BleedDamage,
/// #[sprite_sheet]
/// sprite_sheet: Sprite,
/// }
/// ```
///
/// ### `#[worldly]`
/// Indicates that a component is [Worldly].
///
/// [Worldly] entities don't despawn when their birth level despawns, and they don't respawn when
/// their birth level respawns.
/// For a more detailed explanation, please see the
/// [*Worldly Entities*](https://trouv.github.io/bevy_ecs_ldtk/v0.14.0/explanation/anatomy-of-the-world.html#worldly-entities) <!-- x-release-please-version -->
/// section of the `bevy_ecs_ldtk` book.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Player;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct PlayerBundle {
/// player: Player,
/// #[sprite_sheet]
/// sprite_sheet: Sprite,
/// #[worldly]
/// worldly: Worldly,
/// }
/// ```
///
/// ### `#[grid_coords]`
/// Indicates that a [GridCoords] component should be created with the entity's initial grid-based
/// position in LDtk.
///
/// See the [GridCoords] documentation for more details about this component.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Block;
/// # #[derive(Component, Default)]
/// # struct Movable;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct BlockBundle {
/// block: Block,
/// movable: Movable,
/// #[sprite_sheet]
/// sprite_sheet: Sprite,
/// #[grid_coords]
/// grid_coords: GridCoords,
/// }
/// ```
///
/// ### `#[ldtk_entity]`
/// Indicates that a component or bundle that implements [LdtkEntity] should be created with
/// [LdtkEntity::bundle_entity], allowing for nested [LdtkEntity]s.
///
/// Note: the [LdtkEntity] field decorated with this attribute doesn't have to be a [Bundle].
/// This can be useful if a [Component]'s construction requires the additional access to the world
/// provided by [LdtkEntity::bundle_entity].
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Damage;
/// # #[derive(Component, Default)]
/// # struct BleedDamage;
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Weapon {
/// damage: Damage,
/// #[sprite]
/// sprite: Sprite,
/// }
///
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct Dagger {
/// #[ldtk_entity]
/// weapon_bundle: Weapon,
/// bleed_damage: BleedDamage,
/// }
/// ```
///
/// ### `#[from_entity_instance]`
/// Indicates that a component or bundle that implements [From<&EntityInstance>] should be created
/// using that conversion.
/// This allows for more modular and custom component construction, and for different structs that
/// contain the same component to have different constructions of that component, without having to
/// `impl LdtkEntity` for both of them.
/// It also allows you to have an [EntityInstance] field, since `EntityInstance` implements
/// `From<&EntityInstance>`.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Sellable { value: i32 }
/// impl From<&EntityInstance> for Sellable {
/// fn from(entity_instance: &EntityInstance) -> Sellable {
/// let sell_value = match entity_instance.identifier.as_str() {
/// "gem" => 1000,
/// "nickel" => 5,
/// _ => 10,
/// };
///
/// Sellable {
/// value: sell_value,
/// }
/// }
/// }
///
/// #[derive(Bundle, LdtkEntity, Default)]
/// pub struct NickelBundle {
/// #[sprite]
/// sprite: Sprite,
/// #[from_entity_instance]
/// sellable: Sellable,
/// #[from_entity_instance]
/// entity_instance: EntityInstance,
/// }
/// ```
///
/// ### `#[with(...)]`
///
/// Indicates that this component or bundle should be initialized with the given
/// function.
///
/// Note: The given function should have signature `fn (entity: &EntityInstance) -> T`
/// where `T` is the field type. The function should also be accessible in the scope.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # pub struct Player;
/// # #[derive(Component, Default, Clone)]
/// # pub struct Money(f32);
/// #[derive(Clone, Default, Bundle)]
/// pub struct InventoryBundle {
/// pub money: Money,
/// }
///
/// #[derive(Bundle, Default, LdtkEntity)]
/// pub struct PlayerBundle {
/// player: Player,
/// #[with(player_initial_inventory)]
/// collider: InventoryBundle,
/// }
///
/// fn player_initial_inventory(_: &EntityInstance) -> InventoryBundle {
/// InventoryBundle {
/// money: Money(4.0)
/// }
/// }
/// ```
///
/// ### `#[default]`
///
/// Indicates that this component or bundle should be initialized using
/// [`Default::default`].
/// This can be useful when implementing `Default` for the whole `LdtkEntity` is
/// not easily possible, because some of the fields do not implement `Default`.
///
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # mod other_crate {
/// # #[derive(bevy::prelude::Component)]
/// # pub struct ForeignComponentWithNoDefault;
/// # }
/// # fn custom_constructor(_: &EntityInstance) -> ForeignComponentWithNoDefault { todo!(); }
/// # #[derive(Component, Default)]
/// # struct Damage;
/// use other_crate::ForeignComponentWithNoDefault;
///
/// #[derive(Bundle, LdtkEntity)]
/// pub struct MyBundle {
/// #[default]
/// damage: Damage,
/// #[with(custom_constructor)]
/// foreign: ForeignComponentWithNoDefault,
/// }
/// ```
/// Used by [LdtkEntityAppExt](super::LdtkEntityAppExt) to associate Ldtk entity identifiers with [LdtkEntity]s.
pub type LdtkEntityMap = ;