feat: the minimap shows the land (lakes, forests, hills, mountains) from a shared utils/terrain.ts, villages in their relation colour, the view frame moved by a transform
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
33a29bbc56
commit
a657e65955
|
|
@ -1,19 +1,59 @@
|
|||
<script setup lang="ts">
|
||||
import type { MapView } from '~/types/map'
|
||||
import type { MarkerRelation } from '~/utils/relations'
|
||||
import type { Ground } from '~/utils/terrain'
|
||||
|
||||
const props = defineProps<{
|
||||
/** A wide window around the main map's centre. */
|
||||
overview: MapView
|
||||
/** The main map's window, drawn as a frame. */
|
||||
frame: { x: number; y: number; w: number; h: number } | null
|
||||
/** The village the player is in now: drawn white. */
|
||||
current?: number | null
|
||||
}>()
|
||||
const emit = defineEmits<{ center: [at: { x: number; y: number }] }>()
|
||||
const { t } = useI18n()
|
||||
|
||||
// One cell per field, like the Die Stämme minimap: village dots, lines, and the view frame.
|
||||
// One cell per field, like the Die Stämme minimap: the land from the same function as the big
|
||||
// map, village dots in their relation colour, sector lines and the view frame.
|
||||
const CELL = 4
|
||||
const size = computed(() => ({ w: props.overview.w * CELL, h: props.overview.h * CELL }))
|
||||
const color = (v: MapView['villages'][number]) => (v.own ? '#d9b44a' : v.player ? '#962a1f' : '#8b9094')
|
||||
|
||||
/** One path per kind of ground (grass is the background), a run of equal fields per subpath. */
|
||||
const land = computed(() => {
|
||||
const o = props.overview
|
||||
const paths: Partial<Record<Ground, string>> = {}
|
||||
for (let r = 0; r < o.h; r++) {
|
||||
let run: Ground | null = null
|
||||
let from = 0
|
||||
const close = (c: number) => {
|
||||
if (run && run !== 'grass') paths[run] = `${paths[run] ?? ''}M${from * CELL} ${r * CELL}h${(c - from) * CELL}v${CELL}h${-(c - from) * CELL}z`
|
||||
}
|
||||
for (let c = 0; c <= o.w; c++) {
|
||||
const g = c < o.w ? groundAt(o.x + c, o.y + r) : null
|
||||
if (g === run) continue
|
||||
close(c)
|
||||
run = g
|
||||
from = c
|
||||
}
|
||||
}
|
||||
return (Object.entries(paths) as [Ground, string][]).map(([ground, d]) => ({ ground, d, fill: GROUND_COLOURS[ground] }))
|
||||
})
|
||||
|
||||
/** Villages: a dark cell each, so they stand off every ground, and a dot per relation colour. */
|
||||
const villages = computed(() => {
|
||||
const o = props.overview
|
||||
let rims = ''
|
||||
const dots: Partial<Record<MarkerRelation, string>> = {}
|
||||
for (const v of o.villages) {
|
||||
const x = (v.x - o.x) * CELL
|
||||
const y = (v.y - o.y) * CELL
|
||||
rims += `M${x} ${y}h${CELL}v${CELL}h${-CELL}z`
|
||||
const r: MarkerRelation = v.id === props.current ? 'current' : v.relation
|
||||
dots[r] = `${dots[r] ?? ''}M${x + 0.8} ${y + 0.8}h${CELL - 1.6}v${CELL - 1.6}h${-(CELL - 1.6)}z`
|
||||
}
|
||||
return { rims, dots: (Object.entries(dots) as [MarkerRelation, string][]).map(([r, d]) => ({ r, d, fill: RELATION_COLOURS[r] })) }
|
||||
})
|
||||
|
||||
const lines = computed(() => {
|
||||
const out: { x1: number; y1: number; x2: number; y2: number; continent: boolean }[] = []
|
||||
|
|
@ -28,6 +68,18 @@ const lines = computed(() => {
|
|||
return out
|
||||
})
|
||||
|
||||
/** The main map's window as a frame over the minimap, moved by a transform so dragging the map paints nothing here. */
|
||||
const frameBox = computed(() => {
|
||||
const f = props.frame
|
||||
const o = props.overview
|
||||
if (!f || f.w <= 0 || f.h <= 0) return null
|
||||
return {
|
||||
width: `${(f.w / o.w) * 100}%`,
|
||||
height: `${(f.h / o.h) * 100}%`,
|
||||
transform: `translate3d(${((f.x - o.x) / f.w) * 100}%, ${((f.y - o.y) / f.h) * 100}%, 0)`,
|
||||
}
|
||||
})
|
||||
|
||||
function onClick(e: MouseEvent) {
|
||||
const rect = (e.currentTarget as Element).getBoundingClientRect()
|
||||
const scale = size.value.w / rect.width
|
||||
|
|
@ -39,6 +91,7 @@ function onClick(e: MouseEvent) {
|
|||
</script>
|
||||
|
||||
<template>
|
||||
<div class="relative overflow-hidden">
|
||||
<svg
|
||||
:viewBox="`0 0 ${size.w} ${size.h}`"
|
||||
class="block w-full cursor-crosshair"
|
||||
|
|
@ -46,7 +99,8 @@ function onClick(e: MouseEvent) {
|
|||
:aria-label="t('map.minimap')"
|
||||
@click="onClick"
|
||||
>
|
||||
<rect :width="size.w" :height="size.h" fill="#656a45" />
|
||||
<rect :width="size.w" :height="size.h" :fill="GROUND_COLOURS.grass" />
|
||||
<path v-for="g in land" :key="g.ground" :d="g.d" :fill="g.fill" shape-rendering="crispEdges" />
|
||||
<line
|
||||
v-for="(l, i) in lines"
|
||||
:key="i"
|
||||
|
|
@ -57,24 +111,9 @@ function onClick(e: MouseEvent) {
|
|||
:stroke="l.continent ? '#1f150c' : 'rgb(31 21 12 / 0.35)'"
|
||||
:stroke-width="l.continent ? 1.5 : 0.6"
|
||||
/>
|
||||
<rect
|
||||
v-for="v in overview.villages"
|
||||
:key="v.id"
|
||||
:x="(v.x - overview.x) * CELL + 0.5"
|
||||
:y="(v.y - overview.y) * CELL + 0.5"
|
||||
:width="CELL - 1"
|
||||
:height="CELL - 1"
|
||||
:fill="color(v)"
|
||||
/>
|
||||
<rect
|
||||
v-if="frame"
|
||||
:x="(frame.x - overview.x) * CELL"
|
||||
:y="(frame.y - overview.y) * CELL"
|
||||
:width="frame.w * CELL"
|
||||
:height="frame.h * CELL"
|
||||
fill="none"
|
||||
stroke="#f3e7c9"
|
||||
stroke-width="1.5"
|
||||
/>
|
||||
<path :d="villages.rims" fill="#1f150c" />
|
||||
<path v-for="v in villages.dots" :key="v.r" :d="v.d" :fill="v.fill" />
|
||||
</svg>
|
||||
<div v-if="frameBox" class="pointer-events-none absolute top-0 left-0 border-[1.5px] border-[#f3e7c9] will-change-transform" :style="frameBox" />
|
||||
</div>
|
||||
</template>
|
||||
|
|
|
|||
|
|
@ -389,6 +389,7 @@ const continent = computed(() => (rounded.value ? continentOf(rounded.value.x, r
|
|||
v-if="overview.view.value"
|
||||
:overview="overview.view.value"
|
||||
:frame="visible ? { x: visible.left, y: visible.top, w: visible.cols, h: visible.rows } : null"
|
||||
:current="currentVillage"
|
||||
@center="goTo"
|
||||
/>
|
||||
<p v-else class="py-16 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
// The world map's land: lakes, rocky hills that rise to mountains where they bunch up, and
|
||||
// forests, all from smooth noise of the coordinates. A field always looks the same, and the
|
||||
// map and its overview read it from this one function, so the two never disagree. The land
|
||||
// is decoration only: troops cross every field, and travel is a straight line.
|
||||
|
||||
export type Ground = 'grass' | 'forest' | 'water' | 'hill' | 'mountain'
|
||||
|
||||
/** 0..1 from a field's coordinates: the same field, the same value. */
|
||||
export function fieldHash(x: number, y: number): number {
|
||||
let h = (x * 73856093) ^ (y * 19349663)
|
||||
h = Math.imul(h ^ (h >>> 13), 1274126177)
|
||||
return ((h ^ (h >>> 16)) >>> 0) / 4294967296
|
||||
}
|
||||
|
||||
const smooth = (t: number) => t * t * (3 - 2 * t)
|
||||
|
||||
/** Smooth value noise over patches of `size` fields: neighbouring fields get similar values. */
|
||||
export function fieldNoise(x: number, y: number, size: number, seed: number): number {
|
||||
const gx = x / size
|
||||
const gy = y / size
|
||||
const ix = Math.floor(gx)
|
||||
const iy = Math.floor(gy)
|
||||
const fx = smooth(gx - ix)
|
||||
const fy = smooth(gy - iy)
|
||||
const at = (a: number, b: number) => fieldHash(a * 7 + seed * 101, b * 13 - seed * 37)
|
||||
const top = at(ix, iy) + (at(ix + 1, iy) - at(ix, iy)) * fx
|
||||
const bottom = at(ix, iy + 1) + (at(ix + 1, iy + 1) - at(ix, iy + 1)) * fx
|
||||
return top + (bottom - top) * fy
|
||||
}
|
||||
|
||||
/** Patch size and seed of each kind of ground, and where its noise turns the field. */
|
||||
export const TERRAIN = {
|
||||
water: { size: 11, seed: 1, below: 0.17 },
|
||||
/** Hills from `above`; their bunched-up cores, from `mountain`, are mountains. */
|
||||
hill: { size: 8, seed: 2, above: 0.8, mountain: 0.9 },
|
||||
/** Forest patches from `above`, and single trees scattered on `scattered` of the open land. */
|
||||
forest: { size: 6, seed: 3, above: 0.6, scattered: 0.05 },
|
||||
} as const
|
||||
|
||||
/** How deep in a forest a field lies, 0..1: deeper in, more and bigger trees. */
|
||||
export const forestDepth = (x: number, y: number) => fieldNoise(x, y, TERRAIN.forest.size, TERRAIN.forest.seed)
|
||||
|
||||
/** The ground of a field. A village's field is always open land: the map draws it as grass. */
|
||||
export function groundAt(x: number, y: number): Ground {
|
||||
if (fieldNoise(x, y, TERRAIN.water.size, TERRAIN.water.seed) < TERRAIN.water.below) return 'water'
|
||||
const hill = fieldNoise(x, y, TERRAIN.hill.size, TERRAIN.hill.seed)
|
||||
if (hill > TERRAIN.hill.mountain) return 'mountain'
|
||||
if (hill > TERRAIN.hill.above) return 'hill'
|
||||
if (forestDepth(x, y) > TERRAIN.forest.above || fieldHash(x, y) < TERRAIN.forest.scattered) return 'forest'
|
||||
return 'grass'
|
||||
}
|
||||
|
||||
/** Flat colours of the map's ground: grass in four worn shades, the lake and its shore. */
|
||||
export const GRASS = ['#6b7049', '#676c46', '#6f724c', '#73704b']
|
||||
export const WATER = '#4a5b5d'
|
||||
export const SHORE = '#7a6a50'
|
||||
|
||||
/** One colour per ground kind, for the overview where a field is a few pixels. */
|
||||
export const GROUND_COLOURS: Record<Ground, string> = {
|
||||
grass: '#656a45',
|
||||
forest: '#3f4b31',
|
||||
water: WATER,
|
||||
hill: '#7b7769',
|
||||
mountain: '#96918a',
|
||||
}
|
||||
Loading…
Reference in New Issue