123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- import { _decorator, Component, Node, Button, Sprite, Label, SpriteFrame, resources } from 'cc';
- import { DataManager } from './DataManager';
- const { ccclass, property } = _decorator;
- @ccclass('PersonalInfoManager')
- export class PersonalInfoManager extends Component {
- @property({
- type: Node,
- tooltip: '个人资料UI面板'
- })
- personalInfoPanel: Node = null;
-
- @property({
- type: Button,
- tooltip: '关闭按钮'
- })
- closeButton: Button = null;
-
- @property({
- type: Sprite,
- tooltip: '角色头像显示'
- })
- characterAvatar: Sprite = null;
-
- @property({
- type: Label,
- tooltip: '角色信息文本'
- })
- infoText: Label = null;
-
- @property({
- type: Label,
- tooltip: '角色姓名、房间号、ID等信息标签'
- })
- nameInfoLabel: Label = null;
-
- @property({
- type: DataManager,
- tooltip: '数据管理器引用'
- })
- dataManager: DataManager = null;
-
- // 当前显示的角色ID
- private currentCharacterId: number = -1;
-
- start() {
- // 初始化隐藏面板
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = false;
- }
-
- // 注册关闭按钮事件
- this.setupCloseButton();
- }
-
- private setupCloseButton() {
- if (this.closeButton) {
- this.closeButton.node.off('click');
- this.closeButton.node.on('click', () => {
- this.hidePersonalInfoPanel();
- }, this);
- }
- }
-
- /**
- * 显示个人资料面板
- */
- public showPersonalInfoPanel() {
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = true;
- this.personalInfoPanel.setSiblingIndex(999);
- }
- }
-
- /**
- * 隐藏个人资料面板
- */
- public hidePersonalInfoPanel() {
- if (this.personalInfoPanel) {
- this.personalInfoPanel.active = false;
- }
- }
-
- /**
- * 显示角色资料
- * @param data 角色资料数据
- */
- public displayCharacterInfo(data: any) {
- if (!data) {
- console.error('角色资料数据为空');
- return;
- }
-
- console.log(`PersonalInfoManager.displayCharacterInfo: ID=${data.characterId}, 头像路径=${data.avatarPath}`);
-
- // 保存当前显示的角色ID
- this.currentCharacterId = data.characterId;
-
- // 设置文本信息
- if (this.infoText && data.info) {
- this.infoText.string = data.info;
- }
-
- // 加载并设置头像
- if (this.characterAvatar && data.avatarPath) {
- const characterId = data.characterId;
-
- // 列出所有可能尝试的路径格式
- const pathsToTry = [
- `${data.avatarPath}/spriteFrame`,
- data.avatarPath,
- `avatars/${characterId}/avatar_${characterId}_5/spriteFrame`,
- `avatars/${characterId}/avatar_${characterId}_5`,
- `avatars/${characterId}/spriteFrame`,
- `avatars/${characterId}`
- ];
-
- // 递归尝试加载所有可能的路径
- this.tryLoadSpriteFrameWithPaths(this.characterAvatar, pathsToTry, 0);
- }
-
- // 获取更详细的NPC信息并显示
- this.updateDetailedNPCInfo(data.characterId);
-
- // 显示面板
- this.showPersonalInfoPanel();
- }
-
- /**
- * 递归尝试加载所有可能的路径
- * @param sprite 要设置的精灵组件
- * @param paths 尝试的路径数组
- * @param index 当前尝试的路径索引
- */
- private tryLoadSpriteFrameWithPaths(sprite: Sprite, paths: string[], index: number): void {
- if (index >= paths.length) {
- console.error('所有可能的路径都尝试失败,使用默认图像');
- // 所有路径都失败,创建一个默认的灰色方形作为备用
- this.createDefaultSpriteFrame(sprite);
- return;
- }
-
- const currentPath = paths[index];
- console.log(`PersonalInfoManager - 尝试加载路径(${index+1}/${paths.length}): ${currentPath}`);
-
- resources.load(currentPath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.warn(`路径加载失败: ${currentPath}`, err);
- // 尝试下一个路径
- this.tryLoadSpriteFrameWithPaths(sprite, paths, index + 1);
- return;
- }
-
- // 成功加载,设置spriteFrame
- sprite.spriteFrame = spriteFrame;
- console.log(`成功加载头像: ${currentPath}`);
- });
- }
-
- /**
- * 创建一个默认的SpriteFrame用于显示
- * @param sprite 要设置的精灵组件
- */
- private createDefaultSpriteFrame(sprite: Sprite): void {
- console.log('创建默认图像');
-
- // 使用内置资源或默认资源
- const defaultIcon = "default_sprite";
- resources.load(defaultIcon, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.warn(`无法加载默认图像 ${defaultIcon}`, err);
- return;
- }
-
- sprite.spriteFrame = spriteFrame;
- console.log('已设置默认图像');
- });
- }
-
- /**
- * 更新详细的NPC信息
- * @param characterId 角色ID
- */
- private updateDetailedNPCInfo(characterId: number) {
- if (!this.nameInfoLabel) return;
- if (!this.dataManager) {
- console.error('未设置DataManager引用');
- return;
- }
-
- // 获取NPC数据
- const npcData = this.dataManager.getNPCById(characterId);
- if (!npcData) {
- console.error(`无法找到ID为${characterId}的NPC数据`);
- return;
- }
-
- // 获取通行证信息
- const passInfo = npcData.pass;
-
- // 获取个人信息
- const personalInfo = npcData.personal;
-
- // 构建详细信息文本
- let detailedInfo = '';
-
- // 添加姓名
- detailedInfo += `姓名: ${npcData.characterName}\n`;
-
- // 添加房间号
- if (passInfo && passInfo.room) {
- detailedInfo += `房间号: ${passInfo.room}\n`;
- }
-
- // 添加ID
- if (passInfo && passInfo.identityId) {
- detailedInfo += `ID: ${passInfo.identityId}\n`;
- } else if (personalInfo && personalInfo.id) {
- detailedInfo += `ID: ${personalInfo.id}\n`;
- }
-
- // 添加同住人信息
- if (personalInfo && personalInfo.hasRoommate && personalInfo.roommate) {
- detailedInfo += `同住人: ${personalInfo.roommate.id || '未知'}\n`;
- } else if (personalInfo && personalInfo.hasRoommate) {
- detailedInfo += `同住人: 有(未详)\n`;
- } else if (personalInfo) {
- detailedInfo += `同住人: 无\n`;
- }
-
- // 设置详细信息文本
- this.nameInfoLabel.string = detailedInfo;
- }
-
- onDestroy() {
- if (this.closeButton) {
- this.closeButton.node.off('click');
- }
- }
- }
|