blueshell
Version:
A Behavior Tree implementation in modern Javascript
54 lines • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sequence = void 0;
const Composite_1 = require("./Composite");
const models_1 = require("../models");
/**
* Sends an event to each child until one of the returns `FAILURE`, or `RUNNING`, then returns that value.
* If all children return `SUCCESS`, return `SUCCESS`.
* 1/10/16
* @author Joshua Chaitin-Pollak
*/
class Sequence extends Composite_1.Composite {
/**
* Recursively executes children until one of them returns
* failure. If we call all the children successfully, return success.
* @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 failure return success.
if (i >= this.getChildren().length) {
return models_1.rc.SUCCESS;
}
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.SUCCESS) {
// Call the next child
return this.handleChild(state_, event_, ++i);
}
else {
if (this.latched && res_ === models_1.rc.RUNNING) {
storage.running = i;
}
return res_;
}
}
/**
* @ignore
* @param res
* @param state
* @param event
*/
_afterChild(res, state, event) {
return { res, state, event };
}
get symbol() {
return '→';
}
}
exports.Sequence = Sequence;
//# sourceMappingURL=Sequence.js.map