UNPKG

blueshell

Version:

A Behavior Tree implementation in modern Javascript

88 lines 2.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Composite = void 0; const Parent_1 = require("./Parent"); const models_1 = require("../models"); /** * Base class for all Composite Nodes (nodes which have an array of children). * Includes support for latching and keeping track of running status. * @author Joshua Chaitin-Pollak */ class Composite extends Parent_1.Parent { /** * @constructor * @param name * @param _children Children Nodes. * @param _latched */ constructor(name, _children, _latched = false) { super(name); this._children = _children; this._latched = _latched; this.initChildren(_children); } getChildren() { return this._children; } get latched() { return this._latched; } /** * Return an empty object * @ignore * @param state * @param event */ _beforeEvent(state, event) { const res = super._beforeEvent(state, event); const nodeStorage = this.getNodeStorage(state); const pStorage = this._privateStorage(state); const running = nodeStorage.running !== undefined ? nodeStorage.running : -1; this.getChildren().forEach((child, i) => { if (i < running) { // set event counter for each child before our latch point so they are registered // as participating in this event (using the previous results) (0, Parent_1.setEventCounter)(pStorage, state, child); } // else do nothing with the running node or nodes after latch point }); return res; } /** * Logging * @ignore * @param res * @param state * @param event */ _afterEvent(res, state, event) { res = super._afterEvent(res, state, event); if (this.latched) { const storage = this.getNodeStorage(state); // clear latched status if the node is no longer running after processing the event if (storage.running !== undefined && res !== models_1.resultCodes.RUNNING) { storage.running = undefined; } } return res; } /** * Invokes `handleChild` for each child. * @override * @param state * @param event */ onEvent(state, event) { const storage = this.getNodeStorage(state); let firstChild = 0; // Support for latched composites - ignored if not latched // wrapped for clarity - not programmatically necessary if (this.latched) { firstChild = storage.running !== undefined ? storage.running : 0; // Reset running storage.running = undefined; } return this.handleChild(state, event, firstChild); } } exports.Composite = Composite; //# sourceMappingURL=Composite.js.map