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

80 lines
3.4 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { BuildingType } from '~/types/game'
import { buildCloseup, type CloseupState } from '~/diorama/closeup'
import { drillThrust, workInPlace } from '~/diorama/life'
import { SHIRTS, UNIFORM } from '~/diorama/models'
const props = defineProps<{
type: BuildingType
level: number
workers: number
/** Orders running or waiting at it, units in training: builders, materials, a drill. */
state?: CloseupState
}>()
// 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, props.state))
const hammering = workInPlace()
const drilling = drillThrust()
// 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))
</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-[#6a7049]">
<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">
<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' ? 3 : 2.6" />
</g>
<template v-for="(l, i) in scene.layers" :key="l.key">
<template v-if="l.walk">
<VillageWorker
v-if="motion"
:d="l.walk.d"
:walk="l.walk.walk"
:work="l.walk.work"
:tool="l.walk.tool"
:carry="l.walk.carry"
:rest="l.walk.rest"
:indoors="l.walk.indoors"
:shirt="SHIRTS[i % SHIRTS.length]!"
:size="2.6"
:begin="l.walk.begin"
/>
</template>
<g v-else-if="l.life" :transform="`translate(${l.life.at[0]} ${l.life.at[1]}) scale(2.6)`">
<VillageWalker v-if="l.life.guard" :shirt="UNIFORM" :facing="l.life.facing" tool="spear" soldier />
<VillageWalker v-else :shirt="SHIRTS[i % SHIRTS.length]!" :facing="l.life.facing" :tool="l.life.tool" :swing="motion && l.life.tool ? (l.life.drill ? drilling : hammering) : null" />
</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>
<g v-if="motion">
<VillageBird v-for="(b, i) in scene.birds" :key="`bird-${i}`" :d="b.d" :dur="b.dur" :begin="b.begin" :size="2.4" />
</g>
</svg>
</div>
</template>