game-village-politics-frontend/app/pages/map/[worldId].vue

298 lines
12 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 } from '~/types/map'
definePageMeta({ layout: 'game' })
const route = useRoute()
const router = useRouter()
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]!)
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()
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()
observer = new ResizeObserver(measure)
if (box.value) observer.observe(box.value)
})
onBeforeUnmount(() => observer?.disconnect())
/**
* 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 }
})
// 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 = mapSize.value - 1
router.replace({ query: { x: clamp(Math.round(at.x), 0, max), y: clamp(Math.round(at.y), 0, max) } })
}
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(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 })
}
const selected = ref<{ x: number; y: number } | null>(null)
const hovered = ref<MapVillage | null>(null)
const selectedVillage = computed(() =>
selected.value ? (view.value?.villages.find((v) => v.x === selected.value!.x && v.y === selected.value!.y) ?? null) : null,
)
const shown = computed(() => hovered.value ?? selectedVillage.value)
// Up close on the map, like a village's popup: the facts while hovering, actions on a click.
const bar = useVillageContextOrNull()
const from = computed(() => {
const v = bar?.village.value
return v && v.world_id === worldId ? { id: v.id, x: v.x, y: v.y } : null
})
/** Where a field sits inside the map frame, in px (the frame's border is 4 px). */
function fieldAt(x: number, y: number) {
const v = visible.value!
return { left: 4 + RULER + (x - v.left + 0.5) * field.value, top: 4 + RULER + (y - v.top) * field.value }
}
const popupAt = (x: number, y: number, below: boolean) => {
const p = fieldAt(x, y)
const half = 120
return {
left: `${clamp(p.left, half + 4, area.value.w - half)}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 stays hidden. */
const dragging = ref(false)
const menu = computed(() => (selectedVillage.value && visible.value && !dragging.value ? selectedVillage.value : null))
function onDragMove(dx: number, dy: number) {
dragging.value = true
onDrag(dx, dy)
}
function onDragEnd() {
dragging.value = false
if (rounded.value) goTo(rounded.value)
}
/** Villages in the visible area, for the legend. */
const counts = computed(() => {
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,
barbarians: villages.filter((v) => !v.player).length,
}
})
const continent = computed(() => (rounded.value ? `K${Math.floor(rounded.value.y / 100)}${Math.floor(rounded.value.x / 100)}` : ''))
</script>
<template>
<div class="px-4 py-5 lg:px-6">
<header class="mb-4 flex flex-wrap items-end justify-between gap-3">
<div>
<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="rounded" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ rounded.x }}|{{ rounded.y }}</span>
</h1>
</div>
<div class="flex flex-wrap items-center gap-2">
<button type="button" class="btn btn-outline" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button>
<div class="flex items-center rounded-sm border border-wood-600 bg-parchment-50">
<button type="button" class="btn btn-ghost px-2.5" :disabled="zoom === 0" :aria-label="$t('map.zoom_out')" @click="zoom--">−</button>
<button type="button" class="btn btn-ghost px-2.5" :disabled="zoom === ZOOM.length - 1" :aria-label="$t('map.zoom_in')" @click="zoom++">+</button>
</div>
</div>
</header>
<p v-if="error" class="alert mb-4" role="alert">{{ error }}</p>
<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 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="onDragMove"
@dragend="onDragEnd"
@pan="pan"
/>
<p v-else class="py-24 text-center text-sm text-parchment-100">{{ $t('common.loading') }}</p>
</div>
<MapFieldPopup
v-if="tip"
:village="tip"
:from="from"
class="pointer-events-none absolute z-20 -translate-x-1/2 -translate-y-full"
:style="popupAt(tip.x, tip.y, false)"
/>
<MapFieldPopup
v-if="menu"
:village="menu"
:from="from"
actions
class="absolute z-20 -translate-x-1/2"
:style="popupAt(menu.x, menu.y, true)"
@center="goTo(menu)"
@close="selected = null"
/>
<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 v-if="busy">{{ $t('common.loading') }}</span>
</div>
</div>
<aside class="space-y-5">
<section class="panel overflow-hidden">
<div class="panel-head">
<h2 class="panel-title">{{ $t('map.minimap') }}</h2>
<span v-if="overview.view.value" class="text-xs text-ink-500 tabular-nums">
{{ overview.view.value.w }}×{{ overview.view.value.h }}
</span>
</div>
<div class="border-b-2 border-wood-700">
<MapMiniMap
v-if="overview.view.value"
:overview="overview.view.value"
: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>
</div>
<form class="flex items-center gap-1.5 p-3" @submit.prevent="jump">
<span class="mr-1 text-sm font-semibold text-ink-700">{{ $t('map.center_map') }}</span>
<label class="sr-only" for="jump-x">x</label>
<input id="jump-x" v-model.number="jumpX" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
<span class="font-semibold text-ink-500">|</span>
<label class="sr-only" for="jump-y">y</label>
<input id="jump-y" v-model.number="jumpY" class="input w-16 px-1.5 py-1 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
<button type="submit" class="btn btn-primary ml-auto py-1">{{ $t('map.jump') }}</button>
</form>
</section>
<MapInfoCard :village="shown" :at="selected" @center="goTo" />
<section class="panel">
<div class="panel-head">
<h2 class="panel-title">{{ $t('map.legend.title') }}</h2>
</div>
<ul class="panel-body space-y-2 text-sm">
<li class="flex items-center gap-2"><span class="size-3 border border-wood-900 bg-[#d9b44a]" /> {{ $t('map.legend.own') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.own }}</span></li>
<li class="flex items-center gap-2"><span class="size-3 border border-wood-900 bg-[#962a1f]" /> {{ $t('map.legend.players') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.players }}</span></li>
<li class="flex items-center gap-2"><span class="size-3 border border-wood-900 bg-[#8b9094]" /> {{ $t('map.legend.barbarians') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.barbarians }}</span></li>
<li class="pt-1 text-xs text-ink-500">{{ $t('map.legend.lines') }}</li>
</ul>
</section>
</aside>
</div>
</div>
</template>