278 lines
10 KiB
JavaScript
278 lines
10 KiB
JavaScript
// The tunnel mouth: a carved stone serpent head seen from above, jaws splayed
|
|
// around a black gullet the marbles roll into. Canvas rather than Graphics
|
|
// because the whole read depends on gradients — the throat has to go opaque
|
|
// black fast enough to swallow a marble, and the stone needs one consistent
|
|
// light (upper-left, same as the marbles' baked specular).
|
|
//
|
|
// Drawn in the portal's own frame: the path runs along +x, the axis at H/2, the
|
|
// mouth line at x = MOUTH, and everything past it is over the tunnel. Nothing
|
|
// here is direction-specific, which is what lets one texture serve as both an
|
|
// entrance and — rotated a half turn about the mouth — an exit. The head is
|
|
// symmetric across the path axis, so that half turn mirrors nothing the eye
|
|
// can see.
|
|
//
|
|
// The head is ONE closed silhouette with the mouth cut through its front, not
|
|
// two jaw plates: plates leave a gap at the neck that the background shows
|
|
// through, and no amount of nudging control points closes it reliably. So the
|
|
// outline is generated from three profile functions of x — `lip` (the inner
|
|
// edge of the mouth), `outer` = lip + jaw thickness, and `skull` behind the
|
|
// mouth's apex — which makes the jaws thick by construction and lets the fangs
|
|
// land exactly on the lip.
|
|
//
|
|
// It comes out as TWO textures, drawn at the same position but on opposite
|
|
// sides of the chain, because a maw has to obey two rules that pull apart:
|
|
//
|
|
// * the STONE (head, eyes, carving) is scenery, so it hangs BELOW the path —
|
|
// a live section of path crossing over a tunnel, and the marbles riding
|
|
// it, must read as being on top of the gateway, and on a coil or a figure
|
|
// eight a head this size lands on a neighbouring lane all the time;
|
|
// * the MOUTH (the gullet and the fangs around it) is the hole itself, so it
|
|
// hangs ABOVE the chain — that is what swallows a marble as it rolls in,
|
|
// and it is narrow enough to cover only the tunnel's own path.
|
|
//
|
|
// Lives in its own file because ZumaGame draws the real thing and ZumaEditor
|
|
// draws the same art scaled into its board preview; an author placing a mouth
|
|
// needs to see the stone that will actually cover the path there.
|
|
|
|
export const PORTAL = { W: 224, H: 216, MOUTH: 12 };
|
|
|
|
// Origin fraction that puts a portal sprite's anchor exactly on the mouth line.
|
|
export const PORTAL_ORIGIN_X = PORTAL.MOUTH / PORTAL.W;
|
|
|
|
export const PORTAL_STONE = 'zuma-portal-stone';
|
|
export const PORTAL_MOUTH = 'zuma-portal-mouth';
|
|
|
|
// How far the head reaches back from the mouth line. TUNNEL.MIN_LEN is set
|
|
// from this: both maws of a tunnel reach inward, so a tunnel shorter than
|
|
// twice this would have its own two heads growing through each other.
|
|
export const PORTAL_REACH = 196 - PORTAL.MOUTH;
|
|
|
|
const CY = PORTAL.H / 2;
|
|
|
|
const X0 = 6; // front of the head; the mouth is cut through here
|
|
const XL = 132; // apex of the mouth, where the two lips meet
|
|
const XB = 196; // back of the neck
|
|
|
|
// Inner edge of the mouth. Zero at the apex and, by starting the sine before
|
|
// X0, already wide open at the front — that is what cuts the notch through the
|
|
// snout instead of leaving a closed almond inside it. The mouth is kept short
|
|
// relative to the skull: run it too far back and the gullet stops reading as a
|
|
// mouth and becomes a long black wedge.
|
|
function lip(x) {
|
|
const u = (x + 26) / (XL + 26);
|
|
return u <= 0 ? 0 : 62 * Math.sin(Math.PI * u ** 0.72);
|
|
}
|
|
const thick = (x) => 30 + (16 * (x - X0)) / (XL - X0);
|
|
const outer = (x) => lip(x) + thick(x);
|
|
|
|
// Cranium behind the mouth: a slight bulge, then a taper into the neck. It
|
|
// stops at a blunt neck rather than a point — tapered to nothing the whole
|
|
// thing reads as a leaf or an arrowhead, not a head on a body.
|
|
function skull(x) {
|
|
const u = (x - XL) / (XB - XL);
|
|
return 46 + 14 * Math.sin(Math.PI * u) - 6 * u * u;
|
|
}
|
|
|
|
function trace(c, a, b, n, f) {
|
|
for (let i = 0; i <= n; i++) {
|
|
const x = a + ((b - a) * i) / n;
|
|
c.lineTo(x, f(x));
|
|
}
|
|
}
|
|
|
|
// The head: out along one jaw, round the blunt neck, back along the other,
|
|
// then forward into the mouth notch and out again. The jaw tips are bellied
|
|
// forward rather than cut square, so the mouth looks agape instead of sawn off.
|
|
function headPath(c) {
|
|
const oU = CY - outer(X0), lU = CY - lip(X0);
|
|
const oL = CY + outer(X0), lL = CY + lip(X0);
|
|
c.beginPath();
|
|
c.moveTo(X0, oU);
|
|
trace(c, X0, XL, 40, (x) => CY - outer(x));
|
|
trace(c, XL, XB, 18, (x) => CY - skull(x));
|
|
c.quadraticCurveTo(XB + 12, CY - skull(XB) * 0.55, XB + 13, CY);
|
|
c.quadraticCurveTo(XB + 12, CY + skull(XB) * 0.55, XB, CY + skull(XB));
|
|
trace(c, XB, XL, 18, (x) => CY + skull(x));
|
|
trace(c, XL, X0, 40, (x) => CY + outer(x));
|
|
c.quadraticCurveTo(X0 - 11, (oL + lL) / 2, X0, lL);
|
|
trace(c, X0, XL, 40, (x) => CY + lip(x));
|
|
trace(c, XL, X0, 40, (x) => CY - lip(x));
|
|
c.quadraticCurveTo(X0 - 11, (oU + lU) / 2, X0, oU);
|
|
c.closePath();
|
|
}
|
|
|
|
// The gullet, spilling a little forward of the head so the path visibly runs
|
|
// into shadow rather than meeting a hard edge, and rounded off at the back
|
|
// instead of ending in the sharp black wedge a straight taper gives.
|
|
function gulletPath(c) {
|
|
const front = X0 - 14;
|
|
const xa = XL - 26;
|
|
c.beginPath();
|
|
c.moveTo(front, CY - lip(X0));
|
|
trace(c, X0, xa, 36, (x) => CY - lip(x));
|
|
c.quadraticCurveTo(XL + 10, CY, xa, CY + lip(xa));
|
|
trace(c, xa, X0, 36, (x) => CY + lip(x));
|
|
c.lineTo(front, CY + lip(X0));
|
|
c.closePath();
|
|
}
|
|
|
|
// A carved band across the neck. More than decoration: it is what makes the
|
|
// silhouette read as a gateway someone built rather than an animal.
|
|
function collarPath(c, xa, xb, inset) {
|
|
c.beginPath();
|
|
c.moveTo(xa, CY - skull(xa) + inset);
|
|
trace(c, xa, xb, 10, (x) => CY - skull(x) + inset);
|
|
c.lineTo(xb, CY + skull(xb) - inset);
|
|
trace(c, xb, xa, 10, (x) => CY + skull(x) - inset);
|
|
c.closePath();
|
|
}
|
|
|
|
function fillGullet(c) {
|
|
gulletPath(c);
|
|
const g = c.createLinearGradient(X0 - 14, CY, XL, CY);
|
|
g.addColorStop(0, '#170e07');
|
|
g.addColorStop(0.32, '#070403');
|
|
g.addColorStop(1, '#000000');
|
|
c.fillStyle = g;
|
|
c.fill();
|
|
}
|
|
|
|
// Layer 1 — the carved head. Sits under the path.
|
|
function paintStone(c) {
|
|
headPath(c);
|
|
const stone = c.createLinearGradient(0, CY - 108, PORTAL.W * 0.55, CY + 108);
|
|
stone.addColorStop(0, '#a89165');
|
|
stone.addColorStop(0.34, '#7a6440');
|
|
stone.addColorStop(0.68, '#463621');
|
|
stone.addColorStop(1, '#241b0e');
|
|
c.fillStyle = stone;
|
|
c.fill();
|
|
c.lineWidth = 6;
|
|
c.strokeStyle = '#0a0704'; // the same contrast border the path carries
|
|
c.stroke();
|
|
c.lineWidth = 2;
|
|
c.strokeStyle = 'rgba(214,192,146,0.32)';
|
|
c.stroke();
|
|
|
|
// Carved scale chevrons, following the jaw outward and shrinking down the
|
|
// snout, so the stone reads as worked rather than poured.
|
|
c.lineWidth = 3;
|
|
c.strokeStyle = 'rgba(20,13,6,0.5)';
|
|
for (const sg of [-1, 1]) {
|
|
for (let k = 0; k < 5; k++) {
|
|
const x = 28 + k * 22;
|
|
const y = CY + sg * (outer(x) - 15);
|
|
const w = 11 - k;
|
|
c.beginPath();
|
|
c.moveTo(x - w, y - sg * w);
|
|
c.lineTo(x, y);
|
|
c.lineTo(x + w, y - sg * w);
|
|
c.stroke();
|
|
}
|
|
}
|
|
|
|
// Nostril slits on the snout.
|
|
c.fillStyle = 'rgba(16,10,4,0.75)';
|
|
for (const sg of [-1, 1]) {
|
|
const x = 22;
|
|
const y = CY + sg * (outer(x) - 13);
|
|
c.beginPath();
|
|
c.ellipse(x, y, 6, 3, sg * 0.5, 0, Math.PI * 2);
|
|
c.fill();
|
|
}
|
|
|
|
// Carved collar at the neck, catching the same upper-left light. Kept in the
|
|
// family of the head's own stone so it reads as cut from it, not bolted on.
|
|
const band = c.createLinearGradient(0, CY - 62, 0, CY + 62);
|
|
band.addColorStop(0, '#93805c');
|
|
band.addColorStop(0.5, '#5b4a2d');
|
|
band.addColorStop(1, '#221a0d');
|
|
collarPath(c, 176, 194, 4);
|
|
c.fillStyle = band;
|
|
c.fill();
|
|
c.lineWidth = 3;
|
|
c.strokeStyle = 'rgba(12,8,3,0.75)';
|
|
c.stroke();
|
|
|
|
// Eyes, high on the cranium behind the jaw hinge, where a snake's sit.
|
|
for (const sg of [-1, 1]) {
|
|
const ex = 148;
|
|
const ey = CY + sg * 38;
|
|
const tilt = sg * -0.4;
|
|
c.beginPath();
|
|
c.ellipse(ex, ey, 21, 15, tilt, 0, Math.PI * 2);
|
|
c.fillStyle = '#120b05';
|
|
c.fill();
|
|
const eye = c.createRadialGradient(ex, ey, 1, ex, ey, 18);
|
|
eye.addColorStop(0, 'rgba(255,242,190,1)');
|
|
eye.addColorStop(0.34, 'rgba(255,168,44,0.97)');
|
|
eye.addColorStop(1, 'rgba(150,48,8,0)');
|
|
c.fillStyle = eye;
|
|
c.beginPath();
|
|
c.ellipse(ex, ey, 18, 12, tilt, 0, Math.PI * 2);
|
|
c.fill();
|
|
c.fillStyle = 'rgba(18,7,2,0.92)'; // slit pupil
|
|
c.beginPath();
|
|
c.ellipse(ex, ey, 3.5, 10, tilt, 0, Math.PI * 2);
|
|
c.fill();
|
|
}
|
|
}
|
|
|
|
// Layer 2 — the hole. Sits over the chain, and is the thing that actually hides
|
|
// a marble. Kept to the gullet and the teeth around it, so it never reaches a
|
|
// neighbouring lane.
|
|
function paintMouth(c) {
|
|
// The path darkens as it runs into the maw, so marbles look like they are
|
|
// rolling into shadow before the jaws ever touch them. A soft gradient, not
|
|
// an occluder — anything else passing nearby is shaded, never hidden.
|
|
const pool = c.createRadialGradient(PORTAL.MOUTH, CY, 8, PORTAL.MOUTH, CY, 108);
|
|
pool.addColorStop(0, 'rgba(0,0,0,0.85)');
|
|
pool.addColorStop(0.5, 'rgba(0,0,0,0.34)');
|
|
pool.addColorStop(1, 'rgba(0,0,0,0)');
|
|
c.fillStyle = pool;
|
|
c.beginPath();
|
|
c.ellipse(PORTAL.MOUTH, CY, 108, 70, 0, 0, Math.PI * 2);
|
|
c.fill();
|
|
|
|
// Opaque from just outside the mouth line to well inside, so a marble is
|
|
// fully covered by the time TUNNEL.FADE has taken it to alpha 0.
|
|
fillGullet(c);
|
|
|
|
// Fangs standing on the lip, longest at the front. On this layer so a marble
|
|
// passes *between* the teeth on its way down.
|
|
for (const sg of [-1, 1]) {
|
|
for (const [fx, fl] of [[18, 32], [48, 28], [82, 21], [112, 12]]) {
|
|
const base = CY + sg * lip(fx);
|
|
const tip = CY + sg * (lip(fx) - fl);
|
|
c.beginPath();
|
|
c.moveTo(fx - 11, base);
|
|
c.lineTo(fx + 11, base);
|
|
c.lineTo(fx + 2, tip);
|
|
c.closePath();
|
|
const bone = c.createLinearGradient(0, base, 0, tip);
|
|
bone.addColorStop(0, '#b3a583');
|
|
bone.addColorStop(1, '#f4ecd6');
|
|
c.fillStyle = bone;
|
|
c.fill();
|
|
c.lineWidth = 1.5;
|
|
c.strokeStyle = 'rgba(20,13,6,0.75)';
|
|
c.stroke();
|
|
}
|
|
}
|
|
}
|
|
|
|
function bake(scene, key, paint) {
|
|
if (scene.textures.exists(key)) return;
|
|
const tex = scene.textures.createCanvas(key, PORTAL.W, PORTAL.H);
|
|
if (!tex) return;
|
|
const c = tex.getContext();
|
|
c.clearRect(0, 0, PORTAL.W, PORTAL.H);
|
|
paint(c);
|
|
tex.refresh();
|
|
}
|
|
|
|
export function buildPortalTextures(scene) {
|
|
bake(scene, PORTAL_STONE, paintStone);
|
|
bake(scene, PORTAL_MOUTH, paintMouth);
|
|
}
|