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
//! [`Terrain`]: what the movement asks about the world around the character. Your game
//! implements it for its own level (a tilemap, a collision grid, anything): write
//! [`Terrain::solid`] and say what it builds on with [`terrain_layer!`](crate::terrain_layer)
//! (`terrain_layer!()` for nothing, `terrain_layer!(level)` for a field it wraps).
//!
//! Positions and sizes are in the controller's units: the body is 6x12 unless
//! [`super::Tuning::size`] says otherwise, the tuning is made for 8-unit tiles. If your
//! tiles are 16 pixels, give the [`crate::Builder`] a scale of 2
//! ([`crate::Builder::scale`]) and answer in your pixels, or use [`super::Level`], which
//! already speaks units.
//!
//! Hazards, pickups and checkpoints aren't the terrain's business: test the character's
//! hitboxes against them after each update (`docs/hazards.md` has the recipe). Dash
//! refills are the one exception, because the movement decides when one is used (only
//! when a dash has been spent): [`Terrain::refill`].
//!
//! To start the character standing on your ground, give it the feet's position:
//! `Platformer::at(Spot::Feet(x, floor_y), preset)` (see [`super::Spot`]).
//!
//! The smallest terrain is a closure (it builds on nothing):
//!
//! ```
//! use game_toolkit_platformer::sim::{Rect, Terrain};
//!
//! // Solid below y = 100, nothing else.
//! let mut floor = |r: Rect| r.bottom() > 100;
//! assert!(floor.solid(Rect { x: 0, y: 95, w: 6, h: 12 }));
//! ```
//!
//! # Building on another terrain
//!
//! To add state of your own to a terrain (doors, switches, moving platforms, boxes),
//! wrap it and name it as the [`Terrain::base`] with [`terrain_layer!`](crate::terrain_layer):
//! every method you don't write forwards to it, those added later included, and
//! [`find_mut`](trait.Terrain.html#method.find_mut) finds your type. Write only what
//! changes; your own state moves in [`Terrain::tick`], which runs on every layer by
//! itself, so the base's refills come back whatever you write:
//!
//! ```
//! use game_toolkit_platformer::sim::{Level, Rect, Terrain};
//!
//! /// The demo level with a door that opens and shuts every second.
//! struct Stage {
//! level: Level,
//! door: Rect,
//! clock: f32,
//! }
//!
//! impl Terrain for Stage {
//! fn solid(&self, r: Rect) -> bool {
//! let shut = self.clock % 2.0 < 1.0;
//! self.level.solid(r) || (shut && self.door.intersects(r))
//! }
//! fn tick(&mut self, dt: f32, _bodies: &[Rect]) {
//! self.clock += dt; // the level's refills come back on their own
//! }
//! // Platforms, refills, surfaces, falling out: the level's.
//! game_toolkit_platformer::terrain_layer!(level);
//! }
//! # let mut s = Stage { level: Level::demo(), door: Rect { x: 0, y: 0, w: 8, h: 8 }, clock: 0.0 };
//! # assert!(s.solid(Rect { x: 1, y: 1, w: 2, h: 2 }) && !s.fell_out(Rect { x: 9, y: 9, w: 2, h: 2 }));
//! # let t: &mut dyn Terrain = &mut s;
//! # assert!(t.find_mut::<Stage>().is_some() && t.find_mut::<Level>().is_some());
//! ```
//!
//! A terrain that borrows (a view of your game's map, `View<'a>`) writes
//! `terrain_layer!(borrowed)` or `terrain_layer!(borrowed field)`: the plain forms make it
//! findable by [`find_mut`](trait.Terrain.html#method.find_mut), which needs `'static`, and
//! fail with "lifetime may not live long enough" otherwise. See
//! [`terrain_layer!`](crate::terrain_layer).
//!
//! [`Terrain::base`] has no default, so a wrapper can't forget it: without it, the level's
//! platforms, refills, surfaces and pits would silently be gone. This doesn't compile:
//!
//! ```compile_fail,E0046
//! use game_toolkit_platformer::sim::{Level, Rect, Terrain};
//!
//! struct Doors { level: Level }
//! impl Terrain for Doors {
//! fn solid(&self, r: Rect) -> bool { self.level.solid(r) }
//! // missing: game_toolkit_platformer::terrain_layer!(level);
//! }
//! ```
use Any;
pub use BaseRef;
use ;
/// The world around the character, as the movement sees it.
///
/// Write [`Terrain::solid`] and [`Terrain::base`] (with [`terrain_layer!`](crate::terrain_layer));
/// every other method has a default: forward to the base if there is one, otherwise there
/// is nothing of the kind (no platforms, no refills, ...).
/// Whether `t`'s [`Terrain::base`] and [`Terrain::base_mut`] are the same terrain.
Sized>
/// Writes the boilerplate of a [`Terrain`] layer inside its `impl Terrain` block:
/// [`Terrain::base`] and [`Terrain::base_mut`] returning the field named (a name or a
/// tuple index; left out: no base), and [`Terrain::as_any_mut`] and [`Terrain::as_any`] returning the
/// terrain itself, so [`find_mut`](trait.Terrain.html#method.find_mut) finds it.
///
/// A terrain that borrows (has a lifetime) can't be found, since `find_mut` needs
/// `'static`; the plain forms then fail with "lifetime may not live long enough". Write
/// `terrain_layer!(borrowed)` (no base) or `terrain_layer!(borrowed field)` instead; its
/// base is still found. In every form the base field may be the terrain itself, a `&mut`
/// to one, or a `Box<dyn Terrain>`:
///
/// ```
/// use game_toolkit_platformer::sim::{Level, Rect, Terrain};
///
/// struct Map { floor: i32 }
/// struct View<'a> { map: &'a Map }
/// impl Terrain for View<'_> {
/// fn solid(&self, r: Rect) -> bool { r.bottom() > self.map.floor }
/// game_toolkit_platformer::terrain_layer!(borrowed);
/// }
///
/// struct Doors<'a> { level: &'a mut Level, shut: bool }
/// impl Terrain for Doors<'_> {
/// fn solid(&self, r: Rect) -> bool { self.level.solid(r) || (self.shut && r.x < 0) }
/// game_toolkit_platformer::terrain_layer!(borrowed level);
/// }
///
/// struct Lit<'a> { level: Level, map: &'a Map }
/// impl Terrain for Lit<'_> {
/// fn solid(&self, r: Rect) -> bool { self.level.solid(r) || r.bottom() > self.map.floor }
/// game_toolkit_platformer::terrain_layer!(borrowed level);
/// }
///
/// struct Any { inner: Box<dyn Terrain> }
/// impl Terrain for Any {
/// fn solid(&self, r: Rect) -> bool { self.inner.solid(r) }
/// game_toolkit_platformer::terrain_layer!(inner);
/// }
/// # let map = Map { floor: 100 };
/// # let mut lit = Lit { level: Level::demo(), map: &map };
/// # assert!((&mut lit as &mut dyn Terrain).find_mut::<Level>().is_some());
/// # let mut any = Any { inner: Box::new(Level::demo()) };
/// # assert!((&mut any as &mut dyn Terrain).find_mut::<Level>().is_some());
/// # assert!(View { map: &map }.solid(Rect { x: 0, y: 95, w: 6, h: 12 }));
/// # let mut level = Level::demo();
/// # let mut d = Doors { level: &mut level, shut: true };
/// # assert!(!d.fell_out(Rect { x: 9, y: 9, w: 2, h: 2 }));
/// # let t: &mut dyn Terrain = &mut d;
/// # assert!(t.find_mut::<Level>().is_some());
/// ```
///
/// ```
/// use game_toolkit_platformer::sim::{Level, Rect, Terrain};
///
/// struct Doors { level: Level }
/// impl Terrain for Doors {
/// fn solid(&self, r: Rect) -> bool { self.level.solid(r) }
/// game_toolkit_platformer::terrain_layer!(level);
/// }
///
/// struct Room;
/// impl Terrain for Room {
/// fn solid(&self, r: Rect) -> bool { r.bottom() > 100 }
/// game_toolkit_platformer::terrain_layer!(); // no base; still found by find_mut
/// }
/// # let t: &mut dyn Terrain = &mut Room;
/// # assert!(t.find_mut::<Room>().is_some());
/// ```
/// Any `Fn(Rect) -> bool` is a terrain of solid ground, built on nothing.