game-village-politics-frontend/app/components/map/WorldMap.vue

267 lines
8.7 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<script setup lang="ts">
import type { MapVillage, MapView } from '~/types/map'
const props = defineProps<{
view: MapView
/** Field size in px. */
field: number
selected: { x: number; y: number } | null
}>()
const emit = defineEmits<{
select: [at: { x: number; y: number }]
hover: [village: MapVillage | null]
/** Move the centre by whole fields (drag or arrow keys). */
pan: [dx: number, dy: number]
}>()
const { t } = useI18n()
const RULER = 26
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) => {
let h = (x * 73856093) ^ (y * 19349663)
h = Math.imul(h ^ (h >>> 13), 1274126177)
return ((h ^ (h >>> 16)) >>> 0) / 4294967296
}
const GRASS = ['#7f9645', '#78903f', '#86994b', '#73893c']
interface Field {
x: number
y: number
px: number
py: number
grass: string
village: MapVillage | null
forest: number[] | null
}
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
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.
forest: !village && r < 0.2 ? [hash(x + 1, y), hash(x, y + 1), hash(x + 2, y + 3)] : null,
})
}
}
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 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 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' })
}
}
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 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)
function onPointerDown(e: PointerEvent) {
if (e.button !== 0) return
drag.value = { startX: e.clientX, startY: e.clientY, dx: 0, dy: 0, 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
}
function onPointerUp(e: PointerEvent) {
const d = drag.value
drag.value = null
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)
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 })
}
}
function onKey(e: KeyboardEvent) {
const step = e.shiftKey ? 5 : 1
const moves: Record<string, [number, number]> = {
ArrowLeft: [-step, 0],
ArrowRight: [step, 0],
ArrowUp: [0, -step],
ArrowDown: [0, step],
}
const move = moves[e.key]
if (!move) return
e.preventDefault()
emit('pan', move[0], move[1])
}
const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx} ${drag.value.dy})` : undefined))
</script>
<template>
<svg
:width="width"
: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'"
role="application"
tabindex="0"
:aria-label="t('map.aria')"
@pointerdown="onPointerDown"
@pointermove="onPointerMove"
@pointerup="onPointerUp"
@pointercancel="drag = null"
@pointerleave="emit('hover', null)"
@keydown="onKey"
>
<defs>
<clipPath id="map-fields">
<rect :x="RULER" :y="RULER" :width="view.w * field" :height="view.h * field" />
</clipPath>
</defs>
<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>
</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>
</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>
</svg>
</template>