408 lines
18 KiB
Vue
408 lines
18 KiB
Vue
<script setup lang="ts">
|
|
import type { BuildingType, Resources, VillageState } from '~/types/game'
|
|
import { drillThrust, workInPlace } from '~/diorama/life'
|
|
import { buildScene, type SceneBuilding, type SceneInset, type ScenePlot } from '~/diorama/scene'
|
|
import { SHIRTS, SMOKE, UNIFORM } from '~/diorama/models'
|
|
import type { BuildingTag } from '~/utils/buildingTags'
|
|
|
|
const props = defineProps<{
|
|
village: VillageState
|
|
/** Whole seconds until a server timestamp, on the server's clock. */
|
|
secondsUntil: (iso: string) => number
|
|
/** Fill the whole stage at any shape, without the sideways scroll of narrow frames. */
|
|
fill?: boolean
|
|
/** With `fill`: pixels at the edges that windows cover; the whole village fits into the rest. */
|
|
inset?: SceneInset
|
|
/** Projected stock, for the storage's tag. */
|
|
resources?: Resources | null
|
|
/** A window or card is open: a click in the village closes it instead of opening another. */
|
|
dismiss?: boolean
|
|
}>()
|
|
/** 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(() =>
|
|
props.fill ? buildScene(props.village, { width: size.value.w, height: size.value.h, inset: props.inset }) : 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())
|
|
|
|
// Walkers and smoke only move for players who don't ask for reduced motion.
|
|
const motion = ref(false)
|
|
onMounted(() => (motion.value = !window.matchMedia('(prefers-reduced-motion: reduce)').matches))
|
|
|
|
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
|
|
}
|
|
// A pointer click while something is open only closes it, so a stray click never swaps
|
|
// the window for another; keys still open what has focus.
|
|
const press = (id: number) => (props.dismiss ? clear() : toggle(id))
|
|
const pressPlot = (type: BuildingType) => (props.dismiss ? clear() : pick(type))
|
|
|
|
/** 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))
|
|
|
|
// Every building wears a tag like the old village views: its level and the one number
|
|
// that says what it does now (app/utils/buildingTags.ts). Sizes are screen pixels, scaled
|
|
// by k into the scene, so tags read the same however the village is scaled.
|
|
const TAG = { h: 20, chip: 20, icon: 14, gap: 4, pad: 6, font: 12 }
|
|
const FONT = 'ui-sans-serif, system-ui, sans-serif'
|
|
let measurer: CanvasRenderingContext2D | null = null
|
|
const widths = new Map<string, number>()
|
|
function textWidth(text: string) {
|
|
if (!widths.has(text)) {
|
|
measurer ??= import.meta.client ? document.createElement('canvas').getContext('2d') : null
|
|
if (measurer) measurer.font = `700 ${TAG.font}px ${FONT}`
|
|
widths.set(text, measurer ? Math.ceil(measurer.measureText(text).width) : text.length * 7.5)
|
|
}
|
|
return widths.get(text)!
|
|
}
|
|
const TONES = {
|
|
plain: { fill: '#f3e7c9', stroke: '#57391f', text: '#2f2013', icon: '#57391f' },
|
|
warn: { fill: '#e8cd7c', stroke: '#8a6a1c', text: '#2f2013', icon: '#57391f' },
|
|
alarm: { fill: '#7b1e16', stroke: '#4a120d', text: '#f3e7c9', icon: '#f3e7c9' },
|
|
}
|
|
|
|
// Tags that would overlap move up above the one in front, nearest buildings first.
|
|
const badges = computed(() => {
|
|
const K = k.value
|
|
const list = scene.value.objects.flatMap((o) => (o.building?.focusable ? [o.building] : []))
|
|
const placed: { b: SceneBuilding; info: BuildingTag | null; x: number; y: number; w: number; h: number }[] = []
|
|
const overlaps = (x: number, y: number, w: number, h: number) =>
|
|
placed.some((p) => Math.abs(p.x - x) < (p.w + w) / 2 + 2 * K && Math.abs(p.y - y) < (p.h + h) / 2 + 2 * K)
|
|
for (const b of [...list].sort((p, q) => q.label[1] - p.label[1])) {
|
|
const building = props.village.buildings.find((x) => x.id === b.id)
|
|
const info = building && props.resources ? buildingTag(props.village, building, props.resources, props.secondsUntil, t) : null
|
|
const inner = info ? TAG.pad + (info.icon ? TAG.icon + TAG.gap : 0) + textWidth(info.text) + TAG.pad : 0
|
|
const w = (TAG.chip + inner) * K
|
|
const h = TAG.h * K
|
|
const [x] = b.label
|
|
let y = b.label[1]
|
|
for (let n = 0; n < 8 && overlaps(x, y, w, h); n++) y -= h * 1.2
|
|
placed.push({ b, info, x, y, w, h })
|
|
}
|
|
return placed
|
|
})
|
|
const tagTitle = (b: SceneBuilding, info: BuildingTag | null) => [label(b), info?.title].filter(Boolean).join(' · ')
|
|
|
|
// 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: 41 * K, h: 12.5 * 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 + p.w / 2 && Math.abs(p.y - y) < half.h + p.h / 2)
|
|
return scene.value.timers
|
|
.map((tm) => {
|
|
const badge = tm.buildingId !== null ? badges.value.find((p) => p.b.id === tm.buildingId) : undefined
|
|
const lift = badge ? 24 : 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-y-hidden bg-[#6a7049]" :class="fill ? 'overflow-x-hidden' : 'overflow-x-auto'">
|
|
<div ref="stage" class="h-full" :class="fill ? '' : '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>
|
|
|
|
<!-- Villagers going out to work and bringing the load home, under the buildings they
|
|
walk into; sheep and chickens wandering. -->
|
|
<g v-if="motion" class="pointer-events-none">
|
|
<template v-for="(w, i) in scene.walks" :key="`walk-${i}-${w.d.length}`">
|
|
<VillageWorker
|
|
v-for="n in w.count"
|
|
:key="n"
|
|
:d="w.d"
|
|
:walk="w.walk"
|
|
:work="w.work"
|
|
:tool="w.tool"
|
|
:carry="w.carry"
|
|
:shirt="SHIRTS[(i * 3 + n) % SHIRTS.length]!"
|
|
:size="1.6"
|
|
:begin="-((n - 1) * (w.walk * 2 + w.work)) / w.count - i * 3"
|
|
/>
|
|
</template>
|
|
<VillageAnimal v-for="(a, i) in scene.herds" :key="`herd-${i}`" :kind="a.kind" :d="a.d" :dur="a.dur" :begin="a.begin" :size="a.kind === 'sheep' ? 1.7 : 1.4" />
|
|
</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="pressPlot(p.type)"
|
|
@keydown.enter.prevent="pick(p.type)"
|
|
@keydown.space.prevent="pick(p.type)"
|
|
>
|
|
<title>{{ plotLabel(p) }}</title>
|
|
</ellipse>
|
|
|
|
<!-- The wall is thin: a wide band along its ring opens it too. -->
|
|
<ellipse
|
|
v-if="scene.wallBand"
|
|
class="band"
|
|
v-bind="scene.wallBand.ring"
|
|
:stroke-width="scene.wallBand.width"
|
|
aria-hidden="true"
|
|
@click.stop="press(scene.wallBand.id)"
|
|
/>
|
|
|
|
<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="press(obj.building.id)"
|
|
@keydown.enter.prevent="toggle(obj.building.id)"
|
|
@keydown.space.prevent="toggle(obj.building.id)"
|
|
>
|
|
<polygon v-if="obj.building.hit" class="hit" :points="obj.building.hit" />
|
|
<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>
|
|
<!-- Villagers who stay put: talking, hammering at a building in the works, drilling;
|
|
soldiers standing guard at the doors. -->
|
|
<g v-else-if="obj.life" class="pointer-events-none" :transform="`translate(${obj.life.at[0]} ${obj.life.at[1]})`">
|
|
<g v-if="obj.life.guard" transform="scale(1.6)">
|
|
<VillageWalker :shirt="UNIFORM" :facing="obj.life.facing" tool="spear" soldier />
|
|
</g>
|
|
<g v-else transform="scale(1.6)">
|
|
<animateTransform
|
|
v-if="motion && !obj.life.tool"
|
|
attributeName="transform"
|
|
type="translate"
|
|
values="0 0;0 -0.5;0 0;0 0"
|
|
keyTimes="0;0.15;0.3;1"
|
|
:dur="`${2.2 + (obj.life.shirt % 3) * 0.4}s`"
|
|
additive="sum"
|
|
repeatCount="indefinite"
|
|
/>
|
|
<VillageWalker
|
|
:shirt="SHIRTS[obj.life.shirt % SHIRTS.length]!"
|
|
:facing="obj.life.facing"
|
|
:tool="obj.life.tool"
|
|
:swing="motion && obj.life.tool ? (obj.life.drill ? drillThrust() : workInPlace()) : null"
|
|
/>
|
|
</g>
|
|
</g>
|
|
<!-- Villagers coming out of a door and going back in, porters bringing materials. -->
|
|
<g v-else-if="obj.walker" class="pointer-events-none">
|
|
<VillageWorker
|
|
v-if="motion"
|
|
:d="obj.walker.d"
|
|
:walk="obj.walker.walk"
|
|
:work="obj.walker.work"
|
|
:rest="obj.walker.rest"
|
|
:indoors="obj.walker.indoors"
|
|
:tool="obj.walker.tool"
|
|
:carry="obj.walker.carry"
|
|
:soldier="obj.walker.soldier"
|
|
:shirt="obj.walker.soldier ? UNIFORM : SHIRTS[obj.walker.shirt % SHIRTS.length]!"
|
|
:size="1.6"
|
|
:begin="obj.walker.begin"
|
|
/>
|
|
</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>
|
|
|
|
<!-- Smoke from chimneys, forges and kilns: flat puffs that rise, drift and shrink away. -->
|
|
<g v-if="motion" class="pointer-events-none" :fill="SMOKE.fill" :stroke="SMOKE.edge" stroke-width="0.5">
|
|
<g v-for="(p, i) in scene.smoke" :key="`smoke-${i}`" :transform="`translate(${p[0]} ${p[1]})`">
|
|
<circle v-for="n in 3" :key="n" class="puff" r="5.5" :style="{ animationDelay: `${-(n * 1.3 + i * 0.9)}s` }" />
|
|
</g>
|
|
</g>
|
|
|
|
<!-- Birds crossing now and then. -->
|
|
<g v-if="motion" class="pointer-events-none">
|
|
<VillageBird v-for="(b, i) in scene.birds" :key="`bird-${i}`" :d="b.d" :dur="b.dur" :begin="b.begin" :size="1.6" />
|
|
</g>
|
|
|
|
<!-- Building tags (level and what it does now) and countdowns. Flat colours only. -->
|
|
<g class="pointer-events-none" :font-family="FONT" text-anchor="middle">
|
|
<g
|
|
v-for="{ b, info, x, y, w } in badges"
|
|
:key="`badge-${b.id}`"
|
|
class="pointer-events-auto cursor-pointer"
|
|
:transform="`translate(${x - w / 2} ${y}) scale(${k})`"
|
|
@click.stop="press(b.id)"
|
|
>
|
|
<title>{{ tagTitle(b, info) }}</title>
|
|
<rect
|
|
:width="w / k"
|
|
:height="TAG.h"
|
|
:y="-TAG.h / 2"
|
|
:fill="TONES[info?.tone ?? 'plain'].fill"
|
|
:stroke="selected === b.id ? '#7b1e16' : TONES[info?.tone ?? 'plain'].stroke"
|
|
:stroke-width="selected === b.id ? 2.4 : 1.4"
|
|
/>
|
|
<rect :width="TAG.chip" :height="TAG.h" :y="-TAG.h / 2" :fill="b.upgrading ? '#c49a2c' : '#57391f'" />
|
|
<text :x="TAG.chip / 2" :y="4.3" :font-size="TAG.font" font-weight="700" :fill="b.upgrading ? '#1f150c' : '#f3e7c9'">{{ b.level }}</text>
|
|
<template v-if="info">
|
|
<g v-if="info.icon" :transform="`translate(${TAG.chip + TAG.pad} ${-TAG.icon / 2})`" :style="{ color: TONES[info.tone].icon }">
|
|
<IconResource v-if="info.icon.kind === 'resource'" :resource="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
|
|
<IconUnit v-else-if="info.icon.kind === 'unit'" :unit="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
|
|
<IconUi v-else :name="info.icon.name" :width="TAG.icon" :height="TAG.icon" />
|
|
</g>
|
|
<text
|
|
:x="TAG.chip + TAG.pad + (info.icon ? TAG.icon + TAG.gap : 0)"
|
|
:y="4.3"
|
|
text-anchor="start"
|
|
:font-size="TAG.font"
|
|
font-weight="700"
|
|
:fill="TONES[info.tone].text"
|
|
>
|
|
{{ info.text }}
|
|
</text>
|
|
</template>
|
|
</g>
|
|
|
|
<g
|
|
v-for="tm in timers"
|
|
:key="`timer-${tm.orderId}`"
|
|
:class="tm.buildingId ? 'pointer-events-auto cursor-pointer' : ''"
|
|
:transform="`translate(${tm.x} ${tm.y})`"
|
|
@click.stop="tm.buildingId && press(tm.buildingId)"
|
|
>
|
|
<rect :x="-39 * k" :y="-11.5 * k" :width="78 * k" :height="22 * k" :rx="2 * k" fill="#2f2013" stroke="#c49a2c" :stroke-width="1.2 * k" />
|
|
<g :transform="`translate(${-27.5 * k} ${-0.5 * k}) scale(${1.15 * k})`" fill="none" stroke="#d9b44a" stroke-width="1.4" stroke-linecap="round">
|
|
<circle r="5" />
|
|
<path d="M0 -3v3l2 1.5" />
|
|
</g>
|
|
<text :x="7 * k" :y="4.6 * k" :font-size="13 * 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,
|
|
.hit {
|
|
fill: transparent;
|
|
stroke: transparent;
|
|
}
|
|
.band {
|
|
fill: none;
|
|
stroke: transparent;
|
|
pointer-events: stroke;
|
|
cursor: pointer;
|
|
}
|
|
.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;
|
|
}
|
|
|
|
/* A puff starts small at the chimney, grows as it rises and drifts, then shrinks to nothing. */
|
|
.puff {
|
|
transform-box: fill-box;
|
|
transform-origin: center;
|
|
animation: puff 3.9s linear infinite;
|
|
}
|
|
@keyframes puff {
|
|
0% {
|
|
transform: translate(0, 0) scale(0.25);
|
|
}
|
|
55% {
|
|
transform: translate(-6px, -15px) scale(1);
|
|
}
|
|
100% {
|
|
transform: translate(-13px, -27px) scale(0);
|
|
}
|
|
}
|
|
</style>
|