189 lines
8.0 KiB
Vue
189 lines
8.0 KiB
Vue
<script setup lang="ts">
|
||
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 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 clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n))
|
||
|
||
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),
|
||
}
|
||
}
|
||
|
||
let observer: ResizeObserver | undefined
|
||
onMounted(() => {
|
||
measure()
|
||
observer = new ResizeObserver(measure)
|
||
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
|
||
})
|
||
|
||
watch([center, size], () => load(center.value, size.value.w, size.value.h), { immediate: true, deep: true })
|
||
|
||
function goTo(at: { x: number; y: number }) {
|
||
const max = (view.value?.size ?? 1000) - 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 })
|
||
}
|
||
|
||
// 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
|
||
}
|
||
},
|
||
)
|
||
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)
|
||
|
||
const counts = computed(() => {
|
||
const villages = view.value?.villages ?? []
|
||
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(() => (view.value ? `K${Math.floor(view.value.center.y / 100)}${Math.floor(view.value.center.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="view" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ view.center.x }}|{{ view.center.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 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>
|
||
<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="view ? { x: view.x, y: view.y, w: view.w, h: view.h } : 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>
|