import { _decorator, CCFloat, CCInteger, Component, Node, Prefab, randomRangeInt } from 'cc'; import ObjectPool from '../Pool/ObjectPool'; import { ScoreObject } from '../Environments/ScoreObject'; import { EventManger } from './EventManger'; import GameEvent from '../Events/GameEvent'; import GameState from '../Enum/GameState'; import Utilities from '../Utilities'; import ScoreType from '../Enum/ScoreType'; const { ccclass, property } = _decorator; @ccclass('weightedObject') class weightedObject { @property(Prefab) public prefab: Prefab; @property(CCInteger) public readonly weight = 0; @property(CCInteger) public weightStepOnGoal = 0; @property(CCInteger) public maxWeight = 0; @property(CCInteger) public maxObjects = -1; @property({ type: CCInteger, readonly: true }) public currentWeight; } @ccclass('SpawnObjectManager') export class SpawnObjectManager extends Component { //#region singleton private static _instance: SpawnObjectManager = null; public static get instance(): SpawnObjectManager { return SpawnObjectManager._instance; } //#endregion @property({ type: weightedObject, visible: true }) private _objects: weightedObject[] = []; @property({ type: Node, visible: true }) private _spawnPoints: Node[] = []; @property({ type: CCFloat, visible: true, range: [1, 10], slide: true }) private _spawnTime; private _pools: ObjectPool[] = []; private _usedPoints: { [key: string]: Node } = {}; private _playing = false; private _timer = 0; private _weights: number[] = []; protected onLoad(): void { SpawnObjectManager._instance = this; EventManger.instance.on(GameEvent.ScoreObjectRelease, this.onObjectRelease, this); EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this); EventManger.instance.on(GameEvent.Score, this.onScore, this); for (let i = 0; i < this._objects.length; i++) { const prefab = this._objects[i].prefab; this._pools[i] = new ObjectPool(prefab, 10, true, ScoreObject); } } protected update(dt: number): void { if (!this._playing) return; this._timer += dt; if (this._timer >= this._spawnTime) { this._timer = 0; this.spawn(); } } private spawn() { if (Object.keys(this._usedPoints).length == this._spawnPoints.length) return; do { var [randomPool, index] = Utilities.weightedRandom(this._pools, this._weights); } while (this._objects[index].maxObjects != -1 && randomPool.countActive >= this._objects[index].maxObjects); do { var randomPoint = this._spawnPoints[randomRangeInt(0, this._spawnPoints.length)]; } while (Object.values(this._usedPoints).indexOf(randomPoint) != -1); const obj = randomPool.get(this.node); obj.setWorldPosition(randomPoint.worldPosition); this._usedPoints[obj.uuid] = randomPoint; } private onObjectRelease(obj: Node) { delete this._usedPoints[obj.uuid]; } private onScore(score: number, type: ScoreType) { if (type == ScoreType.Goal) { this._objects.forEach((object) => { if (object.currentWeight >= object.maxWeight) { object.currentWeight = object.maxWeight; return; } object.currentWeight += object.weightStepOnGoal; }); this._weights = this._objects.map((obj) => obj.currentWeight); } } private onGameStateChange(state: GameState) { switch (state) { case GameState.Init: this._weights = this._objects.map((obj) => { obj.currentWeight = obj.weight; return obj.currentWeight; }); for (let i = 0; i < randomRangeInt(5, 10); i++) { this.spawn(); } break; case GameState.Playing: this._playing = true; break; case GameState.GameOver: this._playing = false; break; } } }