AIAgent

@three-blocks/proAIAgentConfigurationEvents
new AIAgent()
Extends
THREE.EventDispatcher

AIAgent - Main thread interface for AI configuration and events

Provides a Three.js-style API for adding sensors and intents to agents. Extends EventDispatcher for AI event listening. All methods support variadic arguments for convenience.

Usage

Available Events (AIEvent constants)

Constant Event Source Payload
AIEvent.ENGAGE engage EngageIntent { targetPosition, targetDistance }
AIEvent.FIRE fire EngageIntent { origin, direction, target }
AIEvent.PATROL patrol PatrolIntent { waypoint, waypointIndex }
AIEvent.WAYPOINT_REACHED waypointReached PatrolIntent { waypointIndex, waypoint, isLast }
AIEvent.TARGET_ACQUIRED targetAcquired VisionSensor { targetIndex, targetType, distance, position }
AIEvent.TARGET_LOST targetLost VisionSensor { lastPosition }
AIEvent.DAMAGED damaged DamageSensor { damage, direction, shooterIndex, shooterType }
AIEvent.INTENT_CHANGE intentChange Coordinator { previous, current }
Example
import { AIEvent } from '@three-blocks/pro';

// Add single sensor
body.ai.addSensor(new VisionSensor({ fov: 90 }));

// Add multiple sensors at once
body.ai.addSensor(
  new VisionSensor({ fov: 90 }),
  new DamageSensor()
);

// Add intents with priority
body.ai.addIntent(
  new PatrolIntent({ waypoints: [...], priority: 20 }),
  new EngageIntent({ priority: 80 })
);

// Enable AI
body.ai.enable();

// Listen to AI events using constants
body.ai.addEventListener(AIEvent.FIRE, (event) => {
  createMuzzleFlash(event.origin);
  playSound('gunshot');
});

body.ai.addEventListener(AIEvent.ENGAGE, (event) => {
  console.log('Engaging target at distance:', event.targetDistance);
});

body.ai.addEventListener(AIEvent.TARGET_ACQUIRED, (event) => {
  console.log('Target spotted!');
});

// Chaining
body.ai
  .addSensor(new VisionSensor({ fov: 90 }))
  .addIntent(new PatrolIntent({ waypoints: [...] }))
  .enable();

Properties

.enabled :

Check if AI is enabled.

.currentIntent :

Get the current active intent name. Note: This is read from local state; worker state may differ slightly.

.sensorCount :

Get sensor count.

.intentCount :

Get intent count.

Methods

addSensor#

addSensor(sensors : Sensor) : AIAgent

Add one or more sensors to this agent.

Parameters
Sensors to add.
Returns
AIAgent — This instance for chaining.

addIntent#

addIntent(intents : Intent) : AIAgent

Add one or more intents to this agent.

Parameters
Intents to add.
Returns
AIAgent — This instance for chaining.

removeSensor#

removeSensor(sensor : Sensor) : AIAgent

Remove a sensor from this agent.

Parameters
Sensor to remove.
Returns
AIAgent — This instance for chaining.

removeIntent#

removeIntent(intent : Intent) : AIAgent

Remove an intent from this agent.

Parameters
Intent to remove.
Returns
AIAgent — This instance for chaining.

enable#

enable() : AIAgent

Enable AI control for this agent.

Returns
AIAgent — This instance for chaining.

disable#

disable() : AIAgent

Disable AI control for this agent.

Returns
AIAgent — This instance for chaining.

setCoordinatorConfig#

setCoordinatorConfig(config : Object) : AIAgent

Configure the coordinator.

Parameters
configObject
Coordinator configuration.
  • hysteresisThresholdoptionalnumber
    Priority difference to switch.
Returns
AIAgent — This instance for chaining.

dispose#

dispose()

Dispose of the AI agent.