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

81 lines
2.5 KiB
Vue
Raw Normal View History

<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="#4b5033" />
<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>