blueshell
Version:
A Behavior Tree implementation in modern Javascript
69 lines • 2.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.While = void 0;
const models_1 = require("../../models");
const Base_1 = require("../Base");
const Decorator_1 = require("../Decorator");
const Parent_1 = require("../Parent");
/**
* Given a conditional, have the child Node handle an event given a while condition
* 11/9/21
* @author Timothy Deignan
*/
class While extends Decorator_1.Decorator {
constructor(desc, conditional, child, defaultResult = models_1.rc.SUCCESS) {
super('While-' + desc, child);
this.conditional = conditional;
this.defaultResult = defaultResult;
}
// override getNodeStorage to explicitly return WhileNodeStorage so consumers don't need to cast types
getNodeStorage(state) {
return super.getNodeStorage(state);
}
decorateCall(handleEvent, state, event) {
const storage = this.getNodeStorage(state);
if (storage.running !== undefined || this.conditional(state, event)) {
if (storage.beganAtLeastOneLoop) {
Base_1.Action.treePublisher.publishResult(state, event, false);
(0, Parent_1.clearEventSeenRecursive)(this.child, state);
}
storage.beganAtLeastOneLoop = true;
return handleEvent(state, event);
}
else {
storage.break = true;
return storage.lastLoopResult || this.defaultResult;
}
}
_afterEvent(res, state, event) {
res = super._afterEvent(res, state, event);
const storage = this.getNodeStorage(state);
if (res === models_1.rc.RUNNING) {
// yield to the behavior tree because the child node is running
return res;
}
else if (storage.break) {
// teardown internal state and yield to the behavior tree because the loop has completed
// FIXME - It is likely that if While is used as the root node, then that lastEventSeen
// property of the descendants may not be correct due to the extra call to _beforeEvent
// required for the loop to break.
storage.beganAtLeastOneLoop = undefined;
storage.lastLoopResult = undefined;
storage.break = undefined;
return res;
}
else {
// begin another iteration of the loop
storage.lastLoopResult = res;
return this.handleEvent(state, event);
}
}
onEvent(state, event) {
return this.handleChild(state, event);
}
get symbol() {
return '↻';
}
}
exports.While = While;
//# sourceMappingURL=While.js.map