blueshell
Version:
A Behavior Tree implementation in modern Javascript
60 lines • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LatchedIfElse = void 0;
/**
* Created by masdoorian on 06/23/21
*/
const IfElse_1 = require("./IfElse");
const models_1 = require("../models");
const LATCHED_RUNNING_CONSEQUENT = 0;
const LATCHED_RUNNING_ALTERNATIVE = 1;
/**
* Latched If-Else Latched Composite Node.
*
* The conditional function is not called if the previous result was RUNNING - instead
* the saved result of calling the conditional is used until the consequent/alternative
* returns not RUNNING again and the state is reset
*
* If `conditional(state: S, event: E)` returns true,
* control is passed to the consequent node.
*
* If `conditional(state: S, event: E)` returns false,
* control is passed to the alternative node, or
* if alternative is a result code, that is returned, or
* if one is not provided, 'FAILURE' is returned.
*
* 06/23/21
* @author Mark Asdoorian
*/
class LatchedIfElse extends IfElse_1.IfElse {
constructor(name, conditionalToLatch, consequent, alternative) {
super(name, (state, event) => {
const storage = this.getNodeStorage(state);
let res;
if (storage.running === undefined) {
// we're not latched, so run the conditional and save the result
res = this.conditionalToLatch(state, event);
storage.running = res ? LATCHED_RUNNING_CONSEQUENT : LATCHED_RUNNING_ALTERNATIVE;
}
else {
// we're latched, so return the saved result of the conditional
res = storage.running === LATCHED_RUNNING_CONSEQUENT;
}
return res;
}, consequent, alternative);
this.conditionalToLatch = conditionalToLatch;
}
get latched() {
return true;
}
_afterEvent(res, state, event) {
res = super._afterEvent(res, state, event);
if (res !== models_1.rc.RUNNING) {
const storage = this.getNodeStorage(state);
storage.running = undefined;
}
return res;
}
}
exports.LatchedIfElse = LatchedIfElse;
//# sourceMappingURL=LatchedIfElse.js.map