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
- Object
- 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.Action | Produces input for one tick. |
interface AIBrain.Decision | A condition that switches state. |
class AIBrain.State | A named state: an action and its outgoing transitions. |
class AIBrain.Patrol | Walks back and forth, turning at walls and, if turnAtLedges, at drops. |
class AIBrain.Chase | Moves toward the player, jumping at walls if jumps. |
class AIBrain.Shoot | Stands still, facing the player, and fires every interval ticks. |
class AIBrain.Idle | Does nothing. |
class AIBrain.Hop | Patrols and hops every interval ticks. |
class AIBrain.Both | Runs two actions and combines their buttons, such as a patrol that shoots. |
class AIBrain.TargetWithin | Holds when the player is within a distance, and optionally in front. |
class AIBrain.After | Holds after a number of ticks in the current state. |
class AIBrain.Chance | Holds with a chance per tick, drawn from the world’s deterministic Rng. |
class AIBrain.HealthAtMost | Holds while the actor’s health is at or below a value. |
class AIBrain.Not | Inverts 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
nameString- the name
actionAIBrain.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
selfActor- the actor
worldPlatformWorld- the world
Returns
Input bitsgetCurrent
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
dint- -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
selfActor- the actor
worldPlatformWorld- the world
dirint- -1 or 1
Returns
true at a ledge