2025-12-18 16:04:56 +08:00
|
|
|
import { _decorator, instantiate, Node, Prefab } from 'cc';
|
|
|
|
|
import { ResMgr } from '../../Framework/ResMgr/ResMgr';
|
2025-12-14 23:35:08 +08:00
|
|
|
import { UIBase } from '../../Framework/UI/UIBase';
|
2025-12-18 16:04:56 +08:00
|
|
|
import { PlayerInfo, PlayerInfoData } from './PlayerInfo';
|
2025-12-14 23:35:08 +08:00
|
|
|
|
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* UIGame 游戏主界面
|
|
|
|
|
* 显示游戏UI和世界场景
|
|
|
|
|
*/
|
|
|
|
|
@ccclass('UIGame')
|
|
|
|
|
export class UIGame extends UIBase {
|
2025-12-18 16:04:56 +08:00
|
|
|
/** 玩家信息显示容器 */
|
|
|
|
|
private playerInfoContainer: Node = null;
|
2025-12-14 23:35:08 +08:00
|
|
|
|
2025-12-18 16:04:56 +08:00
|
|
|
/** 玩家信息预制体 */
|
|
|
|
|
private playerInfoPrefab: Prefab = null;
|
2025-12-14 23:35:08 +08:00
|
|
|
|
2025-12-18 16:04:56 +08:00
|
|
|
/** 当前显示的玩家信息组件Map<playerId, PlayerInfo> */
|
|
|
|
|
private playerInfoComponents: Map<string, PlayerInfo> = new Map();
|
2025-12-14 23:35:08 +08:00
|
|
|
|
2025-12-18 16:04:56 +08:00
|
|
|
async onStart() {
|
|
|
|
|
this.initPlayerInfoContainer();
|
|
|
|
|
await this.loadPlayerInfoPrefab();
|
2025-12-14 23:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-18 16:04:56 +08:00
|
|
|
onEnd(): void {
|
|
|
|
|
this.clearPlayerInfoComponents();
|
2025-12-14 23:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取 UI 预制体路径
|
|
|
|
|
*/
|
|
|
|
|
public onGetUrl(): string {
|
2025-12-18 13:27:22 +08:00
|
|
|
return 'res://UI/Game/UIGame';
|
2025-12-14 23:35:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-18 16:04:56 +08:00
|
|
|
/**
|
|
|
|
|
* 初始化玩家信息容器
|
|
|
|
|
*/
|
|
|
|
|
private initPlayerInfoContainer(): void {
|
|
|
|
|
// 查找或创建玩家信息容器
|
|
|
|
|
this.playerInfoContainer = this._node.getChildByName('PlayerInfoContainer');
|
|
|
|
|
if (!this.playerInfoContainer) {
|
|
|
|
|
this.playerInfoContainer = new Node('PlayerInfoContainer');
|
|
|
|
|
this._node.addChild(this.playerInfoContainer);
|
|
|
|
|
}
|
|
|
|
|
console.log('[UIGame] 玩家信息容器已初始化');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载玩家信息预制体
|
|
|
|
|
*/
|
|
|
|
|
private async loadPlayerInfoPrefab(): Promise<void> {
|
|
|
|
|
try {
|
|
|
|
|
this.playerInfoPrefab = await ResMgr.getInstance().load('res', 'UI/Game/PlayerInfo', Prefab);
|
|
|
|
|
console.log('[UIGame] 玩家信息预制体加载成功');
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('[UIGame] 加载玩家信息预制体失败:', error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新玩家信息显示
|
|
|
|
|
* @param playerDataList 玩家数据列表
|
|
|
|
|
*/
|
|
|
|
|
public updatePlayerInfo(playerDataList: PlayerInfoData[]): void {
|
|
|
|
|
if (!this.playerInfoPrefab || !this.playerInfoContainer) {
|
|
|
|
|
console.error('[UIGame] 玩家信息预制体或容器未准备好');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 记录当前更新的玩家ID
|
|
|
|
|
const currentPlayerIds = new Set<string>();
|
|
|
|
|
|
|
|
|
|
for (const playerData of playerDataList) {
|
|
|
|
|
currentPlayerIds.add(playerData.playerId);
|
|
|
|
|
|
|
|
|
|
let playerInfoComponent = this.playerInfoComponents.get(playerData.playerId);
|
|
|
|
|
|
|
|
|
|
if (!playerInfoComponent) {
|
|
|
|
|
// 创建新的玩家信息组件
|
|
|
|
|
const playerInfoNode = instantiate(this.playerInfoPrefab);
|
|
|
|
|
this.playerInfoContainer.addChild(playerInfoNode);
|
|
|
|
|
|
|
|
|
|
playerInfoComponent = playerInfoNode.getComponent(PlayerInfo);
|
|
|
|
|
if (!playerInfoComponent) {
|
|
|
|
|
playerInfoComponent = playerInfoNode.addComponent(PlayerInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.playerInfoComponents.set(playerData.playerId, playerInfoComponent);
|
|
|
|
|
console.log('[UIGame] 创建玩家信息组件:', playerData.playerName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新玩家信息
|
|
|
|
|
playerInfoComponent.updatePlayerInfo(playerData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 移除不再存在的玩家信息
|
|
|
|
|
for (const [playerId, playerInfoComponent] of this.playerInfoComponents.entries()) {
|
|
|
|
|
if (!currentPlayerIds.has(playerId)) {
|
|
|
|
|
playerInfoComponent.node.destroy();
|
|
|
|
|
this.playerInfoComponents.delete(playerId);
|
|
|
|
|
console.log('[UIGame] 移除玩家信息组件:', playerId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 清理所有玩家信息组件
|
|
|
|
|
*/
|
|
|
|
|
private clearPlayerInfoComponents(): void {
|
|
|
|
|
for (const [playerId, playerInfoComponent] of this.playerInfoComponents.entries()) {
|
|
|
|
|
if (playerInfoComponent && playerInfoComponent.node) {
|
|
|
|
|
playerInfoComponent.node.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.playerInfoComponents.clear();
|
|
|
|
|
|
|
|
|
|
// 释放预制体资源
|
|
|
|
|
if (this.playerInfoPrefab) {
|
|
|
|
|
ResMgr.getInstance().release('resources', 'res://UI/Game/PlayerInfo');
|
|
|
|
|
this.playerInfoPrefab = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('[UIGame] 已清理所有玩家信息组件');
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 23:35:08 +08:00
|
|
|
}
|