game-village-politics-frontend/app/components/village/Diorama.vue

197 lines
7.3 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { VillageState } from '~/types/game'
import { buildScene, type SceneBuilding } from '~/diorama/scene'
const props = defineProps<{ village: VillageState }>()
/** Id of the building whose details are open. */
const selected = defineModel<number | null>('selected', { default: null })
const { t } = useI18n()
const scene = computed(() => buildScene(props.village))
const hovered = ref<number | null>(null)
const uid = useId()
const name = (b: SceneBuilding) => t(`buildings.${b.type}`)
const label = (b: SceneBuilding) => t('village.diorama.label', { name: name(b), level: b.level })
const badges = computed(() => scene.value.objects.flatMap((o) => (o.building?.focusable ? [o.building] : [])))
const isActive = (id: number) => selected.value === id || hovered.value === id
function toggle(id: number) {
selected.value = selected.value === id ? null : id
}
// Badges keep the same size on screen however the scene is scaled.
const svg = ref<SVGSVGElement | null>(null)
const unitsPerPx = ref(1)
function measure() {
const el = svg.value
if (!el) return
const [, , w, h] = scene.value.viewBox.split(' ').map(Number) as [number, number, number, number]
const scale = Math.min(el.clientWidth / w, el.clientHeight / h)
// Smaller badges on phones, where the whole village is only a few hundred pixels wide.
if (scale > 0) unitsPerPx.value = (el.clientWidth < 640 ? 0.75 : 1) / scale
}
let observer: ResizeObserver | undefined
onMounted(() => {
measure()
observer = new ResizeObserver(measure)
if (svg.value) observer.observe(svg.value)
})
onBeforeUnmount(() => observer?.disconnect())
watch(() => scene.value.viewBox, () => nextTick(measure))
const k = computed(() => unitsPerPx.value)
const pillWidth = (b: SceneBuilding) => (name(b).length * 6.6 + 18) * k.value
</script>
<template>
<div class="relative h-full w-full">
<!-- Backdrop across the full width: sky, haze and far hills. -->
<svg class="absolute inset-0 h-full w-full" viewBox="0 0 1600 600" preserveAspectRatio="xMidYMax slice" aria-hidden="true">
<defs>
<linearGradient :id="`${uid}-sky`" x1="0" y1="0" x2="0" y2="1">
<stop offset="0" stop-color="#cfe3ee" />
<stop offset="0.6" stop-color="#eef1e2" />
<stop offset="1" stop-color="#f3ead6" />
</linearGradient>
</defs>
<rect width="1600" height="600" :fill="`url(#${uid}-sky)`" />
<g fill="#fff" opacity="0.75">
<polygon points="180,120 230,98 300,104 338,124 300,138 210,138" />
<polygon points="1180,86 1236,66 1310,72 1352,92 1300,106 1206,104" />
<polygon points="760,64 800,50 850,56 874,70 838,80 778,78" />
</g>
<polygon points="0,430 140,392 260,410 400,368 540,402 700,380 860,410 1010,372 1160,398 1320,366 1460,396 1600,378 1600,600 0,600" fill="#c9d8b6" />
<polygon points="0,468 180,440 330,458 470,432 640,456 800,438 960,462 1120,436 1290,458 1450,440 1600,452 1600,600 0,600" fill="#b7c99d" />
<rect y="500" width="1600" height="100" fill="#efe6d1" opacity="0.65" />
</svg>
<svg
ref="svg"
class="relative h-full w-full select-none"
:viewBox="scene.viewBox"
preserveAspectRatio="xMidYMid meet"
role="group"
:aria-label="$t('village.diorama.title')"
@click="selected = null"
>
<defs>
<radialGradient :id="`${uid}-shadow`">
<stop offset="0" stop-color="#3b2f22" stop-opacity="0.32" />
<stop offset="1" stop-color="#3b2f22" stop-opacity="0" />
</radialGradient>
</defs>
<ellipse v-bind="scene.shadow" :fill="`url(#${uid}-shadow)`" />
<g stroke-width="0.6" stroke-linejoin="round">
<polygon v-for="(p, i) in scene.terrain" :key="`t${i}`" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
<polygon v-for="(p, i) in scene.decals" :key="`d${i}`" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
</g>
<template v-for="obj in scene.objects" :key="obj.key">
<g
v-if="obj.building"
v-memo="[obj, isActive(obj.building.id), selected === obj.building.id]"
class="bld"
:class="{ 'is-active': isActive(obj.building.id), 'is-selected': selected === obj.building.id }"
:role="obj.building.focusable ? 'button' : undefined"
:tabindex="obj.building.focusable ? 0 : -1"
:aria-hidden="obj.building.focusable ? undefined : 'true'"
:aria-label="obj.building.focusable ? label(obj.building) : undefined"
:aria-pressed="obj.building.focusable ? selected === obj.building.id : undefined"
stroke-width="0.6"
stroke-linejoin="round"
@click.stop="toggle(obj.building.id)"
@keydown.enter.prevent="toggle(obj.building.id)"
@keydown.space.prevent="toggle(obj.building.id)"
@mouseenter="hovered = obj.building.id"
@mouseleave="hovered = null"
@focus="hovered = obj.building.id"
@blur="hovered = null"
>
<ellipse v-if="obj.building.ring.rx" class="ring" v-bind="obj.building.ring" />
<polygon
v-for="(p, i) in obj.polys"
:key="i"
:points="p.points"
:fill="p.fill"
:stroke="p.stroke"
:fill-opacity="p.opacity"
/>
<title>{{ label(obj.building) }}</title>
</g>
<g v-else v-memo="[obj]" stroke-width="0.6" stroke-linejoin="round" :opacity="obj.planned ? 0.7 : 1">
<polygon
v-for="(p, i) in obj.polys"
:key="i"
:points="p.points"
:fill="p.fill"
:stroke="p.stroke"
:fill-opacity="p.opacity"
/>
<title v-if="obj.planned">{{ $t('village.diorama.planned') }}</title>
</g>
</template>
<!-- Level badges and, for the hovered or selected building, its name. -->
<g class="pointer-events-none" font-family="ui-sans-serif, system-ui, sans-serif" text-anchor="middle">
<g v-for="b in badges" :key="`badge-${b.id}`" :transform="`translate(${b.label[0]} ${b.label[1]})`">
<g v-if="isActive(b.id)">
<rect
:x="-pillWidth(b) / 2"
:y="-30 * k"
:width="pillWidth(b)"
:height="20 * k"
:rx="10 * k"
fill="#2b2118"
fill-opacity="0.86"
/>
<text :y="-16 * k" :font-size="12 * k" font-weight="600" fill="#fff">{{ name(b) }}</text>
</g>
<circle
:r="10 * k"
:fill="b.upgrading ? '#f59e0b' : '#fffaf0'"
:stroke="isActive(b.id) ? '#b45309' : '#a1835e'"
:stroke-width="1.5 * k"
/>
<text :y="4 * k" :font-size="11 * k" font-weight="700" :fill="b.upgrading ? '#fff' : '#4a3b2e'">{{ b.level }}</text>
</g>
</g>
</svg>
</div>
</template>
<style scoped>
.bld {
cursor: pointer;
outline: none;
transition: transform 0.15s ease;
}
.bld.is-active {
transform: translateY(-3px);
}
.ring {
fill: transparent;
stroke: transparent;
}
.bld.is-active .ring {
fill: rgb(251 191 36 / 0.22);
stroke: #d97706;
stroke-width: 1.2;
}
.bld.is-selected .ring {
fill: rgb(251 191 36 / 0.32);
stroke: #b45309;
stroke-width: 2;
}
.bld.is-selected polygon {
stroke: #b45309;
stroke-width: 1;
}
.bld:focus-visible .ring {
stroke: #b45309;
stroke-width: 2;
stroke-dasharray: 4 3;
}
</style>