|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | +// SPDX-FileCopyrightText: 2025-2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// CombatFx -- the render-decision co-processor for the combat FX scattered |
| 5 | +// across the player, combat and training subsystems. Extracted from |
| 6 | +// src/app/combat/CombatFxLogicCoprocessor.res (the wasm binding shim) whose |
| 7 | +// upstream implementation lives in the original combatfxlogic.wasm. |
| 8 | +// |
| 9 | +// Per the DESIGN-VISION ("AffineScript is the brain, Pixi the senses; only |
| 10 | +// primitives cross the wasm boundary"), every AnimatedSprite.setTint, |
| 11 | +// Container.setAlpha, Text.make, Motion.animate and hex palette entry stays in |
| 12 | +// ReScript. This co-processor owns only the scalar arithmetic that decides WHAT |
| 13 | +// the FX should be this frame; the host feeds each result straight to a draw |
| 14 | +// call, so the observable picture is identical while the decision now lives in |
| 15 | +// wasm. |
| 16 | +// |
| 17 | +//## Compiler note: no trunc/float built-ins in wasm codegen |
| 18 | +// `trunc(x)` and `float(n)` resolve at the type-checking stage but are absent |
| 19 | +// from the wasm code-generator. Both are replaced by proved loop idioms from |
| 20 | +// the estate playbook: |
| 21 | +// floor_pos(x) -- counts how many whole 1.0s fit below x (non-negative x) |
| 22 | +// int_to_float_s(n) -- counts up from 0.0 n times (small non-negative n) |
| 23 | +// These lower correctly via i32/f64 Wasm instructions. |
| 24 | +// |
| 25 | +//## Boundary contract (the header the JS host relies on) |
| 26 | +// flash_cycle(timer, period) -> Int |
| 27 | +// floor(timer / period): shared cycle index for both flash routines. |
| 28 | +// flash_on_phase(timer, period, on_frac) -> Int |
| 29 | +// 1 while within the on-window of the current cycle, else 0. |
| 30 | +// Host passes period=0.2, on_frac=0.1 to match PlayerSprite exactly. |
| 31 | +// flash_alpha_phase(timer, period) -> Float |
| 32 | +// 1.0 on even cycle, 0.3 on odd; host passes period=0.1 (PlayerGraphics). |
| 33 | +// flash_alpha_phase_v(timer, period, hi, lo) -> Float |
| 34 | +// As flash_alpha_phase but with host-supplied hi/lo alpha values. |
| 35 | +// flash_is_even_cycle(timer, period) -> Int |
| 36 | +// 1 when cycle index is even, 0 when odd. |
| 37 | +// flash_advance(timer, dt) -> Float |
| 38 | +// timer + dt: per-frame accumulation while invincibility is active. |
| 39 | +// flash_reset() -> Float |
| 40 | +// 0.0: timer reset when invincibility expires. |
| 41 | +// floattext_start_y(y, off) -> Float |
| 42 | +// y - off: spawn Y for damage/pickup floating text (off = 40.0). |
| 43 | +// floattext_end_y(y, rise) -> Float |
| 44 | +// y - rise: rise/fade target Y (rise = 100.0). |
| 45 | +// floattext_rise_distance(start_off, rise) -> Float |
| 46 | +// rise - start_off: total vertical travel independent of y (= 60.0 default). |
| 47 | +// knockback_pop_y(speed) -> Float |
| 48 | +// 0.0 - speed: upward pop magnitude; up is negative Y; host passes 80.0. |
| 49 | + |
| 50 | +//## floor_pos: non-negative float -> integer floor |
| 51 | +// Counts how many whole 1.0s fit below x. Used instead of trunc() which is |
| 52 | +// absent from the wasm code-generator. For the timer/period quotients here |
| 53 | +// (order of magnitude 0..~500) the loop completes in microseconds. |
| 54 | +fn floor_pos(x: Float) -> Int { |
| 55 | + let mut n = 0; |
| 56 | + let mut acc = 0.0; |
| 57 | + while acc + 1.0 <= x { acc = acc + 1.0; n = n + 1; } |
| 58 | + n |
| 59 | +} |
| 60 | + |
| 61 | +//## int_to_float_s: small non-negative Int -> Float |
| 62 | +// Counts from 0.0 up to n. Used instead of float() which is absent from the |
| 63 | +// wasm code-generator. For the cycle indices here (order of magnitude 0..~500) |
| 64 | +// the loop completes in microseconds. |
| 65 | +fn int_to_float_s(n: Int) -> Float { |
| 66 | + let mut f = 0.0; |
| 67 | + let mut i = 0; |
| 68 | + while i < n { f = f + 1.0; i = i + 1; } |
| 69 | + f |
| 70 | +} |
| 71 | + |
| 72 | +//## PlayerSprite / PlayerGraphics -- shared flash cycle index |
| 73 | +// Both flash routines compute floor(timer / period): PlayerSprite uses period=0.2 |
| 74 | +// (the full red-tint cycle), PlayerGraphics uses period=0.1 (the alpha cycle). |
| 75 | +// This kernel is the shared implementation; the host chooses the period. |
| 76 | +pub fn flash_cycle(timer: Float, period: Float) -> Int { |
| 77 | + floor_pos(timer / period) |
| 78 | +} |
| 79 | + |
| 80 | +//## PlayerSprite.updateDamageFlash -- red-tint on/off phase |
| 81 | +// remainder = timer - floor(timer/period) * period; on when remainder < on_frac. |
| 82 | +// Reconstructed without a float modulo (unsupported for variables in this backend) |
| 83 | +// using floor_pos + int_to_float_s. The host passes period=0.2, on_frac=0.1 to |
| 84 | +// match PlayerSprite exactly (first half of each 0.2 s cycle is the on-window). |
| 85 | +pub fn flash_on_phase(timer: Float, period: Float, on_frac: Float) -> Int { |
| 86 | + let cycles = floor_pos(timer / period); |
| 87 | + let remainder = timer - int_to_float_s(cycles) * period; |
| 88 | + if remainder < on_frac { return 1; } |
| 89 | + 0 |
| 90 | +} |
| 91 | + |
| 92 | +//## PlayerGraphics.updateDamageFlash -- container-alpha flicker (cycle parity) |
| 93 | +// alpha = (floor(timer / period) mod 2 == 0) ? 1.0 : 0.3. |
| 94 | +// Returns the f64 alpha directly so the host hands it straight to Container.setAlpha. |
| 95 | +// The host passes period=0.1 to match PlayerGraphics exactly. |
| 96 | +pub fn flash_alpha_phase(timer: Float, period: Float) -> Float { |
| 97 | + let cycle = floor_pos(timer / period); |
| 98 | + if cycle % 2 == 0 { return 1.0; } |
| 99 | + 0.3 |
| 100 | +} |
| 101 | + |
| 102 | +//## PlayerGraphics.updateDamageFlash -- alpha flicker, host-supplied magnitudes |
| 103 | +// As flash_alpha_phase but with the two alpha values passed by the host, in case |
| 104 | +// a render branch wants different on/off opacities while keeping the same parity |
| 105 | +// decision. hi on even cycles, lo on odd. |
| 106 | +pub fn flash_alpha_phase_v(timer: Float, period: Float, hi: Float, lo: Float) -> Float { |
| 107 | + let cycle = floor_pos(timer / period); |
| 108 | + if cycle % 2 == 0 { return hi; } |
| 109 | + lo |
| 110 | +} |
| 111 | + |
| 112 | +//## PlayerGraphics.updateDamageFlash -- the parity flag alone |
| 113 | +// The discrete even/odd decision behind the alpha choice, returned as an Int flag |
| 114 | +// for hosts that own both alpha magnitudes and only need the selector. |
| 115 | +pub fn flash_is_even_cycle(timer: Float, period: Float) -> Int { |
| 116 | + let cycle = floor_pos(timer / period); |
| 117 | + if cycle % 2 == 0 { return 1; } |
| 118 | + 0 |
| 119 | +} |
| 120 | + |
| 121 | +//## PlayerSprite / PlayerGraphics -- effect-lifetime timer accumulation |
| 122 | +// While the invincibility timer is positive both routines accumulate |
| 123 | +// damageFlashTimer += deltaTime each frame; this kernel is that step. The host |
| 124 | +// owns the > 0.0 guard and the mutable field; the brain owns the addition. |
| 125 | +pub fn flash_advance(timer: Float, dt: Float) -> Float { |
| 126 | + timer + dt |
| 127 | +} |
| 128 | + |
| 129 | +//## PlayerSprite / PlayerGraphics -- flash-timer reset |
| 130 | +// When the invincibility timer reaches zero both routines reset damageFlashTimer |
| 131 | +// to 0.0 (and the host restores the base tint / full alpha). The value is a |
| 132 | +// constant, lifted so the reset point is the same total kernel as the advance. |
| 133 | +pub fn flash_reset() -> Float { |
| 134 | + 0.0 |
| 135 | +} |
| 136 | + |
| 137 | +//## ScavengerTraining.showFloatingText -- spawn Y |
| 138 | +// The floating damage/pickup text is created at y - 40.0 (Text.setY(floatText, |
| 139 | +// y - 40.0)). Generalised over the spawn offset; the host passes 40.0. |
| 140 | +pub fn floattext_start_y(y: Float, off: Float) -> Float { |
| 141 | + y - off |
| 142 | +} |
| 143 | + |
| 144 | +//## ScavengerTraining.showFloatingText -- rise/fade target Y |
| 145 | +// Motion.animate drives the text to y - 100.0 with alpha 0.0 over 1.5 s. This is |
| 146 | +// the target Y of that tween; generalised over the rise, the host passes 100.0. |
| 147 | +pub fn floattext_end_y(y: Float, rise: Float) -> Float { |
| 148 | + y - rise |
| 149 | +} |
| 150 | + |
| 151 | +//## ScavengerTraining.showFloatingText -- total vertical travel |
| 152 | +// The distance the text drifts upward over its lifetime: (y - start_off) minus |
| 153 | +// (y - rise) = rise - start_off. Independent of y, so the host can size the tween |
| 154 | +// or a pooled effect without re-deriving it. With 40.0 / 100.0 this is 60.0. |
| 155 | +pub fn floattext_rise_distance(start_off: Float, rise: Float) -> Float { |
| 156 | + rise - start_off |
| 157 | +} |
| 158 | + |
| 159 | +//## PlayerHP.takeDamage -- upward knockback pop |
| 160 | +// On taking a hit the player gets a slight upward pop alongside the signed |
| 161 | +// horizontal knockback (already migrated in PlayerHp). The original |
| 162 | +// hard-codes knockbackVelY = -80.0; lifted as the negation of a host-supplied |
| 163 | +// speed so the FX co-processor owns the sign convention (up is negative Y). |
| 164 | +pub fn knockback_pop_y(speed: Float) -> Float { |
| 165 | + 0.0 - speed |
| 166 | +} |
0 commit comments