55 lines
2.6 KiB
Vue
55 lines
2.6 KiB
Vue
|
|
<script setup lang="ts">
|
||
|
|
import type { BuildingType } from '~/types/game'
|
||
|
|
import { buildCloseup } from '~/diorama/closeup'
|
||
|
|
|
||
|
|
const props = defineProps<{ type: BuildingType; level: number; workers: number }>()
|
||
|
|
|
||
|
|
// Drawn for the stage's own shape, so land and forest fill it edge to edge.
|
||
|
|
const stage = ref<HTMLElement | null>(null)
|
||
|
|
const aspect = ref(2)
|
||
|
|
let observer: ResizeObserver | undefined
|
||
|
|
onMounted(() => {
|
||
|
|
const measure = () => {
|
||
|
|
const el = stage.value
|
||
|
|
if (el?.clientWidth && el.clientHeight) aspect.value = Math.round((el.clientWidth / el.clientHeight) * 10) / 10
|
||
|
|
}
|
||
|
|
measure()
|
||
|
|
observer = new ResizeObserver(measure)
|
||
|
|
if (stage.value) observer.observe(stage.value)
|
||
|
|
})
|
||
|
|
onBeforeUnmount(() => observer?.disconnect())
|
||
|
|
|
||
|
|
const scene = computed(() => buildCloseup(props.type, props.level, props.workers, aspect.value))
|
||
|
|
|
||
|
|
// Workers only walk for players who don't ask for reduced motion.
|
||
|
|
const motion = ref(false)
|
||
|
|
onMounted(() => (motion.value = !window.matchMedia('(prefers-reduced-motion: reduce)').matches))
|
||
|
|
const SHIRTS = ['#6b4f3a', '#7b1e16', '#3d5a26', '#57391f', '#5a5f63']
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<!-- The building up close, its workers walking between it and where they work. -->
|
||
|
|
<template>
|
||
|
|
<div ref="stage" class="h-full w-full overflow-hidden bg-[#74864a]">
|
||
|
|
<svg class="block h-full w-full select-none" :viewBox="scene.viewBox" preserveAspectRatio="xMidYMid slice" aria-hidden="true">
|
||
|
|
<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>
|
||
|
|
<g v-if="motion" stroke="#2f2013" stroke-width="0.5">
|
||
|
|
<g v-for="(w, i) in scene.walks" :key="`walk-${i}-${w.d.length}`">
|
||
|
|
<g transform="scale(2.6)">
|
||
|
|
<rect x="-1.7" y="-7" width="3.4" height="5.4" rx="0.6" :fill="SHIRTS[i % SHIRTS.length]" />
|
||
|
|
<circle cy="-8.8" r="1.7" fill="#d8a982" />
|
||
|
|
<path d="M-1 -1.6v1.8M1 -1.6v1.8" fill="none" stroke-width="0.9" />
|
||
|
|
<rect v-if="w.carry" x="0.6" y="-9.6" width="3.6" height="2.4" :fill="w.carry" />
|
||
|
|
</g>
|
||
|
|
<animateMotion :dur="`${w.seconds}s`" :begin="`${w.delay}s`" repeatCount="indefinite" keyPoints="0;1;0" keyTimes="0;0.5;1" calcMode="linear" :path="w.d" />
|
||
|
|
</g>
|
||
|
|
</g>
|
||
|
|
<g v-for="obj in scene.objects" :key="obj.key" 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" />
|
||
|
|
</g>
|
||
|
|
</svg>
|
||
|
|
</div>
|
||
|
|
</template>
|