122 lines
3.7 KiB
TypeScript
122 lines
3.7 KiB
TypeScript
import { BaseState } from "../../Framework/FSM/BaseState";
|
|
import { NetManager } from "../../Framework/Net/NetManager";
|
|
import { UIMgr } from "../../Framework/UI/UIMgr";
|
|
import { UILogin } from "../Login/UILogin";
|
|
|
|
/**
|
|
* 应用登录状态
|
|
* 职责:
|
|
* - 显示登录界面
|
|
* - 处理玩家ID输入
|
|
* - 发送登录请求(Login API)
|
|
* - 登录成功后直接进入游戏世界
|
|
*/
|
|
export class AppStatusLogin extends BaseState {
|
|
constructor(fsm: any) {
|
|
super(fsm, "Login");
|
|
}
|
|
|
|
/**
|
|
* 进入登录状态
|
|
*/
|
|
async onEnter(params?: any): Promise<void> {
|
|
super.onEnter(params);
|
|
|
|
console.log("[AppStatusLogin] 显示登录界面");
|
|
|
|
// 1. 显示登录界面
|
|
await this.showLoginUI();
|
|
|
|
// 2. 等待用户输入玩家ID并点击登录
|
|
// 在 UI 中处理输入,调用 this.login(playerId, playerName)
|
|
}
|
|
|
|
/**
|
|
* 显示登录界面
|
|
*/
|
|
private async showLoginUI(): Promise<void> {
|
|
console.log("[AppStatusLogin] 加载登录UI...");
|
|
try {
|
|
const uiMgr = UIMgr.getInstance();
|
|
await uiMgr.load(UILogin);
|
|
console.log("[AppStatusLogin] 登录UI加载成功");
|
|
} catch (error) {
|
|
console.error("[AppStatusLogin] 登录UI加载失败:", error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 执行登录
|
|
* @param playerId 玩家ID(用于识别玩家)
|
|
* @param playerName 玩家昵称(可选,新玩家时使用)
|
|
*/
|
|
async login(playerId: string, playerName?: string): Promise<void> {
|
|
console.log(`[AppStatusLogin] 尝试登录, 玩家ID: ${playerId}`);
|
|
|
|
if (!playerId || playerId.trim().length === 0) {
|
|
console.error("[AppStatusLogin] 玩家ID不能为空");
|
|
// TODO: 显示错误提示
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// 1. 发送登录请求到服务器
|
|
// const netManager = NetManager.getInstance();
|
|
// const result = await netManager.callApi("Login", {
|
|
// playerId,
|
|
// playerName
|
|
// });
|
|
|
|
// 模拟登录请求
|
|
await this.delay(500);
|
|
const mockResult = {
|
|
success: true,
|
|
message: "登录成功",
|
|
player: {
|
|
id: playerId,
|
|
name: playerName || `Player_${playerId}`,
|
|
position: { x: 0, y: 0, z: 0 },
|
|
spawnPoint: { x: 0, y: 0, z: 0 },
|
|
hp: 100,
|
|
maxHp: 100,
|
|
isAlive: true,
|
|
createdAt: Date.now(),
|
|
lastLoginAt: Date.now()
|
|
},
|
|
isNewPlayer: false
|
|
};
|
|
|
|
console.log("[AppStatusLogin] 登录成功,玩家信息:", mockResult.player);
|
|
|
|
// 2. 登录成功后直接进入游戏状态
|
|
this._fsm.changeState("Game", {
|
|
player: mockResult.player,
|
|
isNewPlayer: mockResult.isNewPlayer
|
|
});
|
|
|
|
} catch (error) {
|
|
console.error("[AppStatusLogin] 登录失败:", error);
|
|
// TODO: 显示错误提示
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 延迟辅助函数
|
|
*/
|
|
private delay(ms: number): Promise<void> {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
/**
|
|
* 退出登录状态
|
|
*/
|
|
onExit(): void {
|
|
super.onExit();
|
|
console.log("[AppStatusLogin] 离开登录状态");
|
|
|
|
// 隐藏登录界面
|
|
const uiMgr = UIMgr.getInstance();
|
|
uiMgr.hide(UILogin);
|
|
}
|
|
}
|