behaviortree
Version:
A JavaScript implementation of Behavior Trees. They are useful for implementing AIs. For Browsers and NodeJS.
32 lines (31 loc) • 1.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const NOOP_RUN = () => false;
const NOOP_START = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
const NOOP_END = () => { }; // eslint-disable-line @typescript-eslint/no-empty-function
class Node {
constructor({ run = NOOP_RUN, start = NOOP_START, end = NOOP_END, ...props }) {
this.nodeType = 'Node';
this.blueprint = { run, start, end, ...props };
}
run(blackboard, { introspector, rerun = false, registryLookUp = (x) => x, ...config } = {}) {
if (!rerun)
this.blueprint.start(blackboard);
const result = this.blueprint.run(blackboard, { ...config, rerun, registryLookUp });
if (result !== constants_1.RUNNING) {
this.blueprint.end(blackboard);
}
if (introspector) {
introspector.push(this, result, blackboard);
}
return result;
}
get name() {
return this._name || this.blueprint.name;
}
set name(name) {
this._name = name;
}
}
exports.default = Node;