Skip to content

3. An item and a shop ​

Goal: a potion, a sword and boots, sold by a trader on the road. An item is a bonus while carried, or an ability used from the bag with charges.

Add items ​

ts
// src/campaign/units.ts
export const items: ItemDef[] = [
	{
		id: 'healingPotion',
		name: 'Healing Potion',
		description: 'Drunk, heals 200. Three in the bottle',
		cost: { gold: 60 },
		charges: 3,
		ability: 'drinkPotion'
	},
	{ id: 'steelSword', name: 'Steel Sword', description: '+12 attack damage', cost: { gold: 180 }, bonus: { stats: { attackDamage: 12 } } },
	{ id: 'swiftBoots', name: 'Swift Boots', description: '+0.8 move speed', cost: { gold: 120 }, bonus: { stats: { moveSpeed: 0.8 } } }
]

bonus is a StatChange (flat or percent) while carried. ability names the ability used from the slot, charges times:

ts
{
	id: 'drinkPotion',
	name: 'Drink',
	description: 'Heals 200 at once',
	target: 'none',
	cooldown: 1,
	effects: [{ type: 'heal', amount: 200, on: 'caster' }]
}

An item can also be passive, or basedOn another; stock makes a shop restock it.

Add a shop ​

ts
export const trader = building({
	id: UNIT.trader,
	name: 'Trader',
	radius: 1.2,
	footprint: { w: 2, d: 2 },
	stats: { health: 1000 },
	invulnerable: true,
	sight: 8,
	sells: ['healingPotion', 'steelSword', 'swiftBoots']
})

Place the trader for the neutral player (0) by the road. Walk the knight near it: the shop panel lists what it sells; a click buys for the selected unit.

Buy in code ​

ts
world.issue({ type: 'buy', player: PLAYER, shop: trader.id, item: 'steelSword' })
giveItem(world, hero, createItem(world, 'steelSword')) // no shop

Dropping, picking up and selling back are commands too. Items carry over to the next mission in the hero's record (items: [{ item, charges }, null, …]).

Next: 4. The HUD and a test.