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

97 lines
3.2 KiB
TypeScript

import { _decorator, Component, Node, Prefab, Vec2, Vec3, randomRangeInt, CCInteger } from 'cc';
import ObjectPool from '../Pool/ObjectPool';
import { Ball } from '../Gameplay/Ball';
import Utilities from '../Utilities/Utilities';
import GameState from '../Enum/GameState';
import { EventManger } from './EventManger';
import GameEvent from '../Events/GameEvent';
import ScoreType from '../Enum/ScoreType';
import { FloatingText } from '../Environments/FloatingText';
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;
}
@property({ type: Prefab, visible: true })
private _ballPrefab: Prefab;
@property({ type: Prefab, visible: true })
private _floatingScoreText: Prefab;
@property({ type: Node, visible: true })
private _floatingTextContainer: Node;
@property({ visible: true })
private _ballSpawnPosition: Vec3;
@property({ type: CCInteger, visible: true })
private _balls = 3;
private _ballPool: ObjectPool;
private _FloatingScorePool: ObjectPool;
private _gameState = GameState.Init;
public highestStreak: number;
private _score = 0;
protected onLoad(): void {
GameManager._instance = this;
this._ballPool = new ObjectPool(this._ballPrefab, 10, true, Ball);
this._FloatingScorePool = new ObjectPool(this._floatingScoreText, 10, true);
}
protected start() {
this.spawnBall();
}
private changeGameState(state: GameState) {
this._gameState = state;
EventManger.instance.emit(GameEvent.GameStateChange, this._gameState);
}
private addScore(score: number, type: ScoreType, position: Vec3) {
this._score += score;
const floatingScore = this._FloatingScorePool.get(this._floatingTextContainer, FloatingText);
floatingScore.show(`+${score}`, position, score >= 100 ? 1.5 : 1, score >= 100 ? 1 : 0.7);
EventManger.instance.emit(GameEvent.Score, [type, this._score]);
}
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);
ball.throwBall(force.multiply2f(1, 40));
return ball;
}
public async ballOut() {
this._balls--;
EventManger.instance.emit(GameEvent.BallOut, null);
if (this._balls === 0) {
this._ballPool.clear();
return;
}
await Utilities.delay(1000);
this.spawnBall();
}
public async goal(bonusScore: number, position: Vec3) {
this.addScore(bonusScore, ScoreType.Goal, position);
await Utilities.delay(1000);
this.spawnBall();
}
public destroyEnviromentsObject(bonusScore: number, position: Vec3) {
this.addScore(bonusScore, ScoreType.DestroyObject, position);
}
public onRevive() {
throw new Error('Method not implemented.');
}
}