blueshell
Version:
A Behavior Tree implementation in modern Javascript
52 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Decorator = void 0;
const Composite_1 = require("./Composite");
const models_1 = require("../models");
/**
* Base Class for all Decorator Nodes. Can only have one child.
* Decorators intercept and can modify the event sent to or the result from the child.
* They should do this by overriding one or more of the methods decorateEvent, decorateCall, or decorateResult.
* @author Joshua Chaitin-Pollak
*/
class Decorator extends Composite_1.Composite {
/**
* Can only pass in one child.
* @constructor
* @param name
* @param child
*/
constructor(name, child, latched = true) {
super(name, [child], latched);
}
get child() {
return this.getChildren()[0];
}
/**
* Passthrough to child Node.
* @param state
* @param event
*/
handleChild(state, event) {
// Passthrough
event = this.decorateEvent(event, state);
const res = this.decorateResult(this.decorateCall((state, event) => this.child.handleEvent(state, event), state, event), state, event);
if (this.latched && res === models_1.rc.RUNNING) {
const storage = this.getNodeStorage(state);
storage.running = 0;
}
return res;
}
decorateEvent(event, state) {
return event;
}
decorateCall(handleEvent, state, event) {
return handleEvent(state, event);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
decorateResult(res, state, event) {
return res;
}
}
exports.Decorator = Decorator;
//# sourceMappingURL=Decorator.js.map