Build combat for action, RPG and arcade games. Health, damage pipelines (base, crits, damage types and resistances, armor with diminishing returns, blocking, shields), invincibility frames, status effects (burn, poison, slow, stun) with stacking rules, melee attack timing (windup, active, recovery) with hitboxes that hit once, knockback and hit feedback, enemy telegraphs, death and respawn, and...
Download skill (.zip) Files on GitHubCombat system
Drop-in: assets/combat.js (tests: 100 armor = half damage, crits, fire resistance, shields, i-frames, kills, burn ticks,
stacking slows, attack phases, one hit per swing).
import { dealDamage, heal, Effects, attackPhase, swingHits } from "./core/combat.js";
const r = dealDamage(player, enemy, { amount: 12, type: "physical", critChance: 0.1 }, rng.next, time);
if (r.dealt) { popups.add(enemy, r.dealt, r.crit); feel.hit(enemy, r.crit); } // flash, shake, hit-stop (game-feel-engineering)
if (r.killed) game.events.emit("enemyKilled", { type: enemy.type });
enemy.effects.add({ id: "burn", duration: 3, tickEvery: 0.5, onTick: (e) => dealDamage(null, e, { amount: 2, type: "fire", minDamage: 1 }, rng.next, time) });
speed = base * player.effects.mod("speedMult");
Damage pipeline (order matters)
base × attacker multiplier → crit → resistance by type → armor (A / (A + 100): diminishing, never 100%) → block → minimum
damage (1) → shield → health. Keep it in one function so every source of damage follows the same rules.
Melee timing
attackPhase(t): windup (telegraph: pose, flash, sound) → active (hitbox on) → recovery (vulnerable). Use
swingHits so a swing hits each target once. Player attacks: short windup (≤ 0.1 s). Enemy attacks: readable windup (0.3–0.6 s).
Feel
Every hit: flash + sound + knockback + small shake. Big hits and kills: hit-stop (50–100 ms). Damage numbers for RPGs. Player hurt: i-frames (0.6–1 s) with blinking, red flash, and a health bar that drains visibly.
Balancing
- Decide time to kill first: basic enemy 1–3 hits, elite 5–10, boss 30–90 s. Derive HP from DPS × TTK.
- Decide hits to die for the player: 3–6 from basic enemies at the start.
- Status effects: duration 2–5 s, clear icons, don’t stack stuns (diminishing or immunity after one).
- Simulate fights with game-balance-testing before hand-tuning.
Pitfalls
Damage applied twice per frame (collision checked from both sides), damage during death animations, invincibility never ending, effects running while paused (advance with dt only), and floating point HP shown as “13.0000001” (round damage).