2024-02-28 03:25:11 -08:00
|
|
|
import {
|
|
|
|
_decorator,
|
|
|
|
CCInteger,
|
|
|
|
Component,
|
|
|
|
Enum,
|
|
|
|
EventTarget,
|
|
|
|
instantiate,
|
|
|
|
Node,
|
|
|
|
ParticleSystem,
|
|
|
|
Prefab,
|
|
|
|
randomRangeInt,
|
|
|
|
RigidBody2D,
|
|
|
|
Vec2,
|
2024-03-03 00:23:29 -08:00
|
|
|
Vec3,
|
2024-02-28 03:25:11 -08:00
|
|
|
} from 'cc';
|
2024-02-27 18:19:33 -08:00
|
|
|
import { Ball } from './Ball';
|
|
|
|
import { EventType } from './Enums';
|
|
|
|
import { SoundManager } from './SoundManager';
|
|
|
|
import BEConnector from './BEConnector';
|
|
|
|
import { UIController } from './UIController';
|
|
|
|
import { LevelController } from './LevelController';
|
2024-02-28 03:25:11 -08:00
|
|
|
import Utilities from './Utilities/Utilities';
|
2024-02-27 18:19:33 -08:00
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
2024-02-28 03:25:11 -08:00
|
|
|
window.addEventListener('message', (data) => {
|
|
|
|
const { data: res } = data;
|
|
|
|
const objectRes = Utilities.getJson(res);
|
|
|
|
if (objectRes) {
|
|
|
|
const { type, value } = objectRes;
|
|
|
|
if (type === 'newTicket') {
|
|
|
|
BEConnector.instance.numberTicket += value;
|
|
|
|
GameplayController.Instance().OnRevive();
|
|
|
|
}
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
2024-02-28 03:25:11 -08:00
|
|
|
});
|
2024-02-27 18:19:33 -08:00
|
|
|
export enum GameState {
|
|
|
|
MainMenu,
|
|
|
|
StartGame,
|
|
|
|
PlayGame,
|
2024-02-28 03:25:11 -08:00
|
|
|
EndGame,
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
@ccclass('GameplayController')
|
|
|
|
export class GameplayController extends Component {
|
|
|
|
//#region Singleton
|
|
|
|
private static instance: GameplayController;
|
|
|
|
public static Instance(): GameplayController {
|
|
|
|
if (!GameplayController.instance) {
|
|
|
|
GameplayController.instance = new Node().addComponent(GameplayController);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GameplayController.instance;
|
|
|
|
}
|
|
|
|
protected onLoad(): void {
|
|
|
|
GameplayController.instance = this;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
//#region Properties
|
|
|
|
@property(Prefab)
|
|
|
|
private ballPrefab: Prefab;
|
|
|
|
@property([Vec2])
|
|
|
|
private startPositions: Vec2[] = [];
|
|
|
|
@property(UIController)
|
|
|
|
public uiController: UIController = null;
|
|
|
|
@property(LevelController)
|
|
|
|
private levelController: LevelController = null;
|
|
|
|
|
|
|
|
@property({ readonly: true, type: CCInteger })
|
|
|
|
public score: number = 0;
|
|
|
|
@property({ readonly: true, type: CCInteger })
|
|
|
|
private streak: number = 0;
|
|
|
|
@property({ readonly: true, type: CCInteger })
|
|
|
|
public highestStreak: number = 0;
|
|
|
|
// @property({ readonly: true, type: CCInteger })
|
|
|
|
@property(CCInteger)
|
|
|
|
private ball: number = 10;
|
|
|
|
@property(CCInteger)
|
|
|
|
private scoreToSpawnObstacle: number = 5;
|
|
|
|
|
|
|
|
@property({ type: Enum(GameState) })
|
|
|
|
public currentGameState: GameState = GameState.MainMenu;
|
|
|
|
|
2024-02-28 03:25:11 -08:00
|
|
|
@property(ParticleSystem)
|
|
|
|
public particle: ParticleSystem = null;
|
2024-02-27 18:19:33 -08:00
|
|
|
|
|
|
|
@property(CCInteger)
|
|
|
|
public startGameCountDown: number = 3;
|
2024-03-01 03:08:57 -08:00
|
|
|
@property([Node])
|
|
|
|
private randomEnemies: Node[] = [];
|
|
|
|
@property(CCInteger)
|
|
|
|
private maxRandomEnemies = 0;
|
2024-02-27 18:19:33 -08:00
|
|
|
//#endregion
|
|
|
|
|
2024-03-01 03:08:57 -08:00
|
|
|
private _currentRandomEnemies = 0;
|
2024-02-27 18:19:33 -08:00
|
|
|
public eventMenuGame = new EventTarget();
|
2024-03-03 00:23:29 -08:00
|
|
|
private _currentBallsInGame = 0;
|
2024-02-27 18:19:33 -08:00
|
|
|
|
|
|
|
public eventStartGame = new EventTarget();
|
|
|
|
public eventPlayGame = new EventTarget();
|
|
|
|
public eventEndGame = new EventTarget();
|
|
|
|
|
|
|
|
public eventSpawnObstacle = new EventTarget();
|
|
|
|
|
|
|
|
public eventUpdateScore = new EventTarget();
|
|
|
|
public eventUpdateStreak = new EventTarget();
|
|
|
|
public eventUpdateHighestStreak = new EventTarget();
|
|
|
|
public eventUpdateBall = new EventTarget();
|
|
|
|
|
|
|
|
protected start(): void {
|
|
|
|
BEConnector.instance.authenticate();
|
|
|
|
this.ChangeGameState(GameState.MainMenu);
|
2024-03-01 03:08:57 -08:00
|
|
|
this.randomEnemies.forEach((enemy) => (enemy.active = false));
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
2024-03-03 00:23:29 -08:00
|
|
|
public SpawnBall(): Ball {
|
2024-02-27 18:19:33 -08:00
|
|
|
if (this.currentGameState == GameState.EndGame) return;
|
|
|
|
|
2024-03-03 00:23:29 -08:00
|
|
|
const ballClone = instantiate(this.ballPrefab);
|
|
|
|
ballClone.setParent(this.node);
|
|
|
|
let randonPos = randomRangeInt(0, this.startPositions.length);
|
|
|
|
ballClone.setPosition(this.startPositions[randonPos].x, this.startPositions[randonPos].y, 0);
|
|
|
|
const ball = ballClone.getComponent(Ball);
|
|
|
|
ball.eventHitObstacle.on('HitObstacle', this.ObstacleHitControl, this);
|
|
|
|
ball.eventGoal.on('Goal', this.GoalControl, this);
|
|
|
|
this._currentBallsInGame++;
|
|
|
|
return ball;
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
public SpawnBallInTime(time: number): void {
|
|
|
|
if (this.ball <= 0) return;
|
|
|
|
if (this.currentGameState == GameState.EndGame) return;
|
2024-03-01 03:08:57 -08:00
|
|
|
this.spawnEnemies();
|
2024-02-27 18:19:33 -08:00
|
|
|
//this.unscheduleAllCallbacks();
|
2024-03-03 00:23:29 -08:00
|
|
|
console.log(GameState[this.currentGameState]);
|
2024-02-27 18:19:33 -08:00
|
|
|
|
|
|
|
this.scheduleOnce(() => {
|
2024-03-03 00:23:29 -08:00
|
|
|
const ball = this.SpawnBall();
|
|
|
|
let dir = randomRangeInt(-1, 2);
|
|
|
|
while (dir == 0) {
|
|
|
|
dir = randomRangeInt(-1, 2);
|
|
|
|
}
|
|
|
|
const force = new Vec2(dir, 1);
|
|
|
|
ball.getComponent(RigidBody2D).applyLinearImpulse(force.multiply2f(3, 50), Vec2.ZERO, true);
|
|
|
|
SoundManager.Instance().PlayOneShot(SoundManager.Instance().whistle);
|
2024-02-27 18:19:33 -08:00
|
|
|
}, time);
|
|
|
|
this.SetupObstacle();
|
|
|
|
}
|
2024-03-01 03:08:57 -08:00
|
|
|
|
|
|
|
private spawnEnemies() {
|
|
|
|
this._currentRandomEnemies = 0;
|
|
|
|
this.randomEnemies.forEach((enemy) => {
|
|
|
|
if (this._currentRandomEnemies >= this.maxRandomEnemies) return;
|
|
|
|
const chance = randomRangeInt(0, 100) <= 50;
|
|
|
|
enemy.active = chance;
|
|
|
|
if (chance) this._currentRandomEnemies++;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
//#region Events
|
|
|
|
public AddScore(score: number): void {
|
|
|
|
this.score += score;
|
|
|
|
this.streak++;
|
|
|
|
this.eventUpdateStreak.emit(EventType.UpdateStreak, this.streak);
|
|
|
|
if (this.streak > 2) {
|
|
|
|
let addBall = this.streak - 2;
|
|
|
|
this.ball += addBall;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.highestStreak < this.streak) {
|
|
|
|
this.highestStreak = this.streak;
|
|
|
|
this.eventUpdateHighestStreak.emit(EventType.UpdateHighestStreak, this.highestStreak);
|
|
|
|
}
|
|
|
|
this.eventUpdateBall.emit(EventType.UpdateBall, this.ball);
|
|
|
|
|
|
|
|
this.eventUpdateScore.emit(EventType.UpdateScore, this.score);
|
|
|
|
|
|
|
|
SoundManager.Instance().PlayOneShot(SoundManager.Instance().sfxGoal);
|
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
public LoseStreak(): void {
|
|
|
|
this.streak = 0;
|
|
|
|
this.eventUpdateStreak.emit(EventType.UpdateStreak, this.streak);
|
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
public ChangeGameState(newState: GameState): void {
|
|
|
|
if (this.currentGameState == newState) return;
|
|
|
|
|
|
|
|
this.currentGameState = newState;
|
|
|
|
this.StateChangeHandle();
|
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
private StateChangeHandle(): void {
|
|
|
|
switch (this.currentGameState) {
|
|
|
|
case GameState.MainMenu:
|
|
|
|
this.eventMenuGame.emit(EventType.MainMenu);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case GameState.StartGame:
|
|
|
|
this.eventStartGame.emit(EventType.StartGame);
|
|
|
|
this.SpawnBallInTime(this.startGameCountDown);
|
2024-03-01 03:08:57 -08:00
|
|
|
this.levelController?.LevelUp();
|
2024-02-28 03:25:11 -08:00
|
|
|
BEConnector.instance.ticketMinus('auth');
|
|
|
|
|
2024-02-27 18:19:33 -08:00
|
|
|
break;
|
|
|
|
case GameState.PlayGame:
|
|
|
|
this.eventUpdateBall.emit(EventType.UpdateBall, this.ball);
|
|
|
|
this.eventPlayGame.emit(EventType.PlayGame);
|
|
|
|
break;
|
|
|
|
case GameState.EndGame:
|
|
|
|
this.eventEndGame.emit(EventType.EndGame);
|
|
|
|
BEConnector.instance.postScoreToServer(this.score);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public SetBall(ball: number): void {
|
|
|
|
this.ball = ball;
|
|
|
|
this.eventUpdateBall.emit(EventType.UpdateBall, this.ball);
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
private SetupObstacle(): void {
|
2024-02-28 03:25:11 -08:00
|
|
|
if (this.score < this.scoreToSpawnObstacle + 2) return;
|
|
|
|
this.eventSpawnObstacle.emit('SpawnObstacle');
|
2024-02-27 18:19:33 -08:00
|
|
|
// this.harderObstacle.active = true;
|
|
|
|
}
|
|
|
|
private ObstacleHitControl(node: Node): void {
|
|
|
|
node.active = false;
|
|
|
|
this.scheduleOnce(function () {
|
|
|
|
node.active = true;
|
|
|
|
}, 3);
|
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
private GoalControl(positon: Vec3): void {
|
|
|
|
const pos = positon;
|
2024-02-28 03:25:11 -08:00
|
|
|
pos.z = this.particle.node.position.z;
|
2024-03-03 00:23:29 -08:00
|
|
|
this.particle.node.setWorldPosition(pos);
|
2024-02-28 03:25:11 -08:00
|
|
|
this.particle.play();
|
2024-02-27 18:19:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public OnRevive(): void {
|
|
|
|
this.uiController.ShutEndPnl();
|
|
|
|
this.ball += 5;
|
|
|
|
this.eventUpdateBall.emit(EventType.UpdateBall, this.ball);
|
|
|
|
this.SpawnBallInTime(1);
|
|
|
|
}
|
2024-03-03 00:23:29 -08:00
|
|
|
|
|
|
|
public updateCurrentBallsInGame(value: number) {
|
|
|
|
this._currentBallsInGame += value;
|
|
|
|
if (this._currentBallsInGame <= 0) {
|
|
|
|
this.ball--;
|
|
|
|
this.eventUpdateBall.emit(EventType.UpdateBall, this.ball);
|
|
|
|
if (this.ball <= 0) this.ChangeGameState(GameState.EndGame);
|
|
|
|
else this.SpawnBallInTime(1);
|
|
|
|
}
|
|
|
|
}
|
2024-02-28 03:25:11 -08:00
|
|
|
}
|