Items
An item is data: what it costs, what it gives whoever carries it, what it does when used, and what it is built from. Items are Rules.items; heroes carry them (inventory on the unit type gives the slots), shops sell them (sells on a building), what dies drops them (drops). The engine brings a whole tree of fifty you can take as it is, change, or leave: The default set. Everything here is a field with a default, a setting, or a hook where a game wants its own rule.
- Consumables: charges, stacks, refills, power-ups
- Legendaries and uniques: unique effects, groups, passives that change play
- Tags: giving, naming, filtering and searching by them
- Shops and the shop window: stock, prices, the window, undo
- The default set: use it, change some, replace it, in code, in
data.jsonand in the editor - Every default item, made from the engine's data
Quick start
import { heroesPack, type Rules } from '@v12-engine/engine'
export const rules: Rules = {
id: 'my-game',
name: 'My game',
units: [
{
id: 'knight',
name: 'Knight',
kind: 'unit',
radius: 0.5,
sight: 10,
stats: { health: 600, attackDamage: 30, attackSpeed: 1, attackRange: 0.6, moveSpeed: 4 },
inventory: 6,
hero: { growth: { stats: { health: 50 } }, learns: [] }
},
{ id: 'trader', name: 'Trader', kind: 'building', radius: 1.5, sight: 6, stats: { health: 900 }, invulnerable: true, sells: ['ironSword', 'brutalAxe'] }
],
items: [
{ id: 'ironSword', name: 'Iron Sword', tier: 'basic', cost: { gold: 350 }, bonus: { stats: { attackDamage: 15 } }, tags: ['attack'] },
{
id: 'brutalAxe',
name: 'Brutal Axe',
description: '+40 attack damage',
tier: 'intermediate',
cost: { gold: 1100 },
from: ['ironSword', 'ironSword'],
bonus: { stats: { attackDamage: 40 } },
tags: ['attack']
}
],
resources: [{ id: 'gold', name: 'Gold', start: 500 }],
currency: 'gold',
packs: [heroesPack()]
}The knight walks up to the trader and buys an Iron Sword for 350. A Brutal Axe is two swords and 400 more: carrying one sword, the knight pays 750, and the sword becomes part of the axe.
Common tasks
Make an item
{ id: 'stormblade', name: 'Stormblade', tier: 'basic', cost: { gold: 400 }, bonus: { stats: { attackDamage: 15 } }, ability: 'stormStrike' }bonus: stats while carried (stats, flat;percent, shares).ability: cast when the item is used, for no mana. Itshotkeyis the item's.passive: an ability whose passive part works while carried: triggers, auras (Triggers and passives).description: the tooltip. The shop shows it with the tags and what the item builds into.
Build one item from others
from lists the parts. The price is for the whole: parts the buyer carries are used up and taken off, and so are the parts of parts it carries (priceOf). A recipe that costs less than its parts together is flagged by the rules' check (building it would pay the buyer).
{ id: 'kingsedge', tier: 'legendary', cost: { gold: 3200 }, from: ['recurveBow', 'ironSword', 'keenGlass'] }Two of the same part: from: ['ironSword', 'ironSword']. A loop (an item among its own parts, however far down) is refused by the check.
Give items tiers
tier is where an item sits in a shop and in the tree:
| Tier | What it is |
|---|---|
consumable | Used up: potions, wards, scrolls (Consumables) |
starter | What a hero buys first |
basic | The parts: one thing each |
intermediate | Built from the basics, with the first passives. epic, the old word, is still read as this (itemTier) |
legendary | What the tree leads to: a unique effect each (Legendaries) |
relic | Loot of camps, bosses and quests: never sold |
Any other word is a tier of the game's own: the shop and the item tree list it after these. hud.shopTiers on the view names and orders what the shop shows.
Sell it in a shop
sells: ['ironSword'] on a building makes it a shop; a hero standing near buys (Shops). stock: { max: 2, restock: 60 } limits how many it has.
Drop it as loot
{ id: 'ogre', drops: [{ item: 'emberIdol', chance: 0.1 }, { item: 'healingDraught', chance: 0.4 }] }
// or drops: (world, entity, killer) => ['emberIdol']Relics are made for this: a passive no shop sells, found at a camp or a boss. GameMap.itemTables draws random drops by an item's level and class.
Limit how many of a kind a hero carries
group: 'boots' with groupLimit: 1 (or the game's settings.items.groups): one pair, whatever the boots. Buying, picking up or being handed one more is refused and the player told why (Groups).
Give an item in code
import { createItem, giveItem, carryRefusal, hasRoomFor } from '@v12-engine/engine'
if (hasRoomFor(world, hero, 'ironSword') && !carryRefusal(world, hero, 'ironSword')) giveItem(world, hero, createItem(world, 'ironSword'))createItem(world, id, charges?) makes an instance; giveItem puts it in a free slot or a stack of it; takeItem takes one out; pickUp and dropOnGround move it between the ground and a carrier; sell sells it at a shop near. carryRefusal is the engine's rule of what may be carried (a group's limit, then the game's own mayCarry), with the reason as a message.
Make items fit every kind of game
An item's words and rules name general things: a hero, a creep, a structure, an ally, a kill. Never a lane, a minion or a tower. Where a game has those, they match through its units' own tags (a minion is a unit tagged minion), so the same item works in a melee game, a campaign and a lanes map. The creeps of a camp belong to the creeps player, everyone's enemy: an effect with affects: 'enemies' reaches them (Creeps).
Check the tree
The rules' check (checkRules, the editor's Problems) names, for each item:
- a part that is no item, an ability or a passive that is none;
- a loop in the build tree;
- a price under the sum of its parts;
- an item nothing sells, builds or drops (in a game that sells or drops any);
- a
groupLimitunder 1 or without agroup; astackor arefillon an item with no charges; asellRateunder 0; auniquewith nopassive; - a recipe that breaks its own group limit.
Editor
The Data panel edits items as any definition, and the Item tree draws the tree by tier with prices and problems, drag to add a part, search and filter by tag or calling: Data panel, tech tree and item tree. The Tags list names tags and gives them a look; Default sets chooses what the game takes of the engine's items.
Reference
ItemDef
| Field | What it does |
|---|---|
id, name | Its id (give it a prefix of the game's) and what the player sees |
description | The tooltip |
cost | What it costs in a shop, by resource |
tier | consumable, starter, basic, intermediate (epic read as it), legendary, relic, or a word of the game's |
from | The items it is made of; those carried are used up and taken off the price |
bonus | Stats while carried (stats, percent) |
ability | Cast when used, for no mana |
passive | An ability whose passive part works while carried |
unique | The name of its passive: two items sharing it work once (Legendaries) |
group, groupLimit | At most so many of the group per carrier (boots: 1) |
charges | Uses before it is gone (Consumables) |
stack | How many of it go in one slot, their charges added |
refill | How its charges come back: { shop: true }, { every: 60 } |
sellRate | The part of its price a shop gives back, over the game's economy.sellPrice |
stock | How many a shop has (max) and the seconds to get one back (restock) |
tags | Free words for what it is: searched, filtered, named by a TagDef (Tags) |
suits | The callings it is meant for: the shop's suggestions and filter, never a lock |
hotkey | The key that uses it from the command card |
powerUp | Used as it is picked up, never carried (a rune, a tome; the runes pack) |
level, class | For random drop tables (GameMap.itemTables) |
basedOn | The definition this is a copy of, for its look |
Item rules (ItemSettings, settings.items)
| Setting | Default | What it does |
|---|---|---|
groups | { boots: 1 } | The most of a group one carrier may have, by group; an item's own groupLimit comes first |
unique | true | Whether passives sharing a unique name work once for all of them |
mayCarry | The game's own rule, asked after the engine's: a reason to refuse, or nothing |
Functions
| Function | What it does |
|---|---|
priceOf(registry, item, carried) | The price for someone carrying these, and the parts it uses up |
itemTier(def) | The tier, epic read as intermediate |
itemsTagged(registry, tag) | The items with a tag |
carryRefusal(world, carrier, item, uses?) | Why it may not be carried, or nothing |
groupRefusal(registry, carried, item, uses?) | The group's part of it, from the registry alone (for a screen) |
groupLimitOf(world, def) | The most of its group a carrier may have |
hasRoomFor(world, entity, item) | A free slot, or a stack with room |
createItem, giveItem, takeItem, findItem | Make and move instances |
pickUp, dropOnGround | The ground |
buy, sell, shopNear, undoBuy | Shops (Shops) |
Hooks and events
beforeBuy (cost, or cancel with a reason), beforePickUp and beforeDrop (cancelled with a reason). Events: inventoryChanged, itemPicked, itemUndone. Messages the engine tells the player: inventoryFull, groupLimit, theirGroupLimit, outOfStock, noCharges, nothingToUndo.
Tests
packages/engine/tests/items.test.ts (buying, recipes, selling), packages/engine/tests/itemRules.test.ts (groups, stacks, refills, uniques, the checks), packages/engine/tests/defaultItems.test.ts (the default tree, each legendary in a melee, a campaign and a lanes game).