PersonalInfoManager.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import { _decorator, Component, Node, Label, Sprite, Button, resources, SpriteFrame, tween, Vec3, UIOpacity, UITransform, Color } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('PersonalInfoManager')
  4. export class PersonalInfoManager extends Component {
  5. @property({
  6. type: Node,
  7. tooltip: '个人资料UI面板'
  8. })
  9. personalInfoPanel: Node = null;
  10. @property({
  11. type: Button,
  12. tooltip: '关闭按钮'
  13. })
  14. closeButton: Button = null;
  15. @property({
  16. type: Sprite,
  17. tooltip: '角色头像显示'
  18. })
  19. characterAvatar: Sprite = null;
  20. @property({
  21. type: Label,
  22. tooltip: '角色信息文本'
  23. })
  24. infoText: Label = null;
  25. @property({
  26. type: Label,
  27. tooltip: '角色姓名、房间号、ID等信息标签'
  28. })
  29. nameInfoLabel: Label = null;
  30. @property({
  31. tooltip: '动画时间(秒)',
  32. range: [0.1, 2.0, 0.1]
  33. })
  34. animDuration: number = 0.5;
  35. // 当前显示的角色ID
  36. private currentCharacterId: number = -1;
  37. private panelOpacity: UIOpacity = null;
  38. start() {
  39. // 初始化隐藏面板
  40. if (this.personalInfoPanel) {
  41. this.personalInfoPanel.active = false;
  42. // 确保面板有UIOpacity组件
  43. this.panelOpacity = this.personalInfoPanel.getComponent(UIOpacity);
  44. if (!this.panelOpacity) {
  45. this.panelOpacity = this.personalInfoPanel.addComponent(UIOpacity);
  46. }
  47. }
  48. // 注册关闭按钮事件
  49. this.setupCloseButton();
  50. }
  51. private setupCloseButton() {
  52. if (this.closeButton) {
  53. this.closeButton.node.off('click');
  54. this.closeButton.node.on('click', () => {
  55. this.hidePersonalInfoPanel();
  56. }, this);
  57. }
  58. }
  59. /**
  60. * 显示个人资料面板
  61. */
  62. public showPersonalInfoPanel() {
  63. if (this.personalInfoPanel) {
  64. this.personalInfoPanel.active = true;
  65. this.personalInfoPanel.setSiblingIndex(999);
  66. // 播放从口袋拿出来的动画
  67. this.playPocketOutAnimation();
  68. }
  69. }
  70. /**
  71. * 播放从口袋掏出的动画
  72. */
  73. private playPocketOutAnimation() {
  74. // 设置初始状态 - 从右侧口袋拿出
  75. this.personalInfoPanel.setScale(new Vec3(0.7, 0.2, 1)); // 扁平状态
  76. this.personalInfoPanel.setPosition(new Vec3(200, -180, 0)); // 从右侧口袋位置开始
  77. this.personalInfoPanel.setRotationFromEuler(new Vec3(0, 0, -20)); // 初始右倾斜角度
  78. if (this.panelOpacity) {
  79. this.panelOpacity.opacity = 50; // 半透明开始
  80. }
  81. // 创建动画 - 掏出口袋的感觉
  82. tween(this.personalInfoPanel)
  83. // 先上移一点,同时展开
  84. .to(this.animDuration * 0.6, {
  85. scale: new Vec3(0.9, 0.9, 1),
  86. position: new Vec3(100, -50, 0),
  87. eulerAngles: new Vec3(0, 0, -10) // 轻微倾斜,像是手拿着
  88. }, {
  89. easing: 'quadOut'
  90. })
  91. // 然后放到正确位置并恢复正常大小
  92. .to(this.animDuration * 0.4, {
  93. scale: new Vec3(1, 1, 1),
  94. position: new Vec3(0, 0, 0),
  95. eulerAngles: new Vec3(0, 0, 0)
  96. }, {
  97. easing: 'backOut'
  98. })
  99. .start();
  100. // 透明度动画
  101. if (this.panelOpacity) {
  102. tween(this.panelOpacity)
  103. .to(this.animDuration * 0.7, { opacity: 255 })
  104. .start();
  105. }
  106. }
  107. /**
  108. * 隐藏个人资料面板
  109. */
  110. public hidePersonalInfoPanel() {
  111. if (this.personalInfoPanel) {
  112. // 播放放回口袋的动画
  113. this.playPocketInAnimation(() => {
  114. this.personalInfoPanel.active = false;
  115. });
  116. }
  117. }
  118. /**
  119. * 播放放回口袋的动画
  120. */
  121. private playPocketInAnimation(callback?: Function) {
  122. // 创建放回口袋的动画
  123. tween(this.personalInfoPanel)
  124. // 先抬起并旋转
  125. .to(this.animDuration * 0.3, {
  126. position: new Vec3(80, -30, 0),
  127. eulerAngles: new Vec3(0, 0, -10),
  128. scale: new Vec3(0.95, 0.95, 1)
  129. }, {
  130. easing: 'sineIn'
  131. })
  132. // 然后放入口袋
  133. .to(this.animDuration * 0.4, {
  134. position: new Vec3(200, -180, 0),
  135. eulerAngles: new Vec3(0, 0, -20),
  136. scale: new Vec3(0.6, 0.2, 1) // 扁平化,像塞入口袋
  137. }, {
  138. easing: 'quadIn'
  139. })
  140. .call(() => {
  141. if (callback) callback();
  142. })
  143. .start();
  144. // 透明度动画
  145. if (this.panelOpacity) {
  146. tween(this.panelOpacity)
  147. .to(this.animDuration * 0.6, { opacity: 0 })
  148. .start();
  149. }
  150. }
  151. /**
  152. * 显示角色资料
  153. * @param data 包含角色资料的完整数据对象,由GameFlowManager提供
  154. * 所需字段:
  155. * - characterId: 角色ID
  156. * - characterName: 角色名称
  157. * - info: 个人资料文本
  158. * - avatarPath: 头像路径
  159. * - room: 房间号
  160. * - identityId: 身份ID
  161. * - hasRoommate: 是否有室友
  162. * - roommateInfo: 室友信息(可选)
  163. */
  164. public displayCharacterInfo(data: any) {
  165. if (!data) {
  166. console.error('角色资料数据为空');
  167. return;
  168. }
  169. console.log(`PersonalInfoManager.displayCharacterInfo: ID=${data.characterId}, 名称=${data.characterName}`);
  170. // 保存当前显示的角色ID
  171. this.currentCharacterId = data.characterId;
  172. // 设置文本信息
  173. if (this.infoText && data.info) {
  174. this.infoText.string = data.info;
  175. }
  176. // 加载并设置头像
  177. if (this.characterAvatar && data.avatarPath) {
  178. this.loadAvatar(data.avatarPath);
  179. }
  180. // 更新详细信息显示
  181. this.updateDetailedInfo(data);
  182. // 显示面板
  183. this.showPersonalInfoPanel();
  184. }
  185. /**
  186. * 更新详细信息显示
  187. * @param data 角色详细数据
  188. */
  189. private updateDetailedInfo(data: any) {
  190. if (!this.nameInfoLabel) return;
  191. // 构建详细信息文本
  192. let detailedInfo = '';
  193. // 添加姓名
  194. if (data.characterName) {
  195. detailedInfo += `姓名: ${data.characterName}\n`;
  196. }
  197. // 添加房间号
  198. if (data.room) {
  199. detailedInfo += `房间号: ${data.room}\n`;
  200. }
  201. // 添加ID
  202. if (data.identityId) {
  203. detailedInfo += `ID: ${data.identityId}\n`;
  204. }
  205. // 添加同住人信息
  206. if (data.hasRoommate && data.roommateInfo) {
  207. detailedInfo += `同住人: ${data.roommateInfo}\n`;
  208. } else if (data.hasRoommate) {
  209. detailedInfo += `同住人: 有(未详)\n`;
  210. } else {
  211. detailedInfo += `同住人: 无\n`;
  212. }
  213. // 设置详细信息文本
  214. this.nameInfoLabel.string = detailedInfo;
  215. }
  216. /**
  217. * 加载头像
  218. * @param avatarPath 头像路径,PersonalInfo中的avatarPath
  219. */
  220. private loadAvatar(avatarPath: string): void {
  221. if (!avatarPath) {
  222. console.error('头像路径为空');
  223. this.loadDefaultAvatar();
  224. return;
  225. }
  226. // 移除可能存在的.png后缀
  227. const pathWithoutExtension = avatarPath.replace(/\.png$/, '');
  228. // 构建正确的路径,确保以/spriteFrame结尾
  229. const finalPath = pathWithoutExtension.endsWith('/spriteFrame')
  230. ? pathWithoutExtension
  231. : `${pathWithoutExtension}/spriteFrame`;
  232. console.log(`加载头像: ${finalPath}`);
  233. // 加载头像
  234. resources.load(finalPath, SpriteFrame, (err, spriteFrame) => {
  235. if (err) {
  236. console.error(`加载头像失败: ${finalPath}`, err);
  237. this.loadDefaultAvatar();
  238. return;
  239. }
  240. // 设置精灵帧
  241. this.characterAvatar.spriteFrame = spriteFrame;
  242. console.log(`成功加载头像: ${finalPath}`);
  243. });
  244. }
  245. /**
  246. * 加载默认头像
  247. */
  248. private loadDefaultAvatar(): void {
  249. console.log('加载默认头像');
  250. // 使用内置资源或默认资源
  251. const defaultIcon = "default_sprite";
  252. resources.load(defaultIcon, SpriteFrame, (err, spriteFrame) => {
  253. if (err) {
  254. console.warn(`无法加载默认头像 ${defaultIcon}`, err);
  255. return;
  256. }
  257. this.characterAvatar.spriteFrame = spriteFrame;
  258. console.log('已设置默认头像');
  259. });
  260. }
  261. onDestroy() {
  262. if (this.closeButton) {
  263. this.closeButton.node.off('click');
  264. }
  265. }
  266. }