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

90 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-03-06 10:08:30 -08:00
import { _decorator, Component, Prefab, Node, Vec2, Vec3, randomRangeInt, CCInteger, Input } from 'cc';
2024-03-06 03:09:17 -08:00
import ObjectPool from '../Pool/ObjectPool';
2024-03-06 07:10:31 -08:00
import { Ball } from '../Gameplay/Ball';
2024-03-06 10:08:30 -08:00
import Utilities from '../Utilities/Utilities';
import GameState from '../Enum/GameState';
import { EventManger } from './EventManger';
2024-03-07 03:15:08 -08:00
import GameEvent from '../Events/GameEvent';
import ScoreType from '../Enum/ScoreType';
2024-03-06 01:28:01 -08:00
const { ccclass, property } = _decorator;
@ccclass('GameManager')
export class GameManager extends Component {
//singleton
private static _instance: GameManager = null;
public static get instance(): GameManager {
return GameManager._instance;
}
2024-03-06 03:09:17 -08:00
@property({ type: Prefab, visible: true })
private _ballPrefab: Prefab;
2024-03-07 03:15:08 -08:00
@property({ visible: true })
2024-03-06 10:08:30 -08:00
private _ballSpawnPosition: Vec3;
@property({ type: CCInteger, visible: true })
private _balls = 3;
2024-03-06 03:09:17 -08:00
private _ballPool: ObjectPool;
2024-03-06 10:08:30 -08:00
private _gameState = GameState.Init;
2024-03-06 03:09:17 -08:00
2024-03-06 01:28:01 -08:00
public highestStreak: number;
2024-03-07 03:15:08 -08:00
private _score = 0;
2024-03-06 01:28:01 -08:00
2024-03-06 03:09:17 -08:00
protected onLoad(): void {
2024-03-06 10:08:30 -08:00
GameManager._instance = this;
2024-03-07 03:15:08 -08:00
this._ballPool = new ObjectPool(this._ballPrefab, 10, true, Ball);
2024-03-06 03:09:17 -08:00
}
2024-03-07 03:15:08 -08:00
protected start() {
this.spawnBall();
2024-03-06 10:08:30 -08:00
}
private changeGameState(state: GameState) {
this._gameState = state;
EventManger.instance.emit(GameEvent.GameStateChange, this._gameState);
}
2024-03-07 03:15:08 -08:00
private addScore(score: number, type: ScoreType) {
this._score += score;
console.log('score:' + this._score);
EventManger.instance.emit(GameEvent.Score, [type, this._score]);
}
2024-03-06 10:08:30 -08:00
public spawnBall(): Ball {
const ball = this._ballPool.get(this.node, Ball);
ball.node.setPosition(this._ballSpawnPosition);
let dir = randomRangeInt(-1, 2);
while (dir == 0) {
dir = randomRangeInt(-1, 2);
}
const force = new Vec2(dir, 1);
2024-03-07 03:15:08 -08:00
ball.throwBall(force.multiply2f(1, 40));
2024-03-06 10:08:30 -08:00
return ball;
}
public async ballOut(ball: Node) {
this._balls--;
2024-03-07 03:15:08 -08:00
this._ballPool.release(ball);
2024-03-06 10:08:30 -08:00
if (this._balls === 0) {
return;
}
2024-03-07 03:15:08 -08:00
await Utilities.delay(1000);
this.spawnBall();
}
public async goal(bonusScore: number, ball: Node) {
2024-03-06 10:08:30 -08:00
this._ballPool.release(ball);
2024-03-07 03:15:08 -08:00
this.addScore(bonusScore, ScoreType.Goal);
2024-03-06 10:08:30 -08:00
await Utilities.delay(1000);
2024-03-07 03:15:08 -08:00
this.spawnBall();
}
public destroyEnviromentsObject(bonusScore: number) {
this.addScore(bonusScore, ScoreType.DestroyObject);
2024-03-06 10:08:30 -08:00
}
2024-03-06 01:28:01 -08:00
public onRevive() {
throw new Error('Method not implemented.');
}
}