Spaces:
Paused
Touch / mobile controls — play the whole game by finger
Browse filesA full one-button, no-hover touch scheme layered alongside the existing
mouse + keyboard path, plus a responsive HUD so a phone or tablet in
landscape (the natural RTS orientation) is fully playable.
Gestures (input.js touch handlers):
• tap your unit/building ... select it (double-tap = all of that type on screen)
• tap elsewhere ........... command the selection — move · tap a foe to attack ·
tap a node to gather · set a producing building's rally
• one-finger drag ......... box-select your units
• two-finger drag ......... pan · pinch ...... zoom (anchored on the pinch)
• Build / Attack-Move ..... tap places the building / commits the attack-move
The touch and mouse paths now share one set of helpers (applyBoxSelection /
commandAt / selectSameTypeAt), so a finger-tap issues byte-identical orders
to a right-click — the mouse handlers were refactored onto the same helpers
with no behaviour change.
camera.js: dragCamera + pinchZoomPan — pure view math for the two-finger
pan/zoom (zoom about the pinch midpoint, same clamps as the wheel), so it's
unit-testable without a browser.
main.js: an Attack-Move HUD button (the only way to arm attack-move without
an A key, and a discoverable one on desktop — shows ARMED while waiting for
the target tap); a Touch section in the F1/? help; and touch-aware panel
hints. First touch flips on a `touch` body class.
style.css: `touch-action: none` + no-callout on the field and minimap so the
browser can't hijack a gesture as page scroll/zoom; coarse-pointer rules that
grow every tap target; and max-width media queries that trim the side panel,
minimap, and top bar so the battlefield keeps the width on a small screen.
Tests: +6 camera tests (screen/world round-trip, clamp, zoomAt limits,
dragCamera, pinchZoomPan zoom + pan) — 317 total, all passing. The touch
handlers and desktop-mouse refactor were verified end-to-end in a headless
browser driving synthetic touch + mouse events (tap-select, tap-command,
drag-box, double-tap, pinch, two-finger pan, attack-move arm/commit, and the
three mouse paths), and the mobile layout was screenshot at phone-landscape.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AEQUVetLk31Jj7niDQAsXL
- camera.js +33 -0
- input.js +213 -78
- main.js +41 -7
- style.css +55 -0
- test/camera.test.js +81 -0
|
@@ -70,3 +70,36 @@ export function panCamera(camera, map, vw, vh, dx, dy, dt) {
|
|
| 70 |
camera.y += (dy / len) * PAN_SPEED * dt / camera.zoom;
|
| 71 |
clampCamera(camera, map, vw, vh);
|
| 72 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
camera.y += (dy / len) * PAN_SPEED * dt / camera.zoom;
|
| 71 |
clampCamera(camera, map, vw, vh);
|
| 72 |
}
|
| 73 |
+
|
| 74 |
+
// Drag the camera by a raw screen-space delta (touch two-finger pan, or a
|
| 75 |
+
// drag-pan): move the view so the world stays glued to the fingers. dsx/dsy is
|
| 76 |
+
// the finger movement in CSS pixels; dividing by zoom converts it to world
|
| 77 |
+
// units, and the sign is inverted because dragging content right (positive dsx)
|
| 78 |
+
// must decrease camera.x to bring the left-of-view world into frame.
|
| 79 |
+
export function dragCamera(camera, map, vw, vh, dsx, dsy) {
|
| 80 |
+
camera.x -= dsx / camera.zoom;
|
| 81 |
+
camera.y -= dsy / camera.zoom;
|
| 82 |
+
clampCamera(camera, map, vw, vh);
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
// One combined two-finger gesture step. `prev`/`cur` each hold the two touch
|
| 86 |
+
// points in screen space ({ ax, ay, bx, by }); this pinch-zooms by how their
|
| 87 |
+
// spread changed and pans by how their midpoint moved — with the zoom anchored
|
| 88 |
+
// on the current midpoint so the world under the pinch stays put. Pure view
|
| 89 |
+
// math (same clamps as the wheel), so a test can assert it without a browser.
|
| 90 |
+
export function pinchZoomPan(camera, map, vw, vh, prev, cur) {
|
| 91 |
+
const prevMidX = (prev.ax + prev.bx) / 2, prevMidY = (prev.ay + prev.by) / 2;
|
| 92 |
+
const curMidX = (cur.ax + cur.bx) / 2, curMidY = (cur.ay + cur.by) / 2;
|
| 93 |
+
const prevDist = Math.hypot(prev.ax - prev.bx, prev.ay - prev.by) || 1;
|
| 94 |
+
const curDist = Math.hypot(cur.ax - cur.bx, cur.ay - cur.by) || 1;
|
| 95 |
+
|
| 96 |
+
// Zoom about the current midpoint (keep that world point fixed on screen).
|
| 97 |
+
const before = screenToWorld(camera, vw, vh, curMidX, curMidY);
|
| 98 |
+
camera.zoom = Math.min(MAX_ZOOM, Math.max(minZoomFor(map, vw, vh), camera.zoom * (curDist / prevDist)));
|
| 99 |
+
const after = screenToWorld(camera, vw, vh, curMidX, curMidY);
|
| 100 |
+
camera.x += before.x - after.x;
|
| 101 |
+
camera.y += before.y - after.y;
|
| 102 |
+
|
| 103 |
+
// ...then pan by the midpoint's own movement.
|
| 104 |
+
dragCamera(camera, map, vw, vh, curMidX - prevMidX, curMidY - prevMidY);
|
| 105 |
+
}
|
|
@@ -11,7 +11,7 @@
|
|
| 11 |
import { issueMove, issueGather, issueAttack, issueAttackMove, issueBuild, issueAssistBuild, issueSetRally, issueStop, issueScout, issueHold } from "./engine/commands.js";
|
| 12 |
import { UNITS, BUILDINGS } from "./engine/entities.js";
|
| 13 |
import { isVisibleAt, isNodeDiscovered } from "./engine/fog.js";
|
| 14 |
-
import { createCamera, screenToWorld, zoomAt, panCamera, clampCamera } from "./camera.js";
|
| 15 |
import * as sound from "./sound.js";
|
| 16 |
|
| 17 |
const CLICK_THRESHOLD = 4;
|
|
@@ -42,6 +42,14 @@ export function attachInput(canvas, state, onChange) {
|
|
| 42 |
let idleCycle = 0; // round-robins through idle workers on repeated presses
|
| 43 |
let edgePan = [0, 0]; // camera nudge from the cursor sitting at a screen edge
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
function viewport() {
|
| 46 |
return { vw: canvas.clientWidth, vh: canvas.clientHeight };
|
| 47 |
}
|
|
@@ -85,45 +93,20 @@ export function attachInput(canvas, state, onChange) {
|
|
| 85 |
canvas.classList.toggle("aim-cursor", v);
|
| 86 |
}
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
if (
|
| 92 |
-
|
| 93 |
-
// selection drag. Consumes the arm, whether or not anything is selected.
|
| 94 |
-
if (attackMoveArmed) {
|
| 95 |
-
setArmed(false);
|
| 96 |
-
const selected = selectedUnits();
|
| 97 |
-
if (selected.length) { aggressiveMove(selected, p.x, p.y); sound.playOrder(); onChange(); }
|
| 98 |
-
return;
|
| 99 |
-
}
|
| 100 |
-
dragBox = { x1: p.x, y1: p.y, x2: p.x, y2: p.y };
|
| 101 |
-
}, { signal });
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
const rect = canvas.getBoundingClientRect();
|
| 108 |
-
const lx = e.clientX - rect.left, ly = e.clientY - rect.top;
|
| 109 |
-
edgePan = [
|
| 110 |
-
lx < EDGE_SCROLL_MARGIN ? -1 : lx > rect.width - EDGE_SCROLL_MARGIN ? 1 : 0,
|
| 111 |
-
ly < EDGE_SCROLL_MARGIN ? -1 : ly > rect.height - EDGE_SCROLL_MARGIN ? 1 : 0,
|
| 112 |
-
];
|
| 113 |
-
if (!dragBox) return;
|
| 114 |
-
dragBox.x2 = lastWorldPos.x;
|
| 115 |
-
dragBox.y2 = lastWorldPos.y;
|
| 116 |
-
}, { signal });
|
| 117 |
-
canvas.addEventListener("mouseleave", () => { edgePan = [0, 0]; }, { signal });
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
dragBox = null;
|
| 123 |
-
// Ctrl (the game's modifier — see the waypoint note below) adds to the
|
| 124 |
-
// current selection instead of replacing it, so you can pull several groups
|
| 125 |
-
// together. An empty additive click leaves the selection untouched.
|
| 126 |
-
const additive = e.ctrlKey;
|
| 127 |
const dx = Math.abs(box.x2 - box.x1), dy = Math.abs(box.y2 - box.y1);
|
| 128 |
let picks;
|
| 129 |
if (dx < CLICK_THRESHOLD && dy < CLICK_THRESHOLD) {
|
|
@@ -143,14 +126,14 @@ export function attachInput(canvas, state, onChange) {
|
|
| 143 |
}
|
| 144 |
if (picks.length) sound.playSelect();
|
| 145 |
onChange();
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
//
|
| 149 |
-
//
|
| 150 |
-
|
| 151 |
-
const p = toWorld(e.clientX, e.clientY);
|
| 152 |
const hit = entityAt(p.x, p.y);
|
| 153 |
-
if (!hit || hit.owner !== "player" || hit.kind !== "unit") return;
|
| 154 |
const { vw, vh } = viewport();
|
| 155 |
const tl = screenToWorld(camera, vw, vh, 0, 0);
|
| 156 |
const br = screenToWorld(camera, vw, vh, vw, vh);
|
|
@@ -159,19 +142,14 @@ export function attachInput(canvas, state, onChange) {
|
|
| 159 |
&& u.x >= tl.x && u.x <= br.x && u.y >= tl.y && u.y <= br.y)
|
| 160 |
.map(u => u.id);
|
| 161 |
onChange();
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
canvas.addEventListener("contextmenu", e => {
|
| 165 |
-
e.preventDefault();
|
| 166 |
-
if (buildMode) { buildMode = null; onChange(); return; }
|
| 167 |
-
if (attackMoveArmed) { setArmed(false); return; } // right-click cancels a pending attack-move
|
| 168 |
-
|
| 169 |
-
const p = toWorld(e.clientX, e.clientY);
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
if (state.selection.length === 1) {
|
| 176 |
const building = state.buildings.get(state.selection[0]);
|
| 177 |
if (building && building.owner === "player" && BUILDINGS[building.type].produces) {
|
|
@@ -186,47 +164,87 @@ export function attachInput(canvas, state, onChange) {
|
|
| 186 |
const selected = state.selection.map(id => state.units.get(id)).filter(Boolean);
|
| 187 |
if (!selected.length) return;
|
| 188 |
|
| 189 |
-
// Holding Ctrl queues the order as a waypoint instead of replacing what the
|
| 190 |
-
// units are doing, so a sequence of Ctrl+right-clicks lays down a path
|
| 191 |
-
// (move/attack/gather steps) the units run through in order. A plain
|
| 192 |
-
// right-click issues immediately and clears any queued waypoints. NOT Shift:
|
| 193 |
-
// Firefox force-shows the native context menu on Shift+right-click and
|
| 194 |
-
// bypasses preventDefault, so a Shift-queued order could never be captured.
|
| 195 |
-
const queue = e.ctrlKey;
|
| 196 |
-
|
| 197 |
const target = entityAt(p.x, p.y);
|
| 198 |
if (target && target.owner === "player" && target.kind === "building" && target.constructing) {
|
| 199 |
const workers = selected.filter(u => u.cargo);
|
| 200 |
if (workers.length) { issueAssistBuild(workers, target.id, queue); sound.playOrder(); }
|
| 201 |
return;
|
| 202 |
}
|
| 203 |
-
|
| 204 |
if (target && target.owner !== "player") {
|
| 205 |
-
// Anything with an attack stat can be sent in — combat units, and now
|
| 206 |
-
// workers too (weakly). Non-combatants without a weapon are left out.
|
| 207 |
const attackers = selected.filter(u => UNITS[u.type].attack);
|
| 208 |
if (attackers.length) { issueAttack(attackers, target.id, queue); sound.playOrder(); }
|
| 209 |
return;
|
| 210 |
}
|
| 211 |
-
|
| 212 |
const node = nodeAt(p.x, p.y);
|
| 213 |
if (node) {
|
| 214 |
const workers = selected.filter(u => u.cargo);
|
| 215 |
if (workers.length) { issueGather(workers, node.id, queue); sound.playOrder(); }
|
| 216 |
return;
|
| 217 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
//
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
| 228 |
}
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
}, { signal });
|
| 231 |
|
| 232 |
canvas.addEventListener("wheel", e => {
|
|
@@ -236,6 +254,119 @@ export function attachInput(canvas, state, onChange) {
|
|
| 236 |
zoomAt(camera, state.map, vw, vh, e.clientX - rect.left, e.clientY - rect.top, e.deltaY < 0 ? ZOOM_STEP : 1 / ZOOM_STEP);
|
| 237 |
}, { signal, passive: false });
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
function alivePlayerUnitIds(ids) {
|
| 240 |
return ids.filter(id => { const u = state.units.get(id); return u && u.owner === "player"; });
|
| 241 |
}
|
|
@@ -332,6 +463,10 @@ export function attachInput(canvas, state, onChange) {
|
|
| 332 |
stopSelected: () => { stopSelected(); onChange(); },
|
| 333 |
scoutSelected: () => { scoutSelected(); onChange(); },
|
| 334 |
holdSelected: () => { holdSelected(); onChange(); },
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
tickCamera(dt) {
|
| 336 |
let dx = edgePan[0], dy = edgePan[1];
|
| 337 |
for (const key of heldKeys) {
|
|
|
|
| 11 |
import { issueMove, issueGather, issueAttack, issueAttackMove, issueBuild, issueAssistBuild, issueSetRally, issueStop, issueScout, issueHold } from "./engine/commands.js";
|
| 12 |
import { UNITS, BUILDINGS } from "./engine/entities.js";
|
| 13 |
import { isVisibleAt, isNodeDiscovered } from "./engine/fog.js";
|
| 14 |
+
import { createCamera, screenToWorld, zoomAt, panCamera, clampCamera, dragCamera, pinchZoomPan } from "./camera.js";
|
| 15 |
import * as sound from "./sound.js";
|
| 16 |
|
| 17 |
const CLICK_THRESHOLD = 4;
|
|
|
|
| 42 |
let idleCycle = 0; // round-robins through idle workers on repeated presses
|
| 43 |
let edgePan = [0, 0]; // camera nudge from the cursor sitting at a screen edge
|
| 44 |
|
| 45 |
+
// ---- touch state ----
|
| 46 |
+
// One finger = tap-to-select / tap-to-command / drag-a-box; two fingers =
|
| 47 |
+
// pinch-zoom + pan. See the touch handlers below for the full gesture map.
|
| 48 |
+
let touchStart = null; // { cx, cy, wx, wy, moved } for the active one-finger drag (client + world)
|
| 49 |
+
let pinchPrev = null; // last two-finger snapshot { ax, ay, bx, by } for pinchZoomPan
|
| 50 |
+
let gestureActive = false; // a two-finger gesture happened; ignore fingers until all lift
|
| 51 |
+
let lastTapAt = 0, lastTapCX = 0, lastTapCY = 0; // for double-tap detection (client coords + time)
|
| 52 |
+
|
| 53 |
function viewport() {
|
| 54 |
return { vw: canvas.clientWidth, vh: canvas.clientHeight };
|
| 55 |
}
|
|
|
|
| 93 |
canvas.classList.toggle("aim-cursor", v);
|
| 94 |
}
|
| 95 |
|
| 96 |
+
// First real touch flips the app into touch mode: CSS (style.css) grows the
|
| 97 |
+
// tap targets and the HUD swaps to the touch legend. Idempotent.
|
| 98 |
+
function onTouchActive() {
|
| 99 |
+
if (!document.body.classList.contains("touch")) document.body.classList.add("touch");
|
| 100 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
// ---- shared selection / command logic ----
|
| 103 |
+
// Extracted so the mouse and touch paths issue byte-identical orders: a
|
| 104 |
+
// finger-tap runs exactly the same routing as a right-click, a drag-box the
|
| 105 |
+
// same as a mouse box, and a double-tap the same as a double-click.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
// Resolve a world-space selection box (a tiny box is a point-pick) into the new
|
| 108 |
+
// selection, additively when asked. Returns the ids picked.
|
| 109 |
+
function applyBoxSelection(box, additive) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
const dx = Math.abs(box.x2 - box.x1), dy = Math.abs(box.y2 - box.y1);
|
| 111 |
let picks;
|
| 112 |
if (dx < CLICK_THRESHOLD && dy < CLICK_THRESHOLD) {
|
|
|
|
| 126 |
}
|
| 127 |
if (picks.length) sound.playSelect();
|
| 128 |
onChange();
|
| 129 |
+
return picks;
|
| 130 |
+
}
|
| 131 |
|
| 132 |
+
// Grab every same-type unit of yours currently on screen (the double-click /
|
| 133 |
+
// double-tap gesture). Returns true if it hit one of your units.
|
| 134 |
+
function selectSameTypeAt(p) {
|
|
|
|
| 135 |
const hit = entityAt(p.x, p.y);
|
| 136 |
+
if (!hit || hit.owner !== "player" || hit.kind !== "unit") return false;
|
| 137 |
const { vw, vh } = viewport();
|
| 138 |
const tl = screenToWorld(camera, vw, vh, 0, 0);
|
| 139 |
const br = screenToWorld(camera, vw, vh, vw, vh);
|
|
|
|
| 142 |
&& u.x >= tl.x && u.x <= br.x && u.y >= tl.y && u.y <= br.y)
|
| 143 |
.map(u => u.id);
|
| 144 |
onChange();
|
| 145 |
+
return true;
|
| 146 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
// The context command at a world point (the right-click / touch command-tap):
|
| 149 |
+
// a single selected production building sets its rally; otherwise the current
|
| 150 |
+
// unit selection assists-builds, attacks, gathers, or moves as the target
|
| 151 |
+
// warrants. `queue` chains it as a waypoint instead of replacing the order.
|
| 152 |
+
function commandAt(p, queue) {
|
| 153 |
if (state.selection.length === 1) {
|
| 154 |
const building = state.buildings.get(state.selection[0]);
|
| 155 |
if (building && building.owner === "player" && BUILDINGS[building.type].produces) {
|
|
|
|
| 164 |
const selected = state.selection.map(id => state.units.get(id)).filter(Boolean);
|
| 165 |
if (!selected.length) return;
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
const target = entityAt(p.x, p.y);
|
| 168 |
if (target && target.owner === "player" && target.kind === "building" && target.constructing) {
|
| 169 |
const workers = selected.filter(u => u.cargo);
|
| 170 |
if (workers.length) { issueAssistBuild(workers, target.id, queue); sound.playOrder(); }
|
| 171 |
return;
|
| 172 |
}
|
|
|
|
| 173 |
if (target && target.owner !== "player") {
|
|
|
|
|
|
|
| 174 |
const attackers = selected.filter(u => UNITS[u.type].attack);
|
| 175 |
if (attackers.length) { issueAttack(attackers, target.id, queue); sound.playOrder(); }
|
| 176 |
return;
|
| 177 |
}
|
|
|
|
| 178 |
const node = nodeAt(p.x, p.y);
|
| 179 |
if (node) {
|
| 180 |
const workers = selected.filter(u => u.cargo);
|
| 181 |
if (workers.length) { issueGather(workers, node.id, queue); sound.playOrder(); }
|
| 182 |
return;
|
| 183 |
}
|
| 184 |
+
if (queue) aggressiveMove(selected, p.x, p.y, true);
|
| 185 |
+
else issueMove(selected, p.x, p.y);
|
| 186 |
+
sound.playOrder();
|
| 187 |
+
}
|
| 188 |
|
| 189 |
+
canvas.addEventListener("mousedown", e => {
|
| 190 |
+
if (e.button !== 0) return;
|
| 191 |
+
const p = toWorld(e.clientX, e.clientY);
|
| 192 |
+
if (buildMode) { placeBuildingAt(p); return; }
|
| 193 |
+
// Armed attack-move: this click commits the order instead of starting a
|
| 194 |
+
// selection drag. Consumes the arm, whether or not anything is selected.
|
| 195 |
+
if (attackMoveArmed) {
|
| 196 |
+
setArmed(false);
|
| 197 |
+
const selected = selectedUnits();
|
| 198 |
+
if (selected.length) { aggressiveMove(selected, p.x, p.y); sound.playOrder(); onChange(); }
|
| 199 |
+
return;
|
| 200 |
}
|
| 201 |
+
dragBox = { x1: p.x, y1: p.y, x2: p.x, y2: p.y };
|
| 202 |
+
}, { signal });
|
| 203 |
+
|
| 204 |
+
canvas.addEventListener("mousemove", e => {
|
| 205 |
+
lastWorldPos = toWorld(e.clientX, e.clientY); // tracked continuously for the build-placement ghost
|
| 206 |
+
// Edge scroll: cursor within a margin of a canvas edge nudges the camera
|
| 207 |
+
// that way, so you can drag the view without touching the keyboard.
|
| 208 |
+
const rect = canvas.getBoundingClientRect();
|
| 209 |
+
const lx = e.clientX - rect.left, ly = e.clientY - rect.top;
|
| 210 |
+
edgePan = [
|
| 211 |
+
lx < EDGE_SCROLL_MARGIN ? -1 : lx > rect.width - EDGE_SCROLL_MARGIN ? 1 : 0,
|
| 212 |
+
ly < EDGE_SCROLL_MARGIN ? -1 : ly > rect.height - EDGE_SCROLL_MARGIN ? 1 : 0,
|
| 213 |
+
];
|
| 214 |
+
if (!dragBox) return;
|
| 215 |
+
dragBox.x2 = lastWorldPos.x;
|
| 216 |
+
dragBox.y2 = lastWorldPos.y;
|
| 217 |
+
}, { signal });
|
| 218 |
+
canvas.addEventListener("mouseleave", () => { edgePan = [0, 0]; }, { signal });
|
| 219 |
+
|
| 220 |
+
window.addEventListener("mouseup", e => {
|
| 221 |
+
if (e.button !== 0 || !dragBox) return;
|
| 222 |
+
const box = dragBox;
|
| 223 |
+
dragBox = null;
|
| 224 |
+
// Ctrl (the game's modifier — see the waypoint note below) adds to the
|
| 225 |
+
// current selection instead of replacing it, so you can pull several groups
|
| 226 |
+
// together. An empty additive click leaves the selection untouched.
|
| 227 |
+
applyBoxSelection(box, e.ctrlKey);
|
| 228 |
+
}, { signal });
|
| 229 |
+
|
| 230 |
+
// Double-click a unit to grab every same-type unit of yours currently on
|
| 231 |
+
// screen — the standard "select all of this type" gesture.
|
| 232 |
+
canvas.addEventListener("dblclick", e => {
|
| 233 |
+
selectSameTypeAt(toWorld(e.clientX, e.clientY));
|
| 234 |
+
}, { signal });
|
| 235 |
+
|
| 236 |
+
canvas.addEventListener("contextmenu", e => {
|
| 237 |
+
e.preventDefault();
|
| 238 |
+
if (buildMode) { buildMode = null; onChange(); return; }
|
| 239 |
+
if (attackMoveArmed) { setArmed(false); return; } // right-click cancels a pending attack-move
|
| 240 |
+
|
| 241 |
+
// Holding Ctrl queues the order as a waypoint instead of replacing what the
|
| 242 |
+
// units are doing, so a sequence of Ctrl+right-clicks lays down a path
|
| 243 |
+
// (move/attack/gather steps) the units run through in order. A plain
|
| 244 |
+
// right-click issues immediately and clears any queued waypoints. NOT Shift:
|
| 245 |
+
// Firefox force-shows the native context menu on Shift+right-click and
|
| 246 |
+
// bypasses preventDefault, so a Shift-queued order could never be captured.
|
| 247 |
+
commandAt(toWorld(e.clientX, e.clientY), e.ctrlKey);
|
| 248 |
}, { signal });
|
| 249 |
|
| 250 |
canvas.addEventListener("wheel", e => {
|
|
|
|
| 254 |
zoomAt(camera, state.map, vw, vh, e.clientX - rect.left, e.clientY - rect.top, e.deltaY < 0 ? ZOOM_STEP : 1 / ZOOM_STEP);
|
| 255 |
}, { signal, passive: false });
|
| 256 |
|
| 257 |
+
/* ---------- touch (phones / tablets) ----------
|
| 258 |
+
A one-button, no-hover parallel to the mouse controls, so the game is fully
|
| 259 |
+
playable by finger:
|
| 260 |
+
• tap your unit/building ....... select it (double-tap = all of that type on screen)
|
| 261 |
+
• tap elsewhere ................ command the selection (move / attack a foe / gather a
|
| 262 |
+
node), or set a selected production building's rally
|
| 263 |
+
• one-finger drag .............. box-select your units
|
| 264 |
+
• two-finger drag .............. pan · pinch ...... zoom (about the pinch)
|
| 265 |
+
• Build mode / Attack-move ..... tap places the building / commits the attack-move
|
| 266 |
+
Runs the SAME applyBoxSelection / commandAt / selectSameTypeAt / placeBuildingAt as the
|
| 267 |
+
mouse, so touch and desktop issue identical orders. `touch-action: none` on the canvas
|
| 268 |
+
(style.css) stops the browser hijacking these as page scroll / zoom. */
|
| 269 |
+
const TAP_MOVE_TOL = 14; // client px a finger may travel and still count as a tap, not a drag
|
| 270 |
+
const DOUBLE_TAP_MS = 300, DOUBLE_TAP_DIST = 30;
|
| 271 |
+
|
| 272 |
+
function twoSnapshot(t0, t1) {
|
| 273 |
+
const rect = canvas.getBoundingClientRect();
|
| 274 |
+
return { ax: t0.clientX - rect.left, ay: t0.clientY - rect.top,
|
| 275 |
+
bx: t1.clientX - rect.left, by: t1.clientY - rect.top };
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
canvas.addEventListener("touchstart", e => {
|
| 279 |
+
e.preventDefault();
|
| 280 |
+
onTouchActive();
|
| 281 |
+
if (e.touches.length >= 2) {
|
| 282 |
+
// Second finger down: abandon any one-finger drag/box and start pinch-pan.
|
| 283 |
+
touchStart = null; dragBox = null; gestureActive = true;
|
| 284 |
+
pinchPrev = twoSnapshot(e.touches[0], e.touches[1]);
|
| 285 |
+
return;
|
| 286 |
+
}
|
| 287 |
+
if (e.touches.length === 1 && !gestureActive) {
|
| 288 |
+
const t = e.touches[0];
|
| 289 |
+
const w = toWorld(t.clientX, t.clientY);
|
| 290 |
+
touchStart = { cx: t.clientX, cy: t.clientY, wx: w.x, wy: w.y, moved: false };
|
| 291 |
+
lastWorldPos = w; // so the build ghost sits under the finger from the first touch
|
| 292 |
+
}
|
| 293 |
+
}, { signal, passive: false });
|
| 294 |
+
|
| 295 |
+
canvas.addEventListener("touchmove", e => {
|
| 296 |
+
e.preventDefault();
|
| 297 |
+
const { vw, vh } = viewport();
|
| 298 |
+
if (e.touches.length >= 2 && pinchPrev) {
|
| 299 |
+
const cur = twoSnapshot(e.touches[0], e.touches[1]);
|
| 300 |
+
pinchZoomPan(camera, state.map, vw, vh, pinchPrev, cur);
|
| 301 |
+
pinchPrev = cur;
|
| 302 |
+
return;
|
| 303 |
+
}
|
| 304 |
+
if (e.touches.length === 1 && touchStart && !gestureActive) {
|
| 305 |
+
const t = e.touches[0];
|
| 306 |
+
const w = toWorld(t.clientX, t.clientY);
|
| 307 |
+
lastWorldPos = w;
|
| 308 |
+
if (!touchStart.moved && Math.hypot(t.clientX - touchStart.cx, t.clientY - touchStart.cy) > TAP_MOVE_TOL) {
|
| 309 |
+
touchStart.moved = true;
|
| 310 |
+
// A drag that isn't placing a building becomes a selection box.
|
| 311 |
+
if (!buildMode) dragBox = { x1: touchStart.wx, y1: touchStart.wy, x2: w.x, y2: w.y };
|
| 312 |
+
}
|
| 313 |
+
if (dragBox) { dragBox.x2 = w.x; dragBox.y2 = w.y; }
|
| 314 |
+
}
|
| 315 |
+
}, { signal, passive: false });
|
| 316 |
+
|
| 317 |
+
function endTouch(e) {
|
| 318 |
+
e.preventDefault();
|
| 319 |
+
if (e.touches.length >= 2) { pinchPrev = twoSnapshot(e.touches[0], e.touches[1]); return; }
|
| 320 |
+
if (e.touches.length === 1) {
|
| 321 |
+
// Dropped from two fingers to one: don't let the leftover finger act — wait
|
| 322 |
+
// for a clean, all-up release so a lifted pinch never fires a stray tap.
|
| 323 |
+
pinchPrev = null;
|
| 324 |
+
return;
|
| 325 |
+
}
|
| 326 |
+
// All fingers up.
|
| 327 |
+
if (gestureActive) { gestureActive = false; touchStart = null; dragBox = null; return; }
|
| 328 |
+
if (!touchStart) return;
|
| 329 |
+
const start = touchStart; touchStart = null;
|
| 330 |
+
|
| 331 |
+
if (start.moved && dragBox) { // a drag: finish the selection box
|
| 332 |
+
const box = dragBox; dragBox = null;
|
| 333 |
+
applyBoxSelection(box, false);
|
| 334 |
+
return;
|
| 335 |
+
}
|
| 336 |
+
dragBox = null;
|
| 337 |
+
const p = { x: start.wx, y: start.wy };
|
| 338 |
+
|
| 339 |
+
// In build mode, place at where the finger LIFTED (lastWorldPos tracks the
|
| 340 |
+
// move) so a drag-to-position works; for a plain tap that's the tap point.
|
| 341 |
+
if (buildMode) { placeBuildingAt(lastWorldPos); return; }
|
| 342 |
+
if (attackMoveArmed) {
|
| 343 |
+
setArmed(false);
|
| 344 |
+
const sel = selectedUnits();
|
| 345 |
+
if (sel.length) { aggressiveMove(sel, p.x, p.y); sound.playOrder(); onChange(); }
|
| 346 |
+
return;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
// A quick second tap near the first upgrades to select-all-of-type.
|
| 350 |
+
const now = performance.now(); // UI-only timing; the deterministic sim never reads a clock
|
| 351 |
+
const isDouble = now - lastTapAt < DOUBLE_TAP_MS
|
| 352 |
+
&& Math.hypot(start.cx - lastTapCX, start.cy - lastTapCY) < DOUBLE_TAP_DIST;
|
| 353 |
+
lastTapAt = now; lastTapCX = start.cx; lastTapCY = start.cy;
|
| 354 |
+
if (isDouble && selectSameTypeAt(p)) return;
|
| 355 |
+
|
| 356 |
+
// Single tap: your own entity selects; anything else is a command to the
|
| 357 |
+
// current selection (mirrors left-click select vs right-click order).
|
| 358 |
+
const hit = entityAt(p.x, p.y);
|
| 359 |
+
if (hit && hit.owner === "player") {
|
| 360 |
+
state.selection = [hit.id];
|
| 361 |
+
sound.playSelect();
|
| 362 |
+
onChange();
|
| 363 |
+
} else {
|
| 364 |
+
commandAt(p, false);
|
| 365 |
+
}
|
| 366 |
+
}
|
| 367 |
+
canvas.addEventListener("touchend", endTouch, { signal, passive: false });
|
| 368 |
+
canvas.addEventListener("touchcancel", endTouch, { signal, passive: false });
|
| 369 |
+
|
| 370 |
function alivePlayerUnitIds(ids) {
|
| 371 |
return ids.filter(id => { const u = state.units.get(id); return u && u.owner === "player"; });
|
| 372 |
}
|
|
|
|
| 463 |
stopSelected: () => { stopSelected(); onChange(); },
|
| 464 |
scoutSelected: () => { scoutSelected(); onChange(); },
|
| 465 |
holdSelected: () => { holdSelected(); onChange(); },
|
| 466 |
+
// Attack-move as a HUD button (touch has no A key): toggle it, cancel it, and
|
| 467 |
+
// read the armed state so the button can show as active.
|
| 468 |
+
toggleAttackMove: () => { setArmed(!attackMoveArmed); onChange(); },
|
| 469 |
+
get attackArmed() { return attackMoveArmed; },
|
| 470 |
tickCamera(dt) {
|
| 471 |
let dx = edgePan[0], dy = edgePan[1];
|
| 472 |
for (const key of heldKeys) {
|
|
@@ -436,13 +436,35 @@ const HELP_ROWS = [
|
|
| 436 |
["Esc", "Cancel build placement or a pending attack-move"],
|
| 437 |
["F1 or ?", "Toggle this help"],
|
| 438 |
];
|
| 439 |
-
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
`<div class="help-row"><span class="help-key">${k}</span><span>${v}</span></div>`).join("");
|
| 442 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
+ `<p class="help-dismiss">Press F1, ?, or Esc to close</p></div>`;
|
| 444 |
}
|
| 445 |
buildHelpOverlay();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
function toggleHelp(force) {
|
| 447 |
const show = force ?? helpOverlayEl.classList.contains("hidden");
|
| 448 |
helpOverlayEl.classList.toggle("hidden", !show);
|
|
@@ -770,6 +792,13 @@ function rebuildSelectionPanel(sel) {
|
|
| 770 |
panelEl.appendChild(makeButton("Stop ( X )", () => input.stopSelected()));
|
| 771 |
}
|
| 772 |
if (sel.some(e => e.kind === "unit" && UNITS[e.type].role === "combat")) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 773 |
panelEl.appendChild(makeButton("Hold Position ( H )", () => input.holdSelected(),
|
| 774 |
{ tip: "Fire on anything in range, but hold ground — don't chase out of position" }));
|
| 775 |
}
|
|
@@ -779,25 +808,30 @@ function rebuildSelectionPanel(sel) {
|
|
| 779 |
{ tip: "Auto-explore: the Ranger ranges toward the nearest unexplored ground on its own" }));
|
| 780 |
}
|
| 781 |
|
|
|
|
| 782 |
if (input.building) {
|
| 783 |
const hint = document.createElement("p");
|
| 784 |
hint.className = "hint";
|
| 785 |
-
hint.textContent = "
|
|
|
|
| 786 |
panelEl.appendChild(hint);
|
| 787 |
} else if (hasRanger) {
|
| 788 |
const hint = document.createElement("p");
|
| 789 |
hint.className = "hint";
|
| 790 |
-
hint.textContent = "Ranger: all-terrain, far sight. E to auto-
|
|
|
|
| 791 |
panelEl.appendChild(hint);
|
| 792 |
} else if (sel.some(e => e.kind === "unit" && UNITS[e.type].role === "combat")) {
|
| 793 |
const hint = document.createElement("p");
|
| 794 |
hint.className = "hint";
|
| 795 |
-
hint.textContent = "
|
|
|
|
| 796 |
panelEl.appendChild(hint);
|
| 797 |
} else if (sel.length === 1 && (cc || barracks)) {
|
| 798 |
const hint = document.createElement("p");
|
| 799 |
hint.className = "hint";
|
| 800 |
-
hint.textContent = "
|
|
|
|
| 801 |
panelEl.appendChild(hint);
|
| 802 |
}
|
| 803 |
}
|
|
|
|
| 436 |
["Esc", "Cancel build placement or a pending attack-move"],
|
| 437 |
["F1 or ?", "Toggle this help"],
|
| 438 |
];
|
| 439 |
+
// The finger-only control scheme (see input.js's touch handlers).
|
| 440 |
+
const TOUCH_HELP_ROWS = [
|
| 441 |
+
["Tap your unit", "Select it · double-tap grabs all of that type on screen"],
|
| 442 |
+
["Tap elsewhere", "Order the selection — move · tap a foe to attack · tap a node to gather"],
|
| 443 |
+
["One-finger drag", "Box-select your units"],
|
| 444 |
+
["Two fingers", "Drag to pan · pinch to zoom"],
|
| 445 |
+
["Attack-Move button", "Arm it, then tap the map to advance-and-engage"],
|
| 446 |
+
["Tap a node (building selected)", "Rally new workers to mine it"],
|
| 447 |
+
["Minimap", "Tap to jump the view"],
|
| 448 |
+
];
|
| 449 |
+
function helpRows(rows) {
|
| 450 |
+
return rows.map(([k, v]) =>
|
| 451 |
`<div class="help-row"><span class="help-key">${k}</span><span>${v}</span></div>`).join("");
|
| 452 |
+
}
|
| 453 |
+
function buildHelpOverlay() {
|
| 454 |
+
helpOverlayEl.innerHTML = `<div class="help-card"><h2>Controls & Help</h2>`
|
| 455 |
+
+ `<h3 class="help-sub">Mouse & keyboard</h3>${helpRows(HELP_ROWS)}`
|
| 456 |
+
+ `<h3 class="help-sub">Touch</h3>${helpRows(TOUCH_HELP_ROWS)}`
|
| 457 |
+ `<p class="help-dismiss">Press F1, ?, or Esc to close</p></div>`;
|
| 458 |
}
|
| 459 |
buildHelpOverlay();
|
| 460 |
+
|
| 461 |
+
// Touch mode: the first finger anywhere grows the tap targets (style.css) and
|
| 462 |
+
// switches the HUD hints to touch phrasing. input.js sets the class on a canvas
|
| 463 |
+
// touch too; doing it here covers a first touch that lands on a HUD button.
|
| 464 |
+
function isTouchMode() { return document.body.classList.contains("touch"); }
|
| 465 |
+
window.addEventListener("touchstart", () => {
|
| 466 |
+
if (!isTouchMode()) { document.body.classList.add("touch"); if (input) renderHUD(); }
|
| 467 |
+
}, { passive: true });
|
| 468 |
function toggleHelp(force) {
|
| 469 |
const show = force ?? helpOverlayEl.classList.contains("hidden");
|
| 470 |
helpOverlayEl.classList.toggle("hidden", !show);
|
|
|
|
| 792 |
panelEl.appendChild(makeButton("Stop ( X )", () => input.stopSelected()));
|
| 793 |
}
|
| 794 |
if (sel.some(e => e.kind === "unit" && UNITS[e.type].role === "combat")) {
|
| 795 |
+
// Attack-move as a button — the only way to arm it on touch (no A key), and a
|
| 796 |
+
// discoverable one on desktop. Shows ARMED while waiting for the target tap.
|
| 797 |
+
const amBtn = makeButton(input.attackArmed ? "Attack-Move: ARMED ( A )" : "Attack-Move ( A )",
|
| 798 |
+
() => input.toggleAttackMove(),
|
| 799 |
+
{ tip: "Then tap the map: units advance and engage anything met on the way" });
|
| 800 |
+
if (input.attackArmed) amBtn.classList.add("armed");
|
| 801 |
+
panelEl.appendChild(amBtn);
|
| 802 |
panelEl.appendChild(makeButton("Hold Position ( H )", () => input.holdSelected(),
|
| 803 |
{ tip: "Fire on anything in range, but hold ground — don't chase out of position" }));
|
| 804 |
}
|
|
|
|
| 808 |
{ tip: "Auto-explore: the Ranger ranges toward the nearest unexplored ground on its own" }));
|
| 809 |
}
|
| 810 |
|
| 811 |
+
const touch = isTouchMode();
|
| 812 |
if (input.building) {
|
| 813 |
const hint = document.createElement("p");
|
| 814 |
hint.className = "hint";
|
| 815 |
+
hint.textContent = touch ? "Tap the map to place it. Tap Build again to cancel."
|
| 816 |
+
: "Click the map to place it. Right-click to cancel.";
|
| 817 |
panelEl.appendChild(hint);
|
| 818 |
} else if (hasRanger) {
|
| 819 |
const hint = document.createElement("p");
|
| 820 |
hint.className = "hint";
|
| 821 |
+
hint.textContent = touch ? "Ranger: all-terrain, far sight. E / Scout Mode to auto-explore; tap the map to move it."
|
| 822 |
+
: "Ranger: all-terrain, far sight. E to auto-scout; right-click to move it yourself.";
|
| 823 |
panelEl.appendChild(hint);
|
| 824 |
} else if (sel.some(e => e.kind === "unit" && UNITS[e.type].role === "combat")) {
|
| 825 |
const hint = document.createElement("p");
|
| 826 |
hint.className = "hint";
|
| 827 |
+
hint.textContent = touch ? "Tap the map to move. Tap a foe to attack. Attack-Move advances and engages."
|
| 828 |
+
: "A + click to attack-move. Right-click moves (ignores enemies). Ctrl+right-click queues a waypoint.";
|
| 829 |
panelEl.appendChild(hint);
|
| 830 |
} else if (sel.length === 1 && (cc || barracks)) {
|
| 831 |
const hint = document.createElement("p");
|
| 832 |
hint.className = "hint";
|
| 833 |
+
hint.textContent = touch ? "Tap the map to set a rally point — tap a resource node to rally new workers onto it."
|
| 834 |
+
: "Right-click to set a rally point — right-click a resource node to rally new workers straight onto it.";
|
| 835 |
panelEl.appendChild(hint);
|
| 836 |
}
|
| 837 |
}
|
|
@@ -469,3 +469,58 @@ html, body {
|
|
| 469 |
font-weight: 600;
|
| 470 |
margin: 0;
|
| 471 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
font-weight: 600;
|
| 470 |
margin: 0;
|
| 471 |
}
|
| 472 |
+
|
| 473 |
+
/* ---------- touch / mobile controls ----------
|
| 474 |
+
The field owns every gesture on it (tap / drag / two-finger pan-zoom — see
|
| 475 |
+
input.js), so tell the browser to keep its hands off: no page scroll, no
|
| 476 |
+
pinch-zoom-the-page, no long-press text selection / callout stealing a drag.
|
| 477 |
+
The minimap likewise consumes its taps. Everything ELSE (the panel, the setup
|
| 478 |
+
screen) keeps normal touch scrolling. */
|
| 479 |
+
#field {
|
| 480 |
+
touch-action: none;
|
| 481 |
+
-webkit-user-select: none;
|
| 482 |
+
user-select: none;
|
| 483 |
+
-webkit-touch-callout: none;
|
| 484 |
+
}
|
| 485 |
+
.minimap { touch-action: none; }
|
| 486 |
+
/* Stop iOS/Android rubber-band bounce dragging the whole game around. */
|
| 487 |
+
html, body { overscroll-behavior: none; }
|
| 488 |
+
|
| 489 |
+
/* Attack-move armed: the Attack-Move HUD button waits for the target tap. */
|
| 490 |
+
.btn.armed { border-color: var(--warn); color: var(--warn); background: rgba(251, 191, 36, 0.12); }
|
| 491 |
+
.btn.armed:hover { border-color: var(--warn); color: var(--warn); }
|
| 492 |
+
|
| 493 |
+
/* Help overlay section headings (mouse/keyboard vs touch). */
|
| 494 |
+
.help-sub { margin: 16px 0 2px; font-size: 0.85rem; color: var(--accent-2); font-weight: 600; letter-spacing: 0.02em; }
|
| 495 |
+
.help-card h2 + .help-sub { margin-top: 2px; }
|
| 496 |
+
|
| 497 |
+
/* Coarse pointer (finger): grow every tap target to a comfortable size,
|
| 498 |
+
independent of screen width, so a phone and a touch-laptop both get big hit
|
| 499 |
+
areas without changing the desktop mouse layout. */
|
| 500 |
+
@media (pointer: coarse) {
|
| 501 |
+
.btn { padding: 12px 12px; font-size: 0.9rem; }
|
| 502 |
+
.opt-btn { padding: 11px 14px; min-width: 104px; }
|
| 503 |
+
.chip-btn, .mute-btn { padding: 8px 12px; font-size: 0.8rem; }
|
| 504 |
+
.help-btn { width: 40px; height: 40px; }
|
| 505 |
+
.queue-cancel { padding: 6px 12px; font-size: 0.9rem; }
|
| 506 |
+
.idle-workers, .seed-chip { padding: 7px 11px; }
|
| 507 |
+
.obj-close { font-size: 1.5rem; padding: 4px; }
|
| 508 |
+
.map-card { width: 260px; }
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
/* Narrow screens (phones/tablets in landscape — the natural RTS orientation):
|
| 512 |
+
trim the side panel and chrome so the battlefield keeps most of the width. */
|
| 513 |
+
@media (max-width: 820px) {
|
| 514 |
+
#topbar { gap: 10px; padding: 6px 10px; flex-wrap: wrap; }
|
| 515 |
+
#topbar .title { font-size: 0.9rem; }
|
| 516 |
+
.resources { gap: 10px; font-size: 0.78rem; }
|
| 517 |
+
#view { padding: 6px; }
|
| 518 |
+
#selectionPanel { width: 200px; padding: 8px; }
|
| 519 |
+
.minimap { width: 150px; height: 94px; left: 12px; bottom: 12px; }
|
| 520 |
+
.help-key { flex-basis: 130px; }
|
| 521 |
+
}
|
| 522 |
+
@media (max-width: 600px) {
|
| 523 |
+
#topbar .title { display: none; } /* reclaim the width for the resource readout */
|
| 524 |
+
#selectionPanel { width: 168px; }
|
| 525 |
+
.minimap { width: 118px; height: 74px; }
|
| 526 |
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { test } from "node:test";
|
| 2 |
+
import assert from "node:assert/strict";
|
| 3 |
+
import {
|
| 4 |
+
createCamera, screenToWorld, worldToScreen, clampCamera, zoomAt,
|
| 5 |
+
dragCamera, pinchZoomPan, MIN_ZOOM, MAX_ZOOM,
|
| 6 |
+
} from "../camera.js";
|
| 7 |
+
|
| 8 |
+
const MAP = { width: 1600, height: 1000 };
|
| 9 |
+
const VW = 800, VH = 500;
|
| 10 |
+
|
| 11 |
+
test("screenToWorld and worldToScreen are inverses", () => {
|
| 12 |
+
const cam = { x: 700, y: 420, zoom: 1.4 };
|
| 13 |
+
const w = screenToWorld(cam, VW, VH, 123, 456);
|
| 14 |
+
const s = worldToScreen(cam, VW, VH, w.x, w.y);
|
| 15 |
+
assert.ok(Math.abs(s.x - 123) < 1e-9 && Math.abs(s.y - 456) < 1e-9, "round-trips back to the same screen point");
|
| 16 |
+
// The view centre maps to the camera's world focus.
|
| 17 |
+
const c = screenToWorld(cam, VW, VH, VW / 2, VH / 2);
|
| 18 |
+
assert.ok(Math.abs(c.x - cam.x) < 1e-9 && Math.abs(c.y - cam.y) < 1e-9);
|
| 19 |
+
});
|
| 20 |
+
|
| 21 |
+
test("clampCamera keeps the view inside the map, and centres an axis smaller than the viewport", () => {
|
| 22 |
+
const cam = { x: -500, y: 5000, zoom: 1 }; // way off both edges
|
| 23 |
+
clampCamera(cam, MAP, VW, VH);
|
| 24 |
+
assert.ok(cam.x >= VW / 2 && cam.x <= MAP.width - VW / 2, "x is pulled back inside the horizontal range");
|
| 25 |
+
assert.ok(cam.y >= VH / 2 && cam.y <= MAP.height - VH / 2, "y is pulled back inside the vertical range");
|
| 26 |
+
|
| 27 |
+
// Zoomed far out so the viewport is wider than the map -> that axis centres.
|
| 28 |
+
const wide = { x: 10, y: 10, zoom: 0.1 };
|
| 29 |
+
clampCamera(wide, MAP, VW, VH);
|
| 30 |
+
assert.equal(wide.x, MAP.width / 2, "an over-wide viewport centres x");
|
| 31 |
+
assert.equal(wide.y, MAP.height / 2, "an over-tall viewport centres y");
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
test("zoomAt keeps the world point under the cursor fixed and respects the zoom limits", () => {
|
| 35 |
+
const cam = createCamera(MAP);
|
| 36 |
+
const before = screenToWorld(cam, VW, VH, 600, 300);
|
| 37 |
+
zoomAt(cam, MAP, VW, VH, 600, 300, 1.5);
|
| 38 |
+
const after = screenToWorld(cam, VW, VH, 600, 300);
|
| 39 |
+
assert.ok(Math.abs(before.x - after.x) < 1e-6 && Math.abs(before.y - after.y) < 1e-6, "the point under the cursor doesn't drift");
|
| 40 |
+
assert.ok(cam.zoom > 1, "it zoomed in");
|
| 41 |
+
|
| 42 |
+
for (let i = 0; i < 40; i++) zoomAt(cam, MAP, VW, VH, 400, 250, 2);
|
| 43 |
+
assert.ok(cam.zoom <= MAX_ZOOM + 1e-9, "never past MAX_ZOOM");
|
| 44 |
+
for (let i = 0; i < 40; i++) zoomAt(cam, MAP, VW, VH, 400, 250, 0.5);
|
| 45 |
+
assert.ok(cam.zoom >= Math.min(MIN_ZOOM, VW / MAP.width, VH / MAP.height) - 1e-9, "never below the fit-the-map minimum");
|
| 46 |
+
});
|
| 47 |
+
|
| 48 |
+
test("dragCamera moves the world opposite the finger, scaled by zoom, and clamps", () => {
|
| 49 |
+
const cam = { x: 800, y: 500, zoom: 2 };
|
| 50 |
+
dragCamera(cam, MAP, VW, VH, 100, -40); // fingers drag right/up
|
| 51 |
+
assert.equal(cam.x, 800 - 100 / 2, "content follows the finger: +dx screen -> -dx/zoom world");
|
| 52 |
+
assert.equal(cam.y, 500 - (-40) / 2);
|
| 53 |
+
|
| 54 |
+
const edge = { x: MAP.width / 2, y: MAP.height / 2, zoom: 1 };
|
| 55 |
+
dragCamera(edge, MAP, VW, VH, 100000, 0); // shove hard past the edge
|
| 56 |
+
assert.ok(edge.x <= MAP.width - VW / 2 + 1e-9, "a drag can't push the view off the map");
|
| 57 |
+
});
|
| 58 |
+
|
| 59 |
+
test("pinchZoomPan zooms by the spread ratio about the pinch midpoint", () => {
|
| 60 |
+
const cam = { x: 800, y: 500, zoom: 1 };
|
| 61 |
+
// Both fingers straddle the exact view centre and spread 2x apart, midpoint
|
| 62 |
+
// fixed -> pure zoom-in of ~2x, and the world point at the centre stays put.
|
| 63 |
+
const centreWorldBefore = screenToWorld(cam, VW, VH, VW / 2, VH / 2);
|
| 64 |
+
const prev = { ax: VW / 2 - 50, ay: VH / 2, bx: VW / 2 + 50, by: VH / 2 };
|
| 65 |
+
const cur = { ax: VW / 2 - 100, ay: VH / 2, bx: VW / 2 + 100, by: VH / 2 };
|
| 66 |
+
pinchZoomPan(cam, MAP, VW, VH, prev, cur);
|
| 67 |
+
assert.ok(Math.abs(cam.zoom - 2) < 1e-6, "spread doubled -> zoom doubled");
|
| 68 |
+
const centreWorldAfter = screenToWorld(cam, VW, VH, VW / 2, VH / 2);
|
| 69 |
+
assert.ok(Math.abs(centreWorldBefore.x - centreWorldAfter.x) < 1e-6
|
| 70 |
+
&& Math.abs(centreWorldBefore.y - centreWorldAfter.y) < 1e-6, "the pinch midpoint's world point is anchored");
|
| 71 |
+
});
|
| 72 |
+
|
| 73 |
+
test("pinchZoomPan also pans when the two fingers translate together", () => {
|
| 74 |
+
const cam = { x: 800, y: 500, zoom: 1 };
|
| 75 |
+
// Same spread (no zoom), both fingers slide +60px in x -> a pure pan.
|
| 76 |
+
const prev = { ax: 300, ay: 250, bx: 400, by: 250 };
|
| 77 |
+
const cur = { ax: 360, ay: 250, bx: 460, by: 250 };
|
| 78 |
+
pinchZoomPan(cam, MAP, VW, VH, prev, cur);
|
| 79 |
+
assert.ok(Math.abs(cam.zoom - 1) < 1e-9, "equal spread -> no zoom");
|
| 80 |
+
assert.equal(cam.x, 800 - 60 / 1, "the view pans opposite the shared finger movement");
|
| 81 |
+
});
|