AIAgent
new AIAgent()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) : AIAgentAdd one or more sensors to this agent.
Parameters
Returns
AIAgent — This instance for chaining.addIntent#
addIntent(intents : Intent) : AIAgentAdd one or more intents to this agent.
Parameters
Returns
AIAgent — This instance for chaining.removeSensor#
removeSensor(sensor : Sensor) : AIAgentRemove a sensor from this agent.
Parameters
Returns
AIAgent — This instance for chaining.removeIntent#
removeIntent(intent : Intent) : AIAgentRemove an intent from this agent.
Parameters
Returns
AIAgent — This instance for chaining.enable#
enable() : AIAgentEnable AI control for this agent.
Returns
AIAgent — This instance for chaining.disable#
disable() : AIAgentDisable AI control for this agent.
Returns
AIAgent — This instance for chaining.setCoordinatorConfig#
setCoordinatorConfig(config : Object) : AIAgentConfigure the coordinator.
Parameters
configObjectCoordinator configuration.
hysteresisThresholdoptionalnumberPriority difference to switch.
Returns
AIAgent — This instance for chaining.dispose#
dispose()Dispose of the AI agent.