UNPKG

phaser4-rex-plugins

Version:
58 lines (49 loc) 1.42 kB
import Composite from '../Composite.js'; class RandomSelector extends Composite { constructor( { children = [], services, title, name = 'RandomSelector' } = {}, nodePool ) { super( { children, services, title, name, }, nodePool ); } open(tick) { var nodeMemory = this.getNodeMemory(tick); nodeMemory.$runningChild = -1; // No running child } tick(tick) { if (this.children.length === 0) { return ERROR; } var nodeMemory = this.getNodeMemory(tick); var childIndex = nodeMemory.$runningChild; if (childIndex < 0) { childIndex = Math.floor(Math.random() * this.children.length); } 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 RandomSelector;