blueshell
Version:
A Behavior Tree implementation in modern Javascript
44 lines • 1.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Switch = void 0;
const Constant_1 = require("./Constant");
const Parent_1 = require("./Parent");
const models_1 = require("../models");
/**
* Executes the child action associated with the first matching conditional.
* Returns defaultResult if no conditionals match.
*
* 11/9/21
* @author Timothy Deignan
*/
class Switch extends Parent_1.Parent {
constructor(name, entries, defaultResult = models_1.rc.SUCCESS) {
super(name);
this.defaultResult = defaultResult;
this.entries = entries.map((e) => {
return {
conditional: e.conditional || (() => true),
child: (0, models_1.isResultCode)(e.child) ? new Constant_1.Constant(e.child) : e.child,
};
});
this.children = this.entries.map((e) => e.child);
this.initChildren(this.children);
}
onEvent(state, event) {
const entry = this.entries.find((e) => e.conditional(state, event));
if (entry) {
return entry.child.handleEvent(state, event);
}
else {
return this.defaultResult;
}
}
getChildren() {
return this.children;
}
get symbol() {
return '?';
}
}
exports.Switch = Switch;
//# sourceMappingURL=Switch.js.map