123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- import { _decorator, Component, Node, Label, Sprite, Button, resources, SpriteFrame } from 'cc';
- const { ccclass, property } = _decorator;
- /**
- * 通行证数据接口
- */
- interface PassCardData {
- name: string; // 人物姓名
- room: string; // 房间号
- id: string; // 身份ID
- reason: string; // 原因
- characterId: number; // 角色ID,用于加载头像
- }
- @ccclass('PassCardManager')
- export class PassCardManager extends Component {
- @property({
- type: Node,
- tooltip: '通行证面板节点'
- })
- passCardPanel: Node = null;
- @property({
- type: Label,
- tooltip: '姓名标签'
- })
- nameLabel: Label = null;
- @property({
- type: Label,
- tooltip: '房间号标签'
- })
- roomLabel: Label = null;
- @property({
- type: Label,
- tooltip: '身份ID标签'
- })
- idLabel: Label = null;
- @property({
- type: Label,
- tooltip: '原因标签'
- })
- reasonLabel: Label = null;
- @property({
- type: Sprite,
- tooltip: '头像显示组件'
- })
- avatarSprite: Sprite = null;
- @property({
- type: Button,
- tooltip: '关闭按钮'
- })
- closeButton: Button = null;
- // 当前通行证数据
- private currentPassData: PassCardData = null;
- start() {
- // 初始隐藏通行证面板
- if (this.passCardPanel) {
- this.passCardPanel.active = false;
- }
- // 注册关闭按钮点击事件
- if (this.closeButton) {
- // 先移除可能已存在的事件监听(防止重复注册)
- this.closeButton.node.off(Button.EventType.CLICK, this.onCloseButtonClick, this);
- // 重新注册点击事件
- this.closeButton.node.on(Button.EventType.CLICK, this.onCloseButtonClick, this);
- console.log('关闭按钮事件已注册');
- } else {
- console.error('关闭按钮未设置');
- }
- }
- /**
- * 关闭按钮点击事件处理
- */
- private onCloseButtonClick(): void {
- console.log('关闭按钮被点击');
- this.hidePassCard();
- }
- /**
- * 显示通行证
- * @param passData 通行证数据
- */
- public showPassCard(passData: PassCardData): void {
- console.log('PassCardManager.showPassCard 被调用:', passData);
-
- if (!this.passCardPanel) {
- console.error('通行证面板未设置');
- return;
- }
- // 保存当前数据
- this.currentPassData = passData;
- // 更新UI显示
- this.updatePassCardUI();
- // 显示面板
- this.passCardPanel.active = true;
- console.log('通行证面板已显示');
- }
- /**
- * 隐藏通行证
- */
- public hidePassCard(): void {
- console.log('hidePassCard 被调用');
-
- if (this.passCardPanel) {
- this.passCardPanel.active = false;
- console.log('通行证面板已隐藏');
- } else {
- console.error('通行证面板未设置,无法隐藏');
- }
- }
- /**
- * 更新通行证UI
- */
- private updatePassCardUI(): void {
- console.log('更新通行证UI');
-
- if (!this.currentPassData) {
- console.error('没有通行证数据');
- return;
- }
- // 更新文本内容
- if (this.nameLabel) {
- this.nameLabel.string = this.currentPassData.name || '';
- console.log('设置姓名:', this.currentPassData.name);
- } else {
- console.error('姓名标签未设置');
- }
- if (this.roomLabel) {
- this.roomLabel.string = this.currentPassData.room || '';
- console.log('设置房间号:', this.currentPassData.room);
- } else {
- console.error('房间号标签未设置');
- }
- if (this.idLabel) {
- this.idLabel.string = this.currentPassData.id || '';
- console.log('设置ID:', this.currentPassData.id);
- } else {
- console.error('ID标签未设置');
- }
- if (this.reasonLabel) {
- this.reasonLabel.string = this.currentPassData.reason || '';
- console.log('设置原因:', this.currentPassData.reason);
- } else {
- console.error('原因标签未设置');
- }
- // 加载并显示头像
- this.loadAvatar(this.currentPassData.characterId);
- }
- /**
- * 加载角色头像
- * @param characterId 角色ID
- */
- private loadAvatar(characterId: number): void {
- console.log('加载头像:', characterId);
-
- if (!this.avatarSprite) {
- console.error('头像Sprite组件未设置');
- return;
- }
-
- if (characterId <= 0) {
- console.error('无效的角色ID:', characterId);
- return;
- }
- // 我们的目录结构现在是 avatars/1/avatar_1_5.png
- // 正确的资源路径格式是 avatars/1/avatar_1_5(不需要.png后缀)
- const avatarPath = `avatars/${characterId}/avatar_${characterId}_5`;
- console.log('尝试加载头像路径:', avatarPath);
-
- // 加载头像资源
- resources.load(avatarPath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.error(`加载头像失败: ${avatarPath}`, err);
- this.tryAllPossiblePaths(characterId);
- } else {
- // 设置头像
- this.avatarSprite.spriteFrame = spriteFrame;
- console.log('头像加载成功: ' + avatarPath);
- }
- });
- }
- /**
- * 尝试所有可能的头像路径格式
- * @param characterId 角色ID
- */
- private tryAllPossiblePaths(characterId: number): void {
- console.log('尝试所有可能的头像路径格式:', characterId);
-
- // 基于项目目录结构,尝试所有可能的路径格式
- const possiblePaths = [
- // 当前目录格式,不同变体 - 使用新的下划线格式
- `avatars/${characterId}/avatar_${characterId}_1`,
- `avatars/${characterId}/avatar_${characterId}_2`,
- `avatars/${characterId}/avatar_${characterId}_3`,
- `avatars/${characterId}/avatar_${characterId}_4`,
-
- // 其他可能的格式
- `avatars/${characterId}/${characterId}`,
- `avatars/${characterId}`,
- `0${characterId}/${characterId}`,
- `${characterId}`
- ];
-
- console.log('尝试路径列表:', possiblePaths);
- this.tryPathSequentially(possiblePaths, 0);
- }
-
- /**
- * 按顺序尝试路径
- */
- private tryPathSequentially(paths: string[], index: number): void {
- if (index >= paths.length) {
- console.error('所有路径都加载失败');
- return;
- }
-
- const path = paths[index];
- console.log(`尝试路径 ${index+1}/${paths.length}: ${path}`);
-
- resources.load(path, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.warn(`路径 ${path} 加载失败, 尝试下一个`);
- this.tryPathSequentially(paths, index + 1);
- } else {
- if (this.avatarSprite) {
- this.avatarSprite.spriteFrame = spriteFrame;
- console.log(`头像加载成功,使用路径: ${path}`);
- }
- }
- });
- }
- onDestroy() {
- // 清理事件监听
- if (this.closeButton) {
- this.closeButton.node.off(Button.EventType.CLICK, this.onCloseButtonClick, this);
- }
- }
- }
|