A State-Machine Boss
Boss fights are where state machines shine. A good boss is a pattern the player learns: it winds up (a telegraph the player can read), attacks, then recovers and is open to punishment. At half health it changes phase and the pattern speeds up. All of that is a state machine with timers, and you can build and test it without a single sprite.
The design
timer timer timer
Idle ─────────► Telegraph ─────► Attack ─────► Recover ──┐
▲ │ timer
└───────────────────────────────────────────────────────┘
any state ── health ≤ 50% (first time) ──► Enrage ──► Idle (phase 2)| State | Ticks (phase 1 / 2) | What the player should do |
|---|---|---|
| Idle | 2 / 1 | poke it (armored: hits do 1 damage) |
| Telegraph | 3 / 2 | read the wind-up, get ready |
| Attack | 1 / 1 | dodge or take a big hit |
| Recover | 3 / 2 | punish: full damage |
| Enrage | 2 | back off: invulnerable roar, then phase 2 |
The simulation runs in fixed ticks (think of each as a quarter second in a fixed-timestep loop, as in Fixed Timestep). The player is a small bot: it always attacks, and dodges whenever the boss is in its Attack state, except on two ticks where it reacts too late. That models a decent but imperfect human.
using System;
using System.Collections.Generic;
var boss = new Boss();
int playerHp = 40;
var lateDodges = new HashSet<int> { 6, 31 }; // ticks where the player reacts too late
for (int tick = 1; boss.Hp > 0 && playerHp > 0 && tick <= 80; tick++)
{
BossState state = boss.State;
bool dodging = state == BossState.Attack && !lateDodges.Contains(tick);
string playerAction = dodging ? "dodge" : "hit";
int damageToPlayer = boss.AttackDamage(state, dodging);
playerHp = Math.Max(0, playerHp - damageToPlayer);
int dealt = dodging ? 0 : boss.TakeHit(8);
string note = damageToPlayer > 0 ? quot;player takes {damageToPlayer}!" : dodging ? "dodged" : quot;boss takes {dealt}";
Console.WriteLine(quot;t{tick:00} {state,-9} player {playerAction,-5} {note,-16} boss {boss.Hp,3}/120 player {playerHp,2}/40");
string change = boss.EndTick();
if (change != null) Console.WriteLine(quot; -> {change}");
}
Console.WriteLine(boss.Hp == 0 ? "Boss defeated!" : playerHp == 0 ? "You died." : "Time's up.");
enum BossState { Idle, Telegraph, Attack, Recover, Enrage }
class Boss
{
public int Hp { get; private set; } = 120;
public BossState State { get; private set; } = BossState.Idle;
bool enraged;
bool enragePending;
int timer;
public Boss() => timer = Duration(BossState.Idle);
int Duration(BossState s) => s switch
{
BossState.Idle => enraged ? 1 : 2,
BossState.Telegraph => enraged ? 2 : 3,
BossState.Attack => 1,
BossState.Recover => enraged ? 2 : 3,
BossState.Enrage => 2,
_ => 1,
};
public int AttackDamage(BossState state, bool dodging) =>
state != BossState.Attack || dodging ? 0 : enraged ? 16 : 12;
public int TakeHit(int damage)
{
int dealt = State switch
{
BossState.Recover => damage, // open: full damage
BossState.Enrage => 0, // invulnerable while roaring
_ => damage / 8, // armored: 1 damage
};
Hp = Math.Max(0, Hp - dealt);
if (!enraged && Hp <= 60) enragePending = true;
return dealt;
}
// Called once at the end of each tick: count down and change state.
public string EndTick()
{
if (Hp == 0) return null;
if (enragePending) // an interrupt beats the normal pattern
{
enragePending = false;
enraged = true;
return Enter(BossState.Enrage) + " (phase 2: faster pattern, harder hits)";
}
if (--timer > 0) return null;
return Enter(State switch
{
BossState.Idle => BossState.Telegraph,
BossState.Telegraph => BossState.Attack,
BossState.Attack => BossState.Recover,
_ => BossState.Idle, // Recover and Enrage go back to Idle
});
}
string Enter(BossState next)
{
State = next;
timer = Duration(next);
return next == BossState.Telegraph ? "Telegraph: the boss raises its hammer!" : next.ToString();
}
}t01 Idle player hit boss takes 1 boss 119/120 player 40/40
t02 Idle player hit boss takes 1 boss 118/120 player 40/40
-> Telegraph: the boss raises its hammer!
t03 Telegraph player hit boss takes 1 boss 117/120 player 40/40
t04 Telegraph player hit boss takes 1 boss 116/120 player 40/40
t05 Telegraph player hit boss takes 1 boss 115/120 player 40/40
-> Attack
t06 Attack player hit player takes 12! boss 114/120 player 28/40
-> Recover
t07 Recover player hit boss takes 8 boss 106/120 player 28/40
t08 Recover player hit boss takes 8 boss 98/120 player 28/40
t09 Recover player hit boss takes 8 boss 90/120 player 28/40
-> Idle
t10 Idle player hit boss takes 1 boss 89/120 player 28/40
t11 Idle player hit boss takes 1 boss 88/120 player 28/40
-> Telegraph: the boss raises its hammer!
t12 Telegraph player hit boss takes 1 boss 87/120 player 28/40
t13 Telegraph player hit boss takes 1 boss 86/120 player 28/40
t14 Telegraph player hit boss takes 1 boss 85/120 player 28/40
-> Attack
t15 Attack player dodge dodged boss 85/120 player 28/40
-> Recover
t16 Recover player hit boss takes 8 boss 77/120 player 28/40
t17 Recover player hit boss takes 8 boss 69/120 player 28/40
t18 Recover player hit boss takes 8 boss 61/120 player 28/40
-> Idle
t19 Idle player hit boss takes 1 boss 60/120 player 28/40
-> Enrage (phase 2: faster pattern, harder hits)
t20 Enrage player hit boss takes 0 boss 60/120 player 28/40
t21 Enrage player hit boss takes 0 boss 60/120 player 28/40
-> Idle
t22 Idle player hit boss takes 1 boss 59/120 player 28/40
-> Telegraph: the boss raises its hammer!
t23 Telegraph player hit boss takes 1 boss 58/120 player 28/40
t24 Telegraph player hit boss takes 1 boss 57/120 player 28/40
-> Attack
t25 Attack player dodge dodged boss 57/120 player 28/40
-> Recover
t26 Recover player hit boss takes 8 boss 49/120 player 28/40
t27 Recover player hit boss takes 8 boss 41/120 player 28/40
-> Idle
t28 Idle player hit boss takes 1 boss 40/120 player 28/40
-> Telegraph: the boss raises its hammer!
t29 Telegraph player hit boss takes 1 boss 39/120 player 28/40
t30 Telegraph player hit boss takes 1 boss 38/120 player 28/40
-> Attack
t31 Attack player hit player takes 16! boss 37/120 player 12/40
-> Recover
t32 Recover player hit boss takes 8 boss 29/120 player 12/40
t33 Recover player hit boss takes 8 boss 21/120 player 12/40
-> Idle
t34 Idle player hit boss takes 1 boss 20/120 player 12/40
-> Telegraph: the boss raises its hammer!
t35 Telegraph player hit boss takes 1 boss 19/120 player 12/40
t36 Telegraph player hit boss takes 1 boss 18/120 player 12/40
-> Attack
t37 Attack player dodge dodged boss 18/120 player 12/40
-> Recover
t38 Recover player hit boss takes 8 boss 10/120 player 12/40
t39 Recover player hit boss takes 8 boss 2/120 player 12/40
-> Idle
t40 Idle player hit boss takes 1 boss 1/120 player 12/40
-> Telegraph: the boss raises its hammer!
t41 Telegraph player hit boss takes 1 boss 0/120 player 12/40
Boss defeated!
Read the log like a fight: chip damage while the boss is armored, a dodge (or a painful hit on the late ticks) when the hammer falls, then a burst of damage during Recover. Crossing 60 HP interrupts whatever the boss was doing and sends it into Enrage, after which every timer is shorter.
Design lessons in the code
- Telegraphs make difficulty fair. The player always gets a readable warning before damage. Removing the Telegraph state would make the same boss feel cheap, even with the same numbers.
- Vulnerability windows reward learning. Damage is 8 in Recover and 1 otherwise, so a player who learns the pattern wins faster than one who mashes.
- Phases are state too.
enragedchanges durations and damage everywhere throughDurationandAttackDamage; nothing else needed to change. - Interrupts are explicit. The Enrage check runs before the normal timer transition, in one place. Bosses that can be staggered, frozen or stunned use the same idea.
Testing a boss without playing it
Because the fight is a pure simulation, you can run it with different player skill levels and check that the numbers make sense: a skilled player should win comfortably, a careless one should lose. This version reruns the fight with an increasing number of late dodges:
using System;
foreach (int mistakesEvery in new[] { 0, 3, 2, 1 })
{
var (won, ticks, hpLeft) = Fight(mistakesEvery);
string skill = mistakesEvery switch { 0 => "perfect", 3 => "good", 2 => "sloppy", _ => "never dodges" };
Console.WriteLine(quot;{skill,-13} -> {(won ? "WIN " : "LOSS")} after {ticks,2} ticks, player HP {hpLeft}");
}
// A compact version of the boss: one cycle is Idle, Telegraph, Attack, Recover.
static (bool, int, int) Fight(int mistakesEvery)
{
int bossHp = 120, playerHp = 40, attacks = 0, tick = 0;
while (bossHp > 0 && playerHp > 0)
{
bool enraged = bossHp <= 60;
int idle = enraged ? 1 : 2, telegraph = enraged ? 2 : 3, recover = enraged ? 2 : 3;
bossHp -= 1 * (idle + telegraph); tick += idle + telegraph; // armored pokes
if (bossHp <= 0) break;
attacks++; tick++;
bool late = mistakesEvery > 0 && attacks % mistakesEvery == 0;
if (late) playerHp -= enraged ? 16 : 12;
if (playerHp <= 0) break;
bossHp -= 8 * recover; tick += recover; // punish window
}
return (bossHp <= 0, tick, Math.Max(0, playerHp));
}perfect -> WIN after 39 ticks, player HP 40
good -> WIN after 39 ticks, player HP 28
sloppy -> WIN after 39 ticks, player HP 12
never dodges -> LOSS after 31 ticks, player HP 0
This kind of quick simulation won't tell you whether the fight is fun, but it catches broken numbers (an unbeatable phase 2, a boss that dies before its first attack) before anyone plays it.
In the engines
The same pattern appears in every engine, usually with the animation system driving the timing:
- Unity: a C# state machine in a MonoBehaviour, with timers in
Update; the Animator's state machine plays the wind-up and uses animation events to open the damage window. - Unreal: Behavior Trees or StateTree for the decisions, Animation Montages with notifies for the windows.
- Godot: a node-based state machine (one child node per state) or an
AnimationTreestate machine, withAnimationPlayermethod tracks for the windows.
Test yourself
What is the purpose of the Telegraph state?
- To heal the boss
- To give the player a readable warning before the attack
- To load animations
- To change phases
Why is the Enrage check done before the normal timer transition in EndTick?
- It is faster
- An interrupt must override whatever transition the pattern would make
- Enrage has the shortest timer
- C# evaluates it first anyway
In phase 2, what does the enraged flag change?
- Only the boss's color
- The state durations and the attack damage
- The player's damage
- Nothing