Compare commits
No commits in common. "fc7f5f83ca31c48cda3ffc0bdc422c34827a1069" and "e608c5b2053d5a6e8d6beb5b94a713cf101764cd" have entirely different histories.
fc7f5f83ca
...
e608c5b205
|
|
@ -68,11 +68,8 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
this.carSprites = new Map();
|
this.carSprites = new Map();
|
||||||
this.carRender = new Map();
|
this.carRender = new Map();
|
||||||
this.shimmerCells = [];
|
this.shimmerCells = [];
|
||||||
this.dragMode = null; // 'erase' | null
|
this.dragMode = null; // 'draw' | 'erase' | null
|
||||||
this.dragCell = null;
|
this.dragCell = null;
|
||||||
this.drawStart = null;
|
|
||||||
this.drawPath = [];
|
|
||||||
this.lastPathCell = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
create() {
|
create() {
|
||||||
|
|
@ -661,7 +658,7 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.hintText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 16,
|
this.hintText = this.add.text(GAME_WIDTH / 2, GAME_HEIGHT - 16,
|
||||||
'Click & drag to route roads · Right-drag to erase · Esc cancels tool', {
|
'Drag to draw roads · Right-drag to erase · Esc cancels a tool', {
|
||||||
fontFamily: '"Julius Sans One"', fontSize: '17px', color: COLORS.textDarkHex,
|
fontFamily: '"Julius Sans One"', fontSize: '17px', color: COLORS.textDarkHex,
|
||||||
}).setOrigin(0.5).setAlpha(0.55);
|
}).setOrigin(0.5).setAlpha(0.55);
|
||||||
this.uiRoot.add(this.hintText);
|
this.uiRoot.add(this.hintText);
|
||||||
|
|
@ -768,9 +765,6 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
|
|
||||||
setTool(tool) {
|
setTool(tool) {
|
||||||
this.tool = tool;
|
this.tool = tool;
|
||||||
this.drawStart = null;
|
|
||||||
this.drawPath = [];
|
|
||||||
this.lastPathCell = null;
|
|
||||||
this.ghostG.clear();
|
this.ghostG.clear();
|
||||||
for (const rec of Object.values(this.chips)) {
|
for (const rec of Object.values(this.chips)) {
|
||||||
rec.active = (tool === 'eraser' && rec.key === 'eraser')
|
rec.active = (tool === 'eraser' && rec.key === 'eraser')
|
||||||
|
|
@ -812,20 +806,11 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
// ── Input ─────────────────────────────────────────────────────────────────────
|
// ── Input ─────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
wireInput() {
|
wireInput() {
|
||||||
this.input.keyboard.on('keydown-ESC', () => {
|
this.input.keyboard.on('keydown-ESC', () => this.setTool(null));
|
||||||
if (this.drawStart !== null) {
|
|
||||||
this.drawStart = null;
|
|
||||||
this.drawPath = [];
|
|
||||||
this.lastPathCell = null;
|
|
||||||
this.ghostG.clear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.setTool(null);
|
|
||||||
});
|
|
||||||
|
|
||||||
this.input.on('pointerdown', (pointer, over) => {
|
this.input.on('pointerdown', (pointer, over) => {
|
||||||
if (this.view !== 'play' || this.overlayUp || !this.sim || this.sim.paused || this.sim.gameOver) return;
|
if (this.view !== 'play' || this.overlayUp || !this.sim || this.sim.paused || this.sim.gameOver) return;
|
||||||
if (over && over.length) return;
|
if (over && over.length) return; // a HUD element took it
|
||||||
const cell = this.worldCell(pointer);
|
const cell = this.worldCell(pointer);
|
||||||
if (cell === null) return;
|
if (cell === null) return;
|
||||||
|
|
||||||
|
|
@ -845,13 +830,9 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
}
|
}
|
||||||
if (this.tool) { this.useToolAt(cell); return; }
|
if (this.tool) { this.useToolAt(cell); return; }
|
||||||
|
|
||||||
const sim = this.sim;
|
this.dragMode = 'draw';
|
||||||
const startable = sim.roads.has(cell)
|
this.dragCell = cell;
|
||||||
|| (sim.terrain[cell] === TERRAIN.LAND && !sim.occupiedAt(cell));
|
this.tryPaint(cell);
|
||||||
if (!startable) return;
|
|
||||||
this.drawStart = cell;
|
|
||||||
this.drawPath = [cell];
|
|
||||||
this.lastPathCell = cell;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.input.on('pointermove', (pointer) => {
|
this.input.on('pointermove', (pointer) => {
|
||||||
|
|
@ -860,22 +841,10 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
if (this.dragMode && pointer.isDown && cell !== null) {
|
if (this.dragMode && pointer.isDown && cell !== null) {
|
||||||
this.stepDragTo(cell);
|
this.stepDragTo(cell);
|
||||||
}
|
}
|
||||||
if (this.drawStart !== null && pointer.isDown && cell !== null && cell !== this.lastPathCell) {
|
|
||||||
this.lastPathCell = cell;
|
|
||||||
const path = this.findDrawPath(this.drawStart, cell);
|
|
||||||
this.drawPath = path ?? [this.drawStart];
|
|
||||||
}
|
|
||||||
this.updateGhost(cell);
|
this.updateGhost(cell);
|
||||||
});
|
});
|
||||||
|
|
||||||
const endDrag = () => {
|
const endDrag = () => { this.dragMode = null; this.dragCell = null; };
|
||||||
if (this.drawStart !== null) this.buildDrawPath();
|
|
||||||
this.dragMode = null;
|
|
||||||
this.dragCell = null;
|
|
||||||
this.drawStart = null;
|
|
||||||
this.drawPath = [];
|
|
||||||
this.lastPathCell = null;
|
|
||||||
};
|
|
||||||
this.input.on('pointerup', endDrag);
|
this.input.on('pointerup', endDrag);
|
||||||
this.input.on('pointerupoutside', endDrag);
|
this.input.on('pointerupoutside', endDrag);
|
||||||
|
|
||||||
|
|
@ -883,117 +852,6 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
this.input.keyboard.on('keydown-TWO', () => this.pickUpgradeKey(1));
|
this.input.keyboard.on('keydown-TWO', () => this.pickUpgradeKey(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
findDrawPath(from, to) {
|
|
||||||
const sim = this.sim;
|
|
||||||
if (from === to) return [from];
|
|
||||||
|
|
||||||
const isPassable = (k) => sim.roads.has(k)
|
|
||||||
|| (sim.terrain[k] === TERRAIN.LAND && !sim.occupiedAt(k));
|
|
||||||
|
|
||||||
// State = cell * 9 + dirIndex (0-7 = DIRS entry, 8 = "no prior direction").
|
|
||||||
// Primary cost: new road tiles. Tiebreaker: tiny turn penalty so straight
|
|
||||||
// and diagonal runs beat zigzags when tile counts are equal.
|
|
||||||
const ND = 9;
|
|
||||||
const TURN_COST = 0.001;
|
|
||||||
const stateCount = WORLD_W * WORLD_H * ND;
|
|
||||||
const dist = new Float64Array(stateCount).fill(Infinity);
|
|
||||||
const par = new Int32Array(stateCount).fill(-1);
|
|
||||||
const settled = new Uint8Array(stateCount);
|
|
||||||
|
|
||||||
// When the cursor is on an existing road and we're not starting from one,
|
|
||||||
// treat ANY road cell as the goal instead of routing along the road to the
|
|
||||||
// cursor. The nearest road cell on a straight/diagonal approach always wins
|
|
||||||
// (0 turns), so you get a clean T- or L-junction rather than a diagonal
|
|
||||||
// shortcut that sneaks up to the road from an unexpected angle.
|
|
||||||
const stopAtRoad = sim.roads.has(to) && !sim.roads.has(from);
|
|
||||||
|
|
||||||
const startState = from * ND + 8;
|
|
||||||
dist[startState] = 0;
|
|
||||||
|
|
||||||
const heap = [startState];
|
|
||||||
const swap = (a, b) => { const t = heap[a]; heap[a] = heap[b]; heap[b] = t; };
|
|
||||||
const siftUp = (i) => {
|
|
||||||
while (i > 0) {
|
|
||||||
const p = (i - 1) >> 1;
|
|
||||||
if (dist[heap[p]] <= dist[heap[i]]) break;
|
|
||||||
swap(p, i); i = p;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const siftDown = (i) => {
|
|
||||||
const len = heap.length;
|
|
||||||
for (;;) {
|
|
||||||
let s = i;
|
|
||||||
const l = 2 * i + 1; const r = l + 1;
|
|
||||||
if (l < len && dist[heap[l]] < dist[heap[s]]) s = l;
|
|
||||||
if (r < len && dist[heap[r]] < dist[heap[s]]) s = r;
|
|
||||||
if (s === i) break;
|
|
||||||
swap(s, i); i = s;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
const heapPop = () => {
|
|
||||||
const top = heap[0];
|
|
||||||
const last = heap.pop();
|
|
||||||
if (heap.length > 0) { heap[0] = last; siftDown(0); }
|
|
||||||
return top;
|
|
||||||
};
|
|
||||||
const reconstruct = (state) => {
|
|
||||||
const path = [];
|
|
||||||
let s = state;
|
|
||||||
while (s !== -1) { path.unshift((s / ND) | 0); s = par[s]; }
|
|
||||||
return path;
|
|
||||||
};
|
|
||||||
|
|
||||||
while (heap.length > 0) {
|
|
||||||
const state = heapPop();
|
|
||||||
if (settled[state]) continue;
|
|
||||||
settled[state] = 1;
|
|
||||||
|
|
||||||
const cur = (state / ND) | 0;
|
|
||||||
|
|
||||||
// Terminate: reached specific target, or (in road-stop mode) any road cell.
|
|
||||||
if (cur !== from && (cur === to || (stopAtRoad && sim.roads.has(cur)))) {
|
|
||||||
return reconstruct(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
const cd = dist[state];
|
|
||||||
const curDir = state % ND;
|
|
||||||
const cx = xOf(cur); const cy = yOf(cur);
|
|
||||||
|
|
||||||
for (let di = 0; di < 8; di++) {
|
|
||||||
const [dx, dy] = DIRS[di];
|
|
||||||
const nx = cx + dx; const ny = cy + dy;
|
|
||||||
if (nx < 0 || nx >= WORLD_W || ny < 0 || ny >= WORLD_H) continue;
|
|
||||||
const nk = keyOf(nx, ny);
|
|
||||||
if (!isPassable(nk)) continue;
|
|
||||||
const cellCost = sim.roads.has(nk) ? 0 : 1;
|
|
||||||
const turnCost = (curDir !== 8 && di !== curDir) ? TURN_COST : 0;
|
|
||||||
const nd = cd + cellCost + turnCost;
|
|
||||||
const nState = nk * ND + di;
|
|
||||||
if (nd < dist[nState]) {
|
|
||||||
dist[nState] = nd;
|
|
||||||
par[nState] = state;
|
|
||||||
heap.push(nState);
|
|
||||||
siftUp(heap.length - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null; // target unreachable
|
|
||||||
}
|
|
||||||
|
|
||||||
buildDrawPath() {
|
|
||||||
if (!this.drawPath || this.drawPath.length === 0) return;
|
|
||||||
let painted = false;
|
|
||||||
for (const cell of this.drawPath) {
|
|
||||||
if (this.sim.roads.has(cell)) continue;
|
|
||||||
if (this.sim.canPlaceRoad(cell)) {
|
|
||||||
this.sim.placeRoad(cell);
|
|
||||||
painted = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (painted) playSound(this, SFX.PIECE_CLICK);
|
|
||||||
}
|
|
||||||
|
|
||||||
worldCell(pointer) {
|
worldCell(pointer) {
|
||||||
const p = pointer.positionToCamera(this.cameras.main);
|
const p = pointer.positionToCamera(this.cameras.main);
|
||||||
const x = Math.floor(p.x / CELL); const y = Math.floor(p.y / CELL);
|
const x = Math.floor(p.x / CELL); const y = Math.floor(p.y / CELL);
|
||||||
|
|
@ -1086,21 +944,6 @@ export default class MiniMotorwaysGame extends Phaser.Scene {
|
||||||
const t = this.tool;
|
const t = this.tool;
|
||||||
|
|
||||||
if (!t) {
|
if (!t) {
|
||||||
if (this.drawStart !== null && this.drawPath.length > 0) {
|
|
||||||
let budget = sim.stock.roads;
|
|
||||||
for (const k of this.drawPath) {
|
|
||||||
const isNew = !sim.roads.has(k);
|
|
||||||
if (isNew) budget--;
|
|
||||||
const affordable = !isNew || budget >= 0;
|
|
||||||
const px = xOf(k) * CELL; const py = yOf(k) * CELL;
|
|
||||||
g.fillStyle(affordable ? 0x57d977 : 0xe35050, isNew ? 0.48 : 0.2);
|
|
||||||
g.fillRoundedRect(px + 4, py + 4, CELL - 8, CELL - 8, 12);
|
|
||||||
}
|
|
||||||
// Highlight the start cell
|
|
||||||
g.fillStyle(0xffffff, 0.55);
|
|
||||||
g.fillCircle(cellCx(this.drawStart), cellCy(this.drawStart), CELL * 0.22);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const ok = sim.canPlaceRoad(cell) || sim.roads.has(cell);
|
const ok = sim.canPlaceRoad(cell) || sim.roads.has(cell);
|
||||||
g.fillStyle(ok ? 0xffffff : 0xe35050, ok ? 0.22 : 0.2);
|
g.fillStyle(ok ? 0xffffff : 0xe35050, ok ? 0.22 : 0.2);
|
||||||
g.fillRoundedRect(x + 4, y + 4, CELL - 8, CELL - 8, 12);
|
g.fillRoundedRect(x + 4, y + 4, CELL - 8, CELL - 8, 12);
|
||||||
|
|
|
||||||
|
|
@ -383,15 +383,12 @@ export class Sim {
|
||||||
const nk = keyOf(nx, ny);
|
const nk = keyOf(nx, ny);
|
||||||
if (!this.roads.has(nk)) continue;
|
if (!this.roads.has(nk)) continue;
|
||||||
if (dx !== 0 && dy !== 0) {
|
if (dx !== 0 && dy !== 0) {
|
||||||
// Suppress diagonal if either orthogonal intermediary is a road: either
|
// Crossing rule: if both shared corners carry road, the two diagonals
|
||||||
// the X-crossing case (both corners are roads, two diagonals would
|
// of this 2x2 block would cross — and the corners already connect the
|
||||||
// cross) or the T-junction case (one corner is road — traffic already
|
// cells orthogonally — so the diagonal edge is suppressed.
|
||||||
// flows through that cell, so the diagonal shortcut is spurious and
|
|
||||||
// would make a perpendicular approach fan out to three connections
|
|
||||||
// instead of one clean junction).
|
|
||||||
const cornerA = keyOf(x + dx, y);
|
const cornerA = keyOf(x + dx, y);
|
||||||
const cornerB = keyOf(x, y + dy);
|
const cornerB = keyOf(x, y + dy);
|
||||||
if (this.roads.has(cornerA) || this.roads.has(cornerB)) continue;
|
if (this.roads.has(cornerA) && this.roads.has(cornerB)) continue;
|
||||||
}
|
}
|
||||||
out.push({ k: nk, cost: dx !== 0 && dy !== 0 ? SQRT2 : 1 });
|
out.push({ k: nk, cost: dx !== 0 && dy !== 0 ? SQRT2 : 1 });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue