behaviortree
Version:
A JavaScript implementation of Behavior Trees. They are useful for implementing AIs. For Browsers and NodeJS.
44 lines (43 loc) • 1.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const Node_1 = __importDefault(require("./Node"));
class Decorator extends Node_1.default {
constructor({ config = {}, ...props } = { config: {} }) {
super(props);
this.nodeType = 'Decorator';
this.setConfig(config);
}
decorate(run, blackboard, config) {
// This method should be overridden to make it useful
return run(run, blackboard, config);
}
run(blackboard, { introspector, rerun, registryLookUp = (x) => x, ...config } = {}) {
if (!rerun)
this.blueprint.start(blackboard);
let runCount = 0;
const result = this.decorate(() => {
++runCount;
return registryLookUp(this.blueprint.node).run(blackboard, {
...config,
rerun,
introspector,
registryLookUp
});
}, blackboard, this.config);
if (result !== constants_1.RUNNING) {
this.blueprint.end(blackboard);
}
if (introspector) {
introspector.wrapLast(runCount, this, result, blackboard);
}
return result;
}
setConfig(config) {
this.config = config;
}
}
exports.default = Decorator;