LieDetectorManager.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { _decorator, Component, Node, Button, Sprite, SpriteFrame, resources } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. /**
  4. * 测谎仪管理器,负责测谎仪UI的显示和功能实现
  5. */
  6. @ccclass('LieDetectorManager')
  7. export class LieDetectorManager extends Component {
  8. @property({
  9. type: Node,
  10. tooltip: '测谎仪UI面板'
  11. })
  12. lieDetectorPanel: Node = null;
  13. @property({
  14. type: Button,
  15. tooltip: '关闭按钮'
  16. })
  17. closeButton: Button = null;
  18. @property({
  19. type: Button,
  20. tooltip: '测谎按钮'
  21. })
  22. detectButton: Button = null;
  23. @property({
  24. type: Sprite,
  25. tooltip: '结果显示'
  26. })
  27. resultDisplay: Sprite = null;
  28. @property({
  29. type: SpriteFrame,
  30. tooltip: '真实结果图片'
  31. })
  32. trueResultFrame: SpriteFrame = null;
  33. @property({
  34. type: SpriteFrame,
  35. tooltip: '虚假结果图片'
  36. })
  37. falseResultFrame: SpriteFrame = null;
  38. @property({
  39. type: Node,
  40. tooltip: '提示信息Label节点'
  41. })
  42. hintLabel: Node = null;
  43. // 当前角色是否为伪装者
  44. private isCharacterFake: boolean = false;
  45. start() {
  46. // 初始化隐藏面板
  47. if (this.lieDetectorPanel) {
  48. this.lieDetectorPanel.active = false;
  49. }
  50. // 隐藏结果
  51. this.hideResult();
  52. // 注册按钮事件
  53. this.setupButtons();
  54. }
  55. private setupButtons() {
  56. // 关闭按钮
  57. if (this.closeButton) {
  58. this.closeButton.node.on(Button.EventType.CLICK, this.hideLieDetectorPanel, this);
  59. } else {
  60. console.error('测谎仪关闭按钮未设置');
  61. }
  62. // 测谎按钮
  63. if (this.detectButton) {
  64. this.detectButton.node.on(Button.EventType.CLICK, this.detectLie, this);
  65. } else {
  66. console.error('测谎按钮未设置');
  67. }
  68. }
  69. /**
  70. * 设置当前角色的真实性
  71. * @param isFake 当前角色是否是伪装者
  72. */
  73. public setCharacterStatus(isFake: boolean) {
  74. this.isCharacterFake = isFake;
  75. // 重置结果显示
  76. this.hideResult();
  77. // 显示提示信息
  78. this.showHint();
  79. }
  80. /**
  81. * 显示测谎仪面板
  82. */
  83. public showLieDetectorPanel() {
  84. if (this.lieDetectorPanel) {
  85. this.lieDetectorPanel.active = true;
  86. this.lieDetectorPanel.setSiblingIndex(999); // 确保显示在最前面
  87. // 初始隐藏结果
  88. this.hideResult();
  89. // 显示提示信息
  90. this.showHint();
  91. } else {
  92. console.error('测谎仪面板未设置');
  93. }
  94. }
  95. /**
  96. * 隐藏测谎仪面板
  97. */
  98. public hideLieDetectorPanel() {
  99. if (this.lieDetectorPanel) {
  100. this.lieDetectorPanel.active = false;
  101. }
  102. }
  103. /**
  104. * 执行测谎操作
  105. */
  106. private detectLie() {
  107. // 显示测谎结果
  108. if (this.resultDisplay) {
  109. if (this.isCharacterFake) {
  110. // 显示"假"结果
  111. this.resultDisplay.spriteFrame = this.falseResultFrame;
  112. } else {
  113. // 显示"真"结果
  114. this.resultDisplay.spriteFrame = this.trueResultFrame;
  115. }
  116. this.resultDisplay.node.active = true;
  117. }
  118. // 隐藏提示信息
  119. this.hideHint();
  120. }
  121. /**
  122. * 隐藏测谎结果
  123. */
  124. private hideResult() {
  125. if (this.resultDisplay) {
  126. this.resultDisplay.node.active = false;
  127. }
  128. }
  129. /**
  130. * 显示提示信息
  131. */
  132. private showHint() {
  133. if (this.hintLabel) {
  134. this.hintLabel.active = true;
  135. }
  136. }
  137. /**
  138. * 隐藏提示信息
  139. */
  140. private hideHint() {
  141. if (this.hintLabel) {
  142. this.hintLabel.active = false;
  143. }
  144. }
  145. onDestroy() {
  146. // 清理按钮事件
  147. if (this.closeButton) {
  148. this.closeButton.node.off(Button.EventType.CLICK, this.hideLieDetectorPanel, this);
  149. }
  150. if (this.detectButton) {
  151. this.detectButton.node.off(Button.EventType.CLICK, this.detectLie, this);
  152. }
  153. }
  154. }