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

39 lines
1.7 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { BuildingType } from '~/types/game'
import { mix, plane, project, toPolys, type V3 } from '~/diorama/iso'
import { buildingModel } from '~/diorama/models'
const props = defineProps<{ type: BuildingType; level: number }>()
// The building as it stands in the village, on its own patch of grass: the same model
// and shading as the diorama, framed to its bounds.
const S = 24
const picture = computed(() => {
const model = buildingModel(props.type, Math.max(1, props.level), S)
const patch = Array.from({ length: 14 }, (_, i): V3 => {
const a = (i / 14) * Math.PI * 2
const r = S * (1.45 + 0.08 * Math.sin(i * 2.3))
return [Math.cos(a) * r, Math.sin(a) * r, 0]
})
const ground = toPolys([plane(patch, '#7f8f4b')])
const rim = toPolys([plane(patch.map(([x, y]): V3 => [x * 1.06, y * 1.06, -3]), mix('#7f8f4b', '#3e2b1a', 0.45))])
const polys = [...rim, ...ground, ...toPolys(model.faces)]
const pts = polys.flatMap((p) => p.points.split(' ').map((xy) => xy.split(',').map(Number) as [number, number]))
const top = project([0, 0, model.height])[1]
const xs = pts.map((p) => p[0])
const ys = [...pts.map((p) => p[1]), top]
const pad = 4
const x0 = Math.min(...xs) - pad
const y0 = Math.min(...ys) - pad
return { polys, viewBox: `${x0.toFixed(1)} ${y0.toFixed(1)} ${(Math.max(...xs) - x0 + pad).toFixed(1)} ${(Math.max(...ys) - y0 + pad).toFixed(1)}` }
})
</script>
<template>
<svg :viewBox="picture.viewBox" class="block" aria-hidden="true">
<g stroke-width="0.6" stroke-linejoin="round">
<polygon v-for="(p, i) in picture.polys" :key="i" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
</g>
</svg>
</template>