UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

70 lines (48 loc) 1.7 kB
import { seededRandom } from "../../../../core/math/random/seededRandom.js"; import { GridCellAction } from "../GridCellAction.js"; import { assert } from "../../../../core/assert.js"; import { randomFromArray } from "../../../../core/math/random/randomFromArray.js"; export class CellActionSelectRandom extends GridCellAction { constructor() { super(); /** * * @type {function} * @private */ this.__random = seededRandom(0); /** * * @type {GridCellAction[]} */ this.options = []; } /** * * @param {GridCellAction[]} options * @returns {CellActionSelectRandom} */ static from(options) { assert.isArray(options, 'options'); assert.greaterThan(options.length, 0, 'number of options must be greater than 0'); const r = new CellActionSelectRandom(); r.options = options; return r; } initialize(data, seed) { super.initialize(data, seed); this.__random.setCurrentSeed(seed); const options = this.options; const n = options.length; assert.greaterThan(n, 0, 'number of options must be greater than 0') for (let i = 0; i < n; i++) { const action = options[i]; assert.equal(action.isGridCellAction, true, 'action.isGridCellAction !== true'); action.initialize(data, seed); } } execute(data, x, y, rotation) { const option = randomFromArray(this.__random, this.options); option.execute(data, x, y, rotation); } }