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

215 lines
8.7 KiB
Vue

<script setup lang="ts">
import type { BuildingType, VillageState } from '~/types/game'
import { buildScene, type SceneBuilding, type ScenePlot } from '~/diorama/scene'
const props = defineProps<{
village: VillageState
/** Whole seconds until a server timestamp, on the server's clock. */
secondsUntil: (iso: string) => number
}>()
/** Id of the building whose details are open. */
const selected = defineModel<number | null>('selected', { default: null })
/** Type whose empty plot was picked, to build it. */
const plot = defineModel<BuildingType | null>('plot', { 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))
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 plotLabel = (p: ScenePlot) => t(p.another ? 'village.diorama.plot_another' : 'village.diorama.plot', { name: t(`buildings.${p.type}`) })
// Buildings stand still: hovering changes nothing but the cursor and the tooltip.
// A click opens the details and marks the ground under it.
function toggle(id: number) {
plot.value = null
selected.value = selected.value === id ? null : id
}
function pick(type: BuildingType) {
selected.value = null
plot.value = plot.value === type ? null : type
}
function clear() {
selected.value = null
plot.value = null
}
/** 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))
// 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
})
// Countdowns sit right above what is being built: over the level badge, or over the
// scaffold of a new building.
// A timer that would cover another timer or a level badge moves up until it is clear.
const timers = computed(() => {
const K = k.value
const half = { w: 36 * K, h: 11 * K }
const placed: { x: number; y: number }[] = []
const hits = (x: number, y: number) =>
placed.some((p) => Math.abs(p.x - x) < half.w * 2 && Math.abs(p.y - y) < half.h * 2) ||
badges.value.some((p) => Math.abs(p.x - x) < half.w + 11 * K && Math.abs(p.y - y) < half.h + 11 * K)
return scene.value.timers
.map((tm) => {
const badge = tm.buildingId !== null ? badges.value.find((p) => p.b.id === tm.buildingId) : undefined
const lift = badge ? 25 : 2
return { ...tm, x: badge?.x ?? tm.at[0], y: (badge?.y ?? tm.at[1]) - lift * K }
})
.sort((a, b) => b.y - a.y)
.map((tm) => {
let y = tm.y
for (let n = 0; n < 8 && hits(tm.x, y); n++) y -= half.h * 2.1
placed.push({ x: tm.x, y })
return { ...tm, y }
})
})
</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-[640px]">
<svg
class="block h-full w-full select-none"
:viewBox="scene.viewBox"
preserveAspectRatio="xMidYMid slice"
role="group"
:aria-label="$t('village.diorama.title')"
@click="clear"
>
<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>
<!-- Empty plots: nothing to see, but each opens its building type's build card. -->
<ellipse
v-for="p in scene.plots"
:key="p.key"
class="plot"
:class="{ 'is-selected': plot === p.type }"
v-bind="p.ring"
role="button"
tabindex="0"
:aria-label="plotLabel(p)"
:aria-pressed="plot === p.type"
@click.stop="pick(p.type)"
@keydown.enter.prevent="pick(p.type)"
@keydown.space.prevent="pick(p.type)"
>
<title>{{ plotLabel(p) }}</title>
</ellipse>
<template v-for="obj in scene.objects" :key="obj.key">
<g
v-if="obj.building"
v-memo="[obj, selected === obj.building.id]"
class="bld"
:class="{ '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)"
>
<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 countdowns. 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})`">
<circle
:r="10 * k"
:fill="b.upgrading ? '#c49a2c' : '#f3e7c9'"
:stroke="selected === 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 v-for="tm in timers" :key="`timer-${tm.orderId}`" :transform="`translate(${tm.x} ${tm.y})`">
<rect :x="-34 * k" :y="-10 * k" :width="68 * k" :height="19 * k" :rx="2 * k" fill="#2f2013" stroke="#c49a2c" :stroke-width="1.2 * k" />
<g :transform="`translate(${-24 * k} ${-0.5 * k}) scale(${k})`" fill="none" stroke="#d9b44a" stroke-width="1.4" stroke-linecap="round">
<circle r="5" />
<path d="M0 -3v3l2 1.5" />
</g>
<text :x="6 * k" :y="4 * k" :font-size="11.5 * k" font-weight="700" fill="#f3e7c9" class="tabular-nums">
{{ formatDuration(secondsUntil(tm.finishesAt)) }}
</text>
</g>
</g>
</svg>
</div>
</div>
</template>
<style scoped>
/* Buildings are massive and stay put: no lift, no glow, nothing on hover. Only a click
(or keyboard focus) outlines the ground they stand on. */
.bld,
.plot {
cursor: pointer;
outline: none;
}
.ring,
.plot {
fill: transparent;
stroke: transparent;
}
.bld.is-selected .ring,
.plot.is-selected {
stroke: #7b1e16;
stroke-width: 2;
}
.bld:focus-visible .ring,
.plot:focus-visible {
stroke: #7b1e16;
stroke-width: 2;
stroke-dasharray: 4 3;
}
</style>