Skip to content

Add a unit type ​

A unit type is an entry of units in your rules. Its look is the view's, under the same id.

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

export const UNIT = defineIds('scout', 'archer')
export type Unit = IdOf<typeof UNIT>

export const rules: Rules = {
	id: 'my-game',
	name: 'My game',
	units: [
		{ id: UNIT.scout, name: 'Scout', kind: 'unit', radius: 0.4, stats: { health: 100, moveSpeed: 5 }, sight: 10 },
		{
			id: UNIT.archer,
			name: 'Archer',
			kind: 'unit',
			radius: 0.4,
			sight: 9,
			hotkey: 'A',
			// health, speed, and the attack's damage, rate and reach
			stats: { health: 80, moveSpeed: 4, attackDamage: 12, attackSpeed: 1.2, attackRange: 6 },
			// an attack makes it fight: pick-target range, when the hit lands in the swing, arrow speed
			attacks: [{ acquisitionRange: 8, damagePoint: 0.4, projectileSpeed: 20, damageType: 'physical' }],
			cost: { gold: 90 }
		}
	],
	victory: null
}

Done: spawn one with world.spawn(UNIT.archer, { at, owner: 1 }), train it from a building (trains: [UNIT.archer]), or place it in the editor.

Give it a look ​

ts
// in src/view.ts
export const view: GameView<Unit> = {
	units: {
		looks: {
			archer: {
				height: 1.6,
				model: { src: 'archer.glb', scale: 0.8 }
			}
		}
	},
	// sounds by type: files in the game's folder (sounds/bow.mp3)
	sounds: { types: { archer: { attack: 'bow', select: 'archer-ready', death: 'archer-dies' } } }
}

No model yet? Draw it from shapes, as the empty starter's scout does. See Bring in a model.

Give it icons ​

ts
// in src/view.ts
import type { GameView } from '@v12-engine/client'

export const view: Pick<GameView, 'hud'> = {
	hud: {
		icons: {
			units: { archer: 'icons/archer.png' },
			commands: { 'train:archer': 'icons/train-archer.png' }
		}
	}
}

Tell the computer what it is for ​

The AI guesses a role from the definition. Override it when the guess is wrong:

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

export const archer: UnitTypeDef = {
	id: 'archer',
	name: 'Archer',
	kind: 'unit',
	radius: 0.4,
	sight: 9,
	stats: { health: 80, moveSpeed: 4, attackDamage: 12, attackSpeed: 1.2, attackRange: 6 },
	attacks: [{ acquisitionRange: 8, damagePoint: 0.4, projectileSpeed: 20, damageType: 'physical' }],
	cost: { gold: 90 },
	// `view.roles.explain('archer')` in a brain says what it would have guessed
	ai: { role: 'soldier', tags: ['ranged'], counters: ['air'], priority: 1 }
}

Finish a unit: the checklist ​

PartWherePage
Definitionunits (or game.unit(…)) in src/rules.tsthis page
What makes ittrains / builds of another type, cost, requiresAdd a building
Looklooks of the view, or looks.json from the editorBring in a model
Plate over its headunitHud of the view, or hud.jsonChange the plate over a unit
Balancing numbersRules.editable, kept in data.jsonThe Data panel
AI roleai of the definitionAdd a computer player
Soundssounds.types of the view, files in sounds/Add sounds and music
Iconshud.icons of the viewabove
Teststests/rules.test.tsTest my rules

Run npm test and npm run typecheck after each part, then Play: select it, order it, hear it, kill it. A look named for a type the rules don't have is reported as the page loads it.

The example's card tests catch two buttons on one key or slot (apps/example/tests/card.test.ts). Its card snapshot (apps/example/tests/cardSize.test.ts) fails when a new unit adds a button: check the diff, then run KEEP_CARDS=1 npx vitest run cardSize.

In the editor ​

  1. Open the Code panel and click New with code (or New › With code in the Data panel). It writes a stub and opens it.
  2. Find the type in the Place panel under its group, and click the map to put one down.
  3. Drop a model on the Assets panel and click Set up as a unit to write its scale, turn and clips into looks.json.
  4. Edit its numbers in the Data panel (the fields Rules.editable allows) and click Keep.

Reference ​

FieldWhat it does
idThe type's id. Name ids once with defineIds, then write UNIT.archer.
kindunit, building or resource.
radiusIts size on the ground, in cells.
sightHow far it sees, in cells.
statsThe numbers rules read (Stats): health, mana, moveSpeed, attackDamage…
attacksIts attacks. A second one is for air or has its own numbers (Who fights whom).
costWhat training it takes, by resource.
hotkeyThe key of its button on the card.
basedOnStart from another type's fields.
aiIts role for computer players.

See Rules and their look for how rules and view meet by id, and UnitTypeDef in the API reference.