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

308 lines
13 KiB
Vue
Raw Normal View History

<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))
// On the fullscreen stage the map fills the screen under the menu bar, with the village
// bar and the dock floating over it; without one it fills its box on the page.
const stage = useStage()
const staged = !!stage
/** The frame's border around the map, in px; none on the stage. */
const edge = staged ? 0 : 2
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 = staged
? { w: Math.floor(rect.width), h: Math.floor(rect.height) }
: { 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)
/** Lowest and highest coordinate: the world starts at 0|0, negative included. */
const lo = computed(() => view.value?.min ?? mapMin(mapSize.value))
const hi = computed(() => lo.value + mapSize.value - 1)
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 }) {
router.replace({ query: { x: clamp(Math.round(at.x), lo.value, hi.value), y: clamp(Math.round(at.y), lo.value, hi.value) } })
}
function onDrag(dx: number, dy: number) {
if (!center.value) return
center.value = {
x: clamp(center.value.x - dx / field.value, lo.value, hi.value),
y: clamp(center.value.y - dy / field.value, lo.value, hi.value),
}
}
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. */
function fieldAt(x: number, y: number) {
const v = visible.value!
return { left: edge + RULER + (x - v.left + 0.5) * field.value, top: edge + 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 ? continentOf(rounded.value.x, rounded.value.y, mapSize.value) : ''))
</script>
<template>
<div>
<div
ref="box"
:class="staged ? 'fixed inset-x-0 bottom-0 z-0' : 'relative min-w-0 p-2.5'"
:style="staged ? { top: `${stage!.menuBottom.value}px` } : undefined"
>
<!-- A dark wooden frame around the map on a page; edge to edge on the stage. -->
<div class="h-full overflow-hidden bg-[#4f6129]" :class="staged ? '' : 'border-2 border-wood-900'">
<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"
/>
<!-- Phones have no dock: home and zoom float over the map instead. -->
<div v-if="staged" class="float absolute bottom-16 left-2 z-20 flex border border-wood-900 bg-parchment-200 lg:hidden">
<button type="button" class="btn btn-ghost" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button>
<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>
<!-- The map's panels go into the stage's dock on the right. -->
<Teleport defer to="#stage-dock" :disabled="!staged">
<div class="space-y-2" :class="staged ? '' : 'p-2.5 pt-0'">
<section class="panel">
<div class="panel-head">
<h1 class="panel-title">{{ $t('map.continent', { k: continent }) }}</h1>
<span v-if="rounded" class="text-sm text-ink-500 tabular-nums">{{ rounded.x }}|{{ rounded.y }}</span>
</div>
<div class="flex items-center gap-1.5 p-2">
<button type="button" class="btn btn-outline" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button>
<div class="ml-auto flex items-center 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>
<p v-if="error" class="alert m-2 mt-0" role="alert">{{ error }}</p>
</section>
<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 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 p-2" @submit.prevent="jump">
<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="lo" :max="hi" />
<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="lo" :max="hi" />
<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-1.5 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>
<li class="text-xs text-ink-500">{{ $t('map.controls') }}<template v-if="busy"> · {{ $t('common.loading') }}</template></li>
</ul>
</section>
</div>
</Teleport>
</div>
</template>