88 lines
2.5 KiB
TypeScript
88 lines
2.5 KiB
TypeScript
|
|
import { _decorator, Component, Label, Vec3 } from 'cc';
|
|||
|
|
|
|||
|
|
const { ccclass, property } = _decorator;
|
|||
|
|
|
|||
|
|
export interface PlayerInfoData {
|
|||
|
|
/** 玩家ID */
|
|||
|
|
playerId: string;
|
|||
|
|
/** 玩家名称 */
|
|||
|
|
playerName: string;
|
|||
|
|
/** 屏幕坐标位置(用于设置UI位置) */
|
|||
|
|
screenPosition: Vec3;
|
|||
|
|
/** 真实位置(用于显示到文本中) */
|
|||
|
|
realPosition: Vec3;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* PlayerInfo 玩家信息显示组件
|
|||
|
|
* 显示玩家名称和坐标信息
|
|||
|
|
*/
|
|||
|
|
@ccclass('PlayerInfo')
|
|||
|
|
export class PlayerInfo extends Component {
|
|||
|
|
/** 文本显示组件 */
|
|||
|
|
private textLabel: Label = null;
|
|||
|
|
|
|||
|
|
/** 当前玩家数据 */
|
|||
|
|
private playerData: PlayerInfoData = null;
|
|||
|
|
|
|||
|
|
onLoad() {
|
|||
|
|
// 查找text子节点的Label组件
|
|||
|
|
const textNode = this.node.getChildByName('text');
|
|||
|
|
if (textNode) {
|
|||
|
|
this.textLabel = textNode.getComponent(Label);
|
|||
|
|
if (!this.textLabel) {
|
|||
|
|
console.error('[PlayerInfo] text子节点没有Label组件');
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
console.error('[PlayerInfo] 找不到text子节点');
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 更新玩家信息
|
|||
|
|
* @param playerData 玩家数据
|
|||
|
|
*/
|
|||
|
|
public updatePlayerInfo(playerData: PlayerInfoData): void {
|
|||
|
|
this.playerData = playerData;
|
|||
|
|
|
|||
|
|
if (!this.textLabel) {
|
|||
|
|
console.error('[PlayerInfo] 文本组件未初始化');
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 格式化显示文本:玩家名和真实坐标信息
|
|||
|
|
const displayText = `${playerData.playerName}\n坐标: (${Math.round(playerData.realPosition.x)}, ${Math.round(playerData.realPosition.y)})`;
|
|||
|
|
this.textLabel.string = displayText;
|
|||
|
|
|
|||
|
|
// 设置UI位置为屏幕坐标
|
|||
|
|
this.node.setPosition(playerData.screenPosition);
|
|||
|
|
|
|||
|
|
//console.log(`[PlayerInfo] 更新玩家信息: ${playerData.playerName}, 屏幕坐标: (${playerData.screenPosition.x}, ${playerData.screenPosition.y}), 真实坐标: (${playerData.realPosition.x}, ${playerData.realPosition.y})`);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取当前玩家数据
|
|||
|
|
*/
|
|||
|
|
public getPlayerData(): PlayerInfoData {
|
|||
|
|
return this.playerData;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 隐藏玩家信息
|
|||
|
|
*/
|
|||
|
|
public hide(): void {
|
|||
|
|
this.node.active = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 显示玩家信息
|
|||
|
|
*/
|
|||
|
|
public show(): void {
|
|||
|
|
this.node.active = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
onDestroy() {
|
|||
|
|
this.textLabel = null;
|
|||
|
|
this.playerData = null;
|
|||
|
|
}
|
|||
|
|
}
|