GameDojo / environments /games /web /engine /renderer.js
hanoiy's picture
Add GameDojo game environments
dc5a369 verified
Raw History Blame
5.82 kB
// Renderer: 854x480 frame of any registered case. Pure presentation: reads the Case, never writes it.
// Camera is fixed (the whole arena is always visible) so pixel agents see the same framing every frame.
// Layers: scene background (cached) -> ambient -> boss hazards + boss -> body -> particles -> scene foreground
// (cached) -> flashes -> HUD (body HUD, boss bar, status line). The look comes from the game's hooks:
// ART.scene(doc, caseId) -> {bg, fg}, ART.ambient(x, caseId, t) (game art/scenes.js)
// BB.BOSS_DRAW[caseId] = {hazards(x, c, t, V), body(x, c, t, V), bar(boss) -> colour (optional)}
// BB.BODY_DRAW[body.id] = {draw(x, c, t, V, juice), hud(x, c, V)}
// Layout (BB.VIEW, ART.view) is computed here from the game's BB.ARENA, so load this after arena.js and before
// the game's art / draw files; the game adds its palette as BB.VIEW.PAL.
(function (g) {
var BB = g.BB, ART = BB.ART;
var A = BB.ARENA, W = BB.RES[0], H = BB.RES[1];
var S = 30; // px per game unit
var OX = Math.round((W - A.W * S) / 2); // 72 px side walls
var FLOOR = 466;
var ROOF = Math.round(FLOOR - A.H * S);
var cameraX = 0;
function sx(x) { return OX + (x - cameraX) * S; }
function sy(y) { return FLOOR - y * S; }
BB.BOSS_DRAW = BB.BOSS_DRAW || {};
BB.BODY_DRAW = BB.BODY_DRAW || {};
var V = BB.VIEW = { S: S, OX: OX, FLOOR: FLOOR, PAL: {}, sx: sx, sy: sy, W: W, H: H };
ART.view = { W: W, H: H, OX: OX, FLOOR: FLOOR, ROOF: ROOF, S: S };
var sceneCache = {};
function scene(doc, id) { return sceneCache[id] || (sceneCache[id] = ART.scene(doc, id)); }
// ---------- HUD: the body's own HUD (top left), framed boss bar (top right), status line ----------
function hud(x, c, extra) {
var w = c.boss, bd = BB.BODY_DRAW[c.body.id];
if (bd && bd.fullHud) { bd.fullHud(x, c, V, extra); return; }
x.save();
if (bd && bd.hud) bd.hud(x, c, V);
// boss bar: thin ornate frame, colour from the boss module, a pale "recent damage" trail is not kept (no memory)
var bw = 300, bx = W - OX - bw - 14, by = 24, fr2 = Math.max(0, w.hp / w.max_hp);
var dr = BB.BOSS_DRAW[c.id], col = dr && dr.bar ? dr.bar(w) : w.escalated ? '#e8784a' : '#d8c27a';
var prev = ART.smooth('bossbar', { f: fr2 }, 3).f;
x.fillStyle = 'rgba(4,7,10,0.75)'; x.fillRect(bx - 3, by - 3, bw + 6, 11);
x.fillStyle = 'rgba(255,240,220,0.35)'; x.fillRect(bx, by, bw * Math.max(fr2, prev), 5);
var bg2 = x.createLinearGradient(0, by, 0, by + 5); bg2.addColorStop(0, '#ffffff'); bg2.addColorStop(0.35, col); bg2.addColorStop(1, col);
x.fillStyle = bg2; x.fillRect(bx, by, bw * fr2, 5);
x.strokeStyle = 'rgba(220,226,230,0.55)'; x.lineWidth = 1; x.strokeRect(bx - 3.5, by - 3.5, bw + 7, 12);
[bx - 3.5, bx + bw + 3.5].forEach(function (ex, k) { // diamond finials
x.fillStyle = '#d9dfe2'; x.beginPath(); x.moveTo(ex, by - 2); x.lineTo(ex + (k ? 6 : -6), by + 2.5); x.lineTo(ex, by + 7); x.lineTo(ex + (k ? -2 : 2), by + 2.5); x.closePath(); x.fill();
});
x.font = '600 12px Georgia, "Times New Roman", serif'; x.textAlign = 'right';
var title = String(c.def && c.def.title || 'Boss').toUpperCase().split('').join(String.fromCharCode(8202));
x.fillStyle = 'rgba(0,0,0,0.6)'; x.fillText(title + ' ' + w.hp + ' / ' + w.max_hp, bx + bw + 1, by + 26);
x.fillStyle = '#e9ecee'; x.fillText(title + ' ' + w.hp + ' / ' + w.max_hp, bx + bw, by + 25);
x.textAlign = 'left'; x.font = '12px "Segoe UI", sans-serif';
x.fillStyle = 'rgba(233,241,242,0.7)';
var secs = c.t / BB.FPS, mm = Math.floor(secs / 60), ss = Math.floor(secs % 60);
x.fillText('d=' + c.d.toFixed(2) + ' ' + mm + ':' + (ss < 10 ? '0' : '') + ss + (extra ? ' ' + extra : ''), OX + 12, 72);
x.restore();
}
// ---------- public ----------
function Renderer(canvas, opts) {
// willReadFrequently: the policy env and the video harness call getImageData/toDataURL every frame.
// Without it Chromium may switch rasteriser mid-run, so identical replays yield different pixels.
this.canvas = canvas; this.ctx = canvas.getContext('2d', { willReadFrequently: true });
canvas.width = W; canvas.height = H;
this.opts = opts || {};
this.juice = new BB.Juice();
this.artStore = {};
}
// t: presentation time in seconds; feeds juice with the case events since the last draw
Renderer.prototype.draw = function (c, t, extra) {
var x = this.ctx, J = this.juice;
cameraX = c.arena && c.arena.cameraX != null ? c.arena.cameraX :
c.arena && c.arena.W > A.W ? BB.clamp(c.body.x - A.W/2, 0, c.arena.W - A.W) : 0;
V.cameraX = cameraX;
ART.begin(this.artStore, c, t);
J.consume(c, this);
x.setTransform(1, 0, 0, 1, 0, 0);
var sc = scene(this.canvas.ownerDocument || document, c.id), dr = BB.BOSS_DRAW[c.id], bd = BB.BODY_DRAW[c.body.id];
x.drawImage(typeof sc.background === 'function' ? sc.background(c) : sc.bg, sc.world ? -cameraX*S : 0, 0);
ART.ambient(x, c.id, t);
var sh = this.opts.noShake ? [0, 0] : J.offset(t);
x.translate(sh[0], sh[1]);
if (dr) { dr.hazards(x, c, t, V); dr.body(x, c, t, V); }
if (bd) bd.draw(x, c, t, V, J);
J.drawParticles(x);
x.setTransform(1, 0, 0, 1, 0, 0);
x.drawImage(sc.fg, sc.world ? -cameraX*S : 0, 0);
if (J.flash > 0) { x.fillStyle = 'rgba(255,255,255,' + (J.flash * 0.35) + ')'; x.fillRect(0, 0, W, H); }
if (J.redFlash > 0) { x.fillStyle = 'rgba(160,0,0,' + (J.redFlash * 0.3) + ')'; x.fillRect(0, 0, W, H); }
hud(x, c, extra);
};
Renderer.prototype.sx = sx; Renderer.prototype.sy = sy;
BB.Renderer = Renderer;
})(typeof window !== 'undefined' ? window : globalThis);