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

197 lines
7.2 KiB
TypeScript
Raw Normal View History

2024-06-07 01:45:52 -07:00
import { _decorator, CCFloat, CCInteger, Color, Component, Node, Prefab, randomRangeInt } from 'cc';
import { BoosterBase } from '../Booster/BoosterBase';
2024-03-11 04:12:22 -07:00
import GameState from '../Enum/GameState';
2024-03-12 01:50:54 -07:00
import ScoreType from '../Enum/ScoreType';
2024-06-07 01:45:52 -07:00
import { ScoreObject } from '../Environments/ScoreObject';
import GameEvent from '../Events/GameEvent';
import Gizmos2D from '../Gizmos/Gizmos2D';
import ObjectPool from '../Pool/ObjectPool';
import { EventManger } from './EventManger';
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;
2024-03-21 01:59:08 -07:00
@property({ type: CCInteger, 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 {
2024-03-12 01:50:54 -07:00
@property({ type: weightedObject, visible: true })
private _objects: weightedObject[] = [];
2024-03-27 04:00:23 -07:00
@property({ type: weightedObject, visible: true })
private _boosters: 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-28 20:35:44 -07:00
@property({ type: CCFloat, visible: true })
private readonly _spawnTime;
@property({ type: CCFloat, visible: true })
private readonly _spawnTimeWhenBoosterActive;
@property({ type: CCFloat, visible: true })
2024-03-27 04:00:23 -07:00
private _spawnBoosterTime;
2024-03-07 09:45:13 -08:00
private _pools: ObjectPool[] = [];
2024-03-27 04:00:23 -07:00
private _boosterPools: ObjectPool[] = [];
2024-03-07 09:45:13 -08:00
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-27 04:00:23 -07:00
private _timerBooster = 0;
2024-03-12 01:50:54 -07:00
private _weights: number[] = [];
2024-03-28 20:35:44 -07:00
private _boosterWeights: number[] = [];
2024-03-26 20:04:28 -07:00
private _isReplay = false;
2024-03-27 04:00:23 -07:00
private _boosterSpawned = false;
2024-03-28 20:35:44 -07:00
private _spawnTimeObject = 0;
2024-03-07 03:15:08 -08:00
2024-06-07 04:27:38 -07:00
onDrawGizmosSelected(): void {
2024-06-07 01:45:52 -07:00
for (let i = 0; i < this._spawnPoints.length; i++) {
Gizmos2D.beginColor(this.node, new Color(0, 255, 0, 180));
Gizmos2D.drawSolidCircle(this.node, this._spawnPoints[i].worldPosition, 15);
}
}
2024-03-07 03:15:08 -08:00
protected onLoad(): void {
2024-03-27 04:00:23 -07:00
EventManger.instance.on(GameEvent.ObjectRelease, 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-27 04:00:23 -07:00
EventManger.instance.on(GameEvent.BoosterActive, this.onBoosterActive, this);
2024-03-28 20:35:44 -07:00
EventManger.instance.on(GameEvent.BoosterDisable, this.onBoosterDisable, this);
this._spawnTimeObject = this._spawnTime;
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-27 04:00:23 -07:00
for (let i = 0; i < this._boosters.length; i++) {
const prefab = this._boosters[i].prefab;
2024-03-28 20:35:44 -07:00
this._boosterPools[i] = new ObjectPool(prefab, 2, true, BoosterBase);
2024-03-27 04:00:23 -07:00
}
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;
2024-03-28 20:35:44 -07:00
if (this._timer >= this._spawnTimeObject) {
2024-03-07 09:45:13 -08:00
this._timer = 0;
this.spawn();
}
2024-03-27 04:00:23 -07:00
if (this._boosterSpawned) return;
this._timerBooster += dt;
if (this._timerBooster >= this._spawnBoosterTime) {
this._timerBooster = 0;
this.spawnBooster();
}
2024-03-07 09:45:13 -08:00
}
private spawn() {
if (Object.keys(this._usedPoints).length == this._spawnPoints.length) return;
2024-03-12 21:36:19 -07:00
do {
2024-05-07 03:20:37 -07:00
var randomPool = this._pools.getRandom(this._weights);
2024-03-26 20:04:28 -07:00
var index = this._pools.indexOf(randomPool);
2024-03-12 21:36:19 -07:00
} while (this._objects[index].maxObjects != -1 && randomPool.countActive >= this._objects[index].maxObjects);
2024-03-27 04:00:23 -07: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 spawnBooster() {
if (Object.keys(this._usedPoints).length == this._spawnPoints.length) return;
do {
2024-05-07 03:20:37 -07:00
var randomPool = this._boosterPools.getRandom(this._boosterWeights);
2024-03-27 04:00:23 -07:00
var index = this._boosterPools.indexOf(randomPool);
} while (this._boosters[index].maxObjects != -1 && randomPool.countActive >= this._boosters[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);
2024-03-27 04:00:23 -07:00
2024-03-07 09:45:13 -08:00
const obj = randomPool.get(this.node);
obj.setWorldPosition(randomPoint.worldPosition);
this._usedPoints[obj.uuid] = randomPoint;
2024-03-27 04:00:23 -07:00
this._boosterSpawned = true;
2024-03-07 09:45:13 -08:00
}
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-27 04:00:23 -07:00
private onBoosterActive() {
this._boosterSpawned = false;
2024-03-28 20:35:44 -07:00
this._spawnTimeObject = this._spawnTimeWhenBoosterActive;
}
private onBoosterDisable() {
this._spawnTimeObject = this._spawnTime;
2024-03-27 04:00:23 -07:00
}
2024-03-28 20:35:44 -07:00
private onScore(score: number, points: number, type: ScoreType) {
2024-03-12 01:50:54 -07:00
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
});
2024-03-28 20:35:44 -07:00
this._boosters.forEach((object) => {
if (object.currentWeight >= object.maxWeight) {
object.currentWeight = object.maxWeight;
return;
}
object.currentWeight += object.weightStepOnGoal;
});
this._weights = this._objects.map((obj) => obj.currentWeight);
2024-03-28 20:35:44 -07:00
this._boosterWeights = this._boosters.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:
2024-03-26 20:04:28 -07:00
break;
2024-04-03 02:43:18 -07:00
case GameState.Ready:
break;
2024-03-26 20:04:28 -07:00
case GameState.Playing:
this._playing = true;
if (this._isReplay) return;
2024-03-12 21:54:27 -07:00
this._weights = this._objects.map((obj) => {
obj.currentWeight = obj.weight;
return obj.currentWeight;
});
2024-03-28 20:35:44 -07:00
this._boosterWeights = this._boosters.map((obj) => {
obj.currentWeight = obj.weight;
return obj.currentWeight;
});
2024-03-21 01:59:08 -07:00
for (let i = 0; i < randomRangeInt(5, 10); i++) {
this.spawn();
}
2024-03-11 04:12:22 -07:00
break;
case GameState.GameOver:
this._playing = false;
break;
2024-03-26 20:04:28 -07:00
case GameState.Relive:
this._isReplay = true;
break;
case GameState.End:
break;
2024-03-11 04:12:22 -07:00
}
}
2024-03-07 03:15:08 -08:00
}