Files
rougelike-demo/client/assets/scripts/App/AppStatus/AppStatusGame.ts

208 lines
5.2 KiB
TypeScript
Raw Normal View History

import { find } from "cc";
2025-12-14 22:41:10 +08:00
import { BaseState } from "../../Framework/FSM/BaseState";
2025-12-14 23:35:24 +08:00
import { UIMgr } from "../../Framework/UI/UIMgr";
2025-12-18 16:04:56 +08:00
import { MsgResLogin, PlayerInfo } from "../../Shared/protocols/MsgResLogin";
2025-12-14 23:35:24 +08:00
import { UIGame } from "../Game/UIGame";
import { World } from "../Game/World";
2025-12-14 22:41:10 +08:00
/**
*
* :
* -
* -
* - 广()
* -
*/
export class AppStatusGame extends BaseState {
2025-12-18 16:04:56 +08:00
private _cmd: MsgResLogin
2025-12-14 23:35:24 +08:00
private _player: PlayerInfo = null;
2025-12-14 22:41:10 +08:00
private _isNewPlayer: boolean = false;
2025-12-14 23:35:24 +08:00
private _uiGame: UIGame = null;
2025-12-14 22:41:10 +08:00
constructor(fsm: any) {
super(fsm, "Game");
}
2025-12-14 22:41:10 +08:00
/**
*
*/
2025-12-18 16:04:56 +08:00
async onEnter(params?: MsgResLogin): Promise<void> {
2025-12-14 22:41:10 +08:00
super.onEnter(params);
2025-12-18 16:04:56 +08:00
console.log("[AppStatusGame] 进入游戏世界", params);
this._cmd = params
2025-12-14 22:41:10 +08:00
// 保存玩家信息
if (params) {
this._player = params.player || null;
this._isNewPlayer = params.isNewPlayer || false;
console.log(`[AppStatusGame] 玩家信息:`, this._player);
console.log(`[AppStatusGame] 是否新玩家: ${this._isNewPlayer}`);
}
2025-12-14 22:41:10 +08:00
try {
// 1. 加载游戏场景
await this.loadGameScene();
2025-12-14 22:41:10 +08:00
// 2. 初始化游戏
await this.initGame();
2025-12-14 22:41:10 +08:00
// 3. 开始监听服务器广播
this.listenServerMessages();
2025-12-14 22:41:10 +08:00
// 4. 开始游戏
this.startGame();
2025-12-14 22:41:10 +08:00
} catch (error) {
console.error("[AppStatusGame] 进入游戏失败:", error);
// 返回登录
this._fsm.changeState("Login");
}
}
2025-12-14 22:41:10 +08:00
/**
*
*/
private async loadGameScene(): Promise<void> {
console.log("[AppStatusGame] 加载游戏场景...");
2025-12-14 23:35:24 +08:00
// 加载游戏UI
this._uiGame = await UIMgr.getInstance().load(UIGame);
2025-12-14 23:35:24 +08:00
console.log("[AppStatusGame] 游戏场景加载完成");
2025-12-14 22:41:10 +08:00
}
2025-12-14 22:41:10 +08:00
/**
*
*/
private async initGame(): Promise<void> {
console.log("[AppStatusGame] 初始化游戏...");
2025-12-14 23:35:24 +08:00
if (!this._uiGame) {
throw new Error("UIGame 未加载");
}
// 获取世界根节点
const worldRoot = find("Game")
2025-12-14 23:35:24 +08:00
if (!worldRoot) {
throw new Error("世界根节点未找到");
}
// 初始化世界,传入本地玩家信息
2025-12-18 16:04:56 +08:00
await World.getInstance().init(worldRoot, this._player, this._cmd.otherPlayers);
2025-12-14 23:35:24 +08:00
console.log("[AppStatusGame] 游戏初始化完成");
2025-12-14 22:41:10 +08:00
}
2025-12-14 22:41:10 +08:00
/**
* 广
*/
private listenServerMessages(): void {
console.log("[AppStatusGame] 开始监听服务器广播...");
2025-12-14 23:35:24 +08:00
// 网络消息监听已在 World 中注册
// World 会自动处理 MsgPlayerJoin 和 MsgPlayerMove
2025-12-14 23:35:24 +08:00
console.log("[AppStatusGame] 服务器广播监听已设置");
2025-12-14 22:41:10 +08:00
}
2025-12-14 22:41:10 +08:00
/**
*
*/
private startGame(): void {
console.log("[AppStatusGame] 游戏开始!");
2025-12-14 23:35:24 +08:00
// 游戏已启动,玩家可以通过 WASD 控制角色移动
2025-12-14 22:41:10 +08:00
}
2025-12-14 22:41:10 +08:00
/**
* ()
*/
onUpdate(dt: number): void {
2025-12-18 16:04:56 +08:00
// 更新世界状态,包括玩家信息显示
World.getInstance().update(dt);
// TODO: 其他游戏主循环逻辑
2025-12-14 22:41:10 +08:00
// - 更新角色位置
// - 检测碰撞
// - 更新敌人AI
// - 同步网络状态
}
2025-12-14 22:41:10 +08:00
/**
*
*/
pauseGame(): void {
console.log("[AppStatusGame] 游戏暂停");
// TODO: 暂停游戏逻辑
// - 停止游戏更新
// - 显示暂停菜单
}
2025-12-14 22:41:10 +08:00
/**
*
*/
resumeGame(): void {
console.log("[AppStatusGame] 游戏恢复");
// TODO: 恢复游戏逻辑
// - 继续游戏更新
// - 隐藏暂停菜单
}
2025-12-14 22:41:10 +08:00
/**
*
*/
onPlayerDeath(): void {
console.log("[AppStatusGame] 玩家死亡");
2025-12-14 22:41:10 +08:00
// TODO: 处理玩家死亡
// - 显示死亡界面
// - 显示复活选项或返回登录
2025-12-14 22:41:10 +08:00
// 延迟后返回登录
setTimeout(() => {
this._fsm.changeState("Login");
}, 3000);
}
2025-12-14 22:41:10 +08:00
/**
* 退()
*/
quitGame(): void {
console.log("[AppStatusGame] 退出游戏");
2025-12-14 22:41:10 +08:00
// TODO: 断开连接或通知服务器
// const netManager = NetManager.getInstance();
// await netManager.disconnect();
2025-12-14 22:41:10 +08:00
// 返回登录
this._fsm.changeState("Login");
}
2025-12-14 22:41:10 +08:00
/**
*
*/
private delay(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
2025-12-14 22:41:10 +08:00
/**
* 退
*/
onExit(): void {
super.onExit();
console.log("[AppStatusGame] 离开游戏状态");
2025-12-14 23:35:24 +08:00
// 清理世界
World.clear();
2025-12-14 23:35:24 +08:00
// 卸载游戏UI
if (this._uiGame) {
UIMgr.getInstance().unload(UIGame);
this._uiGame = null;
}
2025-12-14 23:35:24 +08:00
this._player = null;
2025-12-14 22:41:10 +08:00
}
}