feat: map fills its frame and pans smoothly by the pixel, loading ahead as it moves

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 21:08:56 +01:00
parent 36e47d48ce
commit d8d752c202
2 changed files with 284 additions and 194 deletions

View File

@ -2,7 +2,16 @@
import type { MapVillage, MapView } from '~/types/map' import type { MapVillage, MapView } from '~/types/map'
const props = defineProps<{ const props = defineProps<{
view: MapView /** Loaded villages; fields outside it show plain land until the next load. */
view: MapView | null
/** Visible area in fields: left/top edge (fractional) and how many fit. */
left: number
top: number
cols: number
rows: number
/** Map size in px; the map fills it exactly. */
width: number
height: number
/** Field size in px. */ /** Field size in px. */
field: number field: number
selected: { x: number; y: number } | null selected: { x: number; y: number } | null
@ -10,14 +19,18 @@ const props = defineProps<{
const emit = defineEmits<{ const emit = defineEmits<{
select: [at: { x: number; y: number }] select: [at: { x: number; y: number }]
hover: [village: MapVillage | null] hover: [village: MapVillage | null]
/** Move the centre by whole fields (drag or arrow keys). */ /** Pixel movement while dragging, for smooth panning. */
drag: [dx: number, dy: number]
dragend: []
/** Whole fields, from the arrow keys. */
pan: [dx: number, dy: number] pan: [dx: number, dy: number]
}>() }>()
const { t } = useI18n() const { t } = useI18n()
const RULER = 26 const RULER = 26
const mapSize = computed(() => props.view?.size ?? 1000)
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. // Same field, same look: terrain comes from a hash of the coordinates.
const hash = (x: number, y: number) => { const hash = (x: number, y: number) => {
@ -27,11 +40,16 @@ const hash = (x: number, y: number) => {
} }
const GRASS = ['#7f9645', '#78903f', '#86994b', '#73893c'] const GRASS = ['#7f9645', '#78903f', '#86994b', '#73893c']
// 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(0, Math.floor(props.left)))
const y0 = computed(() => Math.max(0, Math.floor(props.top)))
const x1 = computed(() => Math.min(mapSize.value, Math.ceil(props.left + props.cols)))
const y1 = computed(() => Math.min(mapSize.value, Math.ceil(props.top + props.rows)))
interface Field { interface Field {
x: number x: number
y: number y: number
px: number
py: number
grass: string grass: string
village: MapVillage | null village: MapVillage | null
forest: number[] | null forest: number[] | null
@ -39,17 +57,13 @@ interface Field {
const fields = computed<Field[]>(() => { const fields = computed<Field[]>(() => {
const out: Field[] = [] const out: Field[] = []
for (let row = 0; row < props.view.h; row++) { for (let y = y0.value; y < y1.value; y++) {
for (let col = 0; col < props.view.w; col++) { for (let x = x0.value; x < x1.value; x++) {
const x = props.view.x + col
const y = props.view.y + row
const r = hash(x, y) const r = hash(x, y)
const village = villageAt.value.get(`${x}|${y}`) ?? null const village = villageAt.value.get(`${x}|${y}`) ?? null
out.push({ out.push({
x, x,
y, y,
px: col * props.field,
py: row * props.field,
grass: GRASS[Math.floor(r * GRASS.length)]!, grass: GRASS[Math.floor(r * GRASS.length)]!,
village, village,
// About one empty field in five is forest: 2–3 trees at hashed spots. // About one empty field in five is forest: 2–3 trees at hashed spots.
@ -59,77 +73,82 @@ const fields = computed<Field[]>(() => {
} }
return out return out
}) })
const villageFields = computed(() => fields.value.filter((f) => f.village)) const villageFields = computed(() => fields.value.filter((f) => f.village))
const tier = (points: number): 1 | 2 | 3 => (points < 60 ? 1 : points < 200 ? 2 : 3) const tier = (points: number): 1 | 2 | 3 => (points < 60 ? 1 : points < 200 ? 2 : 3)
const relation = (v: MapVillage) => (v.own ? 'own' : v.player ? 'player' : 'barbarian') const relation = (v: MapVillage) => (v.own ? 'own' : v.player ? 'player' : 'barbarian')
const width = computed(() => RULER + props.view.w * props.field)
const height = computed(() => RULER + props.view.h * props.field)
// Sector lines every 10 fields, continent borders every 100. // Sector lines every 10 fields, continent borders every 100.
const lines = computed(() => { const lines = computed(() => {
const F = props.field
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 col = 0; col <= props.view.w; col++) { for (let x = x0.value; x <= x1.value; x++) {
const x = props.view.x + col 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: col * props.field, y1: 0, x2: col * props.field, y2: props.view.h * props.field, kind: x % 100 === 0 ? 'continent' : 'sector' })
} }
for (let y = y0.value; y <= y1.value; 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' })
} }
for (let row = 0; row <= props.view.h; row++) { return out
const y = props.view.y + row })
if (y % 10 === 0) {
out.push({ x1: 0, y1: row * props.field, x2: props.view.w * props.field, y2: row * props.field, kind: y % 100 === 0 ? 'continent' : 'sector' }) // One label per continent in view, at its top-left corner or the first fully visible field.
const continents = computed(() => {
const out: { label: string; x: number; y: number }[] = []
const left = Math.ceil(props.left)
const top = Math.ceil(props.top)
for (let cy = Math.floor(y0.value / 100); cy * 100 < y1.value; cy++) {
for (let cx = Math.floor(x0.value / 100); cx * 100 < x1.value; cx++) {
out.push({ label: `K${cy}${cx}`, x: Math.max(cx * 100, left), y: Math.max(cy * 100, top) })
} }
} }
return out return out
}) })
// One label per continent in view, in its top-left visible corner. const columns = computed(() => Array.from({ length: x1.value - x0.value }, (_, i) => x0.value + i))
const continents = computed(() => { const rowsList = computed(() => Array.from({ length: y1.value - y0.value }, (_, i) => y0.value + i))
const seen = new Map<string, { label: string; px: number; py: number }>()
for (const f of fields.value) {
const label = `K${Math.floor(f.y / 100)}${Math.floor(f.x / 100)}`
if (!seen.has(label)) seen.set(label, { label, px: f.px, py: f.py })
}
return [...seen.values()]
})
const rulerFont = computed(() => Math.max(9, Math.min(12, props.field / 4))) const rulerFont = computed(() => Math.max(9, Math.min(12, props.field / 4)))
// Dragging pans by whole fields when released; a click without movement selects. const shiftX = computed(() => RULER - props.left * props.field)
const drag = ref<{ startX: number; startY: number; dx: number; dy: number; moved: boolean } | null>(null) const shiftY = computed(() => RULER - props.top * props.field)
// Dragging pans pixel by pixel; a click without movement selects a field.
let drag: { lastX: number; lastY: number; moved: boolean } | null = null
const dragging = ref(false)
function onPointerDown(e: PointerEvent) { function onPointerDown(e: PointerEvent) {
if (e.button !== 0) return if (e.button !== 0) return
drag.value = { startX: e.clientX, startY: e.clientY, dx: 0, dy: 0, moved: false } drag = { lastX: e.clientX, lastY: e.clientY, moved: false }
;(e.currentTarget as Element).setPointerCapture(e.pointerId) ;(e.currentTarget as Element).setPointerCapture(e.pointerId)
} }
function onPointerMove(e: PointerEvent) { function onPointerMove(e: PointerEvent) {
if (!drag.value) return if (!drag) return
drag.value.dx = e.clientX - drag.value.startX const dx = e.clientX - drag.lastX
drag.value.dy = e.clientY - drag.value.startY const dy = e.clientY - drag.lastY
if (Math.abs(drag.value.dx) + Math.abs(drag.value.dy) > 5) drag.value.moved = true if (!drag.moved && Math.abs(dx) + Math.abs(dy) < 4) return
drag.moved = true
dragging.value = true
drag.lastX = e.clientX
drag.lastY = e.clientY
emit('drag', dx, dy)
} }
function onPointerUp(e: PointerEvent) { function onPointerUp(e: PointerEvent) {
const d = drag.value const d = drag
drag.value = null drag = null
dragging.value = false
if (!d) return if (!d) return
if (d.moved) { if (d.moved) {
const dx = -Math.round(d.dx / props.field) emit('dragend')
const dy = -Math.round(d.dy / props.field)
if (dx || dy) emit('pan', dx, dy)
return return
} }
const rect = (e.currentTarget as Element).getBoundingClientRect() const rect = (e.currentTarget as Element).getBoundingClientRect()
const col = Math.floor((e.clientX - rect.left - RULER) / props.field) const px = e.clientX - rect.left - RULER
const row = Math.floor((e.clientY - rect.top - RULER) / props.field) const py = e.clientY - rect.top - RULER
if (col >= 0 && row >= 0 && col < props.view.w && row < props.view.h) { if (px < 0 || py < 0) return
emit('select', { x: props.view.x + col, y: props.view.y + row }) const x = Math.floor(props.left + px / props.field)
} const y = Math.floor(props.top + py / props.field)
if (x >= 0 && y >= 0 && x < mapSize.value && y < mapSize.value) emit('select', { x, y })
} }
function onKey(e: KeyboardEvent) { function onKey(e: KeyboardEvent) {
@ -145,8 +164,6 @@ function onKey(e: KeyboardEvent) {
e.preventDefault() e.preventDefault()
emit('pan', move[0], move[1]) emit('pan', move[0], move[1])
} }
const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx} ${drag.value.dy})` : undefined))
</script> </script>
<template> <template>
@ -155,7 +172,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:height="height" :height="height"
:viewBox="`0 0 ${width} ${height}`" :viewBox="`0 0 ${width} ${height}`"
class="block touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-gold-500" class="block touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-gold-500"
:class="drag?.moved ? 'cursor-grabbing' : 'cursor-grab'" :class="dragging ? 'cursor-grabbing' : 'cursor-grab'"
role="application" role="application"
tabindex="0" tabindex="0"
:aria-label="t('map.aria')" :aria-label="t('map.aria')"
@ -168,16 +185,24 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
> >
<defs> <defs>
<clipPath id="map-fields"> <clipPath id="map-fields">
<rect :x="RULER" :y="RULER" :width="view.w * field" :height="view.h * field" /> <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> </clipPath>
</defs> </defs>
<rect :width="width" :height="height" fill="#4f6129" />
<g clip-path="url(#map-fields)"> <g clip-path="url(#map-fields)">
<g :transform="`translate(${RULER} ${RULER})`"> <!-- Fields sit at their world position; panning only moves this group. -->
<g :transform="panOffset"> <g :transform="`translate(${shiftX} ${shiftY})`">
<g v-for="f in fields" :key="`${f.x}|${f.y}`" @pointerenter="emit('hover', f.village)"> <g v-for="f in fields" :key="`${f.x}|${f.y}`" @pointerenter="emit('hover', f.village)">
<rect :x="f.px" :y="f.py" :width="field" :height="field" :fill="f.grass" /> <rect :x="f.x * field" :y="f.y * field" :width="field" :height="field" :fill="f.grass" />
<g v-if="f.forest" :transform="`translate(${f.px} ${f.py}) scale(${field / 100})`"> <g v-if="f.forest" :transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`">
<g v-for="(r, i) in f.forest" :key="i" :transform="`translate(${18 + r * 52 + i * 8} ${34 + ((r * 7) % 1) * 40 + i * 6})`"> <g v-for="(r, i) in f.forest" :key="i" :transform="`translate(${18 + r * 52 + i * 8} ${34 + ((r * 7) % 1) * 40 + i * 6})`">
<rect x="-2" y="18" width="4" height="9" fill="#57391f" /> <rect x="-2" y="18" width="4" height="9" fill="#57391f" />
<polygon points="0,-10 -13,20 0,16" fill="#2f5222" /> <polygon points="0,-10 -13,20 0,16" fill="#2f5222" />
@ -190,8 +215,8 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
<text <text
v-for="c in continents" v-for="c in continents"
:key="c.label" :key="c.label"
:x="c.px + 5" :x="c.x * field + 5"
:y="c.py + 19" :y="c.y * field + 19"
font-size="17" font-size="17"
font-weight="700" font-weight="700"
font-family="Georgia, serif" font-family="Georgia, serif"
@ -199,7 +224,6 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
> >
{{ c.label }} {{ c.label }}
</text> </text>
<line <line
v-for="(l, i) in lines" v-for="(l, i) in lines"
:key="i" :key="i"
@ -214,16 +238,16 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
<g <g
v-for="f in villageFields" v-for="f in villageFields"
:key="`v${f.x}|${f.y}`" :key="`v${f.x}|${f.y}`"
:transform="`translate(${f.px} ${f.py}) scale(${field / 100})`" :transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`"
class="pointer-events-none" class="pointer-events-none"
> >
<MapVillageMarker :tier="tier(f.village!.points)" :relation="relation(f.village!)" /> <MapVillageMarker :tier="tier(f.village!.points)" :relation="relation(f.village!)" />
</g> </g>
<rect <rect
v-if="selected && selected.x >= view.x && selected.y >= view.y && selected.x < view.x + view.w && selected.y < view.y + view.h" v-if="selected"
:x="(selected.x - view.x) * field + 1" :x="selected.x * field + 1"
:y="(selected.y - view.y) * field + 1" :y="selected.y * field + 1"
:width="field - 2" :width="field - 2"
:height="field - 2" :height="field - 2"
rx="4" rx="4"
@ -233,34 +257,42 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
/> />
</g> </g>
</g> </g>
</g>
<!-- 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. -->
<rect x="0" y="0" :width="width" :height="RULER" fill="#dcc595" /> <rect x="0" y="0" :width="width" :height="RULER" fill="#dcc595" />
<rect x="0" y="0" :width="RULER" :height="height" fill="#dcc595" /> <rect x="0" y="0" :width="RULER" :height="height" fill="#dcc595" />
<g clip-path="url(#map-ruler-x)">
<g :transform="`translate(${shiftX} 0)`">
<text <text
v-for="col in view.w" v-for="x in columns"
:key="`x${col}`" :key="`x${x}`"
:x="RULER + (col - 0.5) * field" :x="(x + 0.5) * field"
:y="RULER / 2 + 4" :y="RULER / 2 + 4"
text-anchor="middle" text-anchor="middle"
:font-size="rulerFont" :font-size="rulerFont"
fill="#2f2013" fill="#2f2013"
class="tabular-nums" class="tabular-nums"
> >
{{ view.x + col - 1 }} {{ x }}
</text> </text>
</g>
</g>
<g clip-path="url(#map-ruler-y)">
<g :transform="`translate(0 ${shiftY})`">
<text <text
v-for="row in view.h" v-for="y in rowsList"
:key="`y${row}`" :key="`y${y}`"
:x="RULER / 2" :x="RULER / 2"
:y="RULER + (row - 0.5) * field + 4" :y="(y + 0.5) * field + 4"
text-anchor="middle" text-anchor="middle"
:font-size="rulerFont" :font-size="rulerFont"
fill="#2f2013" fill="#2f2013"
class="tabular-nums" class="tabular-nums"
> >
{{ view.y + row - 1 }} {{ y }}
</text> </text>
</g>
</g>
<rect x="0" y="0" :width="RULER" :height="RULER" fill="#c4a676" />
</svg> </svg>
</template> </template>

View File

@ -3,38 +3,24 @@ import type { MapVillage } from '~/types/map'
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
const { view, error, busy, load } = useMap(Number(route.params.worldId)) const worldId = Number(route.params.worldId)
useCurrentWorld().enter(Number(route.params.worldId)) const { view, error, busy, load } = useMap(worldId)
useCurrentWorld().enter(worldId)
// The minimap: a 61 × 61 window around the same centre, refetched when the centre moves.
const overview = useMap(Number(route.params.worldId))
watch(
() => view.value?.center,
(c, old) => {
if (c && (c.x !== old?.x || c.y !== old?.y)) overview.load(c, 61, 61)
},
)
const ZOOM = [36, 52, 72] const ZOOM = [36, 52, 72]
const zoom = ref(1) const zoom = ref(1)
const field = computed(() => ZOOM[zoom.value]!) const field = computed(() => ZOOM[zoom.value]!)
const RULER = 26
// The window fills the space the page gives it: odd sizes keep the centre field in the middle.
const box = ref<HTMLElement | null>(null)
const size = ref({ w: 15, h: 11 })
const odd = (n: number) => (n % 2 ? n : n - 1)
const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n)) const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n))
// The map fills its box exactly: full width, and the height left on screen.
const box = ref<HTMLElement | null>(null)
const area = ref({ w: 800, h: 520 })
function measure() { function measure() {
if (!box.value) return if (!box.value) return
const rect = box.value.getBoundingClientRect() const rect = box.value.getBoundingClientRect()
const height = Math.max(360, window.innerHeight - rect.top - 24) area.value = { w: Math.floor(rect.width) - 8, h: Math.max(360, Math.floor(window.innerHeight - rect.top - 32)) }
size.value = {
w: clamp(odd(Math.floor((rect.width - 26) / field.value)), 5, 41),
h: clamp(odd(Math.floor((height - 26) / field.value)), 5, 41),
} }
}
let observer: ResizeObserver | undefined let observer: ResizeObserver | undefined
onMounted(() => { onMounted(() => {
measure() measure()
@ -42,39 +28,101 @@ onMounted(() => {
if (box.value) observer.observe(box.value) if (box.value) observer.observe(box.value)
}) })
onBeforeUnmount(() => observer?.disconnect()) onBeforeUnmount(() => observer?.disconnect())
watch(field, measure)
/** Centre from the URL (?x=&y=), or null to open on the player's village. */ /**
const center = computed(() => { * The field in the middle of the map, fractional while dragging. Null until known:
const x = Number(route.query.x) * without ?x=&y= the server opens on the player's village.
const y = Number(route.query.y) */
return Number.isFinite(x) && Number.isFinite(y) && route.query.x !== undefined ? { x, y } : null const center = ref<{ x: number; y: number } | null>(null)
const mapSize = computed(() => view.value?.size ?? 1000)
const rounded = computed(() => (center.value ? { x: Math.round(center.value.x), y: Math.round(center.value.y) } : null))
/** The visible area in fields. */
const visible = computed(() => {
if (!center.value) return null
const cols = (area.value.w - RULER) / field.value
const rows = (area.value.h - RULER) / field.value
return { left: center.value.x + 0.5 - cols / 2, top: center.value.y + 0.5 - rows / 2, cols, rows }
}) })
watch([center, size], () => load(center.value, size.value.w, size.value.h), { immediate: true, deep: true }) // Load a window with some margin around what is visible, whenever the visible fields
// leave what is loaded (or already on its way).
const MARGIN = 6
let requested: { x0: number; y0: number; x1: number; y1: number } | null = null
function ensureLoaded() {
const v = visible.value
if (!v || !rounded.value) return
const need = { x0: Math.floor(v.left) - 1, y0: Math.floor(v.top) - 1, x1: Math.ceil(v.left + v.cols) + 1, y1: Math.ceil(v.top + v.rows) + 1 }
const covers = (r: typeof requested) => !!r && need.x0 >= r.x0 && need.y0 >= r.y0 && need.x1 <= r.x1 && need.y1 <= r.y1
const have = view.value
if (have && covers({ x0: have.x, y0: have.y, x1: have.x + have.w, y1: have.y + have.h })) return
if (covers(requested)) return
const w = Math.min(61, need.x1 - need.x0 + MARGIN * 2)
const h = Math.min(61, need.y1 - need.y0 + MARGIN * 2)
const { x, y } = rounded.value
requested = { x0: x - Math.floor(w / 2), y0: y - Math.floor(h / 2), x1: x - Math.floor(w / 2) + w, y1: y - Math.floor(h / 2) + h }
load({ x, y }, w, h)
}
watch(visible, ensureLoaded)
// ?x=&y= is where the map is; it changes on jumps, the minimap and after a drag.
const queryCenter = computed(() => {
const x = Number(route.query.x)
const y = Number(route.query.y)
return route.query.x !== undefined && Number.isFinite(x) && Number.isFinite(y) ? { x, y } : null
})
watch(
queryCenter,
(q) => {
if (!q) {
// Home: let the server centre on the player's village.
center.value = null
requested = null
load(null, 41, 31)
return
}
if (!rounded.value || rounded.value.x !== q.x || rounded.value.y !== q.y) center.value = { ...q }
},
{ immediate: true },
)
watch(view, (v) => {
if (v && !center.value) center.value = { ...v.center }
})
function goTo(at: { x: number; y: number }) { function goTo(at: { x: number; y: number }) {
const max = (view.value?.size ?? 1000) - 1 const max = mapSize.value - 1
router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } }) router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } })
} }
function pan(dx: number, dy: number) { function onDrag(dx: number, dy: number) {
const from = view.value?.center if (!center.value) return
if (from) goTo({ x: from.x + dx, y: from.y + dy }) const max = mapSize.value - 1
center.value = {
x: clamp(center.value.x - dx / field.value, 0, max),
y: clamp(center.value.y - dy / field.value, 0, max),
} }
}
function pan(dx: number, dy: number) {
if (rounded.value) goTo({ x: rounded.value.x + dx, y: rounded.value.y + dy })
}
// The minimap: a 61 × 61 window, refetched once the map has moved a few fields from it.
const overview = useMap(worldId)
watch(rounded, (c) => {
const o = overview.view.value?.center
if (c && (!o || Math.abs(o.x - c.x) > 8 || Math.abs(o.y - c.y) > 8)) overview.load(c, 61, 61)
})
// Jump-to form, prefilled with the centre. // Jump-to form, prefilled with the centre.
const jumpX = ref<number | null>(null) const jumpX = ref<number | null>(null)
const jumpY = ref<number | null>(null) const jumpY = ref<number | null>(null)
watch( watch(rounded, (c) => {
() => view.value?.center,
(c) => {
if (c) { if (c) {
jumpX.value = c.x jumpX.value = c.x
jumpY.value = c.y jumpY.value = c.y
} }
}, })
)
function jump() { function jump() {
if (jumpX.value !== null && jumpY.value !== null) goTo({ x: jumpX.value, y: jumpY.value }) if (jumpX.value !== null && jumpY.value !== null) goTo({ x: jumpX.value, y: jumpY.value })
} }
@ -86,8 +134,12 @@ const selectedVillage = computed(() =>
) )
const shown = computed(() => hovered.value ?? selectedVillage.value) const shown = computed(() => hovered.value ?? selectedVillage.value)
/** Villages in the visible area, for the legend. */
const counts = computed(() => { const counts = computed(() => {
const villages = view.value?.villages ?? [] const v = visible.value
const villages = (view.value?.villages ?? []).filter(
(village) => v && village.x >= v.left - 1 && village.x < v.left + v.cols && village.y >= v.top - 1 && village.y < v.top + v.rows,
)
return { return {
own: villages.filter((v) => v.own).length, own: villages.filter((v) => v.own).length,
players: villages.filter((v) => v.player && !v.own).length, players: villages.filter((v) => v.player && !v.own).length,
@ -95,7 +147,7 @@ const counts = computed(() => {
} }
}) })
const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.y / 100)}${Math.floor(view.value.center.x / 100)}` : '')) const continent = computed(() => (rounded.value ? `K${Math.floor(rounded.value.y / 100)}${Math.floor(rounded.value.x / 100)}` : ''))
</script> </script>
<template> <template>
@ -105,7 +157,7 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
<p class="text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t('map.title') }}</p> <p class="text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t('map.title') }}</p>
<h1 class="font-display text-2xl font-semibold text-wood-900 sm:text-3xl" style="font-variant: small-caps"> <h1 class="font-display text-2xl font-semibold text-wood-900 sm:text-3xl" style="font-variant: small-caps">
{{ $t('map.continent', { k: continent }) }} {{ $t('map.continent', { k: continent }) }}
<span v-if="view" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ view.center.x }}|{{ view.center.y }}</span> <span v-if="rounded" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ rounded.x }}|{{ rounded.y }}</span>
</h1> </h1>
</div> </div>
@ -123,19 +175,25 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
<div class="grid gap-5 xl:grid-cols-[1fr_20rem]"> <div class="grid gap-5 xl:grid-cols-[1fr_20rem]">
<div ref="box" class="relative min-w-0"> <div ref="box" class="relative min-w-0">
<!-- A dark wooden frame around the map, like a board on the table. --> <!-- A dark wooden frame around the map, like a board on the table. -->
<div class="overflow-hidden rounded-md border-4 border-wood-800 shadow-hard"> <div class="overflow-hidden rounded-md border-4 border-wood-800 bg-[#4f6129] shadow-hard">
<div class="flex justify-center overflow-hidden bg-[#5f7634]">
<MapWorldMap <MapWorldMap
v-if="view" v-if="visible"
:view="view" :view="view"
:left="visible.left"
:top="visible.top"
:cols="visible.cols"
:rows="visible.rows"
:width="area.w"
:height="area.h"
:field="field" :field="field"
:selected="selected" :selected="selected"
@select="selected = $event" @select="selected = $event"
@hover="hovered = $event" @hover="hovered = $event"
@drag="onDrag"
@dragend="rounded && goTo(rounded)"
@pan="pan" @pan="pan"
/> />
<p v-else class="py-24 text-sm text-ink-500">{{ $t('common.loading') }}</p> <p v-else class="py-24 text-center text-sm text-parchment-100">{{ $t('common.loading') }}</p>
</div>
</div> </div>
<div class="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-ink-500"> <div class="mt-2 flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-ink-500">
<span>{{ $t('map.controls') }}</span> <span>{{ $t('map.controls') }}</span>
@ -155,7 +213,7 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
<MapMiniMap <MapMiniMap
v-if="overview.view.value" v-if="overview.view.value"
:overview="overview.view.value" :overview="overview.view.value"
:frame="view ? { x: view.x, y: view.y, w: view.w, h: view.h } : null" :frame="visible ? { x: visible.left, y: visible.top, w: visible.cols, h: visible.rows } : null"
@center="goTo" @center="goTo"
/> />
<p v-else class="py-16 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p> <p v-else class="py-16 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>