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

171 lines
6.6 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()
// The scene is cut to the stage's shape, so the land always fills it edge to edge.
// On narrow screens the stage keeps a minimum width and scrolls sideways instead.
const scroller = ref<HTMLElement | null>(null)
const stage = ref<HTMLElement | null>(null)
const size = ref({ w: 1200, h: 520 })
const aspect = computed(() => Math.round((size.value.w / Math.max(1, size.value.h)) * 10) / 10)
const scene = computed(() => buildScene(props.village, aspect.value))
const hovered = ref<number | null>(null)
function measure() {
const el = stage.value
if (el && el.clientWidth && el.clientHeight) size.value = { w: el.clientWidth, h: el.clientHeight }
}
let observer: ResizeObserver | undefined
onMounted(() => {
measure()
observer = new ResizeObserver(measure)
if (stage.value) observer.observe(stage.value)
// Start a scrolled phone view on the village centre.
const sc = scroller.value
if (sc) sc.scrollLeft = (sc.scrollWidth - sc.clientWidth) / 2
})
onBeforeUnmount(() => observer?.disconnect())
const name = (b: SceneBuilding) => t(`buildings.${b.type}`)
const label = (b: SceneBuilding) => t('village.diorama.label', { name: name(b), level: b.level })
const isActive = (id: number) => selected.value === id || hovered.value === id
function toggle(id: number) {
selected.value = selected.value === id ? null : id
}
/** viewBox units per screen pixel: badges keep the same size however the scene is scaled. */
const k = computed(() => (scene.value.width / Math.max(1, size.value.w)) * (size.value.w < 900 ? 0.85 : 1))
const pillWidth = (b: SceneBuilding) => (name(b).length * 6.6 + 18) * k.value
// Badges that would overlap move up above the one in front, nearest buildings first.
const badges = computed(() => {
const list = scene.value.objects.flatMap((o) => (o.building?.focusable ? [o.building] : []))
const r = 11 * k.value
const placed: { b: SceneBuilding; x: number; y: number }[] = []
for (const b of [...list].sort((p, q) => q.label[1] - p.label[1])) {
const [x] = b.label
let y = b.label[1]
for (let n = 0; n < 6 && placed.some((p) => Math.abs(p.x - x) < r * 2.1 && Math.abs(p.y - y) < r * 2.1); n++) y -= r * 2.2
placed.push({ b, x, y })
}
return placed
})
</script>
<template>
<div ref="scroller" class="h-full w-full overflow-x-auto overflow-y-hidden bg-[#74864a]">
<div ref="stage" class="h-full min-w-[760px]">
<svg
class="block h-full w-full select-none"
:viewBox="scene.viewBox"
preserveAspectRatio="xMidYMid slice"
role="group"
:aria-label="$t('village.diorama.title')"
@click="selected = null"
>
<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" />
<title>{{ label(obj.building) }}</title>
</g>
<g v-else v-memo="[obj]" stroke-width="0.6" stroke-linejoin="round">
<polygon v-for="(p, i) in obj.polys" :key="i" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
<title v-if="obj.planned">{{ $t('village.diorama.planned') }}</title>
</g>
</template>
<!-- Level badges and, for the hovered or selected building, its name. Flat colours only. -->
<g class="pointer-events-none" font-family="ui-sans-serif, system-ui, sans-serif" text-anchor="middle">
<g v-for="{ b, x, y } in badges" :key="`badge-${b.id}`" :transform="`translate(${x} ${y})`">
<g v-if="isActive(b.id)">
<rect
:x="-pillWidth(b) / 2"
:y="-33 * k"
:width="pillWidth(b)"
:height="20 * k"
:rx="3 * k"
fill="#2f2013"
stroke="#9c7a1f"
:stroke-width="1 * k"
/>
<text :y="-19 * k" :font-size="12 * k" font-weight="600" fill="#f3e7c9">{{ name(b) }}</text>
</g>
<circle
:r="10 * k"
:fill="b.upgrading ? '#c49a2c' : '#f3e7c9'"
:stroke="isActive(b.id) ? '#7b1e16' : '#57391f'"
:stroke-width="1.6 * k"
/>
<text :y="4 * k" :font-size="11 * k" font-weight="700" :fill="b.upgrading ? '#1f150c' : '#2f2013'">{{ b.level }}</text>
</g>
</g>
</svg>
</div>
</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: #c4a676;
stroke: #9c7a1f;
stroke-width: 1.2;
}
.bld.is-selected .ring {
fill: #d9b44a;
stroke: #7b1e16;
stroke-width: 2;
}
.bld.is-selected polygon {
stroke: #7b1e16;
stroke-width: 1;
}
.bld:focus-visible .ring {
stroke: #7b1e16;
stroke-width: 2;
stroke-dasharray: 4 3;
}
</style>