Class 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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceProduces input for one tick.static final classHolds after a number of ticks in the current state.static final classRuns two actions and combines their buttons, such as a patrol that shoots.static final classHolds with a chance per tick, drawn from the world's deterministicRng.static final classMoves toward the player, jumping at walls ifjumps.static interfaceA condition that switches state.static final classHolds while the actor's health is at or below a value.static final classPatrols and hops everyintervalticks.static final classDoes nothing.static final classInverts a decision.static final classWalks back and forth, turning at walls and, ifturnAtLedges, at drops.static final classStands still, facing the player, and fires everyintervalticks.static final classA named state: an action and its outgoing transitions.static final classHolds when the player is within a distance, and optionally in front. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddState(String name, AIBrain.Action action) Adds a state.The current state.intThe patrol direction, -1 or 1, shared by the stock actions.intTicks spent in the current state.static booleanledgeAhead(Actor self, PlatformWorld world, int dir) Whether the cell ahead of an actor's feet has no floor.voidsetDirection(int d) Sets the patrol direction.intthink(Actor self, PlatformWorld world) Computes the actor's input for this tick.
-
Constructor Details
-
AIBrain
public AIBrain()
-
-
Method Details
-
addState
Adds a state. The first one added is where the brain starts.
Parameters
name: the nameaction: what it does each tick
Returns
the state, to add transitions to
-
think
Computes the actor's input for this tick. Called by
PlatformWorld.Parameters
self: the actorworld: the world
Returns
Inputbits -
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: -1 or 1
-
ledgeAhead
Whether the cell ahead of an actor's feet has no floor.
Parameters
self: the actorworld: the worlddir: -1 or 1
Returns
true at a ledge
-