Party & battles
Party members with stats, levels and equipment, turn-based battles and difficulty levels.
Party, stats, levels, equipment
// src/content/stats.ts — the stats (typed everywhere), slots and the XP curve
export default defineStats({
stats: {
hp: { name: 'HP', pool: true }, // pool = has a current value (12 / 30)
mp: { name: 'MP', pool: true },
attack: { name: 'Attack' }, defense: { name: 'Defense' }, magic: { name: 'Magic' },
resistance: { name: 'Resistance' }, speed: { name: 'Speed' }, luck: { name: 'Luck' },
},
elements: { fire: { name: 'Fire', color: '#ff7043' }, ice: { name: 'Ice' } }, // damage types, see Battles
slots: { weapon: tx('Weapon'), armor: tx('Armor'), accessory: tx('Accessory') },
maxLevel: 50,
xpForLevel: (level) => Math.round(8 * (level - 1) ** 2 + 12 * (level - 1)), // total XP for a level
maxMembers: 4,
})
// characters/hero/index.ts — who can join, and their numbers
defineCharacter({ name: 'Hero', battle: { base: { hp: 32, attack: 6 }, growth: { hp: 6, attack: 1.5 }, skills: ['power_strike'] } })
// items/wooden_sword/index.ts — wearable
defineItem({ name: 'Wooden sword', price: 30, max: 1, equip: { slot: 'weapon', stats: { attack: 3 } } })
// equip can also take elements: { fire: 0.5 }, immune: ['sleep'], skills: ['fire']
game.party.add('anna', { level: 2, notify: true }); game.party.remove('anna')
game.party.members(); game.party.has('anna')
game.party.giveXp('party', 50, { notify: true }) // everyone (or one id); returns { anna: 1 } levels gained
game.party.stat('hero', 'attack') // base + growth × (level − 1) + equipment, rounded down
game.party.current('hero', 'hp'); game.party.change('hero', 'hp', -5); game.party.restore()
game.party.equip('hero', 'wooden_sword'); game.party.unequip('hero', 'weapon')
game.party.member('hero') // level, xp, nextLevelXp, stats, current, equipment
const hero = useMember('hero') // React; usePartyMembers() for the list
- A level up raises the max and adds the new points to current HP/MP (it doesn't heal fully).
- Equipping takes the item out of the inventory; what was in the slot goes back.
- Members who leave keep their level and gear for when they come back.
- The hero joins in
save/index.ts(newGame, andafterLoadfor older saves). Anna joins from her event once her relationship is 10+. - Window
party(pause menu): HP/MP/XP bars, stats, equipment pickers.
Battles
Turn-based fights on a timeline: fast fighters act more often, and the strip at the top shows who is next. Everything you tune is in src/content: enemies, skills, troops, statuses.ts, battle.ts (settings and formulas) and stats.ts (stats and elements).
const result = await game.battles.start({ enemies: ['slime', 'bat'], canFlee: false }) // 'win' | 'lose' | 'flee'
await game.battles.start('wolf_pack') // a troop, see below
| Rule | |
|---|---|
| Turns | Each fighter's next turn comes after 1000 / (speed + 10) × the action's delay. Speed 20 acts about twice as often as speed 5. The strip previews the order while a skill is focused or being aimed. |
| Damage | attack × power − defense / 2, ±10%, at least 1. Spells use magic against resistance. ×1.5 on a crit, × the target's element rate, halved when defending. |
| Hit and crit | Hit: 95% ± 1% per point of speed difference (70–100%). Crit: 5% + 1% per luck (up to 50%). |
| Commands | Attack, Skills, Items, Defend, Check (look at any fighter: HP, statuses and what they do; costs no turn), Run. |
| Run | Better odds when the party is faster (10–95%). Off with canFlee: false. |
| After | Win: XP to the party, money, drops. HP/MP carry over to the map; statuses end with the fight. Lose: onLose in content/battle.ts. |
Skills and effects
// src/content/skills/fire/index.ts
export default defineSkill({
name: 'Fire', description: 'Burns every enemy a little.',
mp: 5,
target: 'all_enemies', // enemy | all_enemies | ally | all_allies | self | fallen_ally
element: 'fire', // from content/stats.ts `elements`
delay: 1, // 1.5 = the user's next turn comes later, 0.5 = sooner
effects: [
{ type: 'damage', power: 1.1, uses: 'magic', against: 'resistance' },
{ type: 'status', status: 'burn', chance: 0.3 },
],
})
| Effect | |
|---|---|
damage | power (× the user's uses stat, default attack, against the target's against, default defense) or flat (fixed, for items). Optional element, drain (share returned as HP), canMiss, canCrit. pure: true = nothing on the target changes it: no defense or resistance stat, no element rate (weak, resist, immune), no defending. A miss stops the skill's other effects on that target. |
heal | amount + percent of the target's max + power × the user's magic. pool: 'mp' restores MP. |
status | Gives status with a chance (default 1); turns overrides its length. |
cure | Removes one status, or 'bad' for every harmful one. |
revive | Brings a fallen target back with percent of its HP (use target fallen_ally). |
delay | Moves the target's next turn: amount: 0.5 = half a turn later, negative = sooner. |
custom | run: ({ battle, actor, target, random }) => { … } for anything else. |
Every effect also takes chance (its own roll, 0–1). By default effects are chained to the hit: after a damage effect misses, the ones after it are skipped for that target. Add evenOnMiss: true to make one separate, so it runs (and rolls its own chance) either way.
effects: [
{ type: 'damage', power: 0.7 },
{ type: 'delay', amount: 0.5 }, // chained: only if the hit landed
{ type: 'status', status: 'slow', chance: 0.3, evenOnMiss: true }, // separate: 30% whether it hit or not
]
Battle items use the same list: defineItem({ battle: { target: 'ally', effects: [{ type: 'heal', amount: 20 }, { type: 'cure', status: 'bad' }] } }). One is used up.
Worn equipment can act by itself with the same effects, on its wearer: onStart once when the fight begins, onTurn at the start of each of their turns.
// src/content/items/cursed_ring/index.ts
equip: {
slot: 'accessory', stats: { magic: 2 },
battle: {
onStart: [{ type: 'status', status: 'strong' }],
onTurn: [{ type: 'heal', amount: 6 }, { type: 'status', status: 'poison', turns: 1 }], // heals, but keeps you poisoned
},
}
Who has which skill
// characters/hero/index.ts — from the start, at a level, or once a quest is done
battle: { skills: ['trip', { skill: 'power_strike', level: 3 }, { skill: 'smite', quest: 'jacks_letter' }], /* … */ }
// items/wooden_sword/index.ts — while the sword is worn
equip: { slot: 'weapon', stats: { attack: 3 }, skills: ['slash'] }
// from an event, a quest's onComplete, anywhere: taught for good (saved)
game.party.learn('anna', 'fire', { notify: true }) // "Anna learned Fire!"; false if she knew it
game.party.forget('anna', 'fire'); game.party.knows('anna', 'fire'); game.party.skills('anna')
- Conditions are checked whenever the list is read, so a battle always starts with the current skills. With
notify,giveXpannounces skills that come with the new level. learnworks for anyone who can be in the party, also before they join or after they left.- Take the sword off and its skill is gone;
forgetonly removes skills given withlearn.
Statuses and elements
// src/content/statuses.ts — all of them in one file
export default defineStatuses({
poison: { name: 'Poison', icon: '☠', color: '#ab47bc', bad: true, turns: [3, 5], tick: { hp: -0.08 } },
sleep: { name: 'Sleep', icon: 'z', bad: true, turns: [2, 3], restrict: 'skip', wakeOnHit: true },
haste: { name: 'Haste', icon: '»', turns: 4, stats: { speed: 1.6 } },
})
| Field | |
|---|---|
turns | How many of the fighter's own turns it lasts: a number or [min, max]. Giving it again refreshes it. |
tick | Share of max HP / MP at the start of each own turn (-0.08 = loses 8%, positive = regains). |
stats | Stat multipliers while it lasts. Buffs and debuffs are statuses. |
restrict | 'skip' loses the turn, 'attack_random' attacks anyone, 'no_skills'. |
wakeOnHit, bad, icon, description | Ends when damaged; harmful (for cure: 'bad'); a character or image shown on the fighter; text shown by Check. |
Included: poison, burn, regen, sleep, stun, haste, slow, strong, brittle. Elements are listed in content/stats.ts (elements: { fire: { name: 'Fire', color: '#ff7043' }, … }). A fighter's elements: { fire: 2, ice: 0.5, poison: 0 } says how much of each it takes: above 1 is a weak spot (“Weak!”), below 1 resists, 0 is immune. immune: ['sleep'] lists statuses that never stick. Characters (battle), enemies and equipment (equip) all take both; equipment can also add skills.
Statuses that outlast the fight
lasting: true on a status: party members still standing keep it after the fight, with the turns it had left, and start the next fight with it. It shows in the Party window.
game.party.statuses('hero') // { poison: 3 }
game.party.cure('hero', 'poison') // one status; 'bad' = every harmful one; nothing = all
game.party.cure() // the whole party
game.party.restore() // full HP / MP, and cured
Enemies and troops
// src/content/enemies/wolf/index.ts
export default defineEnemy({
name: 'Wolf',
battleSprite, // import battleSprite from './battle_sprite/wolf.gen'
color: '#90a4ae', // without a sprite: a blob (or image: wolfUrl)
stats: { hp: 34, attack: 9, defense: 3, resistance: 2, speed: 7, luck: 3 }, // any stat from stats.ts
elements: { fire: 1.5, ice: 0.5 },
actions: [ // one is picked by weight from those it can use; none = always attack
{ skill: 'attack', weight: 2 },
{ skill: 'bite', weight: 3, when: ({ self }) => self.hp < 17 },
],
xp: 18, money: 10, drops: [{ item: 'bread', chance: 0.5 }],
})
// src/content/troops/wolf_pack/index.ts — a named fight: game.battles.start('wolf_pack'), `battle wolf_pack` in .vn
export default defineTroop({
enemies: ['wolf', 'wolf', 'bat'], background: 'forest', canFlee: false,
rewards: { xp: 60, money: 40 }, // instead of what each enemy gives
})
Enemies heal the ally who needs it most and prefer targets that don't have a status yet. when also gets battle (battle.turn, battle.side('party')…).
Passives and showing the next move
// on a character (battle: { … }) or an enemy
passive: { name: 'Regrowth', onStart: [{ type: 'status', status: 'haste' }], onTurn: [{ type: 'heal', percent: 0.05 }] },
// src/content/battle.ts
showIntent: true, // a label over each enemy: what it will do next
encounters: { minSteps: 5, rate: 0.08 }, // random battles: tiles before the next one, default chance per tile
onStart runs once when the fight begins, onTurn at the start of each of their turns; both take the same effects as skills. With showIntent enemies decide one turn ahead; a plan that became impossible is chosen again.
Settings and formulas
// src/content/battle.ts
export default defineBattle({
music: 'music/battle', // optional; the map's music comes back afterwards
backgrounds: { forest, night: 'linear-gradient(#1b2735, #090a0f)' }, // see below
background: 'night', // when a battle names none
spriteScale: 4, // screen pixels per sprite pixel
formulas: { // optional: replace any of the maths
turnDelay: (speed) => 1000 / (speed + 10),
critMultiplier: 2,
// damage, hitChance, critChance, defendMultiplier, fleeChance
},
// onLose: (game) => { … }, // after a lost battle, INSTEAD of the game over below
})
The default formulas read the stats named hp, mp, attack, defense, magic, resistance, speed, luck. The battle screen is the minigame content/minigames/battle: change its look freely. The rules are in core/battle/engine.ts (pure and tested).
Game over
When the whole party is down (a lost battle, lethal ground) the game-over screen opens. Call it yourself with game.gameOver().
// src/content/config/gameover.ts
export default defineGameOver({
mode: 'screen', // 'wakeUp' = no screen: everyone is healed, the game carries on
title: tx('Game over'),
text: tx('The party has fallen.'),
buttons: ['load', 'wakeUp', 'title'], // in this order; 'load' hides itself when there is no save
// sound: 'sfx/game_over', music: 'music/game_over',
musicFadeMs: 800,
wakeUpMessage: tx('You wake up, sore but alive.'),
})
const how = await game.gameOver() // 'wakeUp' (the game carries on) | 'load' | 'title'
| Button | |
|---|---|
load | Loads the newest save (any slot, the autosave too). |
wakeUp | Heals the party, shows wakeUpMessage and carries on where they fell. Leave it out for a strict game over. |
title | Back to the title screen, without asking about unsaved progress. |
The screen is the window content/windows/gameover (it cannot be closed without picking a button). An event that started the battle keeps running after wakeUp and gets 'lose' as the result; after load or title it is dropped.
Sprites and backgrounds
Enemies stand on the left facing right, the party on the right facing left, over the background.
| Sprites | An Aseprite file in battle_sprite/ next to the enemy or character, imported as battleSprite. Characters without one use their map sprite; enemies fall back to image, then a color blob. |
| Tags | idle (or idle_right/idle_left, mirrored as needed; else idle_down). Optional: hurt, attack (play once, then back to idle), down (held while knocked out). Missing ones: a shake, a step forward, greyed out. |
| Backgrounds | A name from backgrounds, an imported image (covers the screen) or any CSS background. Picked from start({ background }), else the encounter zone's background, else background in content/battle.ts. |
// src/content/battle.ts
import forest from '@/content/assets/battle/forest.svg'
export default defineBattle({ backgrounds: { forest }, background: 'forest' })
game.battles.start({ enemies: ['wolf'], background: 'forest' })
Encounter zones
// any map object with an area
{ id: 'wild_area', at: { x: 14, y: 1 }, size: { w: 15, h: 9 },
encounters: { groups: [['slime'], ['bat', 'slime'], 'wolf_pack'], rate: 0.08 } } // enemy lists or troop ids
In Tiled: a rectangle with class encounter, properties enemies = slime,slime;bat (groups split by ;, enemies by ,) and / or troops = wolf_pack;slimes, rate (chance per tile walked, default 0.08), optional background. No fight in the first 5 tiles after a map change or a battle. Example: the east half of the forest.
Enemies on the map
// like a character: drawn with the enemy's `sprite` (else its battleSprite)
objects: {
wolf: { enemy: 'wolf', fight: { enemies: ['wolf', 'wolf'], canFlee: false /*, troop: 'wolf_pack', background, respawn: true */ } },
}
Interacting with it starts the fight (default: that enemy alone). After a win it is gone for good (saved), unless respawn: true. Give it your own onInteract to do something else. It is solid; scripts reach it with world.npc(id) (id: its name, else enemy_wolf_x_y).
In Tiled: class enemy, property enemy = wolf; optional enemies (wolf,wolf) or troop, background, canFlee, respawn. Example: the wolf pair in the forest.
Difficulty levels
// src/content/config/difficulty.ts — every number is a multiplier, 1 = unchanged
export default defineDifficulty({
default: 'normal',
askOnNewGame: false, // true = every new game starts with "Pick a difficulty"
canChange: true, // the player can change it in Settings while playing
levels: {
easy: { name: tx('Easy'), description: tx('…'), enemyStats: { hp: 0.75, attack: 0.75 }, encounters: 0.7, terrainDamage: 0.5 },
normal: { name: tx('Normal') },
hard: { name: tx('Hard'), enemyStats: { hp: 1.4, attack: 1.25, speed: 1.1 }, xp: 1.15, money: 1.15 },
},
})
game.difficulty.get() // 'normal' (saved with the game)
game.difficulty.set('hard')
await game.difficulty.ask() // the question window; resolves with the level now in use
game.difficulty.rate('xp') // the current multiplier, for your own rules
const level = useDifficulty() // React
| Field | Multiplies |
|---|---|
enemyStats | Enemy stats in battle, by stat name from stats.ts (rounded, never under 1). |
xp, money | What a won battle gives. |
encounters | The chance of random battles. |
terrainDamage | HP lost on damaging ground. |
prices | Shop prices. Below 1 the shops also pay less, so selling back never makes money. |
With one level only, no difficulty setting is shown anywhere. A level removed from the file later falls back to default in old saves.