game-village-politics-frontend/app/diorama/models.ts

459 lines
17 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Low-poly building models for the diorama, one per type and level tier. Each fits in
// the square [-s, s] around its own origin and returns its faces and roof height.
// Original shapes in the genre's style: tents and lean-tos at level 0, timber next,
// stone and bigger at high levels.
import type { BuildingType } from '~/types/game'
import { box, type Face, hash, house, log, ngon, plane, prism, pyramid, rect, shift, type V2, type V3 } from './iso'
export const C = {
wood: '#b98552',
woodDark: '#8d5f36',
plank: '#cfa16a',
thatch: '#d9b65f',
canvas: '#ece2ca',
canvasDark: '#d7c6a2',
roofRed: '#b9573c',
slate: '#6a7482',
stone: '#b9b3a8',
stoneDark: '#948e85',
field: '#a8c65e',
wheat: '#e3c45f',
furrow: '#c9a24e',
soil: '#9c744a',
leaf: '#5f9c46',
leafDark: '#4b8439',
trunk: '#7b5333',
clay: '#cc7a4f',
clayDark: '#a65a37',
clayWet: '#b9683f',
rock: '#8e8983',
ore: '#5e6a78',
fire: '#f4a53b',
banner: '#b9443a',
hay: '#e6cb6c',
cloth: '#7a5f4a',
skin: '#e7bb92',
dark: '#3e342b',
grass: '#86b456',
}
export interface Model {
faces: Face[]
/** Highest point, for the level badge. */
height: number
}
/** 0 at level 0, 1 for 1–4, 2 for 5–14, 3 from 15 on. */
export const tier = (level: number) => (level <= 0 ? 0 : level < 5 ? 1 : level < 15 ? 2 : 3)
// Small props
function post(x: number, y: number, h: number, r: number, color = C.woodDark): Face[] {
return prism(rect(x, y, r * 2, r * 2), 0, h, color)
}
function banner(x: number, y: number, h: number, size: number, color = C.banner): Face[] {
return [
...post(x, y, h, size * 0.035),
plane(
[
[x, y, h],
[x - size * 0.05, y + size * 0.75, h - size * 0.1],
[x - size * 0.05, y + size * 0.75, h - size * 0.5],
[x, y, h - size * 0.42],
],
color,
),
]
}
function crate(x: number, y: number, z: number, a: number): Face[] {
return box(x, y, a, a, z, a, C.plank, C.wood)
}
export function tree(x: number, y: number, size: number, seed = 0): Face[] {
const leaf = hash(seed, 3) > 0.5 ? C.leaf : C.leafDark
const rot = hash(seed, 7) * Math.PI
return [
...prism(ngon(x, y, size * 0.09, 5, rot), 0, size * 0.32, C.trunk),
...pyramid(ngon(x, y, size * 0.48, 6, rot), size * 0.25, size * 0.75, leaf),
...pyramid(ngon(x, y, size * 0.34, 6, rot + 0.4), size * 0.68, size * 0.62, leaf),
]
}
export function rock(x: number, y: number, size: number, seed = 0): Face[] {
const base = ngon(x, y, size * 0.5, 5, hash(seed, 1) * 6).map(([px, py], i): V2 => {
const k = 0.75 + 0.45 * hash(seed, i)
return [x + (px - x) * k, y + (py - y) * k]
})
return pyramid(base, 0, size * (0.45 + 0.3 * hash(seed, 9)), C.rock, [x + size * 0.08, y - size * 0.06])
}
function person(x: number, y: number, s: number, shirt = C.cloth): Face[] {
const r = s * 0.06
return [...prism(ngon(x, y, r, 5), 0, s * 0.2, shirt), ...box(x, y, r * 1.3, r * 1.3, s * 0.2, r * 1.4, C.skin)]
}
function field(x: number, y: number, w: number, d: number, color: string, rows: number): Face[] {
const faces: Face[] = [plane(rect(x, y, w, d).map(([px, py]) => [px, py, 0.6]), color)]
for (let i = 1; i < rows; i++) {
const fx = x - w / 2 + (w * i) / rows
faces.push(plane([[fx - 0.5, y - d / 2 + 1, 0.8], [fx + 0.5, y - d / 2 + 1, 0.8], [fx + 0.5, y + d / 2 - 1, 0.8], [fx - 0.5, y + d / 2 - 1, 0.8]], C.furrow))
}
return faces
}
function logPile(x: number, y: number, s: number, rows = 2): Face[] {
const r = s * 0.09
const faces: Face[] = []
for (let row = 0; row < rows; row++) {
const n = 3 - row
for (let i = 0; i < n; i++) faces.push(...log(x, y - (n - 1) * r + i * r * 2, row * r * 1.7, s * 0.7, r, C.wood, C.plank))
}
return faces
}
function bricks(x: number, y: number, s: number, layers: number): Face[] {
const faces: Face[] = []
for (let i = 0; i < layers; i++) faces.push(...box(x, y, s * 0.3, s * 0.2, i * s * 0.08, s * 0.08, i % 2 ? C.clayDark : C.clay))
return faces
}
function fire(x: number, y: number, s: number): Face[] {
const faces: Face[] = []
for (const [px, py] of ngon(x, y, s * 0.12, 6)) faces.push(...box(px, py, s * 0.05, s * 0.05, 0, s * 0.04, C.rock))
faces.push(...pyramid(ngon(x, y, s * 0.07, 4), 0, s * 0.2, C.fire).map((f) => ({ ...f, flat: true })))
return faces
}
function kiln(x: number, y: number, s: number): Face[] {
return [
...prism(ngon(x, y, s * 0.32, 7), 0, s * 0.4, C.clay, C.clayDark),
...pyramid(ngon(x, y, s * 0.32, 7), s * 0.4, s * 0.28, C.clayDark),
...box(x + s * 0.05, y - s * 0.05, s * 0.1, s * 0.1, s * 0.5, s * 0.35, C.stoneDark),
]
}
function headframe(x: number, y: number, s: number, h: number): Face[] {
const a = s * 0.28
return [
...post(x - a, y - a, h, s * 0.04),
...post(x + a, y - a, h, s * 0.04),
...post(x - a, y + a, h, s * 0.04),
...post(x + a, y + a, h, s * 0.04),
...box(x, y, a * 2.3, a * 2.3, h, s * 0.06, C.wood),
...pyramid(rect(x, y, a * 2.3, a * 2.3), h + s * 0.06, s * 0.25, C.woodDark),
]
}
// Buildings
const models: Record<BuildingType, (s: number, t: number) => Model> = {
headquarters(s, t) {
if (t === 0) {
return {
faces: [
...pyramid(ngon(0, 0, s * 0.62, 6, 0.3), 0, s * 1.0, C.canvas),
...pyramid(ngon(s * 0.05, s * 0.05, s * 0.2, 6, 0.3), s * 0.85, s * 0.25, C.canvasDark),
...fire(s * 0.62, s * 0.58, s),
...banner(-s * 0.6, s * 0.55, s * 1.25, s * 0.6),
],
height: s * 1.25,
}
}
if (t === 1) {
return {
faces: [
...house(0, 0, s * 1.35, s * 1.0, s * 0.5, s * 0.5, C.wood, C.thatch),
...banner(s * 0.85, -s * 0.6, s * 1.25, s * 0.55),
...fire(s * 0.7, s * 0.72, s),
],
height: s * 1.25,
}
}
if (t === 2) {
return {
faces: [
...box(0, 0, s * 1.6, s * 1.2, 0, s * 0.14, C.stoneDark, C.stone),
...shift(house(0, 0, s * 1.45, s * 1.05, s * 0.5, s * 0.55, C.plank, C.roofRed), 0, 0, s * 0.14),
...box(-s * 0.62, -s * 0.48, s * 0.5, s * 0.5, 0, s * 1.25, C.stone),
...pyramid(rect(-s * 0.62, -s * 0.48, s * 0.6, s * 0.6), s * 1.25, s * 0.45, C.roofRed),
...banner(s * 0.85, s * 0.7, s * 1.3, s * 0.55),
],
height: s * 1.7,
}
}
return {
faces: [
...box(0, 0, s * 1.35, s * 1.35, 0, s * 0.9, C.stone, C.stoneDark),
...pyramid(rect(0, 0, s * 1.5, s * 1.5), s * 0.9, s * 0.65, C.slate),
...prism(ngon(-s * 0.72, s * 0.72, s * 0.28, 6), 0, s * 1.3, C.stone),
...pyramid(ngon(-s * 0.72, s * 0.72, s * 0.34, 6), s * 1.3, s * 0.45, C.slate),
...prism(ngon(s * 0.72, -s * 0.72, s * 0.28, 6), 0, s * 1.3, C.stone),
...pyramid(ngon(s * 0.72, -s * 0.72, s * 0.34, 6), s * 1.3, s * 0.45, C.slate),
...banner(0, 0, s * 2.0, s * 0.6),
],
height: s * 2.0,
}
},
storage(s, t) {
if (t === 0) {
const back = s * 0.85
const front = s * 0.5
return {
faces: [
...post(-s * 0.6, -s * 0.5, back, s * 0.05),
...post(s * 0.6, -s * 0.5, back, s * 0.05),
...post(-s * 0.6, s * 0.5, front, s * 0.05),
...post(s * 0.6, s * 0.5, front, s * 0.05),
plane([[-s * 0.75, -s * 0.6, back], [s * 0.75, -s * 0.6, back], [s * 0.75, s * 0.62, front], [-s * 0.75, s * 0.62, front]], C.plank),
...crate(-s * 0.25, 0, 0, s * 0.3),
...crate(s * 0.15, s * 0.05, 0, s * 0.3),
...crate(-s * 0.05, -s * 0.05, s * 0.3, s * 0.26),
...pyramid(ngon(s * 0.75, s * 0.75, s * 0.12, 5), 0, s * 0.22, C.canvasDark),
],
height: back,
}
}
if (t === 1) return { faces: [...house(0, 0, s * 1.15, s * 1.0, s * 0.55, s * 0.4, C.wood, C.woodDark, 'y'), ...crate(s * 0.75, s * 0.7, 0, s * 0.28)], height: s * 0.95 }
if (t === 2) {
return {
faces: [
...house(0, -s * 0.1, s * 1.5, s * 1.1, s * 0.75, s * 0.55, C.plank, C.roofRed),
plane([[s * 0.76, -s * 0.28, 0], [s * 0.76, s * 0.08, 0], [s * 0.76, s * 0.08, s * 0.45], [s * 0.76, -s * 0.28, s * 0.45]], C.dark),
...crate(s * 0.55, s * 0.78, 0, s * 0.26),
...crate(s * 0.2, s * 0.8, 0, s * 0.26),
],
height: s * 1.3,
}
}
return {
faces: [
...house(-s * 0.1, -s * 0.1, s * 1.55, s * 1.25, s * 0.85, s * 0.55, C.stone, C.slate),
...house(s * 0.7, s * 0.65, s * 0.55, s * 0.5, s * 0.4, s * 0.25, C.wood, C.woodDark),
...crate(-s * 0.55, s * 0.8, 0, s * 0.26),
],
height: s * 1.4,
}
},
farm(s, t) {
if (t === 0) {
return {
faces: [
...field(0, 0, s * 1.5, s * 1.2, C.wheat, 5),
...person(-s * 0.2, s * 0.15, s),
...person(s * 0.35, -s * 0.2, s, C.soil),
...pyramid(ngon(s * 0.85, s * 0.75, s * 0.13, 6), 0, s * 0.16, C.hay),
],
height: s * 0.4,
}
}
if (t === 1) {
return {
faces: [
...field(0, s * 0.3, s * 1.6, s * 1.0, C.wheat, 6),
...house(-s * 0.35, -s * 0.65, s * 0.7, s * 0.55, s * 0.4, s * 0.35, C.wood, C.thatch),
...person(s * 0.3, s * 0.3, s),
],
height: s * 0.8,
}
}
if (t === 2) {
return {
faces: [
...field(-s * 0.4, s * 0.35, s * 1.05, s * 1.1, C.wheat, 4),
...field(s * 0.55, s * 0.35, s * 0.75, s * 1.1, C.field, 3),
...house(-s * 0.2, -s * 0.65, s * 0.9, s * 0.6, s * 0.45, s * 0.4, C.plank, C.roofRed),
...pyramid(ngon(s * 0.65, -s * 0.6, s * 0.22, 7), 0, s * 0.42, C.hay),
],
height: s * 0.85,
}
}
return {
faces: [
...field(-s * 0.3, s * 0.45, s * 1.3, s * 0.95, C.wheat, 5),
...field(s * 0.7, s * 0.45, s * 0.55, s * 0.95, C.field, 2),
...house(-s * 0.3, -s * 0.6, s * 1.1, s * 0.7, s * 0.6, s * 0.45, C.roofRed, C.slate),
...prism(ngon(s * 0.65, -s * 0.6, s * 0.22, 7), 0, s * 1.1, C.stone),
...pyramid(ngon(s * 0.65, -s * 0.6, s * 0.26, 7), s * 1.1, s * 0.3, C.slate),
],
height: s * 1.4,
}
},
timber_camp(s, t) {
if (t === 0) {
return {
faces: [...logPile(0, s * 0.2, s, 2), ...prism(ngon(-s * 0.55, -s * 0.4, s * 0.12, 6), 0, s * 0.14, C.wood, C.plank), ...tree(s * 0.55, -s * 0.55, s * 1.1, 1)],
height: s * 1.1,
}
}
if (t === 1) {
return {
faces: [
...logPile(s * 0.2, s * 0.45, s, 2),
...post(-s * 0.75, -s * 0.15, s * 0.6, s * 0.04),
...post(-s * 0.15, -s * 0.15, s * 0.6, s * 0.04),
...post(-s * 0.75, -s * 0.75, s * 0.75, s * 0.04),
...post(-s * 0.15, -s * 0.75, s * 0.75, s * 0.04),
plane([[-s * 0.85, -s * 0.85, s * 0.75], [-s * 0.05, -s * 0.85, s * 0.75], [-s * 0.05, -s * 0.05, s * 0.6], [-s * 0.85, -s * 0.05, s * 0.6]], C.woodDark),
...tree(s * 0.6, -s * 0.55, s * 1.1, 2),
...tree(-s * 0.7, s * 0.65, s * 0.9, 3),
],
height: s * 1.1,
}
}
if (t === 2) {
return {
faces: [
...house(-s * 0.2, -s * 0.35, s * 1.1, s * 0.8, s * 0.5, s * 0.4, C.wood, C.plank),
...logPile(s * 0.25, s * 0.6, s, 2),
...tree(s * 0.7, -s * 0.6, s * 1.1, 4),
...tree(-s * 0.75, s * 0.7, s, 5),
],
height: s * 1.1,
}
}
return {
faces: [
...house(-s * 0.15, -s * 0.3, s * 1.35, s * 0.95, s * 0.6, s * 0.45, C.plank, C.roofRed),
...logPile(-s * 0.3, s * 0.7, s, 3),
...box(s * 0.55, s * 0.6, s * 0.5, s * 0.25, 0, s * 0.18, C.plank),
...tree(s * 0.75, -s * 0.7, s * 1.2, 6),
],
height: s * 1.2,
}
},
clay_pit(s, t) {
const pit = (x: number, y: number, r: number): Face[] => [
plane(ngon(x, y, r, 7, 0.2).map(([px, py]) => [px, py, 0.4]), C.clayDark),
plane(ngon(x, y, r * 0.65, 7, 0.5).map(([px, py]) => [px, py, 0.6]), C.clayWet),
]
if (t === 0) {
return {
faces: [...pit(-s * 0.1, s * 0.1, s * 0.6), ...pyramid(ngon(s * 0.6, -s * 0.55, s * 0.3, 6), 0, s * 0.3, C.clay), ...person(s * 0.15, -s * 0.3, s)],
height: s * 0.4,
}
}
if (t === 1) {
return {
faces: [...pit(-s * 0.2, s * 0.2, s * 0.6), ...bricks(s * 0.6, -s * 0.45, s, 4), ...bricks(s * 0.25, -s * 0.7, s, 3), ...person(s * 0.4, s * 0.3, s)],
height: s * 0.45,
}
}
if (t === 2) {
return {
faces: [...pit(-s * 0.3, s * 0.3, s * 0.55), ...kiln(s * 0.35, -s * 0.35, s), ...bricks(s * 0.65, s * 0.55, s, 5)],
height: s * 0.85,
}
}
return {
faces: [
...pit(-s * 0.35, s * 0.4, s * 0.5),
...kiln(s * 0.45, -s * 0.15, s),
...kiln(-s * 0.35, -s * 0.55, s),
...bricks(s * 0.65, s * 0.65, s, 6),
],
height: s * 0.85,
}
},
iron_mine(s, t) {
const outcrop = (k: number) => pyramid(ngon(-s * 0.2, -s * 0.25, s * 0.75 * k, 6, 0.4), 0, s * 0.95 * k, C.rock, [-s * 0.35, -s * 0.45])
const mouth = plane([[s * 0.32, -s * 0.35, 0], [s * 0.32, -s * 0.05, 0], [s * 0.2, -s * 0.05, s * 0.3], [s * 0.2, -s * 0.35, s * 0.3]], C.dark)
const ore = (x: number, y: number) => pyramid(ngon(x, y, s * 0.18, 5), 0, s * 0.18, C.ore)
if (t === 0) return { faces: [...outcrop(1), mouth, ...ore(s * 0.55, s * 0.5)], height: s * 0.95 }
if (t === 1) {
return {
faces: [
...outcrop(1),
mouth,
...post(s * 0.36, -s * 0.4, s * 0.38, s * 0.04),
...post(s * 0.36, 0, s * 0.38, s * 0.04),
...box(s * 0.36, -s * 0.2, s * 0.08, s * 0.5, s * 0.38, s * 0.06, C.woodDark),
...box(s * 0.6, s * 0.55, s * 0.3, s * 0.2, s * 0.04, s * 0.12, C.woodDark, C.ore),
...ore(s * 0.1, s * 0.6),
],
height: s * 0.95,
}
}
if (t === 2) return { faces: [...outcrop(1.05), mouth, ...headframe(s * 0.45, s * 0.35, s, s * 1.0), ...ore(-s * 0.5, s * 0.6)], height: s * 1.3 }
return {
faces: [
...outcrop(1.15),
mouth,
...headframe(s * 0.5, s * 0.25, s, s * 1.15),
...box(-s * 0.45, s * 0.6, s * 0.55, s * 0.45, 0, s * 0.4, C.stone),
...box(-s * 0.3, s * 0.5, s * 0.12, s * 0.12, s * 0.4, s * 0.5, C.stoneDark),
{ pts: ngon(-s * 0.3, s * 0.5, s * 0.05, 4).map(([x, y]): V3 => [x, y, s * 0.92]), color: C.fire, flat: true },
],
height: s * 1.45,
}
},
hiding_place(s, t) {
if (t === 0) return { faces: pyramid(ngon(0, 0, s * 0.62, 7), 0, s * 0.85, C.hay), height: s * 0.85 }
const door = plane([[s * 0.45, -s * 0.2, 0], [s * 0.45, s * 0.2, 0], [s * 0.45, s * 0.2, s * 0.35], [s * 0.45, -s * 0.2, s * 0.35]], C.dark)
if (t === 1) return { faces: [...pyramid(rect(0, 0, s * 1.2, s * 1.2), 0, s * 0.55, C.grass), door], height: s * 0.55 }
return {
faces: [...box(0, 0, s * 1.0, s * 1.0, 0, s * 0.4, C.stone), ...pyramid(rect(0, 0, s * 1.1, s * 1.1), s * 0.4, s * 0.3, C.grass), shift([door], s * 0.06, 0)[0]!],
height: s * 0.7,
}
},
rally_point(s, t) {
const stones: Face[] = ngon(0, 0, s * 0.6, 8).flatMap(([x, y]) => box(x, y, s * 0.14, s * 0.14, 0, s * 0.1, C.stone))
if (t === 0) return { faces: [...stones, ...banner(0, 0, s * 1.6, s * 0.8)], height: s * 1.6 }
return {
faces: [
...box(0, 0, s * 0.9, s * 0.9, 0, s * 0.15, C.plank, C.wood),
...banner(-s * 0.3, -s * 0.3, s * 1.8, s * 0.8),
...banner(s * 0.3, s * 0.3, s * 1.5, s * 0.7, C.slate),
],
height: s * 1.8,
}
},
// The wall is drawn as the ring around the plateau, see scene.ts.
wall: () => ({ faces: [], height: 0 }),
}
export function buildingModel(type: BuildingType, level: number, s: number): Model {
return models[type](s, tier(level))
}
/** A queued new building: posts, planks and its outline on the ground. */
export function scaffold(s: number): Model {
const a = s * 0.6
return {
faces: [
plane(rect(0, 0, a * 2.1, a * 2.1).map(([x, y]) => [x, y, 0.5]), C.soil, 0.5),
...post(-a, -a, s * 0.75, s * 0.035),
...post(a, -a, s * 0.75, s * 0.035),
...post(-a, a, s * 0.75, s * 0.035),
...post(a, a, s * 0.75, s * 0.035),
...box(0, a, a * 2.1, s * 0.05, s * 0.45, s * 0.05, C.plank),
...box(a, 0, s * 0.05, a * 2.1, s * 0.6, s * 0.05, C.plank),
...crate(-a * 0.3, a * 0.2, 0, s * 0.22),
],
height: s * 0.8,
}
}
/** A small crane beside a building that is being upgraded or taken down. */
export function crane(s: number, height: number): Face[] {
const x = s * 0.85
const y = -s * 0.85
const h = height + s * 0.35
return [
...post(x, y, h, s * 0.045, C.woodDark),
...box(x - s * 0.3, y, s * 0.75, s * 0.06, h, s * 0.06, C.wood),
...box(x - s * 0.6, y, s * 0.02, s * 0.02, h - s * 0.4, s * 0.4, C.dark),
...crate(x - s * 0.6, y, h - s * 0.55, s * 0.14),
]
}