@petkoneo/phaser3-rex-plugins
Version:
65 lines (54 loc) • 1.47 kB
JavaScript
import Decorator from '../Decorator.js';
import { SUCCESS, ERROR, FAILURE } from '../../constants.js';
class RepeatUntilSuccess extends Decorator {
constructor(
{
maxLoop = -1,
child = null,
title,
name = 'RepeatUntilSuccess'
} = {},
nodePool
) {
super(
{
child,
title,
name,
properties: {
maxLoop
},
},
nodePool
);
this.maxLoopExpression = this.addExpression(maxLoop);
}
open(tick) {
var nodeMemory = this.getNodeMemory(tick);
nodeMemory.$maxLoop = tick.evalExpression(this.maxLoopExpression);
nodeMemory.$i = 0;
}
tick(tick) {
if (!this.child) {
return ERROR;
}
// Won't abort child
var nodeMemory = this.getNodeMemory(tick);
var maxLoop = nodeMemory.$maxLoop;
var i = nodeMemory.$i;
var status = ERROR;
// Open child before exceed maxLoop
// Execute child many times in a tick
while (maxLoop < 0 || i < maxLoop) {
status = this.child._execute(tick);
if (status === FAILURE) {
i++;
} else {
break;
}
}
nodeMemory.$i = i;
return status;
}
};
export default RepeatUntilSuccess;