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; @property({ tooltip: '动画时间(秒)', range: [0.1, 2.0, 0.1] }) animDuration: number = 0.5; // 当前显示的角色ID private currentCharacterId: number = -1; private panelOpacity: UIOpacity = null; start() { // 初始化隐藏面板 if (this.personalInfoPanel) { this.personalInfoPanel.active = false; // 确保面板有UIOpacity组件 this.panelOpacity = this.personalInfoPanel.getComponent(UIOpacity); if (!this.panelOpacity) { this.panelOpacity = this.personalInfoPanel.addComponent(UIOpacity); } } // 注册关闭按钮事件 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); // 播放从口袋拿出来的动画 this.playPocketOutAnimation(); } } /** * 播放从口袋掏出的动画 */ private playPocketOutAnimation() { // 设置初始状态 - 从右侧口袋拿出 this.personalInfoPanel.setScale(new Vec3(0.7, 0.2, 1)); // 扁平状态 this.personalInfoPanel.setPosition(new Vec3(200, -180, 0)); // 从右侧口袋位置开始 this.personalInfoPanel.setRotationFromEuler(new Vec3(0, 0, -20)); // 初始右倾斜角度 if (this.panelOpacity) { this.panelOpacity.opacity = 50; // 半透明开始 } // 创建动画 - 掏出口袋的感觉 tween(this.personalInfoPanel) // 先上移一点,同时展开 .to(this.animDuration * 0.6, { scale: new Vec3(0.9, 0.9, 1), position: new Vec3(100, -50, 0), eulerAngles: new Vec3(0, 0, -10) // 轻微倾斜,像是手拿着 }, { easing: 'quadOut' }) // 然后放到正确位置并恢复正常大小 .to(this.animDuration * 0.4, { scale: new Vec3(1, 1, 1), position: new Vec3(0, 0, 0), eulerAngles: new Vec3(0, 0, 0) }, { easing: 'backOut' }) .start(); // 透明度动画 if (this.panelOpacity) { tween(this.panelOpacity) .to(this.animDuration * 0.7, { opacity: 255 }) .start(); } } /** * 隐藏个人资料面板 */ public hidePersonalInfoPanel() { if (this.personalInfoPanel) { // 播放放回口袋的动画 this.playPocketInAnimation(() => { this.personalInfoPanel.active = false; }); } } /** * 播放放回口袋的动画 */ private playPocketInAnimation(callback?: Function) { // 创建放回口袋的动画 tween(this.personalInfoPanel) // 先抬起并旋转 .to(this.animDuration * 0.3, { position: new Vec3(80, -30, 0), eulerAngles: new Vec3(0, 0, -10), scale: new Vec3(0.95, 0.95, 1) }, { easing: 'sineIn' }) // 然后放入口袋 .to(this.animDuration * 0.4, { position: new Vec3(200, -180, 0), eulerAngles: new Vec3(0, 0, -20), scale: new Vec3(0.6, 0.2, 1) // 扁平化,像塞入口袋 }, { easing: 'quadIn' }) .call(() => { if (callback) callback(); }) .start(); // 透明度动画 if (this.panelOpacity) { tween(this.panelOpacity) .to(this.animDuration * 0.6, { opacity: 0 }) .start(); } } /** * 显示角色资料 * @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'); } } }