Add resources, supply and tech
Resources are what things cost. Supply is a cap that units count against and buildings raise. Tech is what must exist first (requires) and the upgrades a building researches.
ts
// in src/rules.ts
import { defineIds, type Rules } from '@v12-engine/engine'
export const UNIT = defineIds('peasant', 'archer', 'farm', 'barracks', 'forge')
export const UPGRADE = defineIds('sharpArrows')
export const rules: Rules = {
id: 'my-game',
name: 'My game',
// what things cost: gold from the start, lumber none; the first is the currency the HUD counts first
resources: [
{ id: 'gold', name: 'Gold', start: 500 },
{ id: 'lumber', name: 'Lumber', start: 150 }
],
// supply: every unit takes room, farms give it, and nobody goes past a hundred
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 },
cost: { gold: 60 },
supply: { food: 1 },
builds: [UNIT.farm, UNIT.barracks, UNIT.forge]
},
{
id: UNIT.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 }],
cost: { gold: 90, lumber: 20 },
supply: { food: 1 }
},
{
id: UNIT.farm,
name: 'Farm',
kind: 'building',
radius: 1,
sight: 6,
footprint: { w: 2, d: 2 },
construction: 'builder',
buildTime: 30,
cost: { gold: 80, lumber: 20 },
stats: { health: 400 },
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, lumber: 60 },
stats: { health: 900 },
// tech: a farm first
requires: [UNIT.farm],
trains: [UNIT.archer]
},
{
id: UNIT.forge,
name: 'Forge',
kind: 'building',
radius: 1.5,
sight: 8,
footprint: { w: 3, d: 3 },
construction: 'builder',
buildTime: 60,
cost: { gold: 140, lumber: 80 },
stats: { health: 700 },
requires: [UNIT.barracks],
// what it researches
researches: [UPGRADE.sharpArrows]
}
],
upgrades: [
{
id: UPGRADE.sharpArrows,
name: 'Sharp arrows',
cost: { gold: 100, lumber: 50 },
researchTime: 45,
maxLevel: 2,
// what it does, for whom: more damage on archers, a level at a time
effects: [{ units: [UNIT.archer], stats: { attackDamage: 4 } }]
}
],
victory: null
}Done: players start with 500 gold and 150 lumber, farms raise food, the barracks needs a farm, and the forge researches Sharp arrows for +4 archer damage a level.
Gather a resource from the map
Make a type of kind resource, give workers harvester and give a building dropOff. Or give resources in code through world.players.get(id).resources. See Economy.
Reference
| Field | On | What it does |
|---|---|---|
resources | rules | { id, name, start } each. The first is the HUD's main one. |
cost | a type, an upgrade | What it takes, by resource. |
settings.supply.pools | rules | Supply pools with their max. |
supply | a unit | Room it takes. |
supplyGiven | a building | Room it adds. |
upkeep | rules | Tiers that take a share of income past so many units. |
requires | a type, an upgrade | What the player must have built. |
provides | a building | Counts as another type in requires. |
researches | a building | Upgrades it researches. |
upgrades[].effects | an upgrade | Stat changes (flat or percent) on the types named, per level. apply runs your own code. |
In the editor, Map settings › Players sets what each player starts with, and the Data panel shows costs and research times your game marks editable.