blueshell
Version:
A Behavior Tree implementation in modern Javascript
42 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LatchedSwitch = void 0;
const Switch_1 = require("./Switch");
const models_1 = require("../models");
/**
* Executes the child action associated with the first matching conditional.
* Latches on that child action without re-checking the corresponding conditional until
* it returns a result other than RUNNING. Returns defaultResult if no conditionals match.
*
* 11/9/21
* @author Timothy Deignan
*/
class LatchedSwitch extends Switch_1.Switch {
onEvent(state, event) {
const storage = this.getNodeStorage(state);
if (storage.running !== undefined) {
const entry = this.entries[storage.running];
return entry.child.handleEvent(state, event);
}
const entry = this.entries.find((e) => e.conditional(state, event));
if (entry) {
const storage = this.getNodeStorage(state);
storage.running = this.entries.indexOf(entry);
return entry.child.handleEvent(state, event);
}
return this.defaultResult;
}
_afterEvent(res, state, event) {
res = super._afterEvent(res, state, event);
if (res !== models_1.rc.RUNNING) {
const storage = this.getNodeStorage(state);
storage.running = undefined;
}
return res;
}
get latched() {
return true;
}
}
exports.LatchedSwitch = LatchedSwitch;
//# sourceMappingURL=LatchedSwitch.js.map