Files
rougelike-demo/client/assets/scripts/App/Game/PlayerInfo.ts

88 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-12-18 16:04:56 +08:00
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;
}
}