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.
// 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
// 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
// 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:
// 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
| Part | Where | Page |
|---|---|---|
| Definition | units (or game.unit(…)) in src/rules.ts | this page |
| What makes it | trains / builds of another type, cost, requires | Add a building |
| Look | looks of the view, or looks.json from the editor | Bring in a model |
| Plate over its head | unitHud of the view, or hud.json | Change the plate over a unit |
| Balancing numbers | Rules.editable, kept in data.json | The Data panel |
| AI role | ai of the definition | Add a computer player |
| Sounds | sounds.types of the view, files in sounds/ | Add sounds and music |
| Icons | hud.icons of the view | above |
| Tests | tests/rules.test.ts | Test 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
- Open the Code panel and click New with code (or New › With code in the Data panel). It writes a stub and opens it.
- Find the type in the Place panel under its group, and click the map to put one down.
- Drop a model on the Assets panel and click Set up as a unit to write its scale, turn and clips into
looks.json. - Edit its numbers in the Data panel (the fields
Rules.editableallows) and click Keep.
Reference
| Field | What it does |
|---|---|
id | The type's id. Name ids once with defineIds, then write UNIT.archer. |
kind | unit, building or resource. |
radius | Its size on the ground, in cells. |
sight | How far it sees, in cells. |
stats | The numbers rules read (Stats): health, mana, moveSpeed, attackDamage… |
attacks | Its attacks. A second one is for air or has its own numbers (Who fights whom). |
cost | What training it takes, by resource. |
hotkey | The key of its button on the card. |
basedOn | Start from another type's fields. |
ai | Its role for computer players. |
See Rules and their look for how rules and view meet by id, and UnitTypeDef in the API reference.