feat: soldiers stand guard at the doors: two at the headquarters while troops are in the village, one or two at every other door with a big army; in the close-up too

Guards wear a helmet and carry a shield and spear; soldiers stepping out of a
military building look the same.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-29 16:43:23 +01:00
parent 818da1328f
commit ac87ed3618
7 changed files with 84 additions and 11 deletions

View File

@ -1,5 +1,6 @@
<script setup lang="ts">
import type { Building, BuildingEffect, Cost, Resources } from '~/types/game'
import { garrisonOf } from '~/diorama/scene'
const props = defineProps<{
building: Building
@ -26,6 +27,7 @@ const state = computed(() => {
demolishing: running?.action === 'demolish',
queued: orders.length > 0,
training: !!village?.military.find((m) => m.building_id === props.building.id)?.queue.length,
garrison: village ? garrisonOf(village) : 0,
}
})

View File

@ -64,7 +64,8 @@ const SHIRTS = ['#6b4f3a', '#7b1e16', '#3d5a26', '#57391f', '#5a5f63']
/>
</template>
<g v-else-if="l.life" :transform="`translate(${l.life.at[0]} ${l.life.at[1]}) scale(2.6)`">
<VillageWalker :shirt="SHIRTS[i % SHIRTS.length]!" :facing="l.life.facing" :tool="l.life.tool" :swing="motion && l.life.tool ? (l.life.drill ? drilling : hammering) : null" />
<VillageWalker v-if="l.life.guard" shirt="#57391f" :facing="l.life.facing" tool="spear" soldier />
<VillageWalker v-else :shirt="SHIRTS[i % SHIRTS.length]!" :facing="l.life.facing" :tool="l.life.tool" :swing="motion && l.life.tool ? (l.life.drill ? drilling : hammering) : null" />
</g>
<g v-else v-memo="[l]" stroke-width="0.6" stroke-linejoin="round">
<polygon v-for="(p, j) in l.polys" :key="j" :points="p.points" :fill="p.fill" :stroke="p.stroke" />

View File

@ -47,6 +47,8 @@ onBeforeUnmount(() => observer?.disconnect())
const motion = ref(false)
onMounted(() => (motion.value = !window.matchMedia('(prefers-reduced-motion: reduce)').matches))
const SHIRTS = ['#6b4f3a', '#7b1e16', '#3d5a26', '#57391f', '#5a5f63']
/** Soldiers wear the village colour under the shield. */
const UNIFORM = '#57391f'
const name = (b: SceneBuilding) => t(`buildings.${b.type}`)
const label = (b: SceneBuilding) => t('village.diorama.label', { name: name(b), level: b.level })
@ -196,9 +198,13 @@ const timers = computed(() => {
<polygon v-for="(p, i) in obj.polys" :key="i" :points="p.points" :fill="p.fill" :stroke="p.stroke" />
<title>{{ label(obj.building) }}</title>
</g>
<!-- Villagers who stay put: talking, or hammering at a building in the works. -->
<!-- Villagers who stay put: talking, hammering at a building in the works, drilling;
soldiers standing guard at the doors. -->
<g v-else-if="obj.life" class="pointer-events-none" :transform="`translate(${obj.life.at[0]} ${obj.life.at[1]})`">
<g transform="scale(1.6)">
<g v-if="obj.life.guard" transform="scale(1.6)">
<VillageWalker :shirt="UNIFORM" :facing="obj.life.facing" tool="spear" soldier />
</g>
<g v-else transform="scale(1.6)">
<animateTransform
v-if="motion && !obj.life.tool"
attributeName="transform"
@ -228,7 +234,8 @@ const timers = computed(() => {
:indoors="obj.walker.indoors"
:tool="obj.walker.tool"
:carry="obj.walker.carry"
:shirt="SHIRTS[obj.walker.shirt % SHIRTS.length]!"
:soldier="obj.walker.soldier"
:shirt="obj.walker.soldier ? UNIFORM : SHIRTS[obj.walker.shirt % SHIRTS.length]!"
:size="1.6"
:begin="obj.walker.begin"
/>

View File

@ -15,6 +15,8 @@ const props = defineProps<{
begin?: number
/** Which way it faces while standing: 1 right, -1 left. */
facing?: 1 | -1
/** A soldier: an iron helmet instead of hair, a shield in front. */
soldier?: boolean
}>()
const shirtShade = computed(() => scale(props.shirt, 0.72))
@ -74,7 +76,12 @@ const HEADS: Record<Tool, string> = {
<path d="M-1.9 -6.9L0 -7.3V-2.2H-1.7z" :fill="shirt" />
<path d="M0 -7.3L1.9 -6.9L1.7 -2.2H0z" :fill="shirtShade" />
<circle cy="-8.9" r="1.65" fill="#d8a982" />
<path d="M-1.7 -9.1Q0 -11.5 1.7 -9.1Q0 -9.9 -1.7 -9.1z" fill="#4a3220" />
<template v-if="soldier">
<path d="M-1.85 -9Q-1.8 -11.2 0 -11.3Q1.8 -11.2 1.85 -9z" :fill="METAL" />
<path d="M0 -11.3Q1.8 -11.2 1.85 -9H0z" fill="#6f7478" />
<path d="M0.75 -9H1.15V-7.9H0.75z" fill="#6f7478" />
</template>
<path v-else d="M-1.7 -9.1Q0 -11.5 1.7 -9.1Q0 -9.9 -1.7 -9.1z" fill="#4a3220" />
<g v-if="tool">
<animateTransform
@ -92,6 +99,12 @@ const HEADS: Record<Tool, string> = {
<path :d="HEADS[tool]" :fill="METAL" />
</g>
<g v-if="soldier">
<path d="M-0.1 -6.9H2.5V-4.7Q2.5 -3.1 1.2 -2.1Q-0.1 -3.1 -0.1 -4.7z" fill="#7b1e16" />
<path d="M1.2 -6.9H2.5V-4.7Q2.5 -3.1 1.2 -2.1z" fill="#5c1610" />
<path d="M1 -6.9H1.4V-2.3L1.2 -2.1L1 -2.3z" fill="#d6c7a0" />
</g>
<g v-if="carry" :visibility="cycle ? 'hidden' : 'visible'">
<animate v-if="cycle" attributeName="visibility" calcMode="discrete" :values="cycle.load.values" :keyTimes="cycle.load.keyTimes" :dur="`${cycle.dur}s`" :begin="begin" repeatCount="indefinite" />
<path d="M-0.6 -9.3L2.2 -9.8L3.8 -9L1 -8.5z" :fill="carryTop" />

View File

@ -17,6 +17,7 @@ const props = defineProps<{
rest?: number
/** Home is a building: out of sight while resting there. */
indoors?: boolean
soldier?: boolean
}>()
/** Faces right when the way out leads right on screen. */
@ -31,7 +32,7 @@ const cycle = computed(() => workCycle(props.walk, props.work, props.rest ?? 1.2
<template>
<g>
<g :transform="`scale(${size})`">
<VillageWalker :shirt="shirt" :carry="carry" :tool="tool" :cycle="cycle" :begin="begin" />
<VillageWalker :shirt="shirt" :carry="carry" :tool="tool" :cycle="cycle" :begin="begin" :soldier="soldier" />
</g>
<animateMotion
:dur="`${cycle.dur}s`"

View File

@ -7,7 +7,7 @@ import type { BuildingType } from '~/types/game'
import { box, DOOR, type Face, hash, ngon, plane, type Poly, project, pyramid, rect, scale, toPolys, type V2, type V3 } from './iso'
import type { Tool } from './life'
import { broadleaf, buildingModel, bush, C, conifer, rock } from './models'
import { disc, ground, shadow, U, wallSegments } from './scene'
import { disc, ground, guardSpots, guardsAt, shadow, U, wallSegments } from './scene'
export interface Closeup {
viewBox: string
@ -22,7 +22,7 @@ export interface Closeup {
layers: (
| { key: string; polys: Poly[]; walk?: undefined; life?: undefined }
| { key: string; walk: Walk; polys?: undefined; life?: undefined }
| { key: string; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean }; polys?: undefined; walk?: undefined }
| { key: string; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean; guard?: boolean }; polys?: undefined; walk?: undefined }
)[]
/** Sheep at a farm, chickens round the core buildings. */
herds: { kind: 'sheep' | 'chicken'; d: string; dur: number; begin: number }[]
@ -48,6 +48,8 @@ export interface CloseupState {
demolishing?: boolean
queued?: boolean
training?: boolean
/** Units standing in the village, for the guards at the door. */
garrison?: number
}
type Kind = 'timber' | 'clay' | 'iron' | 'farm' | 'wall' | 'core'
@ -194,9 +196,11 @@ export function buildCloseup(type: BuildingType, level: number, workers: number,
}
// A builder works at the building while an order runs: a hammer to build, a pick to tear
// down. Any order for it piles materials in front, and a porter brings more.
const life: { key: string; depth: number; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean } }[] = []
const person = (key: string, [x, y]: V2, facing: 1 | -1, tool: Tool | null, drill = false) =>
life.push({ key, depth: x + y, life: { at: screen([x, y]), facing, tool, drill } })
const life: { key: string; depth: number; life: { at: V2; facing: 1 | -1; tool: Tool | null; drill?: boolean; guard?: boolean } }[] = []
const person = (key: string, [x, y]: V2, facing: 1 | -1, tool: Tool | null, drill = false, guard = false) =>
life.push({ key, depth: x + y, life: { at: screen([x, y]), facing, tool, drill, guard } })
// Soldiers stand guard at the door, as in the village.
if (door) guardSpots(type, door, guardsAt(type, state.garrison ?? 0), U * 1.6).forEach((p, k) => person(`guard${k}`, p, 1, 'spear', false, true))
if ((state.upgrading || state.demolishing) && kind !== 'wall') person('builder', at(deg(15), S * 1.35), -1, state.demolishing ? 'pick' : 'hammer')
if ((state.queued || state.upgrading) && kind !== 'wall') {
const pile = at(deg(80), S * 1.45)

View File

@ -77,6 +77,8 @@ export interface SceneWalker {
rest: number
/** Out of sight while resting: they live in the building they come out of. */
indoors: boolean
/** A soldier: helmet and shield. */
soldier?: boolean
tool: Tool | null
carry: string | null
shirt: number
@ -91,6 +93,8 @@ export interface SceneLife {
shirt: number
/** A recruit thrusting the spear in drill, instead of a tool at work. */
drill?: boolean
/** A soldier standing guard: helmet, shield and spear, standing still. */
guard?: boolean
}
/** What each zone's workers work with and bring home. */
@ -146,6 +150,36 @@ export interface Scene {
wallBand: { id: number; ring: { cx: number; cy: number; rx: number; ry: number }; width: number } | null
}
/** Units in the village (own and support) from which every other door gets a guard, and two. */
const DOOR_GUARDS_FROM = 100
const DOUBLE_GUARDS_FROM = 400
/** Units standing in the village: its own and support from elsewhere. */
export function garrisonOf(village: Pick<VillageState, 'troops' | 'support'>): number {
const count = (units: Partial<Record<string, number>>) => Object.values(units).reduce((n: number, c) => n + (c ?? 0), 0)
return count(village.troops ?? {}) + village.support.here.reduce((n, g) => n + count(g.units), 0)
}
/**
* Soldiers standing guard at a building's door: two at the headquarters as soon as there
* are troops, one at every other door from a big army on, two from a bigger one.
*/
export function guardsAt(type: BuildingType, garrison: number): number {
if (garrison <= 0 || ZONE_OF[type]) return 0
if (type === 'headquarters') return 2
return garrison >= DOUBLE_GUARDS_FROM ? 2 : garrison >= DOOR_GUARDS_FROM ? 1 : 0
}
/** Where guards stand from the point outside a door (facing +x): a step out, to either side. */
export function guardSpots(type: BuildingType, [x, y]: V2, n: number, unit: number): V2[] {
const out = type === 'headquarters' ? 0.55 : 0.3
const side = type === 'headquarters' ? 0.75 : 0.6
return [
[x + unit * out, y + unit * side],
[x + unit * out, y - unit * side],
].slice(0, n) as V2[]
}
/** Buildings that smoke, from this level on. */
const SMOKY: Partial<Record<BuildingType, number>> = { headquarters: 1, barracks: 1, workshop: 1, academy: 1, clay_pit: 1, iron_mine: 5 }
const WATER = '#5f7f87'
@ -856,6 +890,16 @@ export function buildScene(village: VillageState, frame: number | { width: numbe
people(`instructor-${b.id}`, slot.x + slot.r * 1.15 + U * 1.1, slot.y + slot.r * 0.2, -1, null, 4)
}
// Troops at home stand guard at the doors, more doors the bigger the army.
const garrison = garrisonOf(village)
for (const b of village.buildings) {
const door = doorOf.get(b.id)
if (!door) continue
guardSpots(b.type, door, guardsAt(b.type, garrison), U).forEach(([x, y], k) =>
objects.push({ key: `guard-${b.id}-${k}`, depth: x + y, polys: [], life: { at: project([x, y, 1.2]), facing: 1, tool: 'spear', shirt: 1, guard: true } }),
)
}
// Villagers step out of their buildings now and then, stand a moment, and go back in.
const homes = village.buildings.filter((b) => doorOf.has(b.id) && !ZONE_OF[b.type]).slice(0, 6)
homes.forEach((b, k) => {
@ -868,6 +912,7 @@ export function buildScene(village: VillageState, frame: number | { width: numbe
rest: 6 + hash(b.id, 10) * 10,
indoors: true,
tool: soldier ? 'spear' : null,
soldier,
carry: b.type === 'storage' ? C.plank : null,
shirt: b.id,
begin: -k * 4.3,