|
| 1 | +import std::io; |
| 2 | +import std::math; |
| 3 | +import raylib5::rl; |
| 4 | +import future; |
| 5 | + |
| 6 | +// TODO: signature of PlaySoundFunc is incorrect to save time. |
| 7 | +def PlaySoundFunc = fn void(); |
| 8 | + |
| 9 | +const float CYCLE_DURATION = 3.0f; |
| 10 | + |
| 11 | +struct Env { |
| 12 | + float delta_time; |
| 13 | + float screen_width; |
| 14 | + float screen_height; |
| 15 | + bool rendering; |
| 16 | + PlaySoundFunc play_sound; |
| 17 | +} |
| 18 | + |
| 19 | +struct Lerp(Future) { |
| 20 | + float *place; |
| 21 | + float a, b, t; |
| 22 | + float duration; |
| 23 | +} |
| 24 | + |
| 25 | +fn any! Lerp.poll(&self, any env) @dynamic { |
| 26 | + switch (env) { |
| 27 | + case Env: { |
| 28 | + if (self.t >= 1) return self; |
| 29 | + self.t = (self.duration*self.t + env.delta_time)/self.duration; |
| 30 | + *self.place = (self.b - self.a)*self.t; |
| 31 | + if (self.t >= 1) return self; |
| 32 | + return null; |
| 33 | + } |
| 34 | + default: unreachable(); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn Future lerp(float *place, float a, float b, float duration) { |
| 39 | + return @tclone(Lerp { |
| 40 | + .place = place, |
| 41 | + .a = a, |
| 42 | + .b = b, |
| 43 | + .duration = duration, |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +struct Parallel(Future) { |
| 48 | + Future[] futures; |
| 49 | +} |
| 50 | + |
| 51 | +fn any! Parallel.poll(&urmom, any env) @dynamic { |
| 52 | + bool finished = true; |
| 53 | + foreach (future: &urmom.futures) { |
| 54 | + if (future.poll(env)! == null) { |
| 55 | + finished = false; |
| 56 | + } |
| 57 | + } |
| 58 | + if (finished) return urmom; |
| 59 | + return null; |
| 60 | +} |
| 61 | + |
| 62 | +fn Future parallel(Future[] futures) { |
| 63 | + return @tclone(Parallel { |
| 64 | + .futures = futures, |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +struct Seq(Future) { |
| 69 | + Future[] futures; |
| 70 | + usz index; |
| 71 | +} |
| 72 | + |
| 73 | +fn bool Seq.finished(&urmom) { |
| 74 | + return urmom.index >= urmom.futures.len; |
| 75 | +} |
| 76 | + |
| 77 | +fn any! Seq.poll(&urmom, any env) @dynamic { |
| 78 | + if (urmom.finished()) return urmom; |
| 79 | + if (urmom.futures[urmom.index].poll(env)!) urmom.index += 1; |
| 80 | + if (urmom.finished()) return urmom; |
| 81 | + return null; |
| 82 | +} |
| 83 | + |
| 84 | +fn Future seq(Future[] futures) { |
| 85 | + return @tclone(Seq { |
| 86 | + .futures = futures, |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +struct State { |
| 91 | + float t1, t2; |
| 92 | + bool finished; |
| 93 | + Future anim; |
| 94 | +} |
| 95 | + |
| 96 | +State *state = null; |
| 97 | + |
| 98 | +fn void reset_anim() |
| 99 | +{ |
| 100 | + // TODO: clean up allocator::temp() here |
| 101 | + state.anim = parallel(@tclone(Future[*] { |
| 102 | + // TODO: Tuck the whole @tclone(Future[*]{ ... }) under the future constructors |
| 103 | + // See if variadic args can be applied here |
| 104 | + seq(@tclone(Future[*] { |
| 105 | + lerp(&state.t1, 0, 1, CYCLE_DURATION), |
| 106 | + lerp(&state.t1, 1, 0, CYCLE_DURATION/4), |
| 107 | + })), |
| 108 | + seq(@tclone(Future[*] { |
| 109 | + lerp(&state.t2, 0, 1, CYCLE_DURATION + CYCLE_DURATION/4), |
| 110 | + })) |
| 111 | + })); |
| 112 | +} |
| 113 | + |
| 114 | +fn void plug_init() @export("plug_init") |
| 115 | +{ |
| 116 | + state = mem::new(State); |
| 117 | + reset_anim(); |
| 118 | +} |
| 119 | + |
| 120 | +fn void* plug_pre_reload() @export("plug_pre_reload") |
| 121 | +{ |
| 122 | + return state; |
| 123 | +} |
| 124 | + |
| 125 | +fn void plug_post_reload(void *old_state) @export("plug_post_reload") |
| 126 | +{ |
| 127 | + state = old_state; |
| 128 | +} |
| 129 | + |
| 130 | +fn void orbit_circle(Env env, float t, float radius, float orbit, Color color) |
| 131 | +{ |
| 132 | + float angle = 2*math::PI*t; |
| 133 | + float cx = env.screen_width*0.5; |
| 134 | + float cy = env.screen_height*0.5; |
| 135 | + float px = cx + math::cos(angle)*orbit; |
| 136 | + float py = cy + math::sin(angle)*orbit; |
| 137 | + rl::drawCircleV({px, py}, radius, color); |
| 138 | +} |
| 139 | + |
| 140 | +fn void plug_update(Env env) @export("plug_update") |
| 141 | +{ |
| 142 | + state.finished = state.anim.poll(&env)!! != null; |
| 143 | + rl::clearBackground({0x18, 0x18, 0x18, 0xFF}); |
| 144 | + |
| 145 | + float radius = env.screen_width*0.04; |
| 146 | + float orbit = env.screen_width*0.25; |
| 147 | + Color color = rl::BLUE; |
| 148 | + orbit_circle(env, state.t1, radius, orbit, color); |
| 149 | + |
| 150 | + radius = env.screen_width*0.02; |
| 151 | + orbit = env.screen_width*0.10; |
| 152 | + color = rl::RED; |
| 153 | + orbit_circle(env, state.t2, radius, orbit, color); |
| 154 | +} |
| 155 | + |
| 156 | +fn void plug_reset() @export("plug_reset") |
| 157 | +{ |
| 158 | + state.finished = false; |
| 159 | + reset_anim(); |
| 160 | +} |
| 161 | + |
| 162 | +fn bool plug_finished() @export("plug_finished") |
| 163 | +{ |
| 164 | + return state.finished; |
| 165 | +} |
0 commit comments