feat: TW-style map frame, minimap with view frame and centre-map form

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-28 20:22:53 +01:00
parent 5138bdde27
commit 585f88fc81
5 changed files with 145 additions and 35 deletions

View File

@ -0,0 +1,80 @@
<script setup lang="ts">
import type { MapView } from '~/types/map'
const props = defineProps<{
/** A wide window around the main map's centre. */
overview: MapView
/** The main map's window, drawn as a frame. */
frame: { x: number; y: number; w: number; h: number } | null
}>()
const emit = defineEmits<{ center: [at: { x: number; y: number }] }>()
const { t } = useI18n()
// One cell per field, like the Die Stämme minimap: village dots, lines, and the view frame.
const CELL = 4
const size = computed(() => ({ w: props.overview.w * CELL, h: props.overview.h * CELL }))
const color = (v: MapView['villages'][number]) => (v.own ? '#d9b44a' : v.player ? '#962a1f' : '#8b9094')
const lines = computed(() => {
const out: { x1: number; y1: number; x2: number; y2: number; continent: boolean }[] = []
for (let c = 0; c <= props.overview.w; c++) {
const x = props.overview.x + c
if (x % 10 === 0) out.push({ x1: c * CELL, y1: 0, x2: c * CELL, y2: size.value.h, continent: x % 100 === 0 })
}
for (let r = 0; r <= props.overview.h; r++) {
const y = props.overview.y + r
if (y % 10 === 0) out.push({ x1: 0, y1: r * CELL, x2: size.value.w, y2: r * CELL, continent: y % 100 === 0 })
}
return out
})
function onClick(e: MouseEvent) {
const rect = (e.currentTarget as Element).getBoundingClientRect()
const scale = size.value.w / rect.width
emit('center', {
x: props.overview.x + Math.floor(((e.clientX - rect.left) * scale) / CELL),
y: props.overview.y + Math.floor(((e.clientY - rect.top) * scale) / CELL),
})
}
</script>
<template>
<svg
:viewBox="`0 0 ${size.w} ${size.h}`"
class="block w-full cursor-crosshair"
role="img"
:aria-label="t('map.minimap')"
@click="onClick"
>
<rect :width="size.w" :height="size.h" fill="#5f7634" />
<line
v-for="(l, i) in lines"
:key="i"
:x1="l.x1"
:y1="l.y1"
:x2="l.x2"
:y2="l.y2"
:stroke="l.continent ? '#1f150c' : 'rgb(31 21 12 / 0.35)'"
:stroke-width="l.continent ? 1.5 : 0.6"
/>
<rect
v-for="v in overview.villages"
:key="v.id"
:x="(v.x - overview.x) * CELL + 0.5"
:y="(v.y - overview.y) * CELL + 0.5"
:width="CELL - 1"
:height="CELL - 1"
:fill="color(v)"
/>
<rect
v-if="frame"
:x="(frame.x - overview.x) * CELL"
:y="(frame.y - overview.y) * CELL"
:width="frame.w * CELL"
:height="frame.h * CELL"
fill="none"
stroke="#f3e7c9"
stroke-width="1.5"
/>
</svg>
</template>

View File

@ -8,9 +8,9 @@ const props = defineProps<{ tier: 1 | 2 | 3; relation: MapRelation }>()
const tones = computed(() => const tones = computed(() =>
props.relation === 'barbarian' props.relation === 'barbarian'
? { wallLight: '#d6cfc2', wallDark: '#a99f90', roofLight: '#8e857a', roofDark: '#6c6459' } ? { wallLight: '#d6cfc2', wallDark: '#a99f90', roofLight: '#8e857a', roofDark: '#6c6459' }
: { wallLight: '#efdfbd', wallDark: '#c8ad84', roofLight: '#c2643a', roofDark: '#963f1d' }, : { wallLight: '#e3cfa2', wallDark: '#b49870', roofLight: '#9c4a26', roofDark: '#73321a' },
) )
const flag = computed(() => ({ own: '#f2b705', player: '#c0392b', barbarian: '#7d7d7d' })[props.relation]) const flag = computed(() => ({ own: '#d9b44a', player: '#962a1f', barbarian: '#8b9094' })[props.relation])
interface Hut { interface Hut {
cx: number cx: number
@ -63,7 +63,7 @@ const pole = computed(() => {
<template> <template>
<g> <g>
<ellipse cx="50" cy="74" rx="40" ry="17" fill="#8a6a43" opacity="0.28" /> <ellipse cx="50" cy="74" rx="40" ry="17" fill="#57391f" opacity="0.35" />
<g v-for="(hut, i) in huts" :key="i"> <g v-for="(hut, i) in huts" :key="i">
<polygon :points="faces(hut).left" :fill="tones.wallDark" /> <polygon :points="faces(hut).left" :fill="tones.wallDark" />
<polygon :points="faces(hut).right" :fill="tones.wallLight" /> <polygon :points="faces(hut).right" :fill="tones.wallLight" />

View File

@ -25,7 +25,7 @@ const hash = (x: number, y: number) => {
h = Math.imul(h ^ (h >>> 13), 1274126177) h = Math.imul(h ^ (h >>> 13), 1274126177)
return ((h ^ (h >>> 16)) >>> 0) / 4294967296 return ((h ^ (h >>> 16)) >>> 0) / 4294967296
} }
const GRASS = ['#b9d08f', '#b1ca87', '#c0d596', '#acc580'] const GRASS = ['#7f9645', '#78903f', '#86994b', '#73893c']
interface Field { interface Field {
x: number x: number
@ -154,7 +154,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:width="width" :width="width"
:height="height" :height="height"
:viewBox="`0 0 ${width} ${height}`" :viewBox="`0 0 ${width} ${height}`"
class="block touch-none select-none outline-none focus-visible:ring-2 focus-visible:ring-amber-600" 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="drag?.moved ? 'cursor-grabbing' : 'cursor-grab'"
role="application" role="application"
tabindex="0" tabindex="0"
@ -179,9 +179,9 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
<rect :x="f.px" :y="f.py" :width="field" :height="field" :fill="f.grass" /> <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-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})`"> <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="#6b4b2e" /> <rect x="-2" y="18" width="4" height="9" fill="#57391f" />
<polygon points="0,-10 -13,20 0,16" fill="#3f7a37" /> <polygon points="0,-10 -13,20 0,16" fill="#2f5222" />
<polygon points="0,-10 13,20 0,16" fill="#5d9a47" /> <polygon points="0,-10 13,20 0,16" fill="#46702f" />
</g> </g>
</g> </g>
</g> </g>
@ -195,7 +195,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
font-size="17" font-size="17"
font-weight="700" font-weight="700"
font-family="Georgia, serif" font-family="Georgia, serif"
fill="rgb(43 33 24 / 0.28)" fill="rgb(243 231 201 / 0.6)"
> >
{{ c.label }} {{ c.label }}
</text> </text>
@ -207,7 +207,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:y1="l.y1" :y1="l.y1"
:x2="l.x2" :x2="l.x2"
:y2="l.y2" :y2="l.y2"
:stroke="l.kind === 'continent' ? 'rgb(43 33 24 / 0.75)' : 'rgb(43 33 24 / 0.22)'" :stroke="l.kind === 'continent' ? 'rgb(31 21 12 / 0.85)' : 'rgb(31 21 12 / 0.3)'"
:stroke-width="l.kind === 'continent' ? 2.5 : 1" :stroke-width="l.kind === 'continent' ? 2.5 : 1"
/> />
@ -227,8 +227,8 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:width="field - 2" :width="field - 2"
:height="field - 2" :height="field - 2"
rx="4" rx="4"
fill="rgb(255 255 255 / 0.18)" fill="rgb(217 180 74 / 0.18)"
stroke="#b45309" stroke="#d9b44a"
stroke-width="2.5" stroke-width="2.5"
/> />
</g> </g>
@ -236,8 +236,8 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
</g> </g>
<!-- Coordinate rulers, Die Stämme style: x along the top, y down the side. --> <!-- Coordinate rulers, Die Stämme style: x along the top, y down the side. -->
<rect x="0" y="0" :width="width" :height="RULER" fill="#eee1c9" /> <rect x="0" y="0" :width="width" :height="RULER" fill="#dcc595" />
<rect x="0" y="0" :width="RULER" :height="height" fill="#eee1c9" /> <rect x="0" y="0" :width="RULER" :height="height" fill="#dcc595" />
<text <text
v-for="col in view.w" v-for="col in view.w"
:key="`x${col}`" :key="`x${col}`"
@ -245,7 +245,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:y="RULER / 2 + 4" :y="RULER / 2 + 4"
text-anchor="middle" text-anchor="middle"
:font-size="rulerFont" :font-size="rulerFont"
fill="#4a3b2e" fill="#2f2013"
class="tabular-nums" class="tabular-nums"
> >
{{ view.x + col - 1 }} {{ view.x + col - 1 }}
@ -257,7 +257,7 @@ const panOffset = computed(() => (drag.value?.moved ? `translate(${drag.value.dx
:y="RULER + (row - 0.5) * field + 4" :y="RULER + (row - 0.5) * field + 4"
text-anchor="middle" text-anchor="middle"
:font-size="rulerFont" :font-size="rulerFont"
fill="#4a3b2e" fill="#2f2013"
class="tabular-nums" class="tabular-nums"
> >
{{ view.y + row - 1 }} {{ view.y + row - 1 }}

View File

@ -6,6 +6,15 @@ const router = useRouter()
const { view, error, busy, load } = useMap(Number(route.params.worldId)) const { view, error, busy, load } = useMap(Number(route.params.worldId))
useState<number | null>('world.current', () => null).value = Number(route.params.worldId) useState<number | null>('world.current', () => null).value = 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 = [36, 52, 72]
const zoom = ref(1) const zoom = ref(1)
const field = computed(() => ZOOM[zoom.value]!) const field = computed(() => ZOOM[zoom.value]!)
@ -93,24 +102,16 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
<div class="px-4 py-5 lg:px-6"> <div class="px-4 py-5 lg:px-6">
<header class="mb-4 flex flex-wrap items-end justify-between gap-3"> <header class="mb-4 flex flex-wrap items-end justify-between gap-3">
<div> <div>
<p class="text-xs font-medium tracking-wide text-ink-500 uppercase">{{ $t('map.subtitle') }}</p> <p class="text-xs font-semibold tracking-wide text-ink-500 uppercase">{{ $t('map.title') }}</p>
<h1 class="font-display text-2xl font-semibold sm:text-3xl"> <h1 class="font-display text-2xl font-semibold text-wood-900 sm:text-3xl" style="font-variant: small-caps">
{{ $t('map.title') }} {{ $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 }} · {{ continent }}</span> <span v-if="view" class="ml-2 text-lg font-normal text-ink-500 tabular-nums">{{ view.center.x }}|{{ view.center.y }}</span>
</h1> </h1>
</div> </div>
<div class="flex flex-wrap items-center gap-2"> <div class="flex flex-wrap items-center gap-2">
<form class="flex items-center gap-1.5" @submit.prevent="jump">
<label class="sr-only" for="jump-x">x</label>
<input id="jump-x" v-model.number="jumpX" class="input w-20 py-1.5 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-20 py-1.5 text-center tabular-nums" type="number" min="0" :max="(view?.size ?? 1000) - 1" />
<button type="submit" class="btn btn-primary">{{ $t('map.jump') }}</button>
</form>
<button type="button" class="btn btn-outline" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button> <button type="button" class="btn btn-outline" @click="router.replace({ query: {} })">{{ $t('map.home') }}</button>
<div class="flex items-center rounded-lg border border-parchment-300 bg-white"> <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 === 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> <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>
@ -121,8 +122,9 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
<div class="grid gap-5 xl:grid-cols-[1fr_20rem]"> <div class="grid gap-5 xl:grid-cols-[1fr_20rem]">
<div ref="box" class="relative min-w-0"> <div ref="box" class="relative min-w-0">
<div class="panel overflow-hidden p-0"> <!-- A dark wooden frame around the map, like a board on the table. -->
<div class="flex justify-center overflow-hidden bg-[#a9c27d]"> <div class="overflow-hidden rounded-md border-4 border-wood-800 shadow-hard">
<div class="flex justify-center overflow-hidden bg-[#5f7634]">
<MapWorldMap <MapWorldMap
v-if="view" v-if="view"
:view="view" :view="view"
@ -142,15 +144,41 @@ const continent = computed(() => (view.value ? `K${Math.floor(view.value.center.
</div> </div>
<aside class="space-y-5"> <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" /> <MapInfoCard :village="shown" :at="selected" @center="goTo" />
<section class="panel"> <section class="panel">
<div class="panel-head"> <div class="panel-head">
<h2 class="panel-title">{{ $t('map.legend.title') }}</h2> <h2 class="panel-title">{{ $t('map.legend.title') }}</h2>
</div> </div>
<ul class="panel-body space-y-2 text-sm"> <ul class="panel-body space-y-2 text-sm">
<li class="flex items-center gap-2"><span class="size-3 rounded-sm bg-[#f2b705] ring-1 ring-ink-900/30" /> {{ $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-[#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 rounded-sm bg-[#c0392b] ring-1 ring-ink-900/30" /> {{ $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-[#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 rounded-sm bg-[#7d7d7d] ring-1 ring-ink-900/30" /> {{ $t('map.legend.barbarians') }} <span class="ml-auto tabular-nums text-ink-500">{{ counts.barbarians }}</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="pt-1 text-xs text-ink-500">{{ $t('map.legend.lines') }}</li>
</ul> </ul>
</section> </section>

View File

@ -4,7 +4,9 @@ export default {
en: { en: {
map: { map: {
title: 'World map', title: 'World map',
subtitle: 'Map', continent: 'Continent {k}',
minimap: 'Overview',
center_map: 'Centre map',
aria: 'World map. Drag or use the arrow keys to move, click a field to select it.', aria: 'World map. Drag or use the arrow keys to move, click a field to select it.',
jump: 'Go', jump: 'Go',
home: 'My village', home: 'My village',