@petkoneo/phaser3-rex-plugins
Version:
87 lines (74 loc) • 2.3 kB
JavaScript
import Composite from '../Composite.js';
import { SUCCESS, FAILURE, RUNNING, ERROR } from '../../constants.js';
class IfSelector extends Composite {
constructor(
{
expression = 'true',
conditionEvalBreak = false,
children = [],
services,
title,
name = 'IfSelector'
} = {},
nodePool
) {
super(
{
children: children,
services,
title,
name,
properties: {
expression,
conditionEvalBreak,
},
},
nodePool
);
this.expression = this.addBooleanExpression(expression);
this.conditionEvalBreak = conditionEvalBreak;
this.forceSelectChildIndex = undefined;
}
open(tick) {
var nodeMemory = this.getNodeMemory(tick);
nodeMemory.$runningChild = -1; // No running child
}
setSelectChildIndex(index) {
this.forceSelectChildIndex = index;
return this;
}
evalCondition(tick) {
if (this.forceSelectChildIndex !== undefined) {
return this.forceSelectChildIndex;
}
return tick.evalExpression(this.expression) ? 0 : 1;
}
tick(tick) {
if (this.children.length === 0) {
return ERROR;
}
var nodeMemory = this.getNodeMemory(tick);
var childIndex = nodeMemory.$runningChild;
if (childIndex < 0) {
childIndex = this.evalCondition(tick);
if (this.conditionEvalBreak) {
// Resolve runningChild index, but not run child now
nodeMemory.$runningChild = childIndex;
return RUNNING;
}
}
var child = this.children[childIndex];
var status = child._execute(tick);
nodeMemory.$runningChild = (status === RUNNING) ? childIndex : -1;
return status;
}
abortChildren(tick) {
var nodeMemory = this.getNodeMemory(tick);
var child = this.children[nodeMemory.$runningChild];
if (child) {
child._abort(tick);
nodeMemory.$runningChild = -1;
}
}
};
export default IfSelector;