Skip to content

Add a building ​

A building is a unit type of kind building with a footprint. A worker lists it in builds; the building lists what it trains.

ts
// in src/rules.ts
import { defineIds, type Rules } from '@v12-engine/engine'

export const UNIT = defineIds('peasant', 'archer', 'barracks', 'farm')

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	resources: [{ id: 'gold', name: 'Gold', start: 500 }],
	settings: { supply: { pools: { food: { name: 'Food', max: 100 } } } },
	units: [
		{
			id: UNIT.peasant,
			name: 'Peasant',
			kind: 'unit',
			radius: 0.4,
			sight: 8,
			stats: { health: 60, moveSpeed: 4 },
			// what it builds: the buildings by id, in the order its card shows them
			builds: [UNIT.farm, UNIT.barracks],
			supply: { food: 1 }
		},
		{
			id: UNIT.archer,
			name: 'Archer',
			kind: 'unit',
			radius: 0.4,
			sight: 9,
			stats: { health: 80, moveSpeed: 4 },
			cost: { gold: 90 },
			supply: { food: 1 }
		},
		{
			id: UNIT.farm,
			name: 'Farm',
			kind: 'building',
			radius: 1,
			sight: 6,
			// cells it covers (buildings sit on whole cells), how it comes up, how long, what it costs
			footprint: { w: 2, d: 2 },
			construction: 'builder',
			buildTime: 30,
			cost: { gold: 80 },
			stats: { health: 400 },
			// what it gives: room for eight units' food
			supplyGiven: { food: 8 }
		},
		{
			id: UNIT.barracks,
			name: 'Barracks',
			kind: 'building',
			radius: 1.5,
			sight: 8,
			footprint: { w: 3, d: 3 },
			construction: 'builder',
			buildTime: 50,
			cost: { gold: 160 },
			stats: { health: 900 },
			// a farm first, then it can be built; what it trains
			requires: [UNIT.farm],
			trains: [UNIT.archer]
		}
	],
	victory: null
}

Done: select a peasant, build a farm, then a barracks, then train an archer. Rally points, queues and construction sites come with the engine.

Upgrade a building in place ​

Give it upgradesTo to turn a town hall into a keep where it stands. Give a list for a choice: upgradesTo: ['castle', 'citadel'] puts a button for each on the card.

Make it sell, hire or research ​

sells (items), hires (mercenaries) and researches (upgrades) work like trains: a list of ids, a button each. See Add a resource, supply and tech for upgrades.

Show it going up ​

The view's construction on the building's look draws each stage: another model, nodes shown and hidden, an effect and a sound as each begins.

ts
construction: {
	stages: [
		{ at: 0, model: 'burrow/foundation.glb', fx: 'dust' },
		{ at: 0.4, model: 'burrow/scaffold.glb', sound: 'hammering' },
		// the finished model from here, its roof shown from 80%
		{ at: 0.6, hide: ['roof'] },
		{ at: 0.8, show: ['roof'] }
	],
	clip: 'birth',
	motion: 'reveal',
	working: { fx: 'spark-burst', at: 'handslot.r', sound: 'hammer' },
	done: { fx: 'dust', sound: 'built' },
	cancelled: { fx: 'dust', look: 'fold' },
	upgrade: { stages: [{ at: 0, model: 'burrow/upgrading.glb' }] }
}

motion is grow, rise, fade, reveal (a glowing cut from the ground up) or none. working plays at the builder's hand on each blow. GameView.units.constructionFor sets it for every building of a race, and construction: (progress, { entity, root, kit, dt, upgrade }) => … draws it your own way.

The whole process, rules and look, from the worker's order to the finished building (builders, cancelling, repair, upgrades, events, stages, scaffolding): Build a building: the process, start to finish. How it comes down: Break a building to pieces.

In the editor ​

  1. Place the building from the Place panel. It snaps to whole cells.
  2. In the Inspector, turn on Still to be built for a site that workers have to finish (underConstruction).
  3. The editor will not put a building on water, off the map or over something else. The Problems panel flags what stands in deep water.

Reference ​

FieldWhat it does
footprintCells it covers: { w, d }.
constructionbuilder (a worker builds it), self (builds itself once placed) or instant.
buildTimeSeconds to build.
requiresTypes the player must own first.
providesTypes this one counts as in someone else's requires.
trainsUnits it trains. Gives it a queue and a rally point.
supplyGivenSupply room it adds.
upgradesToType (or types) it turns into. The upgradeBuilding command picks one with into.

See Add a unit type for the fields every type has, and Economy for production.