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

120 lines
4.5 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { MapView } from '~/types/map'
import type { MarkerRelation } from '~/utils/relations'
import type { Ground } from '~/utils/terrain'
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
/** The village the player is in now: drawn white. */
current?: number | null
}>()
const emit = defineEmits<{ center: [at: { x: number; y: number }] }>()
const { t } = useI18n()
// One cell per field, like the Die Stämme minimap: the land from the same function as the big
// map, village dots in their relation colour, sector lines and the view frame.
const CELL = 4
const size = computed(() => ({ w: props.overview.w * CELL, h: props.overview.h * CELL }))
/** One path per kind of ground (grass is the background), a run of equal fields per subpath. */
const land = computed(() => {
const o = props.overview
const paths: Partial<Record<Ground, string>> = {}
for (let r = 0; r < o.h; r++) {
let run: Ground | null = null
let from = 0
const close = (c: number) => {
if (run && run !== 'grass') paths[run] = `${paths[run] ?? ''}M${from * CELL} ${r * CELL}h${(c - from) * CELL}v${CELL}h${-(c - from) * CELL}z`
}
for (let c = 0; c <= o.w; c++) {
const g = c < o.w ? groundAt(o.x + c, o.y + r) : null
if (g === run) continue
close(c)
run = g
from = c
}
}
return (Object.entries(paths) as [Ground, string][]).map(([ground, d]) => ({ ground, d, fill: GROUND_COLOURS[ground] }))
})
/** Villages: a dark cell each, so they stand off every ground, and a dot per relation colour. */
const villages = computed(() => {
const o = props.overview
let rims = ''
const dots: Partial<Record<MarkerRelation, string>> = {}
for (const v of o.villages) {
const x = (v.x - o.x) * CELL
const y = (v.y - o.y) * CELL
rims += `M${x} ${y}h${CELL}v${CELL}h${-CELL}z`
const r: MarkerRelation = v.id === props.current ? 'current' : v.relation
dots[r] = `${dots[r] ?? ''}M${x + 0.8} ${y + 0.8}h${CELL - 1.6}v${CELL - 1.6}h${-(CELL - 1.6)}z`
}
return { rims, dots: (Object.entries(dots) as [MarkerRelation, string][]).map(([r, d]) => ({ r, d, fill: RELATION_COLOURS[r] })) }
})
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
})
/** The main map's window as a frame over the minimap, moved by a transform so dragging the map paints nothing here. */
const frameBox = computed(() => {
const f = props.frame
const o = props.overview
if (!f || f.w <= 0 || f.h <= 0) return null
return {
width: `${(f.w / o.w) * 100}%`,
height: `${(f.h / o.h) * 100}%`,
transform: `translate3d(${((f.x - o.x) / f.w) * 100}%, ${((f.y - o.y) / f.h) * 100}%, 0)`,
}
})
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>
<div class="relative overflow-hidden">
<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="GROUND_COLOURS.grass" />
<path v-for="g in land" :key="g.ground" :d="g.d" :fill="g.fill" shape-rendering="crispEdges" />
<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"
/>
<path :d="villages.rims" fill="#1f150c" />
<path v-for="v in villages.dots" :key="v.r" :d="v.d" :fill="v.fill" />
</svg>
<div v-if="frameBox" class="pointer-events-none absolute top-0 left-0 border-[1.5px] border-[#f3e7c9] will-change-transform" :style="frameBox" />
</div>
</template>