Files
dino/assets/seripts/scenes/GameSceneControl.ts
2026-04-30 00:13:37 +08:00

126 lines
3.0 KiB
TypeScript

import { _decorator, Component, google, Label, Node } from "cc";
import { GlobalData } from "../config/GlobalData";
import { EventDispatcher } from "../libs/EventDispatcher";
import { GameState } from "../config/Config";
import { MusicManager } from "../managers/MusicManager";
const { ccclass, property } = _decorator;
@ccclass("GameSceneControl")
export class GameSceneControl extends Component {
// 当前这一局得分label
@property(Label)
curRoundScoreLabel: Label = null;
// 当前这句剩余生命值label
@property(Label)
lifeNumLabel: Label = null;
// ui 节点
@property(Node)
uiNode: Node = null;
// 音频管理器
musicManager: MusicManager = null;
onLoad() {
// 获取音频管理器
this.musicManager = this.node.getComponent(MusicManager);
// 监听小恐龙吃到音符事件
EventDispatcher.getTarget().on(
EventDispatcher.DINO_EAT_NOTE,
this.updateCurRoundScoreLabel,
this,
);
// 监听音符碰到线事件
EventDispatcher.getTarget().on(
EventDispatcher.UPDATE_LIFE,
this.updateLifeNumLabel,
this,
);
// 监听游戏结束事件
EventDispatcher.getTarget().on(
EventDispatcher.GAME_OVER,
this.handleEndGame,
this,
);
// 监听过关事件
EventDispatcher.getTarget().on(
EventDispatcher.PASS_LEVLE,
this.handlePassLevel,
this,
);
}
start() {
// 重置游戏数据
GlobalData.resetGameData();
this.startGame();
}
startGame() {
// 开始游戏
this.uiNode.active = true;
// 更新 ui 节点相关数据
this.updateUi();
// 设置游戏状态为进行中
GlobalData.gameState = GameState.PLAYING;
// 开始游戏提示动画
// 1 s 后正式开始游戏
this.scheduleOnce(() => {
// 开始播放背景音乐
this.playMusic();
// 发送开始游戏事件
EventDispatcher.getTarget().emit(EventDispatcher.GAME_STAET);
}, 1);
}
// 更新 ui
updateUi() {
// 更新本局总得分
this.updateCurRoundScoreLabel();
// 更新生命值
this.updateLifeNumLabel();
// 更新当前速度的显示
this.updateSpeed();
}
// 更新当前这一局得分
updateCurRoundScoreLabel() {
this.curRoundScoreLabel.string = GlobalData.curRoundTotalScore.toString();
}
// 更新当前这句剩余生命值
updateLifeNumLabel() {
this.lifeNumLabel.string = "x" + GlobalData.lifeNumPerRound.toString();
}
// 更新速度显示
updateSpeed() {}
// 暂停游戏
pauseGame() {
EventDispatcher.getTarget().emit(EventDispatcher.SHOW_PAUSE_MODAL);
}
// 处理游戏结束时
handleEndGame() {
// 游戏结束,弹出游戏界面
this.uiNode.active = false;
}
// 闯过当前关卡时 重新开始下一轮
handlePassLevel() {
// 增加速率
GlobalData.speedRate += GlobalData.speedRateAdd;
// 继续开始游戏
this.startGame();
}
// 播放音乐
playMusic() {
this.musicManager.play();
}
}