Animate movement through tiles and Navigation perimeter routes
This commit is contained in:
parent
ac01dd1d0b
commit
ffc5fb0641
6 changed files with 101 additions and 12 deletions
|
|
@ -283,3 +283,9 @@ Collect preselects one resource choice per eligible tile when the number of dist
|
|||
# Viewer 0.1.85 / engine unchanged (0.4.71)
|
||||
|
||||
Carthage's two routes to a pirate-occupied sea space are explicitly labelled Sail peacefully and Attack pirates. The peaceful option previously appeared only as Water. Verified on a read-only local copy of ClashoftheTitans: ship 4 and its settler move from A8 to E8 with no combat. Desktop/mobile browser clicks submit the peaceful action, and type checks and production build pass. No live move was submitted.
|
||||
|
||||
# Viewer 0.1.86 / engine unchanged (0.4.71)
|
||||
|
||||
Movement animations follow successive terrain-compatible tile centers instead of interpolating directly across the board. Ships and carried units stay on connected sea spaces; Navigation-capable players can follow the same perimeter waypoints as the sea-route guide. Land units traverse intermediate land tiles. Animation speed scales with route length, intermediate positions respect terrain surfaces, and unavailable historical routes snap to their destination rather than drawing an invalid shortcut. Reduced-motion behavior is preserved; rules and action costs are unchanged.
|
||||
|
||||
Verified sea/land routing, reverse travel and Navigation perimeter regressions (745 tests), type checks, production build, and desktop/mobile ship movement using a local copy of ClashoftheTitans without submitting live moves.
|
||||
|
|
|
|||
4
web-client/package-lock.json
generated
4
web-client/package-lock.json
generated
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.85",
|
||||
"version": "0.1.86",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.85",
|
||||
"version": "0.1.86",
|
||||
"dependencies": {
|
||||
"@boardgamers/protocol": "0.9.0",
|
||||
"lucide-svelte": "^0.577.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/clash-web-client",
|
||||
"version": "0.1.85",
|
||||
"version": "0.1.86",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { movementWaypoints } from './movement-animation';
|
||||
import { boardContentSignature } from './board-content';
|
||||
import { thumbnailCamera } from './thumbnail-camera';
|
||||
import CollectionMapBadge from './CollectionMapBadge.svelte';
|
||||
|
|
@ -94,17 +95,34 @@ export class World {
|
|||
const d = piece.userData;
|
||||
return `${d.kind}:${d.player}:${d.unit ?? d.cityPosition ?? d.position}:${d.building ?? ''}`;
|
||||
}
|
||||
private animatePieces(previous: Map<string, THREE.Vector3>, appearing: Set<THREE.Group>) {
|
||||
private animatePieces(previous: Map<string, {point: THREE.Vector3; position: string}>, appearing: Set<THREE.Group>, tiles: [string, Terrain][], navigation: string[][], navigationPlayers: Set<number>) {
|
||||
const moves = this.pieces
|
||||
.filter((piece) => !appearing.has(piece))
|
||||
.map((piece) => ({
|
||||
piece,
|
||||
from: previous.get(this.pieceKey(piece)),
|
||||
from: previous.get(this.pieceKey(piece))?.point,
|
||||
origin: previous.get(this.pieceKey(piece))?.position,
|
||||
to: piece.position.clone(),
|
||||
scale: piece.scale.clone(),
|
||||
}))
|
||||
.filter((m) => m.from && m.from.distanceTo(m.to) > 0.02);
|
||||
if (!moves.length) return;
|
||||
const paths = moves.map(m => {
|
||||
const waypoints = movementWaypoints(tiles, m.origin!, m.piece.userData.position, m.piece.userData.ship, navigationPlayers.has(m.piece.userData.player) ? navigation : []);
|
||||
const points = waypoints.map(([x,z],i) => {
|
||||
const position = tiles.find(([p]) => { const [px,pz] = positionXY(p); return Math.hypot(px-x,pz-z) < .001; })?.[0];
|
||||
const tile = position ? this.tiles.get(position) : undefined;
|
||||
const surface = tile ? tile.parent!.position.y + (tile.geometry as THREE.CylinderGeometry).parameters.height / 2 : 0;
|
||||
return new THREE.Vector3(x, Math.max(surface + .08, m.from!.y + (m.to.y-m.from!.y)*i/Math.max(1,waypoints.length-1)), z);
|
||||
});
|
||||
if (points.length > 1) { points[0] = m.from!.clone(); points[points.length-1] = m.to.clone(); }
|
||||
else if (m.origin === m.piece.userData.position) return new THREE.LineCurve3(m.from!,m.to);
|
||||
if (points.length < 2) return null;
|
||||
const path = new THREE.CurvePath<THREE.Vector3>();
|
||||
for (let i=1;i<points.length;i++) path.add(new THREE.LineCurve3(points[i-1],points[i]));
|
||||
return path;
|
||||
});
|
||||
const duration = Math.min(2400, Math.max(650, ...paths.map(path => (path?.getLength() ?? 0)*180)));
|
||||
const start = performance.now();
|
||||
this.settleMotion = () => {
|
||||
cancelAnimationFrame(this.motionFrame);
|
||||
|
|
@ -116,11 +134,11 @@ export class World {
|
|||
this.invalidate();
|
||||
};
|
||||
const tick = () => {
|
||||
const t = Math.min(1, (performance.now() - start) / 650),
|
||||
const t = Math.min(1, (performance.now() - start) / duration),
|
||||
eased = t * t * (3 - 2 * t);
|
||||
for (const m of moves) {
|
||||
if (m.from) m.piece.position.lerpVectors(m.from, m.to, eased);
|
||||
}
|
||||
moves.forEach((m, i) => {
|
||||
m.piece.position.copy(paths[i]?.getPointAt(eased) ?? m.to);
|
||||
});
|
||||
this.invalidate();
|
||||
if (t < 1) this.motionFrame = requestAnimationFrame(tick);
|
||||
else this.settleMotion?.();
|
||||
|
|
@ -1174,7 +1192,7 @@ export class World {
|
|||
this.lastSignature = signature;
|
||||
this.settleMotion?.();
|
||||
this.placement.stop();
|
||||
const previous = new Map(this.pieces.map((p) => [this.pieceKey(p), p.position.clone()]));
|
||||
const previous = new Map(this.pieces.map((p) => [this.pieceKey(p), {point: p.position.clone(), position: p.userData.position as string}]));
|
||||
const previousBuildings = new Set(this.buildings.keys());
|
||||
const previousCities = new Set(
|
||||
this.pieces
|
||||
|
|
@ -1482,7 +1500,7 @@ export class World {
|
|||
unit.pirate,
|
||||
player.civilization,
|
||||
);
|
||||
pawn.userData = { position: unit.position, kind: 'unit', unit: unit.id, player: player.id };
|
||||
pawn.userData = { position: unit.position, kind: 'unit', unit: unit.id, player: player.id, ship: unit.unit_type === 'Ship' };
|
||||
this.pieces.push(pawn);
|
||||
const stackIndex = unitStacks.get(unit.position) ?? 0;
|
||||
unitStacks.set(unit.position, stackIndex + 1);
|
||||
|
|
@ -1656,7 +1674,7 @@ export class World {
|
|||
})),
|
||||
]
|
||||
: [];
|
||||
if (animate) this.animatePieces(previous, new Set(appearing.map(({ model }) => model)));
|
||||
if (animate) this.animatePieces(previous, new Set(appearing.map(({ model }) => model)), mapTiles, s.view?.seaRoutes ?? [], new Set(s.game.players.filter(p => p.advances?.includes('Navigation')).map(p => p.id)));
|
||||
this.placement.play(appearing, () => this.invalidate());
|
||||
// Frame a newly created map, including when the last opponent finishes choosing.
|
||||
if (!hadTiles && this.tiles.size > 0) this.reset();
|
||||
|
|
|
|||
23
web-client/src/movement-animation.test.ts
Normal file
23
web-client/src/movement-animation.test.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { movementWaypoints } from './movement-animation.ts';
|
||||
import { positionXY } from './model.ts';
|
||||
import { seaEdgePoint } from './sea-routes.ts';
|
||||
import type { Terrain } from './types.ts';
|
||||
test('ships and carried units follow connected sea tiles around land in either direction', () => {
|
||||
const tiles: [string,Terrain][] = [['D2','Water'],['E2','Mountain'],['E3','Water'],['F2','Water']];
|
||||
const expected = ['D2','E3','F2'].map(positionXY);
|
||||
assert.deepEqual(movementWaypoints(tiles,'D2','F2',true),expected);
|
||||
assert.deepEqual(movementWaypoints(tiles,'D2','F2',false),expected);
|
||||
assert.deepEqual(movementWaypoints(tiles,'F2','D2',true),[...expected].reverse());
|
||||
});
|
||||
test('land movement visits intermediate land tiles and never cuts through sea', () => {
|
||||
const tiles: [string,Terrain][] = [['D2','Fertile'],['E2','Water'],['E3','Forest'],['F2','Mountain']];
|
||||
assert.deepEqual(movementWaypoints(tiles,'D2','F2',false),['D2','E3','F2'].map(positionXY));
|
||||
});
|
||||
test('Navigation travels along perimeter points rather than across intervening land', () => {
|
||||
const tiles: [string,Terrain][] = [['D2','Water'],['E2','Mountain'],['F2','Water']];
|
||||
const perimeter=['D2','E2','F2'];
|
||||
assert.deepEqual(movementWaypoints(tiles,'D2','F2',true),[]);
|
||||
assert.deepEqual(movementWaypoints(tiles,'D2','F2',true,[perimeter]),[positionXY('D2'),...perimeter.map(p=>seaEdgePoint(p,tiles)),positionXY('F2')]);
|
||||
});
|
||||
42
web-client/src/movement-animation.ts
Normal file
42
web-client/src/movement-animation.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import { positionXY } from './model.ts';
|
||||
import { seaEdgePoint } from './sea-routes.ts';
|
||||
import type { Terrain } from './types';
|
||||
type Point = [number, number];
|
||||
|
||||
// Reconstruct a visual route from public terrain; never invent a straight shortcut
|
||||
// across an incompatible tile. Navigation uses the same perimeter as the sea guide.
|
||||
export function movementWaypoints(tiles: [string, Terrain][], start: string, end: string, ship: boolean, navigation: string[][] = []): Point[] {
|
||||
if (start === end) return [positionXY(start)];
|
||||
const terrain = new Map(tiles);
|
||||
const sea = ship || (terrain.get(start) === 'Water' && terrain.get(end) === 'Water');
|
||||
const nodes = tiles.filter(([p, t]) => p === start || p === end || (sea ? t === 'Water' : t !== 'Water' && t !== 'Unexplored')).map(([p]) => p);
|
||||
const links = new Map<string, {to: string; points: Point[]; length: number}[]>();
|
||||
const add = (from: string, to: string, points: Point[]) => {
|
||||
const length = points.slice(1).reduce((sum, point, i) => sum + Math.hypot(point[0] - points[i][0], point[1] - points[i][1]), 0);
|
||||
links.set(from, [...(links.get(from) ?? []), {to, points, length}]);
|
||||
};
|
||||
for (const a of nodes) for (const b of nodes) {
|
||||
const pa = positionXY(a), pb = positionXY(b);
|
||||
if (Math.abs(Math.hypot(pa[0]-pb[0], pa[1]-pb[1])-Math.sqrt(3)) < .001) add(a,b,[pa,pb]);
|
||||
}
|
||||
if (sea) for (const path of navigation) {
|
||||
const a = path[0], b = path.at(-1);
|
||||
if (!a || !b || !nodes.includes(a) || !nodes.includes(b)) continue;
|
||||
const points: Point[] = [positionXY(a), ...path.map(p => seaEdgePoint(p,tiles)), positionXY(b)];
|
||||
add(a,b,points); add(b,a,[...points].reverse());
|
||||
}
|
||||
const distance = new Map([[start,0]]), routes = new Map<string,Point[]>([[start,[positionXY(start)]]]), pending = new Set(nodes);
|
||||
while (pending.size) {
|
||||
const current = [...pending].sort((a,b)=>(distance.get(a)??Infinity)-(distance.get(b)??Infinity))[0];
|
||||
if (!distance.has(current)) break;
|
||||
if (current === end) return routes.get(current)!;
|
||||
pending.delete(current);
|
||||
for (const link of links.get(current) ?? []) {
|
||||
const next = distance.get(current)! + link.length;
|
||||
if (pending.has(link.to) && next < (distance.get(link.to)??Infinity)) {
|
||||
distance.set(link.to,next); routes.set(link.to,[...routes.get(current)!,...link.points.slice(1)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue