接入ResLogin.otherPlayers
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import { _decorator } from 'cc';
|
||||
import { _decorator, instantiate, Node, Prefab } from 'cc';
|
||||
import { ResMgr } from '../../Framework/ResMgr/ResMgr';
|
||||
import { UIBase } from '../../Framework/UI/UIBase';
|
||||
import { PlayerInfo, PlayerInfoData } from './PlayerInfo';
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -9,21 +11,22 @@ const { ccclass, property } = _decorator;
|
||||
*/
|
||||
@ccclass('UIGame')
|
||||
export class UIGame extends UIBase {
|
||||
/** 玩家信息显示容器 */
|
||||
private playerInfoContainer: Node = null;
|
||||
|
||||
protected onLoad(): void {
|
||||
console.log('[UIGame] onLoad');
|
||||
/** 玩家信息预制体 */
|
||||
private playerInfoPrefab: Prefab = null;
|
||||
|
||||
/** 当前显示的玩家信息组件Map<playerId, PlayerInfo> */
|
||||
private playerInfoComponents: Map<string, PlayerInfo> = new Map();
|
||||
|
||||
async onStart() {
|
||||
this.initPlayerInfoContainer();
|
||||
await this.loadPlayerInfoPrefab();
|
||||
}
|
||||
|
||||
protected onEnable(): void {
|
||||
console.log('[UIGame] onEnable');
|
||||
}
|
||||
|
||||
protected onDisable(): void {
|
||||
console.log('[UIGame] onDisable');
|
||||
}
|
||||
|
||||
protected onDestroy(): void {
|
||||
console.log('[UIGame] onDestroy');
|
||||
onEnd(): void {
|
||||
this.clearPlayerInfoComponents();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,4 +36,95 @@ export class UIGame extends UIBase {
|
||||
return 'res://UI/Game/UIGame';
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化玩家信息容器
|
||||
*/
|
||||
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] 已清理所有玩家信息组件');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user