212 lines
7.7 KiB
JavaScript
212 lines
7.7 KiB
JavaScript
import { state, hasSave, notify, save } from './state.js';
|
|
import { BUILDINGS } from './data/buildings.js';
|
|
import { drawBuildingIcon, drawPipeIcon } from './render.js';
|
|
import { RESOURCES } from './data/pipes.js';
|
|
import { WIND_DIRS, REFUND } from './data/world.js';
|
|
import { windAt } from './terrain.js';
|
|
|
|
const $ = id => document.getElementById(id);
|
|
|
|
export function initUI({ onNew, onContinue, onMenu }) {
|
|
$('btn-new').onclick = onNew;
|
|
$('btn-continue').onclick = onContinue;
|
|
$('btn-menu').onclick = onMenu;
|
|
for (const [name, m] of Object.entries(MENUS)) $(m.btn).onclick = () => toggleMenu(name);
|
|
document.addEventListener('keydown', e => {
|
|
const inGame = !$('game').classList.contains('hidden');
|
|
if (inGame) watchDevCode(e);
|
|
if (e.key === 'Escape') setMenu(null);
|
|
else if ((e.key === 'u' || e.key === 'U') && inGame) toggleMenu('pipes');
|
|
});
|
|
buildLists();
|
|
showMenu();
|
|
}
|
|
|
|
function drawDemolishIcon(ctx) {
|
|
ctx.fillStyle = '#c03030';
|
|
for (let i = 0; i < 18; i++) { // croix en pixels
|
|
ctx.fillRect(7 + i, 7 + i, 3, 3);
|
|
ctx.fillRect(24 - i, 7 + i, 3, 3);
|
|
}
|
|
}
|
|
|
|
function addTool(menu, id, name, drawIcon, sub, cost = 0) {
|
|
const icon = document.createElement('canvas');
|
|
icon.width = icon.height = 32;
|
|
drawIcon(icon.getContext('2d'));
|
|
|
|
const label = document.createElement('span');
|
|
label.textContent = name;
|
|
|
|
const btn = document.createElement('button');
|
|
btn.type = 'button';
|
|
btn.className = 'tool';
|
|
btn.dataset.tool = id;
|
|
btn.dataset.menu = menu; // « Démolir » existe dans les deux menus : on ne surligne que celui qui est ouvert
|
|
btn.setAttribute('aria-pressed', 'false');
|
|
btn.dataset.cost = cost;
|
|
const subLabel = document.createElement('span');
|
|
subLabel.className = 'cost';
|
|
subLabel.textContent = sub;
|
|
btn.append(icon, label, subLabel);
|
|
btn.onclick = () => setTool(state.tool === id ? null : id);
|
|
|
|
const li = document.createElement('li');
|
|
li.append(btn);
|
|
$(MENUS[menu].list).append(li);
|
|
}
|
|
|
|
// Menus de construction : un bouton sur le côté qui ouvre un menu déroulant. Un seul menu ouvert à la fois.
|
|
// build : bâtiments (vue de surface) ; pipes : tuyaux (vue sous-terre, « Retirer » démolit un tuyau).
|
|
const MENUS = {
|
|
build: { btn: 'btn-build', list: 'build-list', mode: 'surface' },
|
|
pipes: { btn: 'btn-layer', list: 'pipe-list', mode: 'underground' },
|
|
};
|
|
let openMenu = null;
|
|
|
|
function buildLists() {
|
|
for (const [id, building] of Object.entries(BUILDINGS)) {
|
|
addTool('build', id, building.name, ctx => drawBuildingIcon(ctx, id), `${building.cost ?? 0} €`, building.cost ?? 0);
|
|
}
|
|
addTool('build', 'demolish', 'Démolir', drawDemolishIcon, `${Math.round(REFUND * 100)} % remboursé`);
|
|
|
|
for (const [id, r] of Object.entries(RESOURCES)) {
|
|
addTool('pipes', `pipe:${id}`, r.pipe, ctx => drawPipeIcon(ctx, id), `${r.cost} € / case`, r.cost);
|
|
}
|
|
addTool('pipes', 'demolish', 'Retirer', drawDemolishIcon, `${Math.round(REFUND * 100)} % remboursé`);
|
|
}
|
|
|
|
export function setTool(tool) {
|
|
state.tool = tool;
|
|
document.querySelectorAll('.dropdown .tool').forEach(btn => {
|
|
const on = btn.dataset.tool === tool && btn.dataset.menu === openMenu;
|
|
btn.classList.toggle('selected', on);
|
|
btn.setAttribute('aria-pressed', String(on));
|
|
});
|
|
}
|
|
|
|
function closeMenu() {
|
|
const m = MENUS[openMenu];
|
|
$(m.list).classList.add('hidden');
|
|
$(m.btn).classList.remove('active');
|
|
$(m.btn).setAttribute('aria-expanded', 'false');
|
|
openMenu = null;
|
|
}
|
|
|
|
// Ouvre un menu (ou ferme tout avec null). Le menu déjà ouvert est fermé d'abord ; la vue suit :
|
|
// sous-terre quand le menu des tuyaux est ouvert, surface sinon. Changer de menu désélectionne l'outil.
|
|
export function setMenu(name) {
|
|
if (name === openMenu) return;
|
|
if (openMenu) closeMenu();
|
|
openMenu = name;
|
|
if (name) {
|
|
const m = MENUS[name];
|
|
$(m.list).classList.remove('hidden');
|
|
$(m.btn).classList.add('active');
|
|
$(m.btn).setAttribute('aria-expanded', 'true');
|
|
}
|
|
state.mode = name ? MENUS[name].mode : 'surface';
|
|
state.hover = null;
|
|
setTool(null);
|
|
}
|
|
|
|
function toggleMenu(name) {
|
|
setMenu(openMenu === name ? null : name);
|
|
}
|
|
|
|
// Mode dev : taper « devmode » pendant une partie (sans rien sélectionner de particulier) ajoute de l'argent.
|
|
const DEV_CODE = 'devmode';
|
|
const DEV_MONEY = 99999;
|
|
let devBuffer = '';
|
|
|
|
function watchDevCode(e) {
|
|
if (e.ctrlKey || e.metaKey || e.altKey || e.key.length !== 1) return; // lettres seules
|
|
devBuffer = (devBuffer + e.key.toLowerCase()).slice(-DEV_CODE.length);
|
|
if (devBuffer !== DEV_CODE) return;
|
|
devBuffer = '';
|
|
state.money += DEV_MONEY;
|
|
notify(`Mode dev : +${DEV_MONEY.toLocaleString('fr-FR')} €`);
|
|
save();
|
|
}
|
|
|
|
export function showMenu() {
|
|
setMenu(null);
|
|
$('menu').classList.remove('hidden');
|
|
$('game').classList.add('hidden');
|
|
$('btn-continue').disabled = !hasSave();
|
|
}
|
|
|
|
export function showGame() {
|
|
setMenu(null);
|
|
state.mode = 'surface';
|
|
$('menu').classList.add('hidden');
|
|
$('game').classList.remove('hidden');
|
|
}
|
|
|
|
// ---------- HUD : production, vent, infos de la case survolée ----------
|
|
|
|
const fmt = n => n.toLocaleString('fr-FR', { minimumFractionDigits: 1, maximumFractionDigits: 1 });
|
|
|
|
function setText(id, text) {
|
|
const el = $(id);
|
|
if (el.textContent !== text) el.textContent = text; // évite de toucher au DOM sans raison
|
|
}
|
|
|
|
function buildingInfo(cell, def) {
|
|
if (cell.status === 'unmanaged') return `${def.name} : non gérée (pas assez de bureaux)`;
|
|
if (cell.status === 'maintenance') return `${def.name} : en entretien (${Math.ceil(cell.maint)} s)`;
|
|
const eff = cell.eff ?? 1;
|
|
const bits = [def.name];
|
|
if (def.power) bits.push(`${fmt(def.power.output * eff)} MW`);
|
|
for (const [res, rate] of Object.entries(def.produces ?? {})) bits.push(`${RESOURCES[res]?.label ?? res} ${fmt(rate * eff)}/s`);
|
|
if (cell.starved) bits.push(RESOURCES[cell.starved].lack);
|
|
if (def.office) bits.push(`${def.office.capacity} postes`);
|
|
if (def.maintenance?.every && cell.mt != null) bits.push(`entretien dans ${Math.ceil(cell.mt)} s`);
|
|
return bits.join(' · ');
|
|
}
|
|
|
|
function cellInfo(state) {
|
|
if (state.notice) return state.notice.text;
|
|
const hv = state.hover;
|
|
if (!hv) return state.mode === 'underground'
|
|
? "Vue sous-terre : relie la pompe, la centrale et la turbine avec des tuyaux (glisser pour en poser plusieurs)."
|
|
: "Molette : zoom · clic maintenu sans outil : déplacer la vue · clic droit : désélectionner l'outil";
|
|
const cell = state.grid[hv.y][hv.x];
|
|
const def = cell && BUILDINGS[cell.type];
|
|
const w = windAt(state.terrain, hv.x, hv.y, state.wind.dir);
|
|
|
|
if (state.water[hv.y][hv.x]) return "Eau · non constructible";
|
|
|
|
const under = state.mode === 'underground';
|
|
const pipe = state.pipes[hv.y][hv.x];
|
|
const net = state.pipeNet[hv.y]?.[hv.x];
|
|
|
|
const parts = [];
|
|
if (def) parts.push(buildingInfo(cell, def));
|
|
if (under && pipe) parts.push(`${RESOURCES[pipe].pipe} · offre ${fmt(net?.supply ?? 0)}/s · demande ${fmt(net?.demand ?? 0)}/s`);
|
|
parts.push(`Altitude ${w.height}`);
|
|
if (!under && (!def || def.power?.scalesWithWind)) parts.push(`${w.label}, vent x${fmt(w.factor)}`);
|
|
return parts.join(' · ');
|
|
}
|
|
|
|
function setNeg(id, neg) {
|
|
$(id).classList.toggle('neg', neg);
|
|
}
|
|
|
|
export function updateHud(state) {
|
|
const dir = WIND_DIRS[state.wind.dir];
|
|
const { capacity, need } = state.slots;
|
|
|
|
setText('hud-money', `Argent : ${Math.trunc(state.money).toLocaleString('fr-FR')} €`);
|
|
setNeg('hud-money', state.money < 0);
|
|
setText('hud-power', `Production : ${fmt(state.power)} MW (+${fmt(state.income)} €/s)`);
|
|
setText('hud-slots', `Gestion : ${need}/${capacity}`);
|
|
setNeg('hud-slots', need > capacity);
|
|
setText('hud-wind', `Vent ${dir.label} ${dir.arrow}`);
|
|
setText('hud-cell', cellInfo(state));
|
|
|
|
document.querySelectorAll('.dropdown .tool').forEach(btn => {
|
|
btn.classList.toggle('poor', state.money < Number(btn.dataset.cost));
|
|
});
|
|
}
|