2. Units, a building, a hero
Goal: peasants, footmen, archers, a hall and barracks, the enemy's raiders, and a knight who levels up, all drawn with the shared art.
Name the ids once
// src/campaign/ids.ts
import { defineIds, type IdOf, type PlayerId } from '@v12-engine/engine'
export const UNIT = defineIds(
'peasant',
'footman',
'archer',
'knight',
'hall',
'farm',
'barracks',
'tower',
'wall',
'trader',
'raider',
'warlock',
'brute',
'warlord',
'gnoll',
'prisoner',
'tree',
'goldmine'
)
export type CampaignUnit = IdOf<typeof UNIT>
/** Who is who: the player at the screen, and the Red Hand */
export const PLAYER: PlayerId = 1
export const ENEMY: PlayerId = 2A typo in UNIT.hall fails to compile. The view is typed by the same ids (GameView<CampaignUnit>), so a unit without a look fails too.
Add a worker, a soldier and buildings
// src/campaign/units.ts
export const peasant: UnitTypeDef = {
id: UNIT.peasant,
name: 'Peasant',
kind: 'unit',
radius: 0.4,
stats: { health: 220, attackDamage: 6, attackSpeed: 0.6, attackRange: 0.4, moveSpeed: 4 },
sight: 8,
attacks: [{ damagePoint: 0.4, acquisitionRange: 0 }],
cost: { gold: 60 },
buildTime: 8,
harvester: { capacity: 10, gatherTime: 1.5 },
builds: [UNIT.farm, UNIT.barracks, UNIT.tower, UNIT.wall],
hotkey: 'P'
}
export const footman: UnitTypeDef = {
id: UNIT.footman,
name: 'Footman',
kind: 'unit',
radius: 0.45,
stats: { health: 420, attackDamage: 14, attackSpeed: 1.1, attackRange: 0.5, armor: 12, moveSpeed: 4 },
sight: 10,
attacks: [{ damagePoint: 0.45, acquisitionRange: 8 }],
cost: { gold: 90 },
buildTime: 12,
hotkey: 'F'
}
const building = (def: Omit<UnitTypeDef, 'kind'>): UnitTypeDef => ({ kind: 'building', ...def })
export const hall = building({
id: UNIT.hall,
name: 'Town Hall',
radius: 2,
footprint: { w: 4, d: 4 },
stats: { health: 2200, armor: 40 },
sight: 12,
trains: [UNIT.peasant],
dropOff: true,
hotkey: 'H'
})
export const barracks = building({
id: UNIT.barracks,
name: 'Barracks',
radius: 1.5,
footprint: { w: 3, d: 3 },
stats: { health: 1600, armor: 30 },
sight: 9,
cost: { gold: 180 },
buildTime: 25,
trains: [UNIT.footman, UNIT.archer],
hotkey: 'B'
})A harvester gathers from a resource (the gold mine) and returns it to a building with dropOff. builds and trains become buttons on the card. The archer is the footman with range and projectileSpeed; the tower is a building with an attack.
Add the enemy
const enemy = (def: Omit<UnitTypeDef, 'kind' | 'sight'> & Partial<Pick<UnitTypeDef, 'sight'>>): UnitTypeDef => ({ kind: 'unit', sight: 8, ...def })
export const raider = enemy({
id: UNIT.raider,
name: 'Raider',
radius: 0.45,
stats: { health: 380, attackDamage: 15, attackSpeed: 1, attackRange: 0.5, armor: 8, moveSpeed: 4 },
attacks: [{ damagePoint: 0.45, acquisitionRange: 8 }],
level: 1,
bounty: [6, 10],
xp: 24
})bounty is gold for the kill, xp the hero's reward. Warlocks, brutes and Varga the warlord follow the same pattern.
Add the hero
export const knight: UnitTypeDef = {
id: UNIT.knight,
name: 'Knight',
kind: 'unit',
radius: 0.5,
stats: { health: 650, healthRegen: 1, mana: 200, manaRegen: 1.5, attackDamage: 42, attackSpeed: 0.8, attackRange: 0.6, armor: 30, moveSpeed: 4.4 },
sight: 12,
attacks: [{ damagePoint: 0.4, acquisitionRange: 8 }],
hero: {
growth: { stats: { health: 80, mana: 30, attackDamage: 3, armor: 3 } },
learns: ['holyLight', 'warCry']
},
inventory: 6,
level: 1,
hotkey: 'K'
}
export const abilities: AbilityDef[] = [
{
id: 'holyLight',
name: 'Holy Light',
description: 'Heals a friendly unit for 150, 250 or 350',
hotkey: 'Q',
target: 'unit',
targets: { allies: true, self: true },
manaCost: 50,
cooldown: 6,
range: 8,
castTime: 0.3,
effects: [{ type: 'heal', amount: [150, 250, 350] }]
},
{
id: 'warCry',
name: 'War Cry',
description: 'Friends within 7 hit harder and faster for 10 seconds',
hotkey: 'W',
target: 'none',
manaCost: 60,
cooldown: 20,
effects: [{ type: 'buff', buff: 'rallied', radius: 7, affects: 'allies', duration: 10 }]
}
]A hero block says how he grows each level and what he learns. Both abilities use the engine's effects: heal, and a buff over a radius (buffs: [{ id: 'rallied', percent: { attackDamage: 0.2, attackSpeed: 0.15 } }]).
Put it all in the rules
// src/campaign/index.ts
export const campaign: Rules = layData(
{
id: 'my-game',
name: 'My game',
units,
abilities,
buffs,
items,
resources: [{ id: 'gold', name: 'Gold', start: 300 }],
currency: 'gold',
tiles,
maps: Object.values(maps),
victory: objectiveVictory,
saves: { version: 1 },
editable: { units: (def) => (def.kind === 'building' ? ['name', { path: 'stats.health', min: 1 }, 'stats.armor'] : null) }
},
dataFile
)victory: objectiveVictory: missions are won by objectives, not by who has buildings left. layData(…, dataFile) lays the editor's Data panel changes (data.json) over the code; editable says which fields.
Draw them
// src/campaign/view/index.ts
const character = (name: string, scale: number, own: Partial<ModelDef> = {}): ModelDef => ({ src: `characters/${name}.glb`, scale, ...own })
const hold = (name: string, hand: 'l' | 'r' = 'r') => ({ src: `gear/${name}.glb`, at: `handslot.${hand}` })
const building = (src: string, size: [number, number, number], width: number): ModelDef => ({ src, scale: width / Math.max(size[0], size[2]) })
export const looks: UnitLooks<CampaignUnit> = {
footman: { height: 1.8, model: character('knight', 0.7, { hold: [hold('sword_1handed'), hold('shield_round', 'l')] }), shapes: person(1.8, 'team') },
knight: {
height: 2.2,
model: character('knight', 0.9, { hold: [hold('sword_2handed')], animations: { attack: 'Melee_2H_Attack_Chop' } }),
shapes: person(2.2, 'team')
},
hall: {
height: 4.4,
model: building('rts/castle.glb', [6.31, 2.81, 5.87], 4.4),
shapes: [{ shape: 'box', size: [3.8, 2.6, 3.8], offset: [0, 1.3, 0], color: STONE }]
}
// …
}
export const campaignView: GameView<CampaignUnit, CampaignResource, CampaignMessage> = layFiles(
{
assets: 'games/campaign',
looks,
tiles: TILE_LOOKS,
effects: { holyLight: { color: [1, 0.95, 0.45] }, rallied: { color: [1, 0.6, 0.2], aura: 'glow' } }
// …
},
[[LOOKS_FILE, looksFile]]
)shapes draw a unit while its model loads. layFiles lays the editor's Set up as a unit results (looks.json) over the code. See Bring in a model.
Try it
- Open the editor. Place lists every type.
- Put down a hall, a barracks, a mine and peasants for player 1, a few raiders for player 2.
- Click Play here. Peasants dig, the barracks trains, raiders attack. Nothing wins yet: that's a mission's job.
Next: 3. Mission one: the base.