312 lines
14 KiB
JavaScript
312 lines
14 KiB
JavaScript
// Total Annihilation — front-end screens.
|
|
//
|
|
// Each builder returns a plain `{ destroy() }` handle and the scene keeps exactly one alive
|
|
// (TotalAnnihilationGame.swapScreen), which is the AdvanceWarsScreens.js router pattern.
|
|
// Screens never touch the simulation: they collect a choice and hand it back through a
|
|
// callback, so the same match can be started from a menu, a save, or the verify script.
|
|
|
|
import * as Phaser from 'phaser';
|
|
import { GAME_WIDTH, GAME_HEIGHT } from '../../config.js';
|
|
import { createOpponentPortrait } from '../../ui/Portrait.js';
|
|
import { FONT } from './TAHud.js';
|
|
|
|
const BG = 0x0b0f16;
|
|
const PANEL = 0x161d29;
|
|
const EDGE = 0x39465e;
|
|
const ACCENT = 0x7dc4ff;
|
|
const TEXT = '#e8eef7';
|
|
const DIM = '#93a3ba';
|
|
|
|
function screen(scene, objs) {
|
|
return {
|
|
objs,
|
|
destroy() { for (const o of objs) o.destroy?.(); objs.length = 0; },
|
|
};
|
|
}
|
|
|
|
function backdrop(scene, objs, title, subtitle) {
|
|
objs.push(scene.add.rectangle(0, 0, GAME_WIDTH, GAME_HEIGHT, BG, 1).setOrigin(0, 0).setDepth(900)
|
|
.setInteractive()); // swallow clicks so they never reach the world beneath
|
|
const t = scene.add.text(GAME_WIDTH / 2, 92, title, {
|
|
fontFamily: FONT, fontSize: '76px', color: TEXT,
|
|
}).setOrigin(0.5).setDepth(901);
|
|
objs.push(t);
|
|
if (subtitle) {
|
|
objs.push(scene.add.text(GAME_WIDTH / 2, 158, subtitle, {
|
|
fontFamily: FONT, fontSize: '24px', color: DIM,
|
|
}).setOrigin(0.5).setDepth(901));
|
|
}
|
|
return t;
|
|
}
|
|
|
|
function button(scene, objs, x, y, w, h, label, onClick, { enabled = true, size = 28 } = {}) {
|
|
const c = scene.add.container(x, y).setDepth(902);
|
|
const bg = scene.add.rectangle(0, 0, w, h, enabled ? 0x22304a : 0x1a2130, 1)
|
|
.setStrokeStyle(2, enabled ? EDGE : 0x2a3244);
|
|
const txt = scene.add.text(0, 0, label, {
|
|
fontFamily: FONT, fontSize: `${size}px`, color: enabled ? TEXT : '#5d6b80',
|
|
}).setOrigin(0.5);
|
|
c.add([bg, txt]);
|
|
// A Container's origin is fixed at 0.5 (read-only in Phaser), so setInteractive() always
|
|
// produces a hit area centred on the container's position. Both children above are drawn
|
|
// centred on (0,0) to match; a child with a top-left origin would render half a button
|
|
// down-right of the area that actually receives the click.
|
|
c.setSize(w, h);
|
|
if (enabled) {
|
|
c.setInteractive({ useHandCursor: true });
|
|
c.on('pointerover', () => bg.setFillStyle(0x2e3f5f, 1));
|
|
c.on('pointerout', () => bg.setFillStyle(0x22304a, 1));
|
|
c.on('pointerdown', (p, lx, ly, ev) => { ev?.stopPropagation(); onClick(); });
|
|
}
|
|
objs.push(c);
|
|
c.label = txt; c.bg = bg;
|
|
return c;
|
|
}
|
|
|
|
/** A labelled left/right cycler over a list of options. */
|
|
function cycler(scene, objs, x, y, label, options, index, onChange) {
|
|
objs.push(scene.add.text(x, y - 30, label, {
|
|
fontFamily: FONT, fontSize: '20px', color: DIM,
|
|
}).setOrigin(0, 0.5).setDepth(902));
|
|
const value = scene.add.text(x + 150, y, String(options[index].label), {
|
|
fontFamily: FONT, fontSize: '28px', color: TEXT,
|
|
}).setOrigin(0.5).setDepth(902);
|
|
objs.push(value);
|
|
let i = index;
|
|
const step = (d) => {
|
|
i = (i + d + options.length) % options.length;
|
|
value.setText(String(options[i].label));
|
|
onChange(options[i].value, i);
|
|
};
|
|
button(scene, objs, x + 26, y, 40, 40, '<', () => step(-1), { size: 22 });
|
|
button(scene, objs, x + 274, y, 40, 40, '>', () => step(1), { size: 22 });
|
|
return { get value() { return options[i].value; } };
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Main menu
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function mainMenu(scene, { hasSave, onSkirmish, onCampaign, onContinue, onLeave }) {
|
|
const objs = [];
|
|
backdrop(scene, objs, 'TOTAL ANNIHILATION', 'Commander — your war machine awaits');
|
|
|
|
const cx = GAME_WIDTH / 2;
|
|
let y = 330;
|
|
button(scene, objs, cx, y, 460, 76, 'SKIRMISH', onSkirmish); y += 100;
|
|
button(scene, objs, cx, y, 460, 76, 'CAMPAIGN', onCampaign); y += 100;
|
|
button(scene, objs, cx, y, 460, 76, 'CONTINUE', onContinue, { enabled: hasSave }); y += 100;
|
|
button(scene, objs, cx, y, 460, 76, 'BACK TO ARCADE', onLeave);
|
|
|
|
objs.push(scene.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 56,
|
|
'Build power and mass, field an army, and leave nothing standing.', {
|
|
fontFamily: FONT, fontSize: '20px', color: DIM,
|
|
}).setOrigin(0.5).setDepth(901));
|
|
return screen(scene, objs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Skirmish setup
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function skirmishSetup(scene, { onBack, onStart }) {
|
|
const objs = [];
|
|
const rules = scene.rules;
|
|
backdrop(scene, objs, 'SKIRMISH', 'Generated map — every seed is a new battlefield');
|
|
|
|
const sk = rules.skirmish ?? {};
|
|
const sizes = Object.keys(sk.sizes ?? { small: 64, medium: 96, large: 128 })
|
|
.map((k) => ({ label: `${k.toUpperCase()} ${sk.sizes?.[k] ?? ''}`.trim(), value: k }));
|
|
const themes = Object.entries(scene.art.themes ?? {}).map(([id, t]) => ({ label: (t.name ?? id).toUpperCase(), value: id }));
|
|
const syms = (sk.symmetries ?? ['mirror-x', 'mirror-y', 'rotational'])
|
|
.map((s) => ({ label: s.replace('-', ' ').toUpperCase(), value: s }));
|
|
const skills = [1, 2, 3, 4, 5].map((n) => ({ label: `SKILL ${n}`, value: n }));
|
|
const victories = (rules.victoryModes ?? []).map((v) => ({ label: v.label, value: v.id, desc: v.desc }));
|
|
const victoryStart = Math.max(0, victories.findIndex((v) => v.value === rules.constants.victoryDefault));
|
|
|
|
const cfg = {
|
|
seed: (Math.random() * 0xffffffff) >>> 0,
|
|
size: sizes[0].value,
|
|
theme: themes[0]?.value ?? 'grasslands',
|
|
symmetry: syms[0].value,
|
|
victory: victories[victoryStart]?.value ?? rules.constants.victoryDefault,
|
|
armies: [
|
|
{ armyId: rules.armies[0].id, commanderId: rules.commanders[0]?.id, skill: 3 },
|
|
{ armyId: rules.armies[1].id, commanderId: rules.commanders[1]?.id, skill: 3 },
|
|
],
|
|
};
|
|
|
|
const left = 300, right = GAME_WIDTH - 700;
|
|
cycler(scene, objs, left, 300, 'MAP SIZE', sizes, 0, (v) => { cfg.size = v; });
|
|
cycler(scene, objs, left, 400, 'THEME', themes, 0, (v) => { cfg.theme = v; });
|
|
cycler(scene, objs, left, 500, 'SYMMETRY', syms, 0, (v) => { cfg.symmetry = v; });
|
|
cycler(scene, objs, left, 600, 'OPPONENT SKILL', skills, 2, (v) => { cfg.armies[1].skill = v; });
|
|
|
|
// Victory condition. The default kills a side with its Commander; the alternative keeps the
|
|
// old rule, where an army is only out once it can no longer build.
|
|
const victoryDesc = scene.add.text(left, 740, victories[victoryStart]?.desc ?? '', {
|
|
fontFamily: FONT, fontSize: '18px', color: DIM, wordWrap: { width: 560 },
|
|
}).setOrigin(0, 0.5).setDepth(902);
|
|
objs.push(victoryDesc);
|
|
if (victories.length) {
|
|
cycler(scene, objs, left, 700, 'VICTORY', victories, victoryStart, (v, i) => {
|
|
cfg.victory = v;
|
|
victoryDesc.setText(victories[i].desc ?? '');
|
|
});
|
|
}
|
|
|
|
const seedText = scene.add.text(left, 810, `SEED ${cfg.seed}`, {
|
|
fontFamily: FONT, fontSize: '22px', color: DIM,
|
|
}).setOrigin(0, 0.5).setDepth(902);
|
|
objs.push(seedText);
|
|
button(scene, objs, left + 420, 810, 150, 44, 'REROLL', () => {
|
|
cfg.seed = (Math.random() * 0xffffffff) >>> 0;
|
|
seedText.setText(`SEED ${cfg.seed}`);
|
|
}, { size: 20 });
|
|
|
|
// Commander picker — the armies double as the shared opponent personas.
|
|
const portraits = [];
|
|
const drawCommanders = () => {
|
|
for (const p of portraits) p.destroy?.();
|
|
portraits.length = 0;
|
|
rules.commanders.forEach((c, i) => {
|
|
const col = i % 2, row = Math.floor(i / 2);
|
|
const x = right + col * 240, y = 320 + row * 210;
|
|
const chosen = cfg.armies[1].commanderId === c.id;
|
|
const box = scene.add.rectangle(x, y, 210, 190, chosen ? 0x2b3f5e : PANEL, 1)
|
|
.setStrokeStyle(2, chosen ? ACCENT : EDGE).setDepth(901)
|
|
.setInteractive({ useHandCursor: true });
|
|
box.on('pointerdown', () => {
|
|
cfg.armies[1].commanderId = c.id;
|
|
cfg.armies[1].armyId = c.armyId;
|
|
// The player always takes the other side, so a CORE opponent means an ARM player.
|
|
const mine = rules.commanders.find((o) => o.armyId !== c.armyId) ?? rules.commanders[0];
|
|
cfg.armies[0].commanderId = mine.id;
|
|
cfg.armies[0].armyId = mine.armyId;
|
|
drawCommanders();
|
|
});
|
|
const label = scene.add.text(x, y + 66, c.name, {
|
|
fontFamily: FONT, fontSize: '20px', color: chosen ? TEXT : DIM,
|
|
}).setOrigin(0.5).setDepth(903);
|
|
portraits.push(box, label);
|
|
objs.push(box, label);
|
|
try {
|
|
const opp = scene.oppById?.[c.opponentId];
|
|
if (opp) {
|
|
const port = createOpponentPortrait(scene, opp, x, y - 24, 52, 902, { playIntro: false });
|
|
for (const o of [].concat(port ?? [])) if (o) { portraits.push(o); objs.push(o); }
|
|
}
|
|
} catch (_) { /* portraits are decorative — a missing one must not block play */ }
|
|
});
|
|
};
|
|
objs.push(scene.add.text(right + 120, 250, 'ENEMY COMMANDER', {
|
|
fontFamily: FONT, fontSize: '22px', color: DIM,
|
|
}).setOrigin(0.5).setDepth(902));
|
|
drawCommanders();
|
|
|
|
button(scene, objs, GAME_WIDTH / 2 - 180, GAME_HEIGHT - 110, 300, 76, 'BACK', onBack);
|
|
button(scene, objs, GAME_WIDTH / 2 + 180, GAME_HEIGHT - 110, 300, 76, 'LAUNCH', () => onStart(cfg));
|
|
return screen(scene, objs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Campaign list
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function campaignList(scene, campaign, cleared, { onBack, onPick }) {
|
|
const objs = [];
|
|
const missions = campaign.missions ?? [];
|
|
backdrop(scene, objs, 'CAMPAIGN',
|
|
missions.length ? `${cleared} of ${missions.length} missions cleared` : 'No missions installed yet');
|
|
|
|
missions.forEach((m, i) => {
|
|
const col = i % 3, row = Math.floor(i / 3);
|
|
const x = GAME_WIDTH / 2 + (col - 1) * 500, y = 300 + row * 130;
|
|
// Missions unlock in order — the same rule the puzzle_progress endpoint enforces.
|
|
const unlocked = i <= cleared;
|
|
const b = button(scene, objs, x, y, 460, 104,
|
|
`${String(i + 1).padStart(2, '0')} ${unlocked ? m.name : 'LOCKED'}`,
|
|
() => onPick(i), { enabled: unlocked, size: 26 });
|
|
if (unlocked && i < cleared) b.label.setColor('#9ce6a0');
|
|
});
|
|
|
|
button(scene, objs, GAME_WIDTH / 2, GAME_HEIGHT - 110, 300, 76, 'BACK', onBack);
|
|
return screen(scene, objs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Pause
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function pause(scene, { settings, onResume, onSave, onQuit, onToggleFog, onToggleEdge, onSpeed }) {
|
|
const objs = [];
|
|
objs.push(scene.add.rectangle(0, 0, GAME_WIDTH, GAME_HEIGHT, 0x000000, 0.62)
|
|
.setOrigin(0, 0).setDepth(900).setInteractive());
|
|
const cx = GAME_WIDTH / 2, cy = GAME_HEIGHT / 2;
|
|
const panel = scene.add.rectangle(cx, cy, 620, 640, PANEL, 0.98)
|
|
.setStrokeStyle(3, EDGE).setDepth(901);
|
|
objs.push(panel);
|
|
objs.push(scene.add.text(cx, cy - 260, 'PAUSED', {
|
|
fontFamily: FONT, fontSize: '54px', color: TEXT,
|
|
}).setOrigin(0.5).setDepth(902));
|
|
|
|
const fogBtn = button(scene, objs, cx, cy - 150, 440, 62,
|
|
`FOG OF WAR: ${settings.fog !== false ? 'ON' : 'OFF'}`, () => {
|
|
const on = !(settings.fog !== false);
|
|
onToggleFog(on);
|
|
fogBtn.label.setText(`FOG OF WAR: ${on ? 'ON' : 'OFF'}`);
|
|
}, { size: 24 });
|
|
|
|
const edgeBtn = button(scene, objs, cx, cy - 76, 440, 62,
|
|
`EDGE SCROLL: ${settings.edgeScroll !== false ? 'ON' : 'OFF'}`, () => {
|
|
const on = !(settings.edgeScroll !== false);
|
|
onToggleEdge(on);
|
|
edgeBtn.label.setText(`EDGE SCROLL: ${on ? 'ON' : 'OFF'}`);
|
|
}, { size: 24 });
|
|
|
|
const speeds = [0.5, 1, 2];
|
|
let si = 1;
|
|
const speedBtn = button(scene, objs, cx, cy - 2, 440, 62, 'SPEED: 1x', () => {
|
|
si = (si + 1) % speeds.length;
|
|
onSpeed(speeds[si]);
|
|
speedBtn.label.setText(`SPEED: ${speeds[si]}x`);
|
|
}, { size: 24 });
|
|
|
|
button(scene, objs, cx, cy + 88, 440, 66, 'RESUME', onResume);
|
|
button(scene, objs, cx, cy + 166, 440, 66, 'SAVE', onSave);
|
|
button(scene, objs, cx, cy + 244, 440, 66, 'QUIT TO MENU', onQuit);
|
|
return screen(scene, objs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Results
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export function results(scene, { won, stats, elapsed }, { onMenu, onAgain }) {
|
|
const objs = [];
|
|
objs.push(scene.add.rectangle(0, 0, GAME_WIDTH, GAME_HEIGHT, 0x000000, 0.7)
|
|
.setOrigin(0, 0).setDepth(900).setInteractive());
|
|
const cx = GAME_WIDTH / 2, cy = GAME_HEIGHT / 2;
|
|
objs.push(scene.add.rectangle(cx, cy, 760, 560, PANEL, 0.98).setStrokeStyle(3, EDGE).setDepth(901));
|
|
objs.push(scene.add.text(cx, cy - 210, won ? 'VICTORY' : 'DEFEAT', {
|
|
fontFamily: FONT, fontSize: '72px', color: won ? '#9ce6a0' : '#ff8a6b',
|
|
}).setOrigin(0.5).setDepth(902));
|
|
|
|
const mins = Math.floor(elapsed / 60), secs = Math.floor(elapsed % 60);
|
|
const lines = [
|
|
`Duration ${mins}m ${String(secs).padStart(2, '0')}s`,
|
|
`Units built ${stats.built}`,
|
|
`Units lost ${stats.lost}`,
|
|
`Kills ${stats.kills}`,
|
|
`Mass left ${stats.mass}`,
|
|
`Energy left ${stats.energy}`,
|
|
];
|
|
objs.push(scene.add.text(cx, cy - 100, lines.join('\n'), {
|
|
fontFamily: FONT, fontSize: '26px', color: TEXT, align: 'left', lineSpacing: 10,
|
|
}).setOrigin(0.5, 0).setDepth(902));
|
|
|
|
button(scene, objs, cx - 170, cy + 210, 300, 72, 'PLAY AGAIN', onAgain);
|
|
button(scene, objs, cx + 170, cy + 210, 300, 72, 'MENU', onMenu);
|
|
return screen(scene, objs);
|
|
}
|