rusting_engine 2.1.0

Vulkan 3D game engine with GPU-accelerated physics for massive physics-heavy scenes
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
//! Named gameplay mechanics that `rusting recipe apply` writes into a
//! project: a Rust source file, the scene changes it needs, and a scenario
//! that proves it works.

use serde_json::{json, Value};

use crate::runtime::{PlatformerController, PlayerController, SceneEntity};

/// Patch operations and a scenario, or why the player cannot take the
/// recipe.
pub type Built = Result<(Vec<Value>, Value), String>;

/// One recipe. The scene changes and scenario are built from the object
/// named `Player` so the scenario passes in any template.
pub struct Recipe {
    /// `snake_case` name; also the source file and scenario name.
    pub name: &'static str,
    /// One sentence on what the mechanic does.
    pub summary: &'static str,
    /// Contents of `src/<name>.rs`; `None` when the scene change is the
    /// whole mechanic.
    pub source: Option<&'static str>,
    /// The call `update` makes once a frame, when there is a source.
    pub call: Option<&'static str>,
    /// Scene patch operations and the scenario, given the object named
    /// `Player`, or why that player cannot take the recipe.
    pub build: fn(&SceneEntity) -> Built,
}

/// Every recipe, in `recipe list` order.
pub const RECIPES: &[Recipe] = &[Recipe {
    name: "checkpoints",
    summary: "Touching an object in class `checkpoint` stores it as the respawn point; falling below y = -10 puts the player back there. Adds `Checkpoint Respawn` at the player and a sensor `Checkpoint 1` four units along +X.",
    source: Some(include_str!("recipes/checkpoints.rs")),
    call: Some("checkpoints::checkpoints(scene);"),
    build: checkpoint_scene,
}, Recipe {
    name: "health",
    summary: "The counter `health` starts at 3; touching an object in class `hazard` costs one point, flashes the player and makes it safe for a second, and at 0 the round restarts. Adds a sensor `Hazard 1` 2.5 units along +X from the player.",
    source: Some(include_str!("recipes/health.rs")),
    call: Some("health::health(scene);"),
    build: health_scene,
}, Recipe {
    name: "double_jump",
    summary: "Sets `air_jumps` to 1 on the player's controller (`rusting.player_controller` or `rusting.platformer_controller`), so a second jump press in the air jumps again; landing refills it. No source file.",
    source: None,
    call: None,
    build: double_jump_scene,
}, Recipe {
    name: "inventory",
    summary: "Touching an object in class `item` picks it up into the counter `inventory_<kind>` (`Key 1` adds to `inventory_key`); touching one in class `locked` while holding a key spends the key and removes it. `inventory::count` and `inventory::take` read and spend items. Adds sensors `Key 1` 2.5 units and `Door 1` 5 units along +X from the player; set the door's `sensor` to false to make it block.",
    source: Some(include_str!("recipes/inventory.rs")),
    call: Some("inventory::inventory(scene);"),
    build: inventory_scene,
}, Recipe {
    name: "day_timer",
    summary: "The counter `day` starts at 1 and goes up every 30 seconds (`DAY_SECONDS`); `day_seconds_left` counts down to the next day. Adds the HUD label `Day Clock` at the top of the screen showing both.",
    source: Some(include_str!("recipes/day_timer.rs")),
    call: Some("day_timer::day_timer(scene, time);"),
    build: day_timer_scene,
}, Recipe {
    name: "pause_menu",
    summary: "The action `menu` (Escape, gamepad Start) pauses the game and shows a HUD menu; pressing it again or clicking `Pause Resume` carries on, and `Pause Quit` closes the game. Adds the input action `Pause Action` and the hidden HUD objects `Pause Title`, `Pause Resume` and `Pause Quit`.",
    source: Some(include_str!("recipes/pause_menu.rs")),
    call: Some("pause_menu::pause_menu(scene);"),
    build: pause_menu_scene,
}, Recipe {
    name: "wave_spawner",
    summary: "When no enemies are left the counter `wave` goes up and 2 x wave copies of the hidden `Wave Enemy` (class `enemy`, kinematic) appear in a row from `Wave Spawn`; game code defeats an enemy by despawning it, and one that falls below y = -10 is removed. `wave_enemies_left` counts the rest. Adds `Wave Spawn` 3 units along +X and 1 up from the player, and the template far below the level.",
    source: Some(include_str!("recipes/wave_spawner.rs")),
    call: Some("wave_spawner::wave_spawner(scene);"),
    build: wave_spawner_scene,
}, Recipe {
    name: "turret",
    summary: "`Turret` shoots the nearest object in class `enemy` within 12 m that it can see, every half second: the shot removes it and adds to the counter `turret_kills`. Adds `Turret` 3 units above the player and red enemies `Target 1`, `Target 2` and `Target Far` 4, 6 and 20 units along +X from it; delete the targets once real enemies exist.",
    source: Some(include_str!("recipes/turret.rs")),
    call: Some("turret::turret(scene);"),
    build: turret_scene,
}, Recipe {
    name: "dash",
    summary: "The action `dash` (Q, gamepad West) sends the player 15 m/s for 0.2 s with no gravity, the way the movement keys point (forward when none is held; a platformer the way it last ran), at most once every 0.6 s; walls still stop it. Uses `GameScene::dash`. Adds the input action `Dash Action`.",
    source: Some(include_str!("recipes/dash.rs")),
    call: Some("dash::dash(scene);"),
    build: dash_scene,
}, Recipe {
    name: "alarm",
    summary: "A state machine with no code: the sensor `Alarm Plate`, 2.5 units along +X from the player, goes from `quiet` to `alarmed` when the `Player` touches it, adds 1 to the counter `alarms`, and goes back to `quiet` 2 seconds later. Edit its `rusting.state` transitions to change the rules. No source file.",
    source: None,
    call: None,
    build: alarm_scene,
}];

/// The recipe called `name`.
#[must_use]
pub fn recipe(name: &str) -> Option<&'static Recipe> {
    RECIPES.iter().find(|recipe| recipe.name == name)
}

/// The player's position, or the origin when it has no transform.
fn position(player: &SceneEntity) -> [f32; 3] {
    player.transform.map_or([0.0; 3], |t| t.position)
}

/// `create` operations for `entities`.
fn creates(entities: Vec<Value>) -> Vec<Value> {
    entities
        .into_iter()
        .map(|entity| json!({"op": "create", "entity": entity}))
        .collect()
}

/// A fixed sensor box called `name` in `class` at `at`.
fn sensor(name: &str, class: &str, at: [f32; 3]) -> Value {
    json!({
        "name": name,
        "classes": [class],
        "transform": {"position": at, "rotation": [0.0, 0.0, 0.0], "scale": [1.0, 1.0, 1.0]},
        "physics_body": {"simulation": "Cpu", "solver": "Simplified", "custom_shader": null},
        "rigid_body": {"kind": "Fixed", "mass": 1.0, "linear_velocity": [0.0, 0.0, 0.0], "angular_velocity": [0.0, 0.0, 0.0], "gravity_scale": 1.0},
        "collider": {"shape": {"Box": {"half_extents": [0.5, 1.0, 0.5]}}, "friction": 0.0, "restitution": 0.0, "sensor": true},
    })
}

/// A lit cube of `color` for `mesh_renderer`.
fn cube(color: [f32; 4]) -> Value {
    json!({
        "mesh": {"BuiltinPrimitive": "Cube"},
        "material": {"Inline": {
            "model": "Pbr", "alpha_mode": "Opaque", "base_color": color,
            "emissive": [0.0, 0.0, 0.0], "metallic": 0.0, "roughness": 0.8,
            "base_color_texture": null, "normal_texture": null,
            "metallic_roughness_texture": null,
            "occlusion_texture": null, "emissive_texture": null
        }},
        "cast_shadows": true, "receive_shadows": true
    })
}

/// A red kinematic box called `name` in class `enemy` at `at`.
fn enemy(name: &str, at: [f32; 3]) -> Value {
    json!({
        "name": name,
        "classes": ["enemy"],
        "transform": {"position": at, "rotation": [0.0, 0.0, 0.0], "scale": [1.0, 1.0, 1.0]},
        "mesh_renderer": cube([0.85, 0.2, 0.2, 1.0]),
        "physics_body": {"simulation": "Cpu", "solver": "Simplified", "custom_shader": null},
        "rigid_body": {"kind": "Kinematic", "mass": 1.0, "linear_velocity": [0.0, 0.0, 0.0], "angular_velocity": [0.0, 0.0, 0.0], "gravity_scale": 1.0},
        "collider": {"shape": {"Box": {"half_extents": [0.5, 0.5, 0.5]}}, "friction": 0.5, "restitution": 0.0, "sensor": false},
    })
}

fn checkpoint_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let checkpoint = [player[0] + 4.0, player[1], player[2]];
    let transform = |at: [f32; 3]| json!({"position": at, "rotation": [0.0, 0.0, 0.0], "scale": [1.0, 1.0, 1.0]});
    let entities = vec![
        json!({"name": "Checkpoint Respawn", "transform": transform(player)}),
        sensor("Checkpoint 1", "checkpoint", checkpoint),
    ];
    let below = [checkpoint[0], -20.0, checkpoint[2]];
    let scenario = json!({
        "name": "checkpoints: touching a checkpoint moves the respawn point, falling returns there",
        "ticks": 20,
        "steps": [
            {"tick": 1, "set": {"entity": "Player", "path": "/transform/position", "value": checkpoint}},
            {"tick": 5, "expect": {"entity": "Checkpoint Respawn", "path": "/transform/position", "equals": checkpoint, "tolerance": 0.01}},
            {"tick": 10, "set": {"entity": "Player", "path": "/transform/position", "value": below}},
            {"tick": 13, "until": 20, "expect": {"entity": "Player", "path": "/transform/position/1", "greater_than": -10.0}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn health_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let hazard = [player[0] + 2.5, player[1], player[2]];
    let entities = vec![sensor("Hazard 1", "hazard", hazard)];
    // One hit a second while the player stands in the hazard: 3, 2, 1, then
    // the restart refills health and puts the player back at the start.
    let scenario = json!({
        "name": "health: a hazard costs one point a second and 0 health restarts the round",
        "ticks": 160,
        "steps": [
            {"tick": 1, "set": {"entity": "Player", "path": "/transform/position", "value": hazard}},
            {"tick": 30, "expect": {"counter": "health", "equals": 2}},
            {"tick": 90, "expect": {"counter": "health", "equals": 1}},
            {"tick": 160, "expect": {"counter": "health", "equals": 3}},
            {"tick": 160, "expect": {"entity": "Player", "path": "/transform/position/0", "less_than": hazard[0] - 1.5}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn double_jump_scene(player: &SceneEntity) -> Built {
    let component = |name: &str| player.components.get(name);
    // Jump speed and gravity give the height of one jump and when it peaks.
    let (key, jump_speed, gravity) = if let Some(text) =
        component("rusting.player_controller")
    {
        let c: PlayerController =
            serde_json::from_str(text).map_err(|e| e.to_string())?;
        ("rusting.player_controller", c.jump_speed, c.gravity)
    } else if let Some(text) = component("rusting.platformer_controller") {
        let c: PlatformerController =
            serde_json::from_str(text).map_err(|e| e.to_string())?;
        ("rusting.platformer_controller", c.jump_speed, c.gravity)
    } else {
        return Err("`Player` has no `rusting.player_controller` or `rusting.platformer_controller`".into());
    };
    if jump_speed <= 0.0 || gravity <= 0.0 {
        return Err(format!(
            "`Player` jumps at {jump_speed} m/s under gravity {gravity}; both must be above 0"
        ));
    }
    // The ground is at or below where the player starts (a template may
    // drop it a little first), so one jump never climbs above this.
    let start = position(player)[1];
    let one_jump = jump_speed * jump_speed / (2.0 * gravity);
    // Second press just before the first jump peaks.
    let peak_ticks = (jump_speed / gravity * 60.0 * 0.9).round() as u32;
    let second = 20 + peak_ticks;
    let operations = vec![json!({
        "op": "set", "id": "Player",
        "path": format!("/components/{key}/air_jumps"), "value": 1,
    })];
    let scenario = json!({
        "name": "double_jump: a second press in the air jumps higher than one jump can",
        "ticks": second + 4 * peak_ticks,
        "steps": [
            {"tick": 20, "press": "player.jump"},
            {"tick": 22, "release": "player.jump"},
            {"tick": second, "press": "player.jump"},
            {"tick": second + 2, "release": "player.jump"},
            {"tick": second + 2, "within": second + 2 * peak_ticks, "expect": {"entity": "Player", "path": "/transform/position/1", "greater_than": start + 1.1 * one_jump}},
        ]
    });
    Ok((operations, scenario))
}

fn inventory_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let key = [player[0] + 2.5, player[1], player[2]];
    let door = [player[0] + 5.0, player[1], player[2]];
    let entities = vec![
        sensor("Key 1", "item", key),
        sensor("Door 1", "locked", door),
    ];
    // The door stays shut without the key, the key is picked up once, and
    // the door then opens and spends it.
    let scenario = json!({
        "name": "inventory: a key is picked up and spent to open the locked door",
        "ticks": 40,
        "steps": [
            {"tick": 1, "set": {"entity": "Player", "path": "/transform/position", "value": door}},
            {"tick": 10, "expect": {"entity": "Door 1", "path": "/transform/position/0", "exists": true}},
            {"tick": 12, "set": {"entity": "Player", "path": "/transform/position", "value": key}},
            {"tick": 20, "expect": {"counter": "inventory_key", "equals": 1}},
            {"tick": 20, "expect": {"entity": "Key 1", "path": "/transform/position/0", "exists": false}},
            {"tick": 22, "set": {"entity": "Player", "path": "/transform/position", "value": door}},
            {"tick": 30, "expect": {"entity": "Door 1", "path": "/transform/position/0", "exists": false}},
            {"tick": 30, "expect": {"counter": "inventory_key", "equals": 0}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn day_timer_scene(_: &SceneEntity) -> Built {
    let hud = json!({"text": "Day {day}  {day_seconds_left}s", "anchor": "Top", "offset": [0.0, 24.0], "font_size": 28.0, "color": [1.0, 1.0, 1.0, 1.0], "button": false, "requires": null, "camera": null});
    let entities = vec![json!({
        "name": "Day Clock",
        "components": {"rusting.hud": hud},
    })];
    // The clock counts down from 30 s; moving the day's start 30 s back
    // then makes the next day begin at once.
    let scenario = json!({
        "name": "day_timer: the clock counts down and a new day begins after 30 seconds",
        "ticks": 200,
        "steps": [
            {"tick": 120, "expect": {"counter": "day", "equals": 1}},
            {"tick": 120, "expect": {"counter": "day_seconds_left", "equals": 28}},
            {"tick": 121, "set": {"counter": "day_started_ms", "value": -28000}},
            {"tick": 180, "expect": {"counter": "day", "equals": 2}},
            {"tick": 180, "expect": {"counter": "day_seconds_left", "equals": 29}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn pause_menu_scene(player: &SceneEntity) -> Built {
    let hud = |text: &str, y: f32, size: f32, button: bool| json!({"text": text, "anchor": "Center", "offset": [0.0, y], "font_size": size, "color": [1.0, 1.0, 1.0, 1.0], "button": button, "requires": null, "camera": null});
    let hidden = |name: &str, hud: Value| json!({"name": name, "visible": false, "components": {"rusting.hud": hud}});
    let entities = vec![
        json!({"name": "Pause Action", "components": {"rusting.input_action": {"action": "menu", "inputs": ["Escape", "PadStart"]}}}),
        hidden("Pause Title", hud("Paused", -80.0, 48.0, false)),
        hidden("Pause Resume", hud("Resume", 0.0, 28.0, true)),
        hidden("Pause Quit", hud("Quit", 60.0, 28.0, true)),
    ];
    // Lifted while paused, the player hangs in the air; after Resume is
    // clicked it falls again.
    let lifted = position(player)[1] + 5.0;
    let scenario = json!({
        "name": "pause_menu: menu pauses the game and shows the menu, clicking Resume carries on",
        "ticks": 100,
        "steps": [
            {"tick": 10, "press": "menu"},
            {"tick": 11, "release": "menu"},
            {"tick": 15, "expect": {"entity": "Pause Title", "path": "/visible", "equals": true}},
            {"tick": 16, "set": {"entity": "Player", "path": "/transform/position/1", "value": lifted}},
            {"tick": 40, "expect": {"entity": "Player", "path": "/transform/position/1", "greater_than": lifted - 0.01}},
            {"tick": 41, "click": "Resume"},
            {"tick": 45, "expect": {"entity": "Pause Resume", "path": "/visible", "equals": false}},
            {"tick": 100, "expect": {"entity": "Player", "path": "/transform/position/1", "less_than": lifted - 1.0}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn wave_spawner_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let spawn = [player[0] + 3.0, player[1] + 1.0, player[2]];
    let transform = |at: [f32; 3]| json!({"position": at, "rotation": [0.0, 0.0, 0.0], "scale": [1.0, 1.0, 1.0]});
    let mut template = enemy("Wave Enemy", [0.0, -1000.0, 0.0]);
    template["visible"] = json!(false);
    let entities = vec![
        json!({"name": "Wave Spawn", "transform": transform(spawn)}),
        template,
    ];
    let exists = |name: &str, exists: bool| json!({"entity": name, "path": "/transform/position/0", "exists": exists});
    // Wave 1 has two enemies; once both fall out, wave 2 brings four.
    let scenario = json!({
        "name": "wave_spawner: a bigger wave comes when every enemy is gone",
        "ticks": 20,
        "steps": [
            {"tick": 5, "expect": {"counter": "wave", "equals": 1}},
            {"tick": 5, "expect": {"counter": "wave_enemies_left", "equals": 2}},
            {"tick": 5, "expect": {"entity": "Wave Enemy 1-2", "path": "/visible", "equals": true}},
            {"tick": 10, "set": {"entity": "Wave Enemy 1-1", "path": "/transform/position/1", "value": -20.0}},
            {"tick": 12, "expect": {"counter": "wave_enemies_left", "equals": 1}},
            {"tick": 13, "set": {"entity": "Wave Enemy 1-2", "path": "/transform/position/1", "value": -20.0}},
            {"tick": 18, "expect": {"counter": "wave", "equals": 2}},
            {"tick": 18, "expect": {"counter": "wave_enemies_left", "equals": 4}},
            {"tick": 18, "expect": exists("Wave Enemy 2-4", true)},
            {"tick": 18, "expect": exists("Wave Enemy 1-1", false)},
            {"tick": 18, "expect": {"entity": "Wave Enemy 2-1", "path": "/transform/position/0", "equals": spawn[0], "tolerance": 0.01}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn turret_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let turret = [player[0], player[1] + 3.0, player[2]];
    let along = |x: f32| [turret[0] + x, turret[1], turret[2]];
    let entities = vec![
        json!({
            "name": "Turret",
            "classes": ["turret"],
            "transform": {"position": turret, "rotation": [0.0, 0.0, 0.0], "scale": [0.6, 0.6, 0.6]},
            "mesh_renderer": cube([0.3, 0.35, 0.4, 1.0]),
        }),
        enemy("Target 1", along(4.0)),
        enemy("Target 2", along(6.0)),
        enemy("Target Far", along(20.0)),
    ];
    let exists = |name: &str, exists: bool| json!({"entity": name, "path": "/transform/position/0", "exists": exists});
    // The near target goes first; the cooldown spares the second for half
    // a second; the far one is out of range.
    let scenario = json!({
        "name": "turret: shoots the nearest enemy in range, one every half second",
        "ticks": 90,
        "steps": [
            {"tick": 15, "expect": exists("Target 1", false)},
            {"tick": 15, "expect": exists("Target 2", true)},
            {"tick": 15, "expect": {"counter": "turret_kills", "equals": 1}},
            {"tick": 50, "expect": exists("Target 2", false)},
            {"tick": 90, "expect": exists("Target Far", true)},
            {"tick": 90, "expect": {"counter": "turret_kills", "equals": 2}},
        ]
    });
    Ok((creates(entities), scenario))
}

fn alarm_scene(player: &SceneEntity) -> Built {
    let player = position(player);
    let plate = [player[0] + 2.5, player[1], player[2]];
    let mut alarm = sensor("Alarm Plate", "alarm", plate);
    alarm["components"] = json!({"rusting.state": {"state": "quiet", "transitions": [
        {"from": "quiet", "to": "alarmed", "touching": "Player", "then_counter": "alarms", "then_add": 1},
        {"from": "alarmed", "to": "quiet", "after_seconds": 2.0},
    ]}});
    let state = "/components/rusting.state/state";
    // Standing on the plate alarms once, it calms after 2 s, and standing
    // there still alarms it again.
    let scenario = json!({
        "name": "alarm: touching the plate alarms it once and it calms after 2 s",
        "ticks": 200,
        "steps": [
            {"tick": 5, "expect": {"entity": "Alarm Plate", "path": state, "equals": "quiet"}},
            {"tick": 10, "set": {"entity": "Player", "path": "/transform/position", "value": plate}},
            {"tick": 20, "expect": {"entity": "Alarm Plate", "path": state, "equals": "alarmed"}},
            {"tick": 20, "expect": {"counter": "alarms", "equals": 1}},
            {"tick": 20, "set": {"entity": "Player", "path": "/transform/position", "value": player}},
            {"tick": 150, "expect": {"entity": "Alarm Plate", "path": state, "equals": "quiet"}},
            {"tick": 150, "expect": {"counter": "alarms", "equals": 1}},
            {"tick": 160, "set": {"entity": "Player", "path": "/transform/position", "value": plate}},
            {"tick": 170, "expect": {"counter": "alarms", "equals": 2}},
        ]
    });
    Ok((creates(vec![alarm]), scenario))
}

fn dash_scene(player: &SceneEntity) -> Built {
    // Which way an idle player dashes: forward by its yaw in 3D, +X in 2D.
    let yaw = if let Some(text) =
        player.components.get("rusting.player_controller")
    {
        let c: PlayerController =
            serde_json::from_str(text).map_err(|e| e.to_string())?;
        Some(c.yaw)
    } else if player
        .components
        .contains_key("rusting.platformer_controller")
    {
        None
    } else {
        return Err("`Player` has no `rusting.player_controller` or `rusting.platformer_controller`".into());
    };
    let way = yaw.map_or([1.0, 0.0], |yaw| [-yaw.sin(), -yaw.cos()]);
    let (axis, sign) = if way[0].abs() >= way[1].abs() {
        (0, way[0])
    } else {
        (2, way[1])
    };
    let start = position(player)[axis];
    let entities = vec![
        json!({"name": "Dash Action", "components": {"rusting.input_action": {"action": "dash", "inputs": ["KeyQ", "PadWest"]}}}),
    ];
    let path = format!("/transform/position/{axis}");
    // Lifted clear of low blocks, one dash covers 3 m; a second press
    // during the cooldown does nothing.
    let lifted = position(player)[1] + 1.0;
    let along = |metres: f32| start + sign.signum() * metres;
    let (past, short) = if sign > 0.0 {
        ("greater_than", "less_than")
    } else {
        ("less_than", "greater_than")
    };
    let moved = json!({"entity": "Player", "path": path, past: along(2.5)});
    let not_far = json!({"entity": "Player", "path": path, short: along(3.5)});
    let scenario = json!({
        "name": "dash: a press dashes 3 m and a second press inside the cooldown does nothing",
        "ticks": 80,
        "steps": [
            {"tick": 19, "set": {"entity": "Player", "path": "/transform/position/1", "value": lifted}},
            {"tick": 20, "press": "dash"},
            {"tick": 22, "release": "dash"},
            {"tick": 40, "expect": moved},
            {"tick": 42, "press": "dash"},
            {"tick": 44, "release": "dash"},
            {"tick": 80, "expect": not_far},
        ]
    });
    Ok((creates(entities), scenario))
}

#[cfg(test)]
#[path = "recipes/dash.rs"]
mod dash;

#[cfg(test)]
#[path = "recipes/checkpoints.rs"]
mod checkpoints;

#[cfg(test)]
#[path = "recipes/health.rs"]
mod health;

#[cfg(test)]
#[path = "recipes/wave_spawner.rs"]
mod wave_spawner;

#[cfg(test)]
#[path = "recipes/turret.rs"]
mod turret;

#[cfg(test)]
#[path = "recipes/pause_menu.rs"]
mod pause_menu;

#[cfg(test)]
#[path = "recipes/day_timer.rs"]
mod day_timer;

#[cfg(test)]
#[path = "recipes/inventory.rs"]
mod inventory;

#[cfg(test)]
mod tests {
    use std::path::Path;

    use bevy_ecs::prelude::World;

    use crate::project::ProjectTemplate;
    use crate::project_runner::GameScene;
    use crate::runtime::{AppError, FrameTime, Plugin, ScheduleStage};
    use crate::App;

    /// Runs a recipe's call as a game's `update` would.
    struct RecipeUpdate(fn(&mut World));

    impl Plugin for RecipeUpdate {
        fn build(&self, app: &mut App) -> Result<(), AppError> {
            app.add_system(ScheduleStage::Update, self.0);
            Ok(())
        }
    }

    /// Applies the recipe `name` to each player template (dry run first,
    /// then for real, then again to see it refused) and runs its scenario
    /// with `update` compiled in.
    fn applies_and_passes(name: &str, update: fn(&mut World)) {
        let recipe = super::recipe(name).unwrap();
        for template in [
            ProjectTemplate::FirstPerson3d,
            ProjectTemplate::ThirdPerson3d,
            ProjectTemplate::Platformer2d,
        ] {
            let parent = std::env::temp_dir()
                .join(format!("rusting-recipe-{}", uuid::Uuid::new_v4()));
            std::fs::create_dir_all(&parent).unwrap();
            let project =
                crate::project::create_project_from(&parent, "g", template)
                    .unwrap();
            let root = project.root.as_path();
            let source = root.join(format!("src/{name}.rs"));
            let scene = std::fs::read(&project.scene_path).unwrap();
            let dry = crate::cli::apply_recipe(root, name, true);
            assert!(dry.ok, "{dry:?}");
            assert!(!source.exists());
            assert_eq!(std::fs::read(&project.scene_path).unwrap(), scene);
            let applied = crate::cli::apply_recipe(root, name, false);
            assert!(applied.ok, "{applied:?}");
            assert_eq!(
                std::fs::read_to_string(&source).ok().as_deref(),
                recipe.source
            );
            let again = crate::cli::apply_recipe(root, name, false);
            assert_eq!(again.diagnostics[0].code, "PROJECT_EXISTS");
            crate::project_runner::run_project_scenario(
                project.scene_path.clone(),
                RecipeUpdate(update),
                root.join(format!("tests/{name}.json")),
                Some(root.join("report.json")),
            )
            .unwrap_or_else(|error| panic!("{name} {template:?}: {error}"));
            let _ = std::fs::remove_dir_all(&parent);
        }
    }

    #[test]
    fn checkpoint_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("checkpoints", |world| {
            super::checkpoints::checkpoints(&mut GameScene { world });
        });
        let unknown = crate::cli::apply_recipe(Path::new("."), "nope", false);
        assert_eq!(unknown.diagnostics[0].code, "RECIPE_UNKNOWN");
    }

    #[test]
    fn health_recipe_applies_to_the_player_templates_and_its_scenario_passes() {
        applies_and_passes("health", |world| {
            super::health::health(&mut GameScene { world });
        });
    }

    #[test]
    fn double_jump_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("double_jump", |_| {});
        let no_controller: crate::runtime::SceneEntity = serde_json::from_value(
            serde_json::json!({"id": uuid::Uuid::nil(), "parent": null, "name": "Player"}),
        )
        .unwrap();
        let error =
            (super::recipe("double_jump").unwrap().build)(&no_controller);
        assert!(error.unwrap_err().contains("rusting.platformer_controller"));
    }

    #[test]
    fn inventory_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("inventory", |world| {
            super::inventory::inventory(&mut GameScene { world });
        });
    }

    #[test]
    fn day_timer_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("day_timer", |world| {
            let time = *world.resource::<FrameTime>();
            super::day_timer::day_timer(&mut GameScene { world }, &time);
        });
    }

    #[test]
    fn pause_menu_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("pause_menu", |world| {
            super::pause_menu::pause_menu(&mut GameScene { world });
        });
    }

    #[test]
    fn wave_spawner_recipe_applies_to_the_player_templates_and_its_scenario_passes(
    ) {
        applies_and_passes("wave_spawner", |world| {
            super::wave_spawner::wave_spawner(&mut GameScene { world });
        });
    }

    #[test]
    fn turret_recipe_applies_to_the_player_templates_and_its_scenario_passes() {
        applies_and_passes("turret", |world| {
            super::turret::turret(&mut GameScene { world });
        });
    }

    #[test]
    fn alarm_recipe_applies_to_the_player_templates_and_its_scenario_passes() {
        applies_and_passes("alarm", |_| {});
    }

    #[test]
    fn dash_recipe_applies_to_the_player_templates_and_its_scenario_passes() {
        applies_and_passes("dash", |world| {
            super::dash::dash(&mut GameScene { world });
        });
    }
}