miaoda-game-behavior-phaser
v0.3.3
Published
Phaser Game Object behavior-tree host for miaoda-game-behavior-core.
Readme
miaoda-game-behavior-phaser
Use this Phaser 4 adapter to run miaoda-game-behavior-core trees on Game Objects. It converts Scene update milliseconds to core seconds and exposes injectable target, obstacle, and optional route queries.
Install
pnpm add miaoda-game-behavior-phaser miaoda-game-behavior-corePhaser >=4 <5 is required.
Minimal controller
const controller = scene.behaviors.create(
enemy,
`root { selector {
sequence { condition [CanSeeTarget] action [MoveToTarget] }
action [Patrol]
} }`,
{ speed: 80, sightRange: 400 },
{
target: () => ({ x: player.x, y: player.y }),
obstacles: () => visionObstacles,
},
{ Patrol: (agent) => patrol(agent) },
);Register BehaviorPlugin as a Scene Plugin to use scene.behaviors.create(), or construct new BehaviorController(scene.events, ...) directly. The adapter reads object.x, object.y, and object.angle and writes movement through setPosition. Scene updates are converted from milliseconds to seconds before BehaviorRunner.step.
Use setTicking(false) to pause and reset() to restart the tree. destroy() and Scene shutdown are terminal; later ticking/reset calls and already-captured Scene update callbacks are inert. Return the same obstacle geometry used for vision rendering. Add findPath to the hooks when MoveToTargetAvoiding should route around terrain. Add applyPosition(next) when collision, Arcade/Matter physics, or a game model must remain the authoritative writer; otherwise the adapter calls object.setPosition directly.
For a game-owned fixed tick, select manual ownership in the existing final options object:
const controller = scene.behaviors.create(
enemy,
tree,
{ speed: 80 },
hooks,
custom,
{ tickSource: 'manual' },
);
fixedStepper.advance(frameDeltaSeconds, ({ dt }) => {
controller.stepSimulation(dt);
});The default tickSource: 'host' preserves Scene-driven behavior. Manual controllers do not
register an update listener but still listen for Scene shutdown. Calling stepSimulation on a
host-owned controller throws before the runner, object, or diagnostics change. setTicking remains
a separate local pause in both modes. stepSimulation accepts seconds; do not pass Phaser update
milliseconds.
The final optional create/constructor argument extends BehaviorRunnerOptions with tickSource;
the same object can inject deterministic RNG, bounded diagnostics, or a pathReplanning cache
policy. Read snapshots and traces from controller.runner. The adapter strips tickSource and
forwards the remaining options to core. phaser-doom is the reference consumer: its behavior tree
chooses attack versus cached A* pursuit while its game core still validates every requested enemy
position against walls.
Public API
BehaviorController, BehaviorPlugin, BehaviorHooks, BehaviorControllerOptions,
BehaviorTickSource, and core behavior types are exported. The adapter owns Scene lifecycle only;
custom actions, combat effects, and entity destruction remain game-owned.
