UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

79 lines (69 loc) 2.26 kB
import Signal from "../../../core/events/signal/Signal.js"; import { BehaviorStatus } from "./BehaviorStatus.js"; /** * Base class of behavior tree implementation * @see "The Behavior Tree Starter Kit" by Alex J. Champandard and Philip Dunstan * @see https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control) * @template CTX * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class Behavior { /** * Any internal state used by the behavior * This provides a way to interact with the outside as well as to pass data between themselves * @type {CTX|null} */ context = null; /** * Dispatched after initialization is complete * @readonly * @type {Signal<this,CTX>} */ onInitialized = new Signal(); /** * Dispatched after finalization is complete * @readonly * @type {Signal<this>} */ onFinalized = new Signal(); /** * Main update function. Every behavior executes some logic, some behaviors are long-running and some are instantaneous * @param {number} timeDelta time step in seconds * @returns {BehaviorStatus} signals status of the behavior, "success" or "failure" are used to signal completion of the behavior */ tick(timeDelta) { throw new Error('Abstract method, needs to be overridden'); } /** * Called before behavior gets executed via {@link #tick} for the first time * Used to prepare the behavior for execution * You can think of it as "start" * @param {CTX} [context] */ initialize(context) { this.context = context; this.onInitialized.send2(this, context); } /** * Called when behavior is finished, or interrupted * Used to clean up any resources * You can think of it as "stop" */ finalize() { // extend in subclasses as necessary this.onFinalized.send1(this); } } /** * Useful for type checking * @readonly * @type {boolean} */ Behavior.prototype.isBehavior = true; /** * Useful for type checking * @readonly * @type {string} */ Behavior.typeName = "Behavior";