UNPKG

blueshell

Version:

A Behavior Tree implementation in modern Javascript

54 lines 1.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Selector = void 0; const Composite_1 = require("./Composite"); const models_1 = require("../models"); /** * Selector Node (a.k.a. Fallback) * Sends an event to each child until one of them returns `SUCCESS` or `RUNNING`, then returns that value. * If all children return `FAILURE`, then this node returns `FAILURE`. * 1/18/16 * @author Joshua Chaitin-Pollak */ class Selector extends Composite_1.Composite { /** * Recursively sends the event to each child until one of them returns * success or running. If we exhaust all the children, return failure. * @param state The state when the event occured. * @param event The event to handle. * @param i The child index. */ handleChild(state, event, i) { const storage = this.getNodeStorage(state); // If we finished all processing without success return failure. if (i >= this.getChildren().length) { return models_1.rc.FAILURE; } const child = this.getChildren()[i]; const res = child.handleEvent(state, event); const { res: res_, state: state_, event: event_ } = this._afterChild(res, state, event); if (res_ !== models_1.rc.FAILURE) { if (this.latched && res_ === models_1.rc.RUNNING) { storage.running = i; } return res_; } else { return this.handleChild(state_, event_, ++i); } } /** * @ignore * @param res * @param state * @param event */ _afterChild(res, state, event) { return { res, state, event }; } get symbol() { return '∣'; } } exports.Selector = Selector; //# sourceMappingURL=Selector.js.map