Client and backend. This class is shared: the same code is compiled into your app and into your server, so everything on this page works in both.

public final class AIBrain

  1. Object
  2. AIBrain

Enemy behaviour as a small state machine of actions and decisions, after Corgi Engine’s AI Brain.

A brain does not move anything itself. Each tick it produces the Input bits its actor would get from a player, so an enemy runs on exactly the same abilities and collision as the hero, and is as deterministic. The current State runs its Action, then its transitions are tried in order and the first Decision that holds switches state.

import com.codename1.gaming.platformer.*;

Actor enemy = new Actor("slime", Fx.of(64), Fx.of(16), Fx.of(12), Fx.of(10));
AIBrain brain = new AIBrain();
AIBrain.State patrol = brain.addState("patrol", new AIBrain.Patrol());
AIBrain.State chase = brain.addState("chase", new AIBrain.Chase(true));
patrol.addTransition(new AIBrain.TargetWithin(Fx.of(96), true), chase);
chase.addTransition(new AIBrain.Not(new AIBrain.TargetWithin(Fx.of(160), false)), patrol);
enemy.setBrain(brain);

Nested types

interface AIBrain.ActionProduces input for one tick.
interface AIBrain.DecisionA condition that switches state.
class AIBrain.StateA named state: an action and its outgoing transitions.
class AIBrain.PatrolWalks back and forth, turning at walls and, if turnAtLedges, at drops.
class AIBrain.ChaseMoves toward the player, jumping at walls if jumps.
class AIBrain.ShootStands still, facing the player, and fires every interval ticks.
class AIBrain.IdleDoes nothing.
class AIBrain.HopPatrols and hops every interval ticks.
class AIBrain.BothRuns two actions and combines their buttons, such as a patrol that shoots.
class AIBrain.TargetWithinHolds when the player is within a distance, and optionally in front.
class AIBrain.AfterHolds after a number of ticks in the current state.
class AIBrain.ChanceHolds with a chance per tick, drawn from the world’s deterministic Rng.
class AIBrain.HealthAtMostHolds while the actor’s health is at or below a value.
class AIBrain.NotInverts a decision.

Constructors

public AIBrain()

Methods

public AIBrain.State addState(String name, AIBrain.Action action)Adds a state.
public int think(Actor self, PlatformWorld world)Computes the actor’s input for this tick.
public AIBrain.State getCurrent()The current state.
public int getTicksInState()Ticks spent in the current state.
public int getDirection()The patrol direction, -1 or 1, shared by the stock actions.
public void setDirection(int d)Sets the patrol direction.
public static boolean ledgeAhead(Actor self, PlatformWorld world, int dir)Whether the cell ahead of an actor’s feet has no floor.

Inherited methods

Constructor details

AIBrain

public AIBrain()

Method details

addState

public AIBrain.State addState(String name, AIBrain.Action action)
Adds a state. The first one added is where the brain starts.

Parameters

name String
the name
action AIBrain.Action
what it does each tick

Returns

the state, to add transitions to

think

public int think(Actor self, PlatformWorld world)
Computes the actor’s input for this tick. Called by PlatformWorld.

Parameters

self Actor
the actor
world PlatformWorld
the world

Returns

Input bits

getCurrent

public AIBrain.State getCurrent()
The current state.

Returns

the state, or null for an empty brain

getTicksInState

public int getTicksInState()
Ticks spent in the current state.

Returns

the tick count

getDirection

public int getDirection()
The patrol direction, -1 or 1, shared by the stock actions.

Returns

the direction

setDirection

public void setDirection(int d)
Sets the patrol direction.

Parameters

d int
-1 or 1

ledgeAhead

public static boolean ledgeAhead(Actor self, PlatformWorld world, int dir)
Whether the cell ahead of an actor’s feet has no floor.

Parameters

self Actor
the actor
world PlatformWorld
the world
dir int
-1 or 1

Returns

true at a ledge