UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

88 lines (69 loc) 1.87 kB
import { UINT32_MAX } from "../../../../core/binary/UINT32_MAX.js"; import { NumericInterval } from "../../../../core/math/interval/NumericInterval.js"; import { seededRandom } from "../../../../core/math/random/seededRandom.js"; import { Behavior } from "../Behavior.js"; import { BehaviorStatus } from "../BehaviorStatus.js"; function nextSeed() { return Date.now() ^ (Math.random() * UINT32_MAX); } /** * Wait for a certain amount of time * @author Alex Goldring * @copyright Company Named Limited (c) 2025 */ export class RandomDelayBehavior extends Behavior { /** * Delay value in seconds * @type {number} */ value = 0; /** * * @type {NumericInterval} */ limits = new NumericInterval(1, 1); /** * Time elapsed * @private * @type {number} */ elapsed = 0; random = seededRandom(nextSeed()) fromJSON({ limits, elapsed = 0 }) { this.limits.fromJSON(limits); this.elapsed = elapsed; } /** * * @param json * @return {RandomDelayBehavior} */ static fromJSON(json) { const r = new RandomDelayBehavior(); r.fromJSON(json); return r; } /** * * @return {RandomDelayBehavior} * @param min * @param max */ static from(min, max) { const r = new RandomDelayBehavior(); r.limits.set(min, max); return r; } initialize(ctx) { super.initialize(ctx); this.elapsed = 0; this.value = this.limits.sampleRandom(this.random) } tick(timeDelta) { this.elapsed += timeDelta; if (this.elapsed >= this.value) { return BehaviorStatus.Succeeded; } return BehaviorStatus.Running; } }