game-village-politics-frontend/app/components/map/Land.vue

135 lines
5.8 KiB
Vue
Raw Normal View History

<script setup lang="ts">
import type { MapVillage } from '~/types/map'
import type { Ground } from '~/utils/terrain'
const props = defineProps<{
/** Whole fields to draw: from x0|y0 up to, not including, x1|y1. */
region: { x0: number; y0: number; x1: number; y1: number }
/** Field size in px. */
field: number
/** Villages by "x|y": their fields are open land. */
villages: Map<string, MapVillage>
}>()
// The ground of every field in the region, at its world position: grass in four shades and
// lakes as one path each, shores, rocky hills, mountains and trees on top. Drawn from
// utils/terrain.ts, the same as the minimap. Only changes when the region or a village does,
// so dragging the map never re-renders it.
interface Field {
x: number
y: number
ground: Ground
/** Trees as [x, y, size] in the field's 100 × 100 box, back to front. */
trees: [number, number, number][]
/** A mountain's faces, a little different on every field. */
peaks: string[] | null
}
const pts = (...p: number[]) => p.reduce((s, v, i) => s + (i % 2 ? `,${v.toFixed(1)}` : `${i ? ' ' : ''}${v.toFixed(1)}`), '')
/**
* A mountain in the field's 100 × 100 box: a big peak and a smaller one behind it, each a lit
* face, a face in shadow and bare rock at the top. The big one rises over the field above, so
* where mountains bunch up they close into a range.
*/
function peaks(x: number, y: number): string[] {
const r = fieldHash(x, y)
const q = fieldHash(y, x)
const out: string[] = []
for (const [ax, ay, lx, rx, by] of [
[24 + q * 14, 18 + r * 10, -2, 60, 84],
[48 + r * 12, -14 + q * 14, 14, 100, 94],
] as const) {
const mx = ax + (rx - ax) * 0.3
const my = by + 4
const k = 0.3
out.push(
pts(lx, by, ax, ay, mx, my),
pts(ax, ay, rx, by, mx, my),
pts(ax, ay, ax + (lx - ax) * k, ay + (by - ay) * k, ax + (mx - ax) * k, ay + (my - ay) * k),
pts(ax, ay, ax + (rx - ax) * k, ay + (by - ay) * k, ax + (mx - ax) * k, ay + (my - ay) * k),
)
}
return out
}
/** Fills of a mountain's faces, in the order `peaks` returns them: lit, in shadow, bare rock on each. */
const PEAK_FILLS = ['#827d74', '#5a5751', '#9a958b', '#6e6b65']
const groundOf = (x: number, y: number): Ground => (props.villages.has(`${x}|${y}`) ? 'grass' : groundAt(x, y))
const fields = computed(() => {
const { x0, y0, x1, y1 } = props.region
const F = props.field
const out: Field[] = []
// The flat ground: one path per grass shade and one for the lakes, a subpath per run of fields.
const flat: Record<string, string> = {}
let shore = ''
let ripples = ''
for (let y = y0; y < y1; y++) {
let run: string | null = null
let from = x0
const close = (to: number) => {
if (run) flat[run] = `${flat[run] ?? ''}M${from * F} ${y * F}h${(to - from) * F}v${F}h${-(to - from) * F}z`
}
for (let x = x0; x <= x1; x++) {
const ground = x < x1 ? groundOf(x, y) : null
const fill = ground === null ? null : ground === 'water' ? WATER : GRASS[Math.floor(fieldHash(x, y) * GRASS.length)]!
if (fill !== run) {
close(x)
run = fill
from = x
}
if (ground === null || ground === 'grass') continue
if (ground === 'water') {
// A sandy shore where the lake meets land, and a ripple.
const land = (gx: number, gy: number) => groundOf(gx, gy) !== 'water'
const s = F * 0.09
if (land(x, y - 1)) shore += `M${x * F} ${y * F}h${F}v${s}h${-F}z`
if (land(x, y + 1)) shore += `M${x * F} ${(y + 1) * F - s}h${F}v${s}h${-F}z`
if (land(x - 1, y)) shore += `M${x * F} ${y * F}h${s}v${F}h${-s}z`
if (land(x + 1, y)) shore += `M${(x + 1) * F - s} ${y * F}h${s}v${F}h${-s}z`
const k = F / 100
ripples += `M${(x * 100 + 28 + fieldHash(x, y) * 30) * k} ${(y * 100 + 50) * k}h${18 * k}M${(x * 100 + 40 + fieldHash(y, x) * 20) * k} ${(y * 100 + 64) * k}h${12 * k}`
continue
}
// Deeper in a forest, more and bigger trees.
const count = ground === 'forest' ? 2 + Math.round(forestDepth(x, y) * 4) : 0
const trees = Array.from({ length: count }, (_, i): [number, number, number] => [
14 + fieldHash(x * 3 + i, y) * 72,
28 + fieldHash(x, y * 3 + i) * 56,
0.75 + fieldHash(x + i, y + i) * 0.5,
]).sort((a, b) => a[1] - b[1])
out.push({ x, y, ground, trees, peaks: ground === 'mountain' ? peaks(x, y) : null })
}
}
return { flat: Object.entries(flat), shore, ripples, raised: out }
})
</script>
<template>
<g>
<path v-for="[fill, d] in fields.flat" :key="fill" :d="d" :fill="fill" />
<path v-if="fields.shore" :d="fields.shore" :fill="SHORE" />
<path v-if="fields.ripples" :d="fields.ripples" stroke="#6a7a80" :stroke-width="field * 0.03" stroke-linecap="round" />
<g v-for="f in fields.raised" :key="`${f.x}|${f.y}`" :transform="`translate(${f.x * field} ${f.y * field}) scale(${field / 100})`">
<template v-if="f.ground === 'hill'">
<ellipse cx="52" cy="70" rx="40" ry="16" fill="#5d6242" />
<polygon points="18,72 40,30 58,72" fill="#8a857c" />
<polygon points="40,30 58,72 64,60" fill="#5f5c56" />
<polygon points="48,74 68,42 86,74" fill="#9a958c" />
<polygon points="68,42 86,74 90,66" fill="#6c6962" />
</template>
<template v-else-if="f.peaks">
<ellipse cx="50" cy="88" rx="52" ry="14" fill="#5a5e3f" />
<polygon v-for="(p, i) in f.peaks" :key="i" :points="p" :fill="PEAK_FILLS[i % 4]" />
</template>
<g v-for="(tr, i) in f.trees" :key="i" :transform="`translate(${tr[0]} ${tr[1]}) scale(${tr[2]})`">
<ellipse cx="4" cy="26" rx="12" ry="4" fill="#4f5535" />
<rect x="-2" y="18" width="4" height="9" fill="#4a3b2e" />
<polygon points="0,-10 -13,20 0,16" fill="#2b3a27" />
<polygon points="0,-10 13,20 0,16" fill="#3e5034" />
</g>
</g>
</g>
</template>