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
use crate::;
use ;
use ;
/// [LdtkIntCellAppExt]: super::LdtkIntCellAppExt
/// [Bundle]: bevy::prelude::Bundle
/// [App]: bevy::prelude::App
/// [Component]: bevy::prelude::Component
///
/// Provides a constructor which can be used for spawning additional components on IntGrid tiles.
///
/// After implementing this trait on a [Bundle], you can register it to spawn automatically for a
/// given int grid value via [LdtkIntCellAppExt] on your [App].
///
/// For common use cases, you'll want to use derive-macro `#[derive(LdtkIntCell)]`, 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 IntGrid tile in the LDtk file whose value is NOT registered, an entity will be
/// spawned with an [IntGridCell] 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(LdtkIntCell)]` on a [Bundle] struct will allow the type to be registered to the
/// [App] via [LdtkIntCellAppExt] functions:
/// ```no_run
/// use bevy::prelude::*;
/// use bevy_ecs_ldtk::prelude::*;
///
/// fn main() {
/// App::empty()
/// .add_plugins(LdtkPlugin)
/// .register_ldtk_int_cell::<MyBundle>(1)
/// // add other systems, plugins, resources...
/// .run();
/// }
///
/// # #[derive(Component, Default)]
/// # struct ComponentA;
/// # #[derive(Component, Default)]
/// # struct ComponentB;
/// # #[derive(Component, Default)]
/// # struct ComponentC;
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct MyBundle {
/// a: ComponentA,
/// b: ComponentB,
/// c: ComponentC,
/// }
/// ```
/// Now, when loading your ldtk file, any IntGrid tiles with the value `1` will be spawned with as
/// tiles with `MyBundle` inserted.
///
/// 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 overriden with some field attribute macros...
///
/// ### `#[ldtk_int_cell]`
/// Indicates that a component or bundle that implements [LdtkIntCell] should be created with
/// [LdtkIntCell::bundle_int_cell], allowing for nested [LdtkIntCell]s.
///
/// Note: the [LdtkIntCell] 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 [LdtkIntCell::bundle_int_cell].
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct RigidBody;
/// # #[derive(Component, Default)]
/// # struct Damage;
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Wall {
/// rigid_body: RigidBody,
/// }
///
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct DestructibleWall {
/// #[ldtk_int_cell]
/// wall: Wall,
/// damage: Damage,
/// }
/// ```
///
/// ### `#[from_int_grid_cell]`
/// Indicates that a component or bundle that implements [`From<IntGridCell>`] 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 LdtkIntCell` for both of them.
/// It also allows you to have an [IntGridCell] field, since all types `T` implement `From<T>`.
/// ```
/// # use bevy::prelude::*;
/// # use bevy_ecs_ldtk::prelude::*;
/// # #[derive(Component, Default)]
/// # struct Fluid { viscosity: i32 }
/// # #[derive(Component, Default)]
/// # struct Damage;
/// impl From<IntGridCell> for Fluid {
/// fn from(int_grid_cell: IntGridCell) -> Fluid {
/// let viscosity = match int_grid_cell.value {
/// 1 => 5,
/// 2 => 20,
/// _ => 0,
/// };
///
/// Fluid {
/// viscosity,
/// }
/// }
/// }
///
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Lava {
/// #[from_int_grid_cell]
/// fluid: Fluid,
/// #[from_int_grid_cell]
/// int_grid_cell: IntGridCell,
/// damage: Damage,
/// }
/// ```
///
/// ### `#[with(...)]`
///
/// Indicates that this component or bundle should be initialized with the given
/// function.
///
/// Note: The given function should have signature `fn (int_grid_cell: IntGridCell) -> 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)]
/// # struct Fluid { viscosity: i32 }
/// # #[derive(Component, Default)]
/// # struct Damage;
/// fn initial_fluid(_: IntGridCell) -> Fluid {
/// Fluid {
/// viscosity: 20
/// }
/// }
///
/// #[derive(Bundle, LdtkIntCell, Default)]
/// pub struct Lava {
/// #[with(initial_fluid)]
/// fluid: Fluid,
/// damage: Damage,
/// }
/// ```
///
/// ### `#[default]`
///
/// Indicates that this component or bundle should be initialized using
/// [`Default::default`].
/// This can be useful when implementing `Default` for the whole `IntGridCell` 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(_: IntGridCell) -> ForeignComponentWithNoDefault { todo!(); }
/// # #[derive(Component, Default)]
/// # struct Damage;
/// use other_crate::ForeignComponentWithNoDefault;
///
/// #[derive(Bundle, LdtkIntCell)]
/// pub struct MyBundle {
/// #[default]
/// damage: Damage,
/// #[with(custom_constructor)]
/// foreign: ForeignComponentWithNoDefault,
/// }
/// ```
/// Used by [LdtkIntCellAppExt](super::LdtkIntCellAppExt) to associate Ldtk IntGrid values with [LdtkIntCell]s.
pub type LdtkIntCellMap = ;