feat: the world map draws each village as its picture with <use>, mountains where hills bunch up, and its fields, rulers and minimap frame in composited layers that dragging only moves

The land comes from utils/terrain.ts like the minimap. One hover handler on the map
instead of one per field; the hover popup hides while dragging like the click menu.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-11-03 10:17:53 +01:00
parent ddd6f9f1bc
commit 42fbb4fd56
4 changed files with 241 additions and 298 deletions

134
app/components/map/Land.vue Normal file
View File

@ -0,0 +1,134 @@
<script setup lang="ts">
import type { MapVillage } from '~/types/map'
import type { Ground } from '~/utils/terrain'
const props = defineProps<{
/** Whole fields to draw: from x0|y0 up to, not including, x1|y1. */
region: { x0: number; y0: number; x1: number; y1: number }
/** Field size in px. */
field: number
/** Villages by "x|y": their fields are open land. */
villages: Map<string, MapVillage>
}>()
// The ground of every field in the region, at its world position: grass in four shades and
// lakes as one path each, shores, rocky hills, mountains and trees on top. Drawn from
// utils/terrain.ts, the same as the minimap. Only changes when the region or a village does,
// so dragging the map never re-renders it.
interface Field {
x: number
y: number
ground: Ground
/** Trees as [x, y, size] in the field's 100 × 100 box, back to front. */
trees: [number, number, number][]
/** A mountain's faces, a little different on every field. */
peaks: string[] | null
}
const pts = (...p: number[]) => p.reduce((s, v, i) => s + (i % 2 ? `,${v.toFixed(1)}` : `${i ? ' ' : ''}${v.toFixed(1)}`), '')
/**
* A mountain in the field's 100 × 100 box: a big peak and a smaller one behind it, each a lit
* face, a face in shadow and bare rock at the top. The big one rises over the field above, so
* where mountains bunch up they close into a range.
*/
function peaks(x: number, y: number): string[] {
const r = fieldHash(x, y)
const q = fieldHash(y, x)
const out: string[] = []
for (const [ax, ay, lx, rx, by] of [
[24 + q * 14, 18 + r * 10, -2, 60, 84],
[48 + r * 12, -14 + q * 14, 14, 100, 94],
] as const) {
const mx = ax + (rx - ax) * 0.3
const my = by + 4
const k = 0.3
out.push(
pts(lx, by, ax, ay, mx, my),
pts(ax, ay, rx, by, mx, my),
pts(ax, ay, ax + (lx - ax) * k, ay + (by - ay) * k, ax + (mx - ax) * k, ay + (my - ay) * k),
pts(ax, ay, ax + (rx - ax) * k, ay + (by - ay) * k, ax + (mx - ax) * k, ay + (my - ay) * k),
)
}
return out
}
/** Fills of a mountain's faces, in the order `peaks` returns them: lit, in shadow, bare rock on each. */
const PEAK_FILLS = ['#827d74', '#5a5751', '#9a958b', '#6e6b65']
const groundOf = (x: number, y: number): Ground => (props.villages.has(`${x}|${y}`) ? 'grass' : groundAt(x, y))
const fields = computed(() => {
const { x0, y0, x1, y1 } = props.region
const F = props.field
const out: Field[] = []
// The flat ground: one path per grass shade and one for the lakes, a subpath per run of fields.
const flat: Record<string, string> = {}
let shore = ''
let ripples = ''
for (let y = y0; y < y1; y++) {
let run: string | null = null
let from = x0
const close = (to: number) => {
if (run) flat[run] = `${flat[run] ?? ''}M${from * F} ${y * F}h${(to - from) * F}v${F}h${-(to - from) * F}z`
}
for (let x = x0; x <= x1; x++) {
const ground = x < x1 ? groundOf(x, y) : null
const fill = ground === null ? null : ground === 'water' ? WATER : GRASS[Math.floor(fieldHash(x, y) * GRASS.length)]!
if (fill !== run) {
close(x)
run = fill
from = x
}
if (ground === null || ground === 'grass') continue
if (ground === 'water') {
// A sandy shore where the lake meets land, and a ripple.
const land = (gx: number, gy: number) => groundOf(gx, gy) !== 'water'
const s = F * 0.09
if (land(x, y - 1)) shore += `M${x * F} ${y * F}h${F}v${s}h${-F}z`
if (land(x, y + 1)) shore += `M${x * F} ${(y + 1) * F - s}h${F}v${s}h${-F}z`
if (land(x - 1, y)) shore += `M${x * F} ${y * F}h${s}v${F}h${-s}z`
if (land(x + 1, y)) shore += `M${(x + 1) * F - s} ${y * F}h${s}v${F}h${-s}z`
const k = F / 100
ripples += `M${(x * 100 + 28 + fieldHash(x, y) * 30) * k} ${(y * 100 + 50) * k}h${18 * k}M${(x * 100 + 40 + fieldHash(y, x) * 20) * k} ${(y * 100 + 64) * k}h${12 * k}`
continue
}
// Deeper in a forest, more and bigger trees.
const count = ground === 'forest' ? 2 + Math.round(forestDepth(x, y) * 4) : 0
const trees = Array.from({ length: count }, (_, i): [number, number, number] => [
14 + fieldHash(x * 3 + i, y) * 72,
28 + fieldHash(x, y * 3 + i) * 56,
0.75 + fieldHash(x + i, y + i) * 0.5,
]).sort((a, b) => a[1] - b[1])
out.push({ x, y, ground, trees, peaks: ground === 'mountain' ? peaks(x, y) : null })
}
}
return { flat: Object.entries(flat), shore, ripples, raised: out }
})
</script>
<template>
<g>
<path v-for="[fill, d] in fields.flat" :key="fill" :d="d" :fill="fill" />
<path v-if="fields.shore" :d="fields.shore" :fill="SHORE" />
<path v-if="fields.ripples" :d="fields.ripples" stroke="#6a7a80" :stroke-width="field * 0.03" stroke-linecap="round" />
<g v-for="f in fields.raised" :key="`${f.x}|${f.y}`" :transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`">
<template v-if="f.ground === 'hill'">
<ellipse cx="52" cy="70" rx="40" ry="16" fill="#5d6242" />
<polygon points="18,72 40,30 58,72" fill="#8a857c" />
<polygon points="40,30 58,72 64,60" fill="#5f5c56" />
<polygon points="48,74 68,42 86,74" fill="#9a958c" />
<polygon points="68,42 86,74 90,66" fill="#6c6962" />
</template>
<template v-else-if="f.peaks">
<ellipse cx="50" cy="88" rx="52" ry="14" fill="#5a5e3f" />
<polygon v-for="(p, i) in f.peaks" :key="i" :points="p" :fill="PEAK_FILLS[i % 4]" />
</template>
<g v-for="(tr, i) in f.trees" :key="i" :transform="`translate(${tr[0]} ${tr[1]}) scale(${tr[2]})`">
<ellipse cx="4" cy="26" rx="12" ry="4" fill="#4f5535" />
<rect x="-2" y="18" width="4" height="9" fill="#4a3b2e" />
<polygon points="0,-10 -13,20 0,16" fill="#2b3a27" />
<polygon points="0,-10 13,20 0,16" fill="#3e5034" />
</g>
</g>
</g>
</template>

View File

@ -1,129 +0,0 @@
<script setup lang="ts">
import type { MarkerRelation } from '~/utils/relations'
const props = defineProps<{ tier: 1 | 2 | 3 | 4 | 5; relation: MarkerRelation; crown?: boolean }>()
// Low-poly settlements in a 100 × 100 box that grow with the village's points, like the
// five village sizes on the Die Stämme map: a hut, a hamlet, a village with a tower, a
// town behind a palisade, and a stone keep behind walls. Each block has two lit wall
// faces and a two-facet roof, so it reads as 3D like the village diorama.
const tones = computed(() =>
props.relation === 'barbarian'
? { wallLight: '#aaa498', wallDark: '#827d74', roofLight: '#6e685f', roofDark: '#524c45' }
: { wallLight: '#b5a486', wallDark: '#8b7c62', roofLight: '#7f4430', roofDark: '#5a3020' },
)
const STONE = { wallLight: '#9d988f', wallDark: '#77736c', roofLight: '#5a5f66', roofDark: '#45494f' }
const flag = computed(() => RELATION_COLOURS[props.relation])
interface Block {
cx: number
cy: number
w: number
d: number
h: number
roof: number
stone?: boolean
}
const blocks = computed<Block[]>(() => {
switch (props.tier) {
case 1:
return [{ cx: 50, cy: 66, w: 18, d: 10, h: 14, roof: 14 }]
case 2:
return [
{ cx: 36, cy: 60, w: 15, d: 8, h: 12, roof: 12 },
{ cx: 62, cy: 70, w: 18, d: 10, h: 14, roof: 14 },
]
case 3:
return [
{ cx: 50, cy: 52, w: 10, d: 6, h: 30, roof: 13 },
{ cx: 30, cy: 66, w: 15, d: 8, h: 13, roof: 12 },
{ cx: 70, cy: 70, w: 16, d: 9, h: 14, roof: 13 },
]
case 4:
return [
{ cx: 50, cy: 46, w: 11, d: 6, h: 34, roof: 14 },
{ cx: 28, cy: 58, w: 14, d: 8, h: 13, roof: 12 },
{ cx: 72, cy: 60, w: 15, d: 8, h: 14, roof: 12 },
{ cx: 48, cy: 70, w: 16, d: 8, h: 12, roof: 11 },
]
default:
return [
{ cx: 50, cy: 50, w: 17, d: 10, h: 38, roof: 16, stone: true },
{ cx: 26, cy: 58, w: 13, d: 7, h: 12, roof: 11 },
{ cx: 74, cy: 60, w: 13, d: 7, h: 12, roof: 11 },
]
}
})
const pts = (...p: number[]) => {
const out: string[] = []
for (let i = 0; i < p.length; i += 2) out.push(`${p[i]},${p[i + 1]}`)
return out.join(' ')
}
function faces(b: Block) {
const { cx, cy, w, d } = b
const top = cy - b.h
const apex = top - b.roof
return {
left: pts(cx - w, cy, cx, cy + d, cx, cy + d - b.h, cx - w, top),
right: pts(cx, cy + d, cx + w, cy, cx + w, top, cx, cy + d - b.h),
roofLeft: pts(cx - w - 2, top + 1, cx, cy + d - b.h + 2, cx, apex),
roofRight: pts(cx, cy + d - b.h + 2, cx + w + 2, top + 1, cx, apex),
colors: b.stone ? STONE : tones.value,
}
}
/** Points along the front half of the ring a palisade or wall stands on. */
const ring = computed(() =>
Array.from({ length: 15 }, (_, i) => {
const a = Math.PI * (0.02 + (i / 14) * 0.96)
return { x: 50 + Math.cos(a) * 44, y: 66 + Math.sin(a) * 19 }
}),
)
const pole = computed(() => {
const first = blocks.value[0]!
return { x: first.cx, y: first.cy - first.h - first.roof }
})
</script>
<template>
<g>
<ellipse cx="50" cy="72" rx="42" ry="18" fill="#5f5640" />
<!-- The relation colour rings the village, so it reads at a glance. -->
<ellipse cx="50" cy="72" rx="44" ry="20" fill="none" :stroke="flag" stroke-width="6" />
<g v-for="(b, i) in blocks" :key="i">
<polygon :points="faces(b).left" :fill="faces(b).colors.wallDark" />
<polygon :points="faces(b).right" :fill="faces(b).colors.wallLight" />
<polygon :points="faces(b).roofLeft" :fill="faces(b).colors.roofDark" />
<polygon :points="faces(b).roofRight" :fill="faces(b).colors.roofLight" />
</g>
<!-- A town's palisade or a keep's stone wall along the front of the ring. -->
<g v-if="tier === 4">
<rect v-for="(p, i) in ring" :key="i" :x="p.x - 2" :y="p.y - 9" width="4" height="10" :fill="i % 2 ? '#574536' : '#7a6249'" />
</g>
<g v-else-if="tier === 5">
<polygon
v-for="(p, i) in ring.slice(0, -1)"
:key="i"
:points="pts(p.x, p.y, ring[i + 1]!.x, ring[i + 1]!.y, ring[i + 1]!.x, ring[i + 1]!.y - 10, p.x, p.y - 10)"
:fill="i % 2 ? '#77736c' : '#928d84'"
/>
<rect v-for="i in [0, 7, 14]" :key="`t${i}`" :x="ring[i]!.x - 4" :y="ring[i]!.y - 16" width="8" height="16" fill="#74716a" />
</g>
<line :x1="pole.x" :y1="pole.y" :x2="pole.x" :y2="pole.y - 20" stroke="#4a3b2e" stroke-width="2" />
<polygon :points="pts(pole.x, pole.y - 20, pole.x + 15, pole.y - 15, pole.x, pole.y - 10)" :fill="flag" stroke="#2b2118" stroke-width="0.8" />
<!-- A crown fort of the season wears a gold crown over its flag. -->
<g v-if="crown">
<polygon
:points="pts(pole.x - 12, pole.y - 22, pole.x - 12, pole.y - 36, pole.x - 6, pole.y - 29, pole.x, pole.y - 40, pole.x + 6, pole.y - 29, pole.x + 12, pole.y - 36, pole.x + 12, pole.y - 22)"
fill="#d9b44a"
stroke="#6b4f16"
stroke-width="1.2"
/>
<rect :x="pole.x - 12" :y="pole.y - 26" width="24" height="4" fill="#a8842e" />
</g>
</g>
</template>

View File

@ -1,4 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { mapVillageSymbol } from '~/diorama/mapVillage'
import type { MapMovement, MapVillage, MapView } from '~/types/map' import type { MapMovement, MapVillage, MapView } from '~/types/map'
import type { MarkerRelation } from '~/utils/relations' import type { MarkerRelation } from '~/utils/relations'
@ -43,91 +44,49 @@ const lo = computed(() => props.view?.min ?? mapMin(mapSize.value))
const villageAt = computed(() => new Map((props.view?.villages ?? []).map((v) => [`${v.x}|${v.y}`, v]))) const villageAt = computed(() => new Map((props.view?.villages ?? []).map((v) => [`${v.x}|${v.y}`, v])))
// Same field, same look: terrain comes from a hash of the coordinates. // Whole fields touching the visible area, for the rulers.
const hash = (x: number, y: number) => {
let h = (x * 73856093) ^ (y * 19349663)
h = Math.imul(h ^ (h >>> 13), 1274126177)
return ((h ^ (h >>> 16)) >>> 0) / 4294967296
}
const GRASS = ['#6b7049', '#676c46', '#6f724c', '#73704b']
const WATER = '#4a5b5d'
/** Smooth value noise: neighbouring fields get similar values, so terrain comes in patches. */
const smooth = (t: number) => t * t * (3 - 2 * t)
function noise(x: number, y: number, size: number, seed: number) {
const gx = x / size
const gy = y / size
const ix = Math.floor(gx)
const iy = Math.floor(gy)
const fx = smooth(gx - ix)
const fy = smooth(gy - iy)
const at = (a: number, b: number) => hash(a * 7 + seed * 101, b * 13 - seed * 37)
const top = at(ix, iy) + (at(ix + 1, iy) - at(ix, iy)) * fx
const bottom = at(ix, iy + 1) + (at(ix + 1, iy + 1) - at(ix, iy + 1)) * fx
return top + (bottom - top) * fy
}
type Ground = 'grass' | 'forest' | 'water' | 'hill'
/** Lakes, rocky hills and forests in patches; a village's field is always open land. */
function groundAt(x: number, y: number): Ground {
if (noise(x, y, 11, 1) < 0.17) return 'water'
if (noise(x, y, 8, 2) > 0.8) return 'hill'
if (noise(x, y, 6, 3) > 0.6 || hash(x, y) < 0.05) return 'forest'
return 'grass'
}
// Whole fields touching the visible area. These only change when an edge crosses a
// field, so dragging moves one transform instead of re-rendering every field.
const x0 = computed(() => Math.max(lo.value, Math.floor(props.left))) const x0 = computed(() => Math.max(lo.value, Math.floor(props.left)))
const y0 = computed(() => Math.max(lo.value, Math.floor(props.top))) const y0 = computed(() => Math.max(lo.value, Math.floor(props.top)))
const x1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.left + props.cols))) const x1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.left + props.cols)))
const y1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.top + props.rows))) const y1 = computed(() => Math.min(lo.value + mapSize.value, Math.ceil(props.top + props.rows)))
interface Field { // The land, the villages and the groups on the march are drawn for a region of whole fields
x: number // round the visible ones, at their world position, in a layer of their own that dragging only
y: number // moves (a CSS transform the browser composites without painting anew). The region stays the
grass: string // same object while the view is inside it, so nothing in it re-renders until the view
village: MapVillage | null // leaves it, PAD fields on, or the zoom changes.
ground: Ground const PAD = 2
/** Trees as [x, y, size] in the field's 100 × 100 box, back to front. */ const region = computed<{ x0: number; y0: number; x1: number; y1: number; field: number }>((last) => {
trees: [number, number, number][] const F = props.field
/** Sides of a lake field that meet land and get a shore. */ if (last && last.field === F && x0.value >= last.x0 && y0.value >= last.y0 && x1.value <= last.x1 && y1.value <= last.y1) return last
shore: { top: boolean; right: boolean; bottom: boolean; left: boolean } | null const hi = lo.value + mapSize.value
} return { x0: Math.max(lo.value, x0.value - PAD), y0: Math.max(lo.value, y0.value - PAD), x1: Math.min(hi, x1.value + PAD), y1: Math.min(hi, y1.value + PAD), field: F }
})
const fields = computed<Field[]>(() => { /** Where the world's 0|0 sits on screen, in whole pixels so the layer stays crisp. */
const out: Field[] = [] const shiftX = computed(() => RULER + Math.round(-props.left * props.field))
for (let y = y0.value; y < y1.value; y++) { const shiftY = computed(() => RULER + Math.round(-props.top * props.field))
for (let x = x0.value; x < x1.value; x++) { const layer = computed(() => {
const r = hash(x, y) const r = region.value
const village = villageAt.value.get(`${x}|${y}`) ?? null const F = props.field
const ground: Ground = village ? 'grass' : groundAt(x, y) return {
const land = (gx: number, gy: number) => !!villageAt.value.get(`${gx}|${gy}`) || groundAt(gx, gy) !== 'water' width: (r.x1 - r.x0) * F,
// Deeper in a forest, more and bigger trees. height: (r.y1 - r.y0) * F,
const count = ground === 'forest' ? 2 + Math.round(noise(x, y, 6, 3) * 4) : 0 viewBox: `${r.x0 * F} ${r.y0 * F} ${(r.x1 - r.x0) * F} ${(r.y1 - r.y0) * F}`,
const trees = Array.from({ length: count }, (_, i): [number, number, number] => [ x: shiftX.value - RULER + r.x0 * F,
14 + hash(x * 3 + i, y) * 72, y: shiftY.value - RULER + r.y0 * F,
28 + hash(x, y * 3 + i) * 56, transform: `translate3d(${shiftX.value - RULER + r.x0 * F}px, ${shiftY.value - RULER + r.y0 * F}px, 0)`,
0.75 + hash(x + i, y + i) * 0.5, }
]).sort((a, b) => a[1] - b[1])
out.push({
x,
y,
grass: GRASS[Math.floor(r * GRASS.length)]!,
village,
ground,
trees,
shore: ground === 'water' ? { top: land(x, y - 1), right: land(x + 1, y), bottom: land(x, y + 1), left: land(x - 1, y) } : null,
})
}
}
return out
}) })
const villageFields = computed(() => fields.value.filter((f) => f.village))
/** Five sizes by points, like the Die Stämme map. */
const tier = (points: number): 1 | 2 | 3 | 4 | 5 => (points < 150 ? 1 : points < 500 ? 2 : points < 1500 ? 3 : points < 5000 ? 4 : 5)
const relation = (v: MapVillage): MarkerRelation => (v.id === props.current ? 'current' : (v.relation ?? (v.own ? 'own' : v.player ? 'none' : 'barbarian'))) const relation = (v: MapVillage): MarkerRelation => (v.id === props.current ? 'current' : (v.relation ?? (v.own ? 'own' : v.player ? 'none' : 'barbarian')))
/** Villages in the region, row by row so lower ones draw over the roofs and towers above. */
const villages = computed(() => {
const r = region.value
return (props.view?.villages ?? [])
.filter((v) => v.x >= r.x0 && v.x < r.x1 && v.y >= r.y0 && v.y < r.y1)
.sort((a, b) => a.y - b.y || a.x - b.x)
.map((v) => ({ id: v.id, x: v.x, y: v.y, look: mapVillageSymbol(v), colour: RELATION_COLOURS[relation(v)] }))
})
// Groups on the march: a line from village to village and the group where it is now, // Groups on the march: a line from village to village and the group where it is now,
// moving straight at constant speed (raiders wait at their camp until they march). A group // moving straight at constant speed (raiders wait at their camp until they march). A group
@ -153,15 +112,15 @@ const marchTitle = (m: MapMovement) =>
' · ' + ' · ' +
formatDuration(Math.max(0, (Date.parse(m.arrives_at) - props.now) / 1000)) formatDuration(Math.max(0, (Date.parse(m.arrives_at) - props.now) / 1000))
// Sector lines every 10 fields, continent borders every 100. // Sector lines every 10 fields, continent borders every 100, across the region.
const lines = computed(() => { const lines = computed(() => {
const F = props.field const { x0, y0, x1, y1, field: F } = region.value
const out: { x1: number; y1: number; x2: number; y2: number; kind: 'sector' | 'continent' }[] = [] const out: { x1: number; y1: number; x2: number; y2: number; kind: 'sector' | 'continent' }[] = []
for (let x = x0.value; x <= x1.value; x++) { for (let x = x0; x <= x1; x++) {
if (x % 10 === 0) out.push({ x1: x * F, y1: y0.value * F, x2: x * F, y2: y1.value * F, kind: x % 100 === 0 ? 'continent' : 'sector' }) if (x % 10 === 0) out.push({ x1: x * F, y1: y0 * F, x2: x * F, y2: y1 * F, kind: x % 100 === 0 ? 'continent' : 'sector' })
} }
for (let y = y0.value; y <= y1.value; y++) { for (let y = y0; y <= y1; y++) {
if (y % 10 === 0) out.push({ x1: x0.value * F, y1: y * F, x2: x1.value * F, y2: y * F, kind: y % 100 === 0 ? 'continent' : 'sector' }) if (y % 10 === 0) out.push({ x1: x0 * F, y1: y * F, x2: x1 * F, y2: y * F, kind: y % 100 === 0 ? 'continent' : 'sector' })
} }
return out return out
}) })
@ -179,12 +138,11 @@ const continents = computed(() => {
return out return out
}) })
const columns = computed(() => Array.from({ length: x1.value - x0.value }, (_, i) => x0.value + i)) // The rulers' numbers run along the region too, in layers that move with the fields.
const rowsList = computed(() => Array.from({ length: y1.value - y0.value }, (_, i) => y0.value + i)) const columns = computed(() => Array.from({ length: region.value.x1 - region.value.x0 }, (_, i) => region.value.x0 + i))
const rowsList = computed(() => Array.from({ length: region.value.y1 - region.value.y0 }, (_, i) => region.value.y0 + i))
const rulerFont = computed(() => Math.max(9, Math.min(12, props.field / 4))) const rulerFont = computed(() => Math.max(9, Math.min(12, props.field / 4)))
const shiftX = computed(() => RULER - props.left * props.field)
const shiftY = computed(() => RULER - props.top * props.field)
// Dragging pans pixel by pixel; a click without movement selects a field. // Dragging pans pixel by pixel; a click without movement selects a field.
let drag: { lastX: number; lastY: number; moved: boolean } | null = null let drag: { lastX: number; lastY: number; moved: boolean } | null = null
@ -196,8 +154,34 @@ function onPointerDown(e: PointerEvent) {
;(e.currentTarget as Element).setPointerCapture(e.pointerId) ;(e.currentTarget as Element).setPointerCapture(e.pointerId)
} }
/** The field under a pointer, or null over the rulers and off the map. */
function fieldUnder(e: PointerEvent | MouseEvent) {
const rect = (e.currentTarget as Element).getBoundingClientRect()
const px = e.clientX - rect.left - RULER
const py = e.clientY - rect.top - RULER
if (px < 0 || py < 0) return null
const x = Math.floor(props.left + px / props.field)
const y = Math.floor(props.top + py / props.field)
const hi = lo.value + mapSize.value
return x >= lo.value && y >= lo.value && x < hi && y < hi ? { x, y } : null
}
// Hover: one handler on the map finds the field, instead of a listener on every field.
let hoveredKey: string | null = null
function hover(e: PointerEvent) {
const at = fieldUnder(e)
const key = at ? `${at.x}|${at.y}` : null
if (key === hoveredKey) return
hoveredKey = key
emit('hover', key ? (villageAt.value.get(key) ?? null) : null)
}
function unhover() {
hoveredKey = null
emit('hover', null)
}
function onPointerMove(e: PointerEvent) { function onPointerMove(e: PointerEvent) {
if (!drag) return if (!drag) return hover(e)
const dx = e.clientX - drag.lastX const dx = e.clientX - drag.lastX
const dy = e.clientY - drag.lastY const dy = e.clientY - drag.lastY
if (!drag.moved && Math.abs(dx) + Math.abs(dy) < 4) return if (!drag.moved && Math.abs(dx) + Math.abs(dy) < 4) return
@ -217,14 +201,8 @@ function onPointerUp(e: PointerEvent) {
emit('dragend') emit('dragend')
return return
} }
const rect = (e.currentTarget as Element).getBoundingClientRect() const at = fieldUnder(e)
const px = e.clientX - rect.left - RULER if (at) emit('select', at)
const py = e.clientY - rect.top - RULER
if (px < 0 || py < 0) return
const x = Math.floor(props.left + px / props.field)
const y = Math.floor(props.top + py / props.field)
const hi = lo.value + mapSize.value
if (x >= lo.value && y >= lo.value && x < hi && y < hi) emit('select', { x, y })
} }
function onKey(e: KeyboardEvent) { function onKey(e: KeyboardEvent) {
@ -243,12 +221,10 @@ function onKey(e: KeyboardEvent) {
</script> </script>
<template> <template>
<svg <div
:width="width" class="relative touch-none select-none overflow-hidden bg-[#585e3c] outline-none focus-visible:ring-2 focus-visible:ring-gold-500"
:height="height"
:viewBox="`0 0 ${width} ${height}`"
class="block touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-gold-500"
:class="dragging ? 'cursor-grabbing' : 'cursor-grab'" :class="dragging ? 'cursor-grabbing' : 'cursor-grab'"
:style="{ width: `${width}px`, height: `${height}px` }"
role="application" role="application"
tabindex="0" tabindex="0"
:aria-label="t('map.aria')" :aria-label="t('map.aria')"
@ -256,52 +232,18 @@ function onKey(e: KeyboardEvent) {
@pointermove="onPointerMove" @pointermove="onPointerMove"
@pointerup="onPointerUp" @pointerup="onPointerUp"
@pointercancel="drag = null" @pointercancel="drag = null"
@pointerleave="emit('hover', null)" @pointerleave="unhover"
@keydown="onKey" @keydown="onKey"
> >
<!-- The fields' layer: the region at its world position, moved as a whole while dragging. -->
<div class="absolute right-0 bottom-0 overflow-hidden" :style="{ left: `${RULER}px`, top: `${RULER}px` }">
<div class="absolute top-0 left-0 will-change-transform" :style="{ transform: layer.transform }">
<svg :width="layer.width" :height="layer.height" :viewBox="layer.viewBox" class="block">
<defs> <defs>
<clipPath id="map-fields"> <MapVillageDefs />
<rect :x="RULER" :y="RULER" :width="Math.max(0, width - RULER)" :height="Math.max(0, height - RULER)" />
</clipPath>
<clipPath id="map-ruler-x">
<rect :x="RULER" y="0" :width="Math.max(0, width - RULER)" :height="RULER" />
</clipPath>
<clipPath id="map-ruler-y">
<rect x="0" :y="RULER" :width="RULER" :height="Math.max(0, height - RULER)" />
</clipPath>
</defs> </defs>
<rect :width="width" :height="height" fill="#585e3c" /> <MapLand :region="region" :field="field" :villages="villageAt" />
<g clip-path="url(#map-fields)">
<!-- Fields sit at their world position; panning only moves this group. -->
<g :transform="`translate(${shiftX} ${shiftY})`">
<g v-for="f in fields" :key="`${f.x}|${f.y}`" @pointerenter="emit('hover', f.village)">
<rect :x="f.x * field" :y="f.y * field" :width="field" :height="field" :fill="f.ground === 'water' ? WATER : f.grass" />
<g v-if="f.ground !== 'grass'" :transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`">
<template v-if="f.shore">
<!-- Sandy shore where the lake meets land, and a ripple. -->
<rect v-if="f.shore.top" x="0" y="0" width="100" height="9" fill="#7a6a50" />
<rect v-if="f.shore.bottom" x="0" y="91" width="100" height="9" fill="#7a6a50" />
<rect v-if="f.shore.left" x="0" y="0" width="9" height="100" fill="#7a6a50" />
<rect v-if="f.shore.right" x="91" y="0" width="9" height="100" fill="#7a6a50" />
<path :d="`M${28 + hash(f.x, f.y) * 30} 50h18M${40 + hash(f.y, f.x) * 20} 64h12`" stroke="#6a7a80" stroke-width="3" stroke-linecap="round" />
</template>
<g v-else-if="f.ground === 'hill'">
<ellipse cx="52" cy="70" rx="40" ry="16" fill="#5d6242" />
<polygon points="18,72 40,30 58,72" fill="#8a857c" />
<polygon points="40,30 58,72 64,60" fill="#5f5c56" />
<polygon points="48,74 68,42 86,74" fill="#9a958c" />
<polygon points="68,42 86,74 90,66" fill="#6c6962" />
</g>
<g v-for="(tr, i) in f.trees" :key="i" :transform="`translate(${tr[0]} ${tr[1]}) scale(${tr[2]})`">
<ellipse cx="4" cy="26" rx="12" ry="4" fill="#4f5535" />
<rect x="-2" y="18" width="4" height="9" fill="#4a3b2e" />
<polygon points="0,-10 -13,20 0,16" fill="#2b3a27" />
<polygon points="0,-10 13,20 0,16" fill="#3e5034" />
</g>
</g>
</g>
<!-- Continent labels and lines lie on the ground, under the villages. --> <!-- Continent labels and lines lie on the ground, under the villages. -->
<text <text
@ -327,14 +269,7 @@ function onKey(e: KeyboardEvent) {
:stroke-width="l.kind === 'continent' ? 2.5 : 1" :stroke-width="l.kind === 'continent' ? 2.5 : 1"
/> />
<g <MapVillages :villages="villages" :field="field" />
v-for="f in villageFields"
:key="`v${f.x}|${f.y}`"
:transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`"
class="pointer-events-none"
>
<MapVillageMarker :tier="tier(f.village!.points)" :relation="relation(f.village!)" :crown="f.village!.crown" />
</g>
<!-- Groups on the march, over the villages; the line and the group open the group's card. --> <!-- Groups on the march, over the villages; the line and the group open the group's card. -->
<g v-for="g in marches" :key="`m${g.m.id}`"> <g v-for="g in marches" :key="`m${g.m.id}`">
@ -381,14 +316,14 @@ function onKey(e: KeyboardEvent) {
stroke="#d9b44a" stroke="#d9b44a"
stroke-width="2.5" stroke-width="2.5"
/> />
</g> </svg>
</g> </div>
</div>
<!-- Coordinate rulers, Die Stämme style: x along the top, y down the side. --> <!-- Coordinate rulers, Die Stämme style: x along the top, y down the side; their numbers move like the fields. -->
<rect x="0" y="0" :width="width" :height="RULER" fill="#dcc595" /> <div class="pointer-events-none absolute top-0 right-0 overflow-hidden bg-[#dcc595]" :style="{ left: `${RULER}px`, height: `${RULER}px` }">
<rect x="0" y="0" :width="RULER" :height="height" fill="#dcc595" /> <div class="absolute top-0 left-0 will-change-transform" :style="{ transform: `translate3d(${layer.x}px, 0, 0)` }">
<g clip-path="url(#map-ruler-x)"> <svg :width="layer.width" :height="RULER" :viewBox="`${region.x0 * field} 0 ${layer.width} ${RULER}`" class="block">
<g :transform="`translate(${shiftX} 0)`">
<text <text
v-for="x in columns" v-for="x in columns"
:key="`x${x}`" :key="`x${x}`"
@ -401,10 +336,12 @@ function onKey(e: KeyboardEvent) {
> >
{{ x }} {{ x }}
</text> </text>
</g> </svg>
</g> </div>
<g clip-path="url(#map-ruler-y)"> </div>
<g :transform="`translate(0 ${shiftY})`"> <div class="pointer-events-none absolute bottom-0 left-0 overflow-hidden bg-[#dcc595]" :style="{ top: `${RULER}px`, width: `${RULER}px` }">
<div class="absolute top-0 left-0 will-change-transform" :style="{ transform: `translate3d(0, ${layer.y}px, 0)` }">
<svg :width="RULER" :height="layer.height" :viewBox="`0 ${region.y0 * field} ${RULER} ${layer.height}`" class="block">
<text <text
v-for="y in rowsList" v-for="y in rowsList"
:key="`y${y}`" :key="`y${y}`"
@ -417,8 +354,9 @@ function onKey(e: KeyboardEvent) {
> >
{{ y }} {{ y }}
</text> </text>
</g>
</g>
<rect x="0" y="0" :width="RULER" :height="RULER" fill="#c4a676" />
</svg> </svg>
</div>
</div>
<div class="pointer-events-none absolute top-0 left-0 bg-[#c4a676]" :style="{ width: `${RULER}px`, height: `${RULER}px` }" />
</div>
</template> </template>

View File

@ -165,9 +165,9 @@ const popupAt = (x: number, y: number, below: boolean) => {
top: below ? `${p.top + field.value + 4}px` : `${p.top - 4}px`, top: below ? `${p.top + field.value + 4}px` : `${p.top - 4}px`,
} }
} }
const tip = computed(() => (hovered.value && hovered.value !== selectedVillage.value && visible.value ? hovered.value : null)) /** While the map is dragged, the click menu and the hover popup stay hidden. */
/** While the map is dragged, the click menu stays hidden. */
const dragging = ref(false) const dragging = ref(false)
const tip = computed(() => (hovered.value && hovered.value !== selectedVillage.value && visible.value && !dragging.value ? hovered.value : null))
const menu = computed(() => (selectedVillage.value && visible.value && !dragging.value ? selectedVillage.value : null)) const menu = computed(() => (selectedVillage.value && visible.value && !dragging.value ? selectedVillage.value : null))
function onDragMove(dx: number, dy: number) { function onDragMove(dx: number, dy: number) {
dragging.value = true dragging.value = true