UNPKG

blueshell

Version:

A Behavior Tree implementation in modern Javascript

74 lines 2.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IfElse = void 0; /** * Created by jpollak on 5/29/16. */ const Constant_1 = require("./Constant"); const Parent_1 = require("./Parent"); const models_1 = require("../models"); /** * If-Else Conditional Composite Node. * * 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. * * 5/29/16 * @author Joshua Chaitin-Pollak */ class IfElse extends Parent_1.Parent { constructor(name, conditional, consequent, alternative) { super(name); this.conditional = conditional; this.consequent = consequent; this.children = [consequent]; if (!!alternative) { if ((0, models_1.isResultCode)(alternative)) { this.alternative = new Constant_1.Constant(alternative); } else { this.alternative = alternative; } this.children.push(this.alternative); } this.initChildren(this.children); } /** * Returns the children of this node, i.e. the `consequent` and the optional `alternative`. * this is to support the DOT & Archy visualizers * see Composite<S,E>.addEventCounterToChildren */ getChildren() { return this.children; } /** * If `conditional` resolves to `true`, then the `consequent` node handles the event. * Otherwise, the `alternative` node handles the event. * If no `alternative` is provided, this node resolves to `FAILURE` * @override * @param state The state when the event occured. * @param event The event to handle. * @param i The child index. */ onEvent(state, event) { if (this.conditional(state, event)) { return this.consequent.handleEvent(state, event); } else if (this.alternative) { return this.alternative.handleEvent(state, event); } else { return models_1.rc.FAILURE; } } get symbol() { return '?'; } } exports.IfElse = IfElse; //# sourceMappingURL=IfElse.js.map