接入登录、游戏逻辑

This commit is contained in:
janing
2025-12-14 23:35:24 +08:00
parent 3612ee74ea
commit d530b48e34
3 changed files with 53 additions and 46 deletions

View File

@@ -1,5 +1,7 @@
import { find } from "cc";
import { BaseState } from "../../Framework/FSM/BaseState";
import { NetManager } from "../../Framework/Net/NetManager";
import { UIMgr } from "../../Framework/UI/UIMgr";
/**
* 应用启动状态
@@ -23,6 +25,10 @@ export class AppStatusBoot extends BaseState {
console.log("[AppStatusBoot] 开始初始化应用...");
try {
// 初始化UI
console.log("[AppStatusBoot] 初始化UI管理器...");
UIMgr.getInstance().setUIRoot(find("Canvas")!);
// 1. 初始化并连接网络
await this.initAndConnectNet();

View File

@@ -1,4 +1,8 @@
import { BaseState } from "../../Framework/FSM/BaseState";
import { UIMgr } from "../../Framework/UI/UIMgr";
import { UIGame } from "../Game/UIGame";
import { World } from "../Game/World";
import { PlayerInfo } from "../../Shared/protocols/PtlLogin";
/**
* 应用游戏状态
@@ -9,8 +13,9 @@ import { BaseState } from "../../Framework/FSM/BaseState";
* - 游戏主循环
*/
export class AppStatusGame extends BaseState {
private _player: any = null;
private _player: PlayerInfo = null;
private _isNewPlayer: boolean = false;
private _uiGame: UIGame = null;
constructor(fsm: any) {
super(fsm, "Game");
@@ -58,13 +63,10 @@ export class AppStatusGame extends BaseState {
private async loadGameScene(): Promise<void> {
console.log("[AppStatusGame] 加载游戏场景...");
// TODO: 使用Cocos场景管理器加载游戏场景
// await director.loadScene("GameScene");
// 加载游戏UI
this._uiGame = await UIMgr.getInstance().load(UIGame);
// 模拟加载延迟
await this.delay(500);
console.log("[AppStatusGame] 游戏场景加载完成(待实现)");
console.log("[AppStatusGame] 游戏场景加载完成");
}
/**
@@ -73,13 +75,20 @@ export class AppStatusGame extends BaseState {
private async initGame(): Promise<void> {
console.log("[AppStatusGame] 初始化游戏...");
// TODO: 初始化游戏逻辑
// - 创建玩家角色(根据this._player信息)
// - 设置玩家位置(this._player.position)
// - 创建敌人
// - 初始化游戏规则
if (!this._uiGame) {
throw new Error("UIGame 未加载");
}
console.log("[AppStatusGame] 游戏初始化完成(待实现)");
// 获取世界根节点
const worldRoot = this._uiGame.getWorldRoot();
if (!worldRoot) {
throw new Error("世界根节点未找到");
}
// 初始化世界,传入本地玩家信息
await World.getInstance().init(worldRoot, this._player);
console.log("[AppStatusGame] 游戏初始化完成");
}
/**
@@ -88,28 +97,10 @@ export class AppStatusGame extends BaseState {
private listenServerMessages(): void {
console.log("[AppStatusGame] 开始监听服务器广播...");
// TODO: 监听服务器广播消息
// const netManager = NetManager.getInstance();
// 网络消息监听已在 World 中注册
// World 会自动处理 MsgPlayerJoin 和 MsgPlayerMove
// 监听玩家加入 (MsgPlayerJoin)
// netManager.listenMsg("PlayerJoin", (msg) => {
// console.log("玩家加入:", msg.playerName);
// // 在场景中创建其他玩家
// });
// 监听玩家移动 (MsgPlayerMove)
// netManager.listenMsg("PlayerMove", (msg) => {
// console.log("玩家移动:", msg.playerName, msg.position);
// // 更新其他玩家位置
// });
// 监听聊天消息 (MsgChat)
// netManager.listenMsg("Chat", (msg) => {
// console.log("聊天消息:", msg);
// // 显示聊天内容
// });
console.log("[AppStatusGame] 服务器广播监听已设置(待实现)");
console.log("[AppStatusGame] 服务器广播监听已设置");
}
/**
@@ -118,10 +109,7 @@ export class AppStatusGame extends BaseState {
private startGame(): void {
console.log("[AppStatusGame] 游戏开始!");
// TODO: 启动游戏逻辑
// - 显示游戏UI
// - 开始接收输入
// - 开始游戏主循环(在onUpdate中)
// 游戏已启动,玩家可以通过 WASD 控制角色移动
}
/**
@@ -199,13 +187,15 @@ export class AppStatusGame extends BaseState {
super.onExit();
console.log("[AppStatusGame] 离开游戏状态");
// TODO: 清理游戏资源
// - 卸载游戏场景
// - 清理角色对象
// - 取消服务器消息监听
// const netManager = NetManager.getInstance();
// netManager.unlistenMsg("PlayerJoin");
// netManager.unlistenMsg("PlayerMove");
// netManager.unlistenMsg("Chat");
// 清理世界
World.clear();
// 卸载游戏UI
if (this._uiGame) {
UIMgr.getInstance().unload(UIGame);
this._uiGame = null;
}
this._player = null;
}
}

View File

@@ -0,0 +1,11 @@
{
"ver": "1.0.1",
"importer": "text",
"imported": true,
"uuid": "87d8c6a8-6b03-44b9-98a2-5eb7b8457271",
"files": [
".json"
],
"subMetas": {},
"userData": {}
}