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

52 lines
2.3 KiB
Vue
Raw Normal View History

<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>
<template v-for="(l, i) in scene.layers" :key="l.key">
<g v-if="l.walk">
<template v-if="motion">
<VillageWalker transform="scale(2.6)" :shirt="SHIRTS[i % SHIRTS.length]!" :carry="l.walk.carry" />
<animateMotion :dur="`${l.walk.seconds}s`" :begin="`${l.walk.delay}s`" repeatCount="indefinite" keyPoints="0;1;0" keyTimes="0;0.5;1" calcMode="linear" :path="l.walk.d" />
</template>
</g>
<g v-else v-memo="[l]" stroke-width="0.6" stroke-linejoin="round">
<polygon v-for="(p, j) in l.polys" :key="j" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
</g>
</template>
</svg>
</div>
</template>