Bash.tv → GitHub → itch.io

Enemy behavior and decision-making for browser games. Finite state machines (idle, patrol, chase, attack, flee, search), steering behaviors (seek, flee, arrive, pursue, wander, separation, path following), perception with sight cones, line of sight and memory, utility scoring for smarter choices, telegraphed attacks, group behavior, difficulty knobs, and keeping AI fair and readable. Includes a...

Download skill (.zip) Files on GitHub

Enemy AI

Drop-in: assets/ai.js (tests: arrive stops at the target, seek reaches top speed, separation spreads a crowd, FSM transitions, sight cone, utility choice). Paths around walls: pathfinding.

import { StateMachine, steer, applySteering, canSee, remember, chooseAction } from "./core/ai.js";
const brain = new StateMachine(bat, {
  patrol: { update: (e, dt, g) => { applySteering(e, steer.followPath(e, e.route), dt); if (canSee(e, g.player, { los: g.los })) return "chase"; } },
  chase:  { enter: (e) => sfx("alert"), update: (e, dt, g) => {
              const seen = canSee(e, g.player, { los: g.los }), mem = remember(e, g.player, seen, g.time);
              if (!mem) return "patrol";
              applySteering(e, steer.pursue(e, g.player), dt);
              if (dist(e, g.player) < 40) return "attack"; } },
  attack: { enter: (e) => (e.windup = 0.4), update: (e, dt) => { if ((e.windup -= dt) <= 0) { strike(e); return "chase"; } } },
}, "patrol");
brain.update(dt, game);

Building blocks

Need Use
Clear behaviors StateMachine with enter / update / exit; return the next state’s name
Smooth movement steer.* + applySteering (maxSpeed, maxForce, reaction per enemy type)
Crowds not stacking add steer.separate(e, neighbors) (neighbors from the spatial grid)
Notice the player canSee (range, field of view, line of sight) + remember (search the last seen spot)
Several options chooseAction (utility): score attack / chase / flee / heal by distance, health, cooldowns

Fair and readable

Difficulty knobs

Reaction time, sight range, speed, accuracy, aggression (attack cooldown), and group size. Put them in config.enemies.<type> and scale them with difficulty-scaling.

Performance

Think less often than you move: run decisions every 0.1–0.25 s (staggered by id), steering every step. Share one flow field for many chasers (pathfinding).