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:
parent
36e47d48ce
commit
d8d752c202
|
|
@ -2,7 +2,16 @@
|
|||
import type { MapVillage, MapView } from '~/types/map'
|
||||
|
||||
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: number
|
||||
selected: { x: number; y: number } | null
|
||||
|
|
@ -10,14 +19,18 @@ const props = defineProps<{
|
|||
const emit = defineEmits<{
|
||||
select: [at: { x: number; y: number }]
|
||||
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]
|
||||
}>()
|
||||
const { t } = useI18n()
|
||||
|
||||
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.
|
||||
const hash = (x: number, y: number) => {
|
||||
|
|
@ -27,11 +40,16 @@ const hash = (x: number, y: number) => {
|
|||
}
|
||||
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 {
|
||||
x: number
|
||||
y: number
|
||||
px: number
|
||||
py: number
|
||||
grass: string
|
||||
village: MapVillage | null
|
||||
forest: number[] | null
|
||||
|
|
@ -39,17 +57,13 @@ interface Field {
|
|||
|
||||
const fields = computed<Field[]>(() => {
|
||||
const out: Field[] = []
|
||||
for (let row = 0; row < props.view.h; row++) {
|
||||
for (let col = 0; col < props.view.w; col++) {
|
||||
const x = props.view.x + col
|
||||
const y = props.view.y + row
|
||||
for (let y = y0.value; y < y1.value; y++) {
|
||||
for (let x = x0.value; x < x1.value; x++) {
|
||||
const r = hash(x, y)
|
||||
const village = villageAt.value.get(`${x}|${y}`) ?? null
|
||||
out.push({
|
||||
x,
|
||||
y,
|
||||
px: col * props.field,
|
||||
py: row * props.field,
|
||||
grass: GRASS[Math.floor(r * GRASS.length)]!,
|
||||
village,
|
||||
// About one empty field in five is forest: 2–3 trees at hashed spots.
|
||||
|
|
@ -59,77 +73,82 @@ const fields = computed<Field[]>(() => {
|
|||
}
|
||||
return out
|
||||
})
|
||||
|
||||
const villageFields = computed(() => fields.value.filter((f) => f.village))
|
||||
|
||||
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 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.
|
||||
const lines = computed(() => {
|
||||
const F = props.field
|
||||
const out: { x1: number; y1: number; x2: number; y2: number; kind: 'sector' | 'continent' }[] = []
|
||||
for (let col = 0; col <= props.view.w; col++) {
|
||||
const x = props.view.x + col
|
||||
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 x = x0.value; x <= x1.value; 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' })
|
||||
}
|
||||
for (let row = 0; row <= props.view.h; row++) {
|
||||
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' })
|
||||
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' })
|
||||
}
|
||||
return out
|
||||
})
|
||||
|
||||
// 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
|
||||
})
|
||||
|
||||
// One label per continent in view, in its top-left visible corner.
|
||||
const continents = computed(() => {
|
||||
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 columns = computed(() => Array.from({ length: x1.value - x0.value }, (_, i) => x0.value + i))
|
||||
const rowsList = computed(() => Array.from({ length: y1.value - y0.value }, (_, i) => y0.value + i))
|
||||
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 drag = ref<{ startX: number; startY: number; dx: number; dy: number; moved: boolean } | null>(null)
|
||||
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.
|
||||
let drag: { lastX: number; lastY: number; moved: boolean } | null = null
|
||||
const dragging = ref(false)
|
||||
|
||||
function onPointerDown(e: PointerEvent) {
|
||||
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)
|
||||
}
|
||||
|
||||
function onPointerMove(e: PointerEvent) {
|
||||
if (!drag.value) return
|
||||
drag.value.dx = e.clientX - drag.value.startX
|
||||
drag.value.dy = e.clientY - drag.value.startY
|
||||
if (Math.abs(drag.value.dx) + Math.abs(drag.value.dy) > 5) drag.value.moved = true
|
||||
if (!drag) return
|
||||
const dx = e.clientX - drag.lastX
|
||||
const dy = e.clientY - drag.lastY
|
||||
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) {
|
||||
const d = drag.value
|
||||
drag.value = null
|
||||
const d = drag
|
||||
drag = null
|
||||
dragging.value = false
|
||||
if (!d) return
|
||||
if (d.moved) {
|
||||
const dx = -Math.round(d.dx / props.field)
|
||||
const dy = -Math.round(d.dy / props.field)
|
||||
if (dx || dy) emit('pan', dx, dy)
|
||||
emit('dragend')
|
||||
return
|
||||
}
|
||||
const rect = (e.currentTarget as Element).getBoundingClientRect()
|
||||
const col = Math.floor((e.clientX - rect.left - RULER) / props.field)
|
||||
const row = Math.floor((e.clientY - rect.top - RULER) / props.field)
|
||||
if (col >= 0 && row >= 0 && col < props.view.w && row < props.view.h) {
|
||||
emit('select', { x: props.view.x + col, y: props.view.y + row })
|
||||
}
|
||||
const px = e.clientX - rect.left - RULER
|
||||
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)
|
||||
if (x >= 0 && y >= 0 && x < mapSize.value && y < mapSize.value) emit('select', { x, y })
|
||||
}
|
||||
|
||||
function onKey(e: KeyboardEvent) {
|
||||
|
|
@ -145,8 +164,6 @@ function onKey(e: KeyboardEvent) {
|
|||
e.preventDefault()
|
||||
emit('pan', move[0], move[1])
|
||||
}
|
||||
|
||||
const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx} ${drag.value.dy})` : undefined))
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -155,7 +172,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
|
|||
: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="drag?.moved ? 'cursor-grabbing' : 'cursor-grab'"
|
||||
:class="dragging ? 'cursor-grabbing' : 'cursor-grab'"
|
||||
role="application"
|
||||
tabindex="0"
|
||||
:aria-label="t('map.aria')"
|
||||
|
|
@ -168,99 +185,114 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
|
|||
>
|
||||
<defs>
|
||||
<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>
|
||||
</defs>
|
||||
|
||||
<rect :width="width" :height="height" fill="#4f6129" />
|
||||
|
||||
<g clip-path="url(#map-fields)">
|
||||
<g :transform="`translate(${RULER} ${RULER})`">
|
||||
<g :transform="panOffset">
|
||||
<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" />
|
||||
<g v-if="f.forest" :transform="`translate(${f.px} ${f.py}) 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})`">
|
||||
<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="#46702f" />
|
||||
</g>
|
||||
<!-- 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.grass" />
|
||||
<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})`">
|
||||
<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="#46702f" />
|
||||
</g>
|
||||
</g>
|
||||
|
||||
<!-- Continent labels and lines lie on the ground, under the villages. -->
|
||||
<text
|
||||
v-for="c in continents"
|
||||
:key="c.label"
|
||||
:x="c.px + 5"
|
||||
:y="c.py + 19"
|
||||
font-size="17"
|
||||
font-weight="700"
|
||||
font-family="Georgia, serif"
|
||||
fill="rgb(243 231 201 / 0.6)"
|
||||
>
|
||||
{{ c.label }}
|
||||
</text>
|
||||
|
||||
<line
|
||||
v-for="(l, i) in lines"
|
||||
:key="i"
|
||||
:x1="l.x1"
|
||||
:y1="l.y1"
|
||||
:x2="l.x2"
|
||||
:y2="l.y2"
|
||||
:stroke="l.kind === 'continent' ? 'rgb(31 21 12 / 0.85)' : 'rgb(31 21 12 / 0.3)'"
|
||||
:stroke-width="l.kind === 'continent' ? 2.5 : 1"
|
||||
/>
|
||||
|
||||
<g
|
||||
v-for="f in villageFields"
|
||||
:key="`v${f.x}|${f.y}`"
|
||||
:transform="`translate(${f.px} ${f.py}) scale(${field / 100})`"
|
||||
class="pointer-events-none"
|
||||
>
|
||||
<MapVillageMarker :tier="tier(f.village!.points)" :relation="relation(f.village!)" />
|
||||
</g>
|
||||
|
||||
<rect
|
||||
v-if="selected && selected.x >= view.x && selected.y >= view.y && selected.x < view.x + view.w && selected.y < view.y + view.h"
|
||||
:x="(selected.x - view.x) * field + 1"
|
||||
:y="(selected.y - view.y) * field + 1"
|
||||
:width="field - 2"
|
||||
:height="field - 2"
|
||||
rx="4"
|
||||
fill="rgb(217 180 74 / 0.18)"
|
||||
stroke="#d9b44a"
|
||||
stroke-width="2.5"
|
||||
/>
|
||||
</g>
|
||||
|
||||
<!-- Continent labels and lines lie on the ground, under the villages. -->
|
||||
<text
|
||||
v-for="c in continents"
|
||||
:key="c.label"
|
||||
:x="c.x * field + 5"
|
||||
:y="c.y * field + 19"
|
||||
font-size="17"
|
||||
font-weight="700"
|
||||
font-family="Georgia, serif"
|
||||
fill="rgb(243 231 201 / 0.6)"
|
||||
>
|
||||
{{ c.label }}
|
||||
</text>
|
||||
<line
|
||||
v-for="(l, i) in lines"
|
||||
:key="i"
|
||||
:x1="l.x1"
|
||||
:y1="l.y1"
|
||||
:x2="l.x2"
|
||||
:y2="l.y2"
|
||||
:stroke="l.kind === 'continent' ? 'rgb(31 21 12 / 0.85)' : 'rgb(31 21 12 / 0.3)'"
|
||||
:stroke-width="l.kind === 'continent' ? 2.5 : 1"
|
||||
/>
|
||||
|
||||
<g
|
||||
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!)" />
|
||||
</g>
|
||||
|
||||
<rect
|
||||
v-if="selected"
|
||||
:x="selected.x * field + 1"
|
||||
:y="selected.y * field + 1"
|
||||
:width="field - 2"
|
||||
:height="field - 2"
|
||||
rx="4"
|
||||
fill="rgb(217 180 74 / 0.18)"
|
||||
stroke="#d9b44a"
|
||||
stroke-width="2.5"
|
||||
/>
|
||||
</g>
|
||||
</g>
|
||||
|
||||
<!-- 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="RULER" :height="height" fill="#dcc595" />
|
||||
<text
|
||||
v-for="col in view.w"
|
||||
:key="`x${col}`"
|
||||
:x="RULER + (col - 0.5) * field"
|
||||
:y="RULER / 2 + 4"
|
||||
text-anchor="middle"
|
||||
:font-size="rulerFont"
|
||||
fill="#2f2013"
|
||||
class="tabular-nums"
|
||||
>
|
||||
{{ view.x + col - 1 }}
|
||||
</text>
|
||||
<text
|
||||
v-for="row in view.h"
|
||||
:key="`y${row}`"
|
||||
:x="RULER / 2"
|
||||
:y="RULER + (row - 0.5) * field + 4"
|
||||
text-anchor="middle"
|
||||
:font-size="rulerFont"
|
||||
fill="#2f2013"
|
||||
class="tabular-nums"
|
||||
>
|
||||
{{ view.y + row - 1 }}
|
||||
</text>
|
||||
<g clip-path="url(#map-ruler-x)">
|
||||
<g :transform="`translate(${shiftX} 0)`">
|
||||
<text
|
||||
v-for="x in columns"
|
||||
:key="`x${x}`"
|
||||
:x="(x + 0.5) * field"
|
||||
:y="RULER / 2 + 4"
|
||||
text-anchor="middle"
|
||||
:font-size="rulerFont"
|
||||
fill="#2f2013"
|
||||
class="tabular-nums"
|
||||
>
|
||||
{{ x }}
|
||||
</text>
|
||||
</g>
|
||||
</g>
|
||||
<g clip-path="url(#map-ruler-y)">
|
||||
<g :transform="`translate(0 ${shiftY})`">
|
||||
<text
|
||||
v-for="y in rowsList"
|
||||
:key="`y${y}`"
|
||||
:x="RULER / 2"
|
||||
:y="(y + 0.5) * field + 4"
|
||||
text-anchor="middle"
|
||||
:font-size="rulerFont"
|
||||
fill="#2f2013"
|
||||
class="tabular-nums"
|
||||
>
|
||||
{{ y }}
|
||||
</text>
|
||||
</g>
|
||||
</g>
|
||||
<rect x="0" y="0" :width="RULER" :height="RULER" fill="#c4a676" />
|
||||
</svg>
|
||||
</template>
|
||||
|
|
|
|||
|
|
@ -3,38 +3,24 @@ import type { MapVillage } from '~/types/map'
|
|||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const { view, error, busy, load } = useMap(Number(route.params.worldId))
|
||||
useCurrentWorld().enter(Number(route.params.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 worldId = Number(route.params.worldId)
|
||||
const { view, error, busy, load } = useMap(worldId)
|
||||
useCurrentWorld().enter(worldId)
|
||||
|
||||
const ZOOM = [36, 52, 72]
|
||||
const zoom = ref(1)
|
||||
const field = computed(() => ZOOM[zoom.value]!)
|
||||
|
||||
// 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 RULER = 26
|
||||
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() {
|
||||
if (!box.value) return
|
||||
const rect = box.value.getBoundingClientRect()
|
||||
const height = Math.max(360, window.innerHeight - rect.top - 24)
|
||||
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),
|
||||
}
|
||||
area.value = { w: Math.floor(rect.width) - 8, h: Math.max(360, Math.floor(window.innerHeight - rect.top - 32)) }
|
||||
}
|
||||
|
||||
let observer: ResizeObserver | undefined
|
||||
onMounted(() => {
|
||||
measure()
|
||||
|
|
@ -42,39 +28,101 @@ onMounted(() => {
|
|||
if (box.value) observer.observe(box.value)
|
||||
})
|
||||
onBeforeUnmount(() => observer?.disconnect())
|
||||
watch(field, measure)
|
||||
|
||||
/** Centre from the URL (?x=&y=), or null to open on the player's village. */
|
||||
const center = computed(() => {
|
||||
const x = Number(route.query.x)
|
||||
const y = Number(route.query.y)
|
||||
return Number.isFinite(x) && Number.isFinite(y) && route.query.x !== undefined ? { x, y } : null
|
||||
/**
|
||||
* The field in the middle of the map, fractional while dragging. Null until known:
|
||||
* without ?x=&y= the server opens on the player's village.
|
||||
*/
|
||||
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 }) {
|
||||
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) } })
|
||||
}
|
||||
|
||||
function pan(dx: number, dy: number) {
|
||||
const from = view.value?.center
|
||||
if (from) goTo({ x: from.x + dx, y: from.y + dy })
|
||||
function onDrag(dx: number, dy: number) {
|
||||
if (!center.value) return
|
||||
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.
|
||||
const jumpX = ref<number | null>(null)
|
||||
const jumpY = ref<number | null>(null)
|
||||
watch(
|
||||
() => view.value?.center,
|
||||
(c) => {
|
||||
if (c) {
|
||||
jumpX.value = c.x
|
||||
jumpY.value = c.y
|
||||
}
|
||||
},
|
||||
)
|
||||
watch(rounded, (c) => {
|
||||
if (c) {
|
||||
jumpX.value = c.x
|
||||
jumpY.value = c.y
|
||||
}
|
||||
})
|
||||
function jump() {
|
||||
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)
|
||||
|
||||
/** Villages in the visible area, for the legend. */
|
||||
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 {
|
||||
own: villages.filter((v) => 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>
|
||||
|
||||
<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>
|
||||
<h1 class="font-display text-2xl font-semibold text-wood-900 sm:text-3xl" style="font-variant: small-caps">
|
||||
{{ $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>
|
||||
</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 ref="box" class="relative min-w-0">
|
||||
<!-- 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="flex justify-center overflow-hidden bg-[#5f7634]">
|
||||
<MapWorldMap
|
||||
v-if="view"
|
||||
:view="view"
|
||||
:field="field"
|
||||
:selected="selected"
|
||||
@select="selected = $event"
|
||||
@hover="hovered = $event"
|
||||
@pan="pan"
|
||||
/>
|
||||
<p v-else class="py-24 text-sm text-ink-500">{{ $t('common.loading') }}</p>
|
||||
</div>
|
||||
<div class="overflow-hidden rounded-md border-4 border-wood-800 bg-[#4f6129] shadow-hard">
|
||||
<MapWorldMap
|
||||
v-if="visible"
|
||||
:view="view"
|
||||
:left="visible.left"
|
||||
:top="visible.top"
|
||||
:cols="visible.cols"
|
||||
:rows="visible.rows"
|
||||
:width="area.w"
|
||||
:height="area.h"
|
||||
:field="field"
|
||||
:selected="selected"
|
||||
@select="selected = $event"
|
||||
@hover="hovered = $event"
|
||||
@drag="onDrag"
|
||||
@dragend="rounded && goTo(rounded)"
|
||||
@pan="pan"
|
||||
/>
|
||||
<p v-else class="py-24 text-center text-sm text-parchment-100">{{ $t('common.loading') }}</p>
|
||||
</div>
|
||||
<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>
|
||||
|
|
@ -155,7 +213,7 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
|
|||
<MapMiniMap
|
||||
v-if="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"
|
||||
/>
|
||||
<p v-else class="py-16 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
|
||||
|
|
|
|||
Loading…
Reference in New Issue