pinball/assets/_Game/Scripts/Manager/SpawnObjectManager.ts

124 lines
4.3 KiB
TypeScript
Raw Normal View History

2024-03-12 01:50:54 -07:00
import { _decorator, CCFloat, CCInteger, Component, Node, Prefab, randomRangeInt } from 'cc';
2024-03-07 09:45:13 -08:00
import ObjectPool from '../Pool/ObjectPool';
import { ScoreObject } from '../Environments/ScoreObject';
import { EventManger } from './EventManger';
import GameEvent from '../Events/GameEvent';
2024-03-11 04:12:22 -07:00
import GameState from '../Enum/GameState';
import Utilities from '../Utilities';
2024-03-12 01:50:54 -07:00
import ScoreType from '../Enum/ScoreType';
2024-03-07 03:15:08 -08:00
const { ccclass, property } = _decorator;
2024-03-12 01:50:54 -07:00
@ccclass('weightedObject')
class weightedObject {
@property(Prefab)
public prefab: Prefab;
@property(CCInteger)
public readonly weight = 0;
2024-03-12 01:50:54 -07:00
@property(CCInteger)
public weightStepOnGoal = 0;
@property(CCInteger)
public maxWeight = 0;
2024-03-12 21:36:19 -07:00
@property(CCInteger)
public maxObjects = -1;
@property({ readonly: true })
public currentWeight;
2024-03-12 01:50:54 -07:00
}
2024-03-07 03:15:08 -08:00
@ccclass('SpawnObjectManager')
export class SpawnObjectManager extends Component {
//#region singleton
private static _instance: SpawnObjectManager = null;
public static get instance(): SpawnObjectManager {
return SpawnObjectManager._instance;
}
//#endregion
2024-03-12 01:50:54 -07:00
@property({ type: weightedObject, visible: true })
private _objects: weightedObject[] = [];
2024-03-08 03:07:41 -08:00
@property({ type: Node, visible: true })
2024-03-07 03:15:08 -08:00
private _spawnPoints: Node[] = [];
2024-03-07 09:45:13 -08:00
@property({ type: CCFloat, visible: true, range: [1, 10], slide: true })
private _spawnTime;
private _pools: ObjectPool[] = [];
private _usedPoints: { [key: string]: Node } = {};
2024-03-11 04:12:22 -07:00
private _playing = false;
2024-03-07 09:45:13 -08:00
private _timer = 0;
2024-03-12 01:50:54 -07:00
private _weights: number[] = [];
2024-03-07 03:15:08 -08:00
protected onLoad(): void {
SpawnObjectManager._instance = this;
2024-03-07 09:45:13 -08:00
EventManger.instance.on(GameEvent.ScoreObjectRelease, this.onObjectRelease, this);
2024-03-11 04:12:22 -07:00
EventManger.instance.on(GameEvent.GameStateChange, this.onGameStateChange, this);
2024-03-12 01:50:54 -07:00
EventManger.instance.on(GameEvent.Score, this.onScore, this);
2024-03-07 09:45:13 -08:00
for (let i = 0; i < this._objects.length; i++) {
2024-03-12 01:50:54 -07:00
const prefab = this._objects[i].prefab;
2024-03-07 09:45:13 -08:00
this._pools[i] = new ObjectPool(prefab, 10, true, ScoreObject);
}
2024-03-12 01:50:54 -07:00
this._weights = this._objects.map((obj) => {
obj.currentWeight = obj.weight;
return obj.currentWeight;
});
2024-03-07 09:45:13 -08:00
}
protected update(dt: number): void {
2024-03-11 04:12:22 -07:00
if (!this._playing) return;
2024-03-07 09:45:13 -08:00
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;
2024-03-12 21:36:19 -07:00
do {
var [randomPool, index] = Utilities.weightedRandom(this._pools, this._weights);
} while (this._objects[index].maxObjects != -1 && randomPool.countActive >= this._objects[index].maxObjects);
2024-03-07 09:45:13 -08:00
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];
2024-03-07 03:15:08 -08:00
}
2024-03-11 04:12:22 -07:00
2024-03-12 01:50:54 -07:00
private onScore(score: number, type: ScoreType) {
if (type == ScoreType.Goal) {
this._objects.forEach((object) => {
if (object.currentWeight >= object.maxWeight) {
object.currentWeight = object.maxWeight;
2024-03-12 01:50:54 -07:00
return;
}
object.currentWeight += object.weightStepOnGoal;
2024-03-12 01:50:54 -07:00
});
this._weights = this._objects.map((obj) => obj.currentWeight);
2024-03-12 01:50:54 -07:00
}
}
2024-03-11 04:12:22 -07:00
private onGameStateChange(state: GameState) {
switch (state) {
case GameState.Init:
break;
case GameState.Playing:
this._playing = true;
for (let i = 0; i < randomRangeInt(5, 10); i++) {
this.spawn();
}
break;
case GameState.GameOver:
this._playing = false;
this._objects.forEach((object) => (object.currentWeight = object.weight));
2024-03-11 04:12:22 -07:00
this._pools.forEach((pool) => pool.releaseAll());
break;
}
}
2024-03-07 03:15:08 -08:00
}