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
//! Hot-patching game code: change a system while the game runs, and the
//! running game picks up the new code without losing any state.
//!
//! ```sh
//! cargo install dioxus-cli@0.7.10 # once; installs `dx`
//! dx serve --hot-patch --example hotpatch --features hotpatch
//! ```
//!
//! Then edit `spin` or `wobble` below and save: the crabs change behavior
//! within a second, mid-spin, where they were.
use ponko::*;
/// Every crab turns. Try a different speed, or `-=` to turn the other way.
// ANCHOR: spin
fn spin(ctx: &mut Ctx) {
let dt = ctx.dt();
for t in ctx.world.query::<&mut Transform2D>().iter() {
t.rotation += -6.0 * dt;
}
}
// ANCHOR_END: spin
/// Crabs breathe. Try a bigger amplitude, or a different shape.
fn wobble(ctx: &mut Ctx) {
let time = ctx.sim_time() as f32;
for (t, crab) in ctx.world.query::<(&mut Transform2D, &Crab)>().iter() {
let s = 1.0 + 0.15 * (time * 3.0 + crab.0).sin();
t.scale = vec2(s, s);
}
}
/// Prints the first crab's rotation once a second, so you can see that
/// state carries over a patch.
fn report(ctx: &mut Ctx) {
let tick = ctx.sim_tick();
if tick.is_multiple_of(60)
&& let Some(t) = ctx.world.query::<&Transform2D>().iter().next()
{
println!("tick {tick:>5} rotation {:>7.2}", t.rotation);
}
}
/// A phase, so crabs don't all move together.
struct Crab(f32);
fn main() {
Game::new("Crabby Engine: hot patching", 1280, 720)
.sim_system(spin)
.sim_system(wobble)
.sim_system(report)
.run(|ctx| {
let crab = ctx.load_texture("crab.png");
for i in 0..12 {
let at = vec2(160.0 + (i % 6) as f32 * 190.0, 220.0 + (i / 6) as f32 * 280.0);
ctx.world
.spawn((Transform2D::at(at), Sprite::new(crab), Crab(i as f32 * 0.7)));
}
});
}