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

192 lines
8.6 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
/** The cluster of firs a forest field wears (a symbol below), by how deep in the forest it lies. */
forest: string | null
/** A mountain's faces, a little different on every field. */
peaks: string[] | null
}
// Forests like the Die Stämme map's: dense clusters of small dark firs. Each forest field
// wears one of a few clusters drawn once as a symbol: a few trees at the edge of the forest,
// more further in, a thicket deep inside, darker the deeper.
interface Fir {
x: number
y: number
size: number
}
const DENSITY = [3, 6, 10]
const FIR_COLOURS = [
{ lit: '#3e5034', dark: '#2b3a27' },
{ lit: '#384a30', dark: '#273624' },
{ lit: '#32432c', dark: '#223020' },
]
const CLUSTERS = DENSITY.flatMap((n, d) =>
[0, 1, 2, 3].map((v) => {
const firs: Fir[] = Array.from({ length: n }, (_, i) => ({
x: 12 + fieldHash(i * 7 + v * 31, d * 13 + 1) * 76,
y: 18 + fieldHash(d * 17 + 3, i * 11 + v * 29) * 70,
size: 0.5 + fieldHash(i + v * 5, d + 9) * 0.28,
})).sort((a, b) => a.y - b.y)
return { id: `map-forest-${d}-${v}`, firs, colours: FIR_COLOURS[d]! }
}),
)
/** Forest floor under the thicker clusters: the grass goes dark there. */
const FLOOR = ['#656c46', '#606843']
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, 30 + r * 10, 0, 58, 82],
[50 + r * 12, 8 + q * 12, 16, 98, 92],
] 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 = ['#78746b', '#55524c', '#8d887f', '#67645e']
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 shallows = ''
let corners = ''
let curvedShore = ''
let curvedShallows = ''
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 depth = ground === 'forest' ? forestDepth(x, y) : 0
const thick = depth > TERRAIN.forest.above ? (depth > 0.7 ? 2 : 1) : 0
const fill = ground === null ? null : ground === 'water' ? WATER : thick ? FLOOR[thick - 1]! : 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, then the shallows, a step lighter than the deep water.
const land = (gx: number, gy: number) => groundOf(gx, gy) !== 'water'
const band = (o: number, s: number) => {
let d = ''
if (land(x, y - 1)) d += `M${x * F} ${y * F + o}h${F}v${s}h${-F}z`
if (land(x, y + 1)) d += `M${x * F} ${(y + 1) * F - o - s}h${F}v${s}h${-F}z`
if (land(x - 1, y)) d += `M${x * F + o} ${y * F}h${s}v${F}h${-s}z`
if (land(x + 1, y)) d += `M${(x + 1) * F - o - s} ${y * F}h${s}v${F}h${-s}z`
return d
}
shallows += band(F * 0.08, F * 0.1)
shore += band(0, F * 0.09)
// Where land meets the lake on two sides, the corner is land, rounded off with the shore.
const r = F * 0.36
for (const [sx, sy] of [[0, 0], [1, 0], [1, 1], [0, 1]] as const) {
if (!land(x + (sx ? 1 : -1), y) || !land(x, y + (sy ? 1 : -1))) continue
const cx = (x + sx) * F
const cy = (y + sy) * F
const dx = sx ? -1 : 1
const dy = sy ? -1 : 1
// The corner square minus a quarter disc, then the shore and the shallows along the quarter circle.
const sweep = dx * dy > 0 ? 0 : 1
corners += `M${cx} ${cy}h${dx * r}A${r} ${r} 0 0 ${sweep} ${cx} ${cy + dy * r}z`
const curve = (outer: number, inner: number) =>
`M${cx + dx * r} ${cy + dy * (r - outer)}A${outer} ${outer} 0 0 ${sweep} ${cx + dx * (r - outer)} ${cy + dy * r}L${cx + dx * (r - inner)} ${cy + dy * r}A${inner} ${inner} 0 0 ${1 - sweep} ${cx + dx * r} ${cy + dy * (r - inner)}z`
curvedShore += curve(r, r - F * 0.09)
curvedShallows += curve(r - F * 0.08, r - F * 0.18)
}
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
}
const forest = ground === 'forest' ? `#map-forest-${thick}-${Math.floor(fieldHash(y, x) * 4)}` : null
out.push({ x, y, ground, forest, peaks: ground === 'mountain' ? peaks(x, y) : null })
}
}
return { flat: Object.entries(flat), shore, shallows, corners, curvedShore, curvedShallows, ripples, raised: out }
})
</script>
<template>
<g>
<defs>
<symbol v-for="c in CLUSTERS" :id="c.id" :key="c.id" v-once viewBox="0 0 100 100" overflow="visible">
<g v-for="(t, i) in c.firs" :key="i" :transform="`translate(${t.x} ${t.y}) scale(${t.size})`">
<ellipse cx="5" cy="25" rx="12" ry="4" fill="#4a5233" />
<rect x="-2" y="18" width="4" height="8" fill="#3f3328" />
<polygon points="0,-12 -12,20 0,16" :fill="c.colours.dark" />
<polygon points="0,-12 12,20 0,16" :fill="c.colours.lit" />
</g>
</symbol>
</defs>
<path v-for="[fill, d] in fields.flat" :key="fill" :d="d" :fill="fill" />
<path v-if="fields.shallows" :d="fields.shallows" fill="#55676a" />
<path v-if="fields.shore" :d="fields.shore" :fill="SHORE" />
<path v-if="fields.corners" :d="fields.corners" :fill="GRASS[0]" />
<path v-if="fields.curvedShallows" :d="fields.curvedShallows" fill="#55676a" />
<path v-if="fields.curvedShore" :d="fields.curvedShore" :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="22,72 40,40 56,72" fill="#7e7a71" />
<polygon points="40,40 56,72 61,63" fill="#5a5751" />
<polygon points="50,74 67,50 84,74" fill="#878279" />
<polygon points="67,50 84,74 88,67" fill="#64615b" />
</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>
<use v-if="f.forest" :href="f.forest" width="100" height="100" />
</g>
</g>
</template>