PhoneManager.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { _decorator, Component, Node, Button, Animation } from 'cc';
  2. import { DialogueManager } from './DialogueManager';
  3. const { ccclass, property } = _decorator;
  4. /**
  5. * 电话管理器,控制电话UI的显示和隐藏,以及显示电话对话
  6. */
  7. @ccclass('PhoneManager')
  8. export class PhoneManager extends Component {
  9. @property({
  10. type: Node,
  11. tooltip: '电话UI面板'
  12. })
  13. phonePanel: Node = null;
  14. @property({
  15. type: Button,
  16. tooltip: '关闭按钮'
  17. })
  18. closeButton: Button = null;
  19. @property({
  20. type: Animation,
  21. tooltip: '电话动画组件(可选)'
  22. })
  23. phoneAnimation: Animation = null;
  24. @property({
  25. type: DialogueManager,
  26. tooltip: '对话管理器引用'
  27. })
  28. dialogueManager: DialogueManager = null;
  29. @property({
  30. tooltip: '电话对话内容'
  31. })
  32. phoneDialogue: string = "您好,这里是总机。有什么可以帮您的吗?";
  33. @property({
  34. tooltip: '对话表情类型',
  35. })
  36. emotionType: string = "normal";
  37. start() {
  38. // 初始化隐藏电话面板
  39. if (this.phonePanel) {
  40. this.phonePanel.active = false;
  41. }
  42. // 注册关闭按钮点击事件
  43. this.setupCloseButton();
  44. }
  45. /**
  46. * 设置关闭按钮事件
  47. */
  48. private setupCloseButton() {
  49. if (this.closeButton) {
  50. // 移除可能已存在的事件监听
  51. this.closeButton.node.off('click');
  52. // 使用按钮的点击事件监听方式
  53. this.closeButton.node.on('click', () => {
  54. console.log('电话关闭按钮被点击');
  55. this.hidePhonePanel();
  56. }, this);
  57. console.log('电话关闭按钮事件已注册');
  58. } else {
  59. console.error('电话关闭按钮未设置');
  60. }
  61. }
  62. /**
  63. * 显示电话面板
  64. */
  65. public showPhonePanel() {
  66. if (this.phonePanel) {
  67. this.phonePanel.active = true;
  68. // 确保面板在最前面
  69. this.phonePanel.setSiblingIndex(999);
  70. // 播放打开动画(如果有)
  71. if (this.phoneAnimation) {
  72. this.phoneAnimation.play('phone_open');
  73. }
  74. // 显示电话对话
  75. this.showPhoneDialogue();
  76. } else {
  77. console.error('电话面板未设置');
  78. }
  79. }
  80. /**
  81. * 显示电话对话
  82. */
  83. private showPhoneDialogue() {
  84. // 使用对话管理器显示电话对话
  85. if (this.dialogueManager && this.phoneDialogue) {
  86. this.dialogueManager.showDialogue(this.phoneDialogue, this.emotionType);
  87. } else {
  88. console.error('对话管理器未设置或对话内容为空');
  89. }
  90. }
  91. /**
  92. * 隐藏电话面板
  93. */
  94. public hidePhonePanel() {
  95. console.log('hidePhonePanel被调用');
  96. if (this.phonePanel) {
  97. // 播放关闭动画,并在动画结束后隐藏面板
  98. if (this.phoneAnimation) {
  99. this.phoneAnimation.play('phone_close');
  100. this.scheduleOnce(() => {
  101. this.phonePanel.active = false;
  102. console.log('电话面板已隐藏(动画后)');
  103. }, 0.5); // 假设动画时长为0.5秒
  104. } else {
  105. this.phonePanel.active = false;
  106. console.log('电话面板已隐藏(立即)');
  107. }
  108. }
  109. }
  110. onDestroy() {
  111. // 移除按钮事件监听
  112. if (this.closeButton) {
  113. this.closeButton.node.off('click');
  114. }
  115. }
  116. }