Applied The Lora Playbook

The Enemy Behavior Sandbox

Last reviewed

Intermediate

What you'll learn

~60 min
  • Plan and build a Phaser 3 combat room with multiple enemy types and configurable AI
  • Implement a finite state machine for enemy behavior with six states
  • Tune enemy aggression, pursuit, and attack patterns with real-time sliders
  • Build three distinct enemy archetypes: melee grunt, ranged soldier, and boss

What you’re building

The combat prototype gave you one enemy that patrols and punches. BloodRayne had dozens of enemy types — soldiers with rifles, supernatural creatures that lunged from shadows, and bosses that demanded pattern recognition and patience. The enemies are what make combat interesting. A perfect combo system is meaningless if the enemies just walk back and forth waiting to die.

In this lesson you are building an enemy behavior sandbox — a Phaser 3 combat room with three enemy types, each running a finite state machine with tunable parameters. A side panel lets you adjust aggression, pursuit distance, attack cooldowns, and more in real time. You will use this sandbox to design enemies that force the player to use every tool from the Combat Loop Prototype: combos for the grunts, dodges for the ranged shots, and pattern reading for the boss.

ℹSoftware pattern: Finite state machine for autonomous agents

A finite state machine (FSM) is one of the most widely used patterns in software: an entity can be in exactly one state at a time, and transitions between states are triggered by conditions. In games, enemy AI uses FSMs. In business software, order processing uses FSMs (pending, approved, shipped, delivered). The same mental model applies — states, transitions, and conditions.


The showcase

Here is what the finished sandbox gives you:

  • Combat room with floor, platforms, and three enemy types active simultaneously
  • Player character (Rayne) with the combat controls from the Combat Loop Prototype
  • Melee Grunt (green) — state machine: idle, patrol, alert, chase, attack, hurt, dead
  • Ranged Soldier (purple) — state machine: idle, patrol, alert, retreat, shoot, hurt, dead
  • Boss (large red) — state machine: idle, approach, telegraph, heavy attack, summon, enrage, hurt, dead
  • Tuning panel with tabs for each enemy type, containing sliders for:
    • Detection range (how far they “see” the player)
    • Pursuit speed (how fast they chase)
    • Attack cooldown (time between attacks)
    • Attack damage
    • Aggression (0-100%, affects how quickly they transition from passive to aggressive states)
    • Telegraph duration (how long the warning animation plays before attacking)
    • Health
  • State indicator above each enemy showing their current FSM state in small text
  • Spawn controls — buttons to add or remove each enemy type from the room

Before you start

If your AI tool is open from the last lesson, leave it (/exit, or Ctrl+D in Antigravity) so you are back at your terminal prompt. Make a folder for this sandbox and step into it:

Terminal window
mkdir -p ~/retro-remake/12-enemy-behavior-sandbox && cd ~/retro-remake/12-enemy-behavior-sandbox

In Windows PowerShell, run mkdir ~/retro-remake/12-enemy-behavior-sandbox; cd ~/retro-remake/12-enemy-behavior-sandbox. In a desktop agent app, open ~/retro-remake/12-enemy-behavior-sandbox as your workspace folder. Then start your AI tool the way Workbench Setup showed.

Switch to plan mode

Switch into Plan mode (Shift+Tab, /plan, or the mode picker) so the agent clarifies each enemy archetype’s state machine and tuning sliders before writing code.

The conversation

Each message below is an example. Use your own words.

Say what you want

Ask for a browser-based enemy behavior sandbox with three archetypes and a real-time tuning panel:

Build a browser-based enemy behavior sandbox with a melee grunt, a ranged soldier, and a boss, plus real-time tuning sliders on the side. Ask me what you need first.

Answer its questions

Your agent will ask about the three enemy behaviors, the player controls, the tuning panel tabs, and visual debug tools:

If it asksYou might say
How should the three enemy types behave?Grunts patrol, chase, and melee; soldiers retreat and shoot projectiles; bosses slam, summon grunts, and enrage.
What controls should the player character have?Arrow keys to move and jump, Z for the three-hit blade combo, and X for a dodge roll.
What sliders and tabs belong in the tuning panel?Grunt, Ranged, and Boss tabs with detection range, pursuit speed, cooldown, damage, telegraph duration, health, and aggression.
What spawn controls and presets should the sidebar include?Buttons to add each enemy type or clear all, plus Cannon Fodder, Dangerous, BloodRayne Classic, and Reset presets.
What visual feedback and debug info should appear?Live behavior labels above enemies, clear attack warnings, a ground danger zone for boss slams, and a kill counter.

Read the plan

Before approving the build, check the agent’s plan for these four points:

  1. Three distinct finite state machines (built with Phaser 3 in a single index.html file):
    • Melee Grunt (green, 80 HP): IDLE -> PATROL -> ALERT (0.5s exclamation flash) -> CHASE -> ATTACK (350ms yellow flash telegraph, 12 damage) -> HURT / DEAD.
    • Ranged Soldier (purple, 60 HP): IDLE -> PATROL -> ALERT -> RETREAT (backs away if player is within 120px) -> SHOOT (400ms arm-raise telegraph, 280px/s projectile dealing 15 damage).
    • Boss (large dark red, 300 HP): IDLE -> APPROACH -> TELEGRAPH (700ms red flash + 100px ground danger zone) -> HEAVY_ATTACK (30 damage + screen shake) -> SUMMON (every third cycle, spawns up to 2 grunts) and ENRAGE (below 30% HP, +50% speed and -30% cooldown).
  2. Real-time shared configuration: sliders update window.enemyConfig.grunt, .ranged, and .boss immediately for both existing and newly spawned enemies.
  3. Aggression meta-parameter (0-100%): at 100% aggression enemies skip ALERT and chase immediately; at 0% they linger in PATROL up to 2 extra seconds.
  4. Toggleable AI state labels: a “Show AI States” checkbox toggles live state text (PATROL, CHASE, RETREAT, TELEGRAPH, etc.) above every enemy’s head.

If the plan makes all three enemy types share one generic chase-and-hit loop, correct it before approving:

Give each enemy archetype its own behavior loop so ranged soldiers back away when I get close and the boss alternates heavy ground slams with summoning grunts.

Once the plan covers all four checks, approve it and let your agent build index.html. After testing all three enemy types in your browser, ask your agent to save a Git checkpoint.

💡Distinct state machines make distinct enemies

Enemy AI has more moving parts than player movement. Checking that each state machine has its own states, transition conditions, and telegraph timers ensures you get a grunt that charges, a soldier that kites, and a boss that demands respect.


What you get

After the AI finishes, the project folder structure looks like this:

12-enemy-behavior-sandbox/
index.html

Play it

Ask your agent to open the sandbox in your browser:

Open the enemy behavior sandbox in my browser so I can test the three enemy types and tuning sliders.

Or open it from a terminal:

Terminal window
cd ~/retro-remake/12-enemy-behavior-sandbox
open index.html # macOS (Linux: xdg-open index.html; WSL: explorer.exe . then double-click index.html)

Click “Add Grunt” a couple of times. Watch them patrol. Walk into their detection range and watch the exclamation mark flash before they chase. Attack them and watch the HURT state stagger. Now add a Ranged Soldier — notice how it backs away when you get close. Add the Boss. Respect the telegraph.

If something is off

ProblemFollow-up prompt
Enemies all do the same thingAll three enemy types behave the same way. Give each archetype its own distinct behavior: grunts should chase and melee, ranged soldiers should back away and shoot projectiles, and the boss should telegraph heavy slams and summon grunts.
State transitions are instant — no telegraph visibleEnemies attack immediately without a visible warning. Make each enemy pause and flash yellow or red for the full telegraph duration before dealing damage so I have time to read the attack and dodge.
Ranged soldier doesn’t retreatThe ranged soldier stands still when I run up to it. Make it back away whenever I get too close until it reaches a safe shooting distance, then resume firing projectiles.
Boss enrage never triggersThe boss never enters its enraged phase at low health. When its health drops below the enrage threshold, turn it red, show an ENRAGED callout, speed up its chase, and shorten its attack cooldown.

Deep dive

Why telegraphs are non-negotiable

A telegraph is the visual warning before an enemy attacks. The grunt flashes yellow. The boss flashes red and shows a ground indicator. Without telegraphs, attacks feel unfair — the player cannot react to something they cannot see coming. With telegraphs, attacks become a conversation: the enemy says “I’m about to hit you” and the player says “I dodge” or “I counter-attack.” Every great action game uses telegraphs. Soulslike bosses wind up long and visibly; fast action games flash an enemy before it lunges. Your telegraph duration slider lets you find the right balance between “readable” and “challenging.”

The aggression slider changes the game

Aggression is a meta-parameter that affects how quickly enemies escalate from passive to hostile. At 0% aggression, grunts linger in PATROL even when they see you — they give you time. At 100%, they skip the ALERT state entirely and sprint at you the moment you enter detection range. This single slider transforms the same enemy from a training dummy into a threat. In the final game, you might set early-level grunts to 30% aggression and late-level grunts to 80% — same code, different feel.

The boss is a pattern test

The boss exists to test whether the player has learned the systems. The heavy slam is dodgeable if you read the telegraph. The summoned grunts force you to manage adds while watching the boss. The enrage phase punishes slow play. This is classic action game boss design: each mechanic is tested individually in the encounter. Your tuning panel lets you dial in exactly how forgiving or demanding each phase is.

🔍Why three enemy types is enough for a prototypeclick to expand

Three archetypes — melee, ranged, boss — cover the fundamental design space. Every other enemy in BloodRayne (or any action game) is a variant of these three. A fast melee enemy is a grunt with higher pursuit speed and lower health. A shotgun enemy is a ranged soldier with a spread pattern. A mini-boss is a boss with fewer phases. Once you have the tuning infrastructure for these three, creating new variants is just adjusting slider values and maybe adding one new state. Start with three, prove the design, then expand.


Customize it

Add a shield grunt variant

Add a Shield checkbox and a Shield Turn Speed slider (0.3 to 2.0s, default 0.8s) on the Grunt tab so frontal attacks spark for 0 damage and I must dodge behind to hit.

Add enemy group coordination

Add a Coordinated checkbox on the Grunt tab so when multiple grunts are alive, only one attacks at a time while the others circle at detection range waiting for the active cooldown.

Add a boss phase 2 attack

Give the boss a telegraphed 1-second crouch and 300px/s full-arena charge after every other heavy slam, with a 1.5-second wall-impact stagger and sliders for charge damage and stagger duration.

Try it yourself

  1. Walk through the plan-mode conversation above and open the sandbox in your browser.
  2. Add one of each enemy type. Turn on “Show AI States” and watch the state labels change in real time as you interact with each enemy.
  3. Set grunt aggression to 100%. Notice the difference. Set it to 0%. Notice the difference. Find the value where the grunt feels dangerous but fair.
  4. Fight the ranged soldier without dodging. Now fight it using only dodge rolls to close distance. The ranged soldier should force you to use dodge — if you can just walk up and combo it, increase its retreat speed or reduce its attack cooldown.
  5. Fight the boss. Die on purpose. Pay attention to the telegraph timing — if you cannot reliably dodge the heavy slam, the telegraph duration is too short. If you can stand in front of the boss and ignore the telegraph, it is too long.
  6. Add 3 grunts and 2 ranged soldiers at once. This is a “crowd” test. If you feel overwhelmed in under 5 seconds, reduce aggression. If you feel unchallenged, add more enemies or increase their stats.

Write down the values that felt best. You will need them when the AI stitches everything together in the demo assembly.


💡Your enemies have brains now

Not big brains — but enough to force the player to think. The grunt pressures, the ranged soldier controls space, and the boss demands respect. These three archetypes, with the tuning infrastructure you just built, can produce dozens of enemy variants. BloodRayne’s enemy roster is closer than it looks. Next up: making every hit, slash, and explosion feel like it matters.

intellimetrics · Arlington, VA
© 2026 intellimetrics.net
Search lessons