Game Logic & AI

Simple Enemy AI with State Machines

Avoid massive "if-else" spaghetti code. Learn how to use Finite State Machines (FSM) to give your enemies clear behaviors: Patrolling, Chasing, and Attacking.

IntermediateJavaScriptLogic9 min read

What is a Finite State Machine?

If you've ever tried programming an enemy by just adding more and more if statements, you probably ended up with a bug where the enemy tries to attack and walk in the opposite direction at the same time. This happens because the enemy doesn't have a singular "state" of mind.

A Finite State Machine (FSM) solves this. The rule is simple: The enemy can only be in one state at a time. It is either Patrolling OR Chasing. It cannot be both.

Defining the States

For a basic top-down game, an enemy usually has three states:

  • PATROL: Moves slowly back and forth between two points.
  • CHASE: If the player comes within a certain radius, stop patrolling and move towards the player at high speed.
  • ATTACK: If the player is very close, stop moving and trigger an attack animation.

Every frame of your game loop, you check the enemy's current state, and run ONLY the code for that state.

Interactive AI Sandbox

Below is a working example of a State Machine. The Red Square is the enemy. Your Mouse Cursor is the player.

Try modifying the VISION_RADIUS to make the enemy blind, or increase chaseSpeed to make them terrifying. Click ▶ Run Code to test your changes!

💻 HTML/JS Editor

Scaling Up to Behavior Trees

State machines are incredible for simple enemies. However, if you are building an RPG boss with 15 different attacks, healing phases, and dodging logic, an FSM can become a tangled mess (often called a "State Explosion").

For complex AI, the industry standard is to move from FSMs to Behavior Trees, which allow you to compose small, reusable nodes of logic (like "Is Player Close?" -> "Sequence: Roar, then Charge"). We'll cover Behavior Trees in a future advanced tutorial!