hanoiy's picture
Add GameDojo game environments
dc5a369 verified
Raw History Blame
8.03 kB
// Juice: screen shake, flashes, particles, hit-stop requests. Presentation only - it reads case events and
// never writes simulation state. Hit-stop is honoured by the *human play loop* as a wall-clock pause between
// ticks, so the tick sequence (and every replay / agent rollout) is identical with or without it.
(function (g) {
var BB = g.BB;
function Juice() { this.reset(); }
Juice.prototype.reset = function () {
this.seen = 0; this.trauma = 0; this.flash = 0; this.redFlash = 0; this.parts = []; this.flares = []; this.hitstop = 0;
this.rng = new BB.RNG(12345); this.lastT = null; this.caseRef = null;
};
Juice.prototype.burst = function (x, y, n, col, spd, life, grav) {
for (var i = 0; i < n; i++) {
var a = this.rng.uniform(0, Math.PI * 2), v = this.rng.uniform(0.3, 1) * spd;
this.parts.push({ x: x, y: y, vx: Math.cos(a) * v, vy: Math.sin(a) * v, life: life * this.rng.uniform(0.6, 1), max: life, col: col, g: grav || 0, r: this.rng.uniform(1.5, 3.5), streak: spd >= 200 });
}
};
Juice.prototype.flare = function (x, y, size, life, rgb) { // impact star: two crossed tapered rays + glow
this.flares.push({ x: x, y: y, s: size, life: life, max: life, a: this.rng.uniform(-0.6, 0.6), rgb: rgb || '255,250,235' });
};
Juice.prototype.dust = function (x, y, n, col, spd) { // soft rising puffs, mostly sideways
for (var i = 0; i < n; i++) {
var dir = i % 2 ? 1 : -1, v = this.rng.uniform(0.3, 1) * spd;
this.parts.push({ x: x + dir * this.rng.uniform(0, 8), y: y, vx: dir * v, vy: -this.rng.uniform(10, 40), life: this.rng.uniform(0.35, 0.6), max: 0.6,
col: col, g: -20, r: this.rng.uniform(3, 6), puff: true });
}
};
Juice.prototype.consume = function (c, R) {
if (this.caseRef !== c || c.events.length < this.seen) { // new / reset case: only react to the last 0.2 s
this.caseRef = c; this.parts = []; this.flares = []; this.seen = c.events.length;
while (this.seen > 0 && c.events[this.seen - 1].t > c.t - 12) this.seen--;
}
var b = c.body, w = c.boss;
for (; this.seen < c.events.length; this.seen++) {
var e = c.events[this.seen], n = e.name;
if (n === 'shake') this.trauma = Math.min(1, this.trauma + e.a);
else if (n === 'boss_hit') {
var sb = b.centre(), hx = R.sx(e.x != null ? e.x : (sb.x + w.x) / 2), hy = R.sy(e.y != null ? e.y : sb.y);
this.burst(hx, hy, 10, '#f4ead2', 260, 0.35, 300); this.flare(hx, hy, 38, 0.16); this.hitstop = Math.max(this.hitstop, 0.05); this.trauma = Math.min(1, this.trauma + 0.12);
} else if (n === 'player_hit') {
this.burst(R.sx(b.x), R.sy(b.y + 0.7), 18, '#ffffff', 340, 0.5, 200); this.flare(R.sx(b.x), R.sy(b.y + 0.7), 60, 0.22, '255,235,235'); this.redFlash = 1; this.hitstop = Math.max(this.hitstop, b.HITSTOP || 0);
} else if (n === 'tink') { this.burst(R.sx(e.x != null ? e.x : w.needle ? w.needle.x : b.x), R.sy(e.y != null ? e.y : w.needle ? w.needle.y : b.y), 8, '#fff2a0', 300, 0.25, 0); this.hitstop = Math.max(this.hitstop, 0.04); }
else if (n === 'pogo') this.burst(R.sx(b.x), R.sy(b.y - 0.2), 6, '#d9f6ff', 180, 0.25, 0);
else if (n === 'land' || n === 'boss_land' || n === 'boss_land_hard') {
var lx = n === 'land' ? b.x : e.x != null ? e.x : w.x;
this.dust(R.sx(lx), R.sy(0) - 3, n === 'boss_land_hard' ? 12 : n === 'boss_land' ? 6 : 4, 'rgba(190,200,195,0.5)', n === 'boss_land_hard' ? 140 : 70);
} else if (n === 'dash') this.dust(R.sx(b.x), R.sy(b.y + 0.3), 4, 'rgba(200,220,255,0.45)', 60);
else if (n === 'heal') { this.burst(R.sx(b.x), R.sy(b.y + 0.8), 16, '#bfefff', 160, 0.6, -80); this.flash = 0.4; }
else if (n === 'boss_stun') { this.burst(R.sx(e.x != null ? e.x : w.x), R.sy(e.y != null ? e.y : w.y), 20, '#ffe9a0', 280, 0.6, 250); this.flash = 0.5; this.hitstop = 0.12; this.flare(R.sx(e.x != null ? e.x : w.x), R.sy(e.y != null ? e.y : w.y + 1), 90, 0.3); }
else if (n === 'boss_dead') { this.burst(R.sx(e.x != null ? e.x : w.x), R.sy(e.y != null ? e.y : w.y), 50, '#f4ead2', 420, 1.2, 150); this.flash = 1; this.hitstop = 0.4; this.flare(R.sx(e.x != null ? e.x : w.x), R.sy(e.y != null ? e.y : w.y + 1), 160, 0.6); }
else if (n === 'boss_parry') { this.burst(R.sx(e.x != null ? e.x : w.x + (w.f || 0) * 0.6), R.sy(e.y != null ? e.y : w.y), 12, '#fff2a0', 300, 0.3, 0); this.flash = 0.3; this.flare(R.sx(e.x != null ? e.x : w.x + (w.f || 0) * 0.6), R.sy(e.y != null ? e.y : w.y), 50, 0.18, '255,240,180'); }
else if (n === 'boss_escalate') this.flash = 0.25;
}
};
Juice.prototype.offset = function (t) {
var dt = this.lastT == null ? 0 : Math.max(0, Math.min(0.1, t - this.lastT)); this.lastT = t;
this.trauma = Math.max(0, this.trauma - dt * 1.6); this.flash = Math.max(0, this.flash - dt * 3); this.redFlash = Math.max(0, this.redFlash - dt * 3);
for (var i = this.parts.length - 1; i >= 0; i--) {
var p = this.parts[i]; p.life -= dt; if (p.life <= 0) { this.parts.splice(i, 1); continue; }
p.vy += p.g * dt; p.x += p.vx * dt; p.y += p.vy * dt; p.vx *= 0.96;
}
for (i = this.flares.length - 1; i >= 0; i--) { this.flares[i].life -= dt; if (this.flares[i].life <= 0) this.flares.splice(i, 1); }
var s = this.trauma * this.trauma * 9; // trauma^2 shake
return [Math.sin(t * 91.3) * s, Math.cos(t * 73.7) * s];
};
Juice.prototype.drawParticles = function (x) {
x.save();
for (var i = 0; i < this.parts.length; i++) {
var p = this.parts[i], k = Math.max(0, p.life / p.max);
if (p.puff) { // dust: grows and fades
x.globalCompositeOperation = 'source-over'; x.globalAlpha = k * 0.8; x.fillStyle = p.col;
x.beginPath(); x.arc(p.x, p.y, p.r * (2 - k), 0, Math.PI * 2); x.fill();
} else if (p.streak) { // spark: additive streak along its velocity, thinning out
var sp = Math.sqrt(p.vx * p.vx + p.vy * p.vy) || 1, L = Math.min(22, 4 + sp * 0.045) * (0.4 + 0.6 * k);
x.globalCompositeOperation = 'lighter'; x.globalAlpha = k; x.strokeStyle = p.col; x.lineCap = 'round'; x.lineWidth = p.r * k + 0.6;
x.beginPath(); x.moveTo(p.x, p.y); x.lineTo(p.x - p.vx / sp * L, p.y - p.vy / sp * L); x.stroke();
} else {
x.globalCompositeOperation = 'lighter'; x.globalAlpha = k; x.fillStyle = p.col;
x.beginPath(); x.arc(p.x, p.y, p.r * (0.5 + 0.5 * k), 0, Math.PI * 2); x.fill();
}
}
x.globalCompositeOperation = 'lighter';
for (i = 0; i < this.flares.length; i++) {
var f = this.flares[i], q = f.life / f.max, S = f.s * (0.6 + 0.4 * (1 - q));
x.globalAlpha = 1;
var gr = x.createRadialGradient(f.x, f.y, 0, f.x, f.y, S * 0.6);
gr.addColorStop(0, 'rgba(' + f.rgb + ',' + (0.8 * q).toFixed(3) + ')'); gr.addColorStop(1, 'rgba(' + f.rgb + ',0)');
x.fillStyle = gr; x.beginPath(); x.arc(f.x, f.y, S * 0.6, 0, Math.PI * 2); x.fill();
x.fillStyle = 'rgba(' + f.rgb + ',' + q.toFixed(3) + ')';
for (var r = 0; r < 2; r++) {
var a = f.a + r * Math.PI / 2, L2 = S * (r ? 0.55 : 1), wd = 2.5 + 3 * q;
x.save(); x.translate(f.x, f.y); x.rotate(a);
x.beginPath(); x.moveTo(-L2, 0); x.quadraticCurveTo(0, -wd, L2, 0); x.quadraticCurveTo(0, wd, -L2, 0); x.fill(); x.restore();
}
}
x.restore();
};
// Wall-clock hit-stop is presentation only. A game whose player hits many times a second (e.g. a run-and-gun)
// can scale or disable it with BB.GAME.hitstop = false | 0..1; the tick sequence is identical either way.
Juice.prototype.takeHitstop = function () {
var h = this.hitstop, scale = BB.GAME && BB.GAME.hitstop;
this.hitstop = 0;
if (scale === false) return 0;
return typeof scale === 'number' ? h * scale : h;
};
BB.Juice = Juice;
})(typeof window !== 'undefined' ? window : globalThis);