LieDetectorManager.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { _decorator, Component, Node, Button, Sprite, SpriteFrame, resources } from 'cc';
  2. import { GameFlowManager } from './GameFlowManager';
  3. import { DataManager } from './DataManager';
  4. const { ccclass, property } = _decorator;
  5. /**
  6. * 测谎仪管理器,控制测谎仪UI的显示和隐藏,并处理测谎功能
  7. */
  8. @ccclass('LieDetectorManager')
  9. export class LieDetectorManager extends Component {
  10. @property({
  11. type: Node,
  12. tooltip: '测谎仪UI面板'
  13. })
  14. detectorPanel: Node = null;
  15. @property({
  16. type: Button,
  17. tooltip: '关闭按钮'
  18. })
  19. closeButton: Button = null;
  20. @property({
  21. type: Button,
  22. tooltip: '测谎按钮(红色按钮)'
  23. })
  24. testButton: Button = null;
  25. @property({
  26. type: Sprite,
  27. tooltip: '结果显示图片'
  28. })
  29. resultDisplay: Sprite = null;
  30. @property({
  31. type: GameFlowManager,
  32. tooltip: '游戏流程管理器引用'
  33. })
  34. gameFlowManager: GameFlowManager = null;
  35. @property({
  36. type: DataManager,
  37. tooltip: '数据管理器引用'
  38. })
  39. dataManager: DataManager = null;
  40. @property({
  41. type: SpriteFrame,
  42. tooltip: '真人结果图片'
  43. })
  44. trueResultImage: SpriteFrame = null;
  45. @property({
  46. type: SpriteFrame,
  47. tooltip: '伪人结果图片'
  48. })
  49. fakeResultImage: SpriteFrame = null;
  50. // 添加默认资源路径属性
  51. @property({
  52. tooltip: '真人结果图片资源路径(可选)',
  53. })
  54. trueResultPath: string = 'ui/liedetector/true_result';
  55. @property({
  56. tooltip: '伪人结果图片资源路径(可选)',
  57. })
  58. fakeResultPath: string = 'ui/liedetector/false_result';
  59. private imagesLoaded: boolean = false;
  60. start() {
  61. // 初始化隐藏测谎仪面板
  62. if (this.detectorPanel) {
  63. this.detectorPanel.active = false;
  64. }
  65. // 隐藏结果显示
  66. if (this.resultDisplay) {
  67. this.resultDisplay.node.active = false;
  68. }
  69. // 注册按钮点击事件
  70. this.setupButtons();
  71. // 预加载结果图片
  72. this.preloadResultImages();
  73. }
  74. /**
  75. * 预加载结果图片
  76. */
  77. private preloadResultImages() {
  78. // 如果已经直接设置了SpriteFrame,则不需要加载
  79. if (this.trueResultImage && this.fakeResultImage) {
  80. this.imagesLoaded = true;
  81. return;
  82. }
  83. let loadedCount = 0;
  84. const totalToLoad = 2;
  85. // 加载真人结果图片
  86. if (!this.trueResultImage && this.trueResultPath) {
  87. resources.load(this.trueResultPath, SpriteFrame, (err, spriteFrame) => {
  88. if (err) {
  89. console.error(`加载真人结果图片失败: ${this.trueResultPath}`, err);
  90. } else {
  91. console.log(`加载真人结果图片成功: ${this.trueResultPath}`);
  92. this.trueResultImage = spriteFrame;
  93. }
  94. loadedCount++;
  95. if (loadedCount >= totalToLoad) {
  96. this.imagesLoaded = true;
  97. }
  98. });
  99. } else {
  100. loadedCount++;
  101. }
  102. // 加载伪人结果图片
  103. if (!this.fakeResultImage && this.fakeResultPath) {
  104. resources.load(this.fakeResultPath, SpriteFrame, (err, spriteFrame) => {
  105. if (err) {
  106. console.error(`加载伪人结果图片失败: ${this.fakeResultPath}`, err);
  107. } else {
  108. console.log(`加载伪人结果图片成功: ${this.fakeResultPath}`);
  109. this.fakeResultImage = spriteFrame;
  110. }
  111. loadedCount++;
  112. if (loadedCount >= totalToLoad) {
  113. this.imagesLoaded = true;
  114. }
  115. });
  116. } else {
  117. loadedCount++;
  118. }
  119. }
  120. /**
  121. * 设置按钮事件
  122. */
  123. private setupButtons() {
  124. // 设置关闭按钮
  125. if (this.closeButton) {
  126. this.closeButton.node.off('click');
  127. this.closeButton.node.on('click', () => {
  128. console.log('测谎仪关闭按钮被点击');
  129. this.hideDetectorPanel();
  130. }, this);
  131. } else {
  132. console.error('测谎仪关闭按钮未设置');
  133. }
  134. // 设置测谎按钮
  135. if (this.testButton) {
  136. this.testButton.node.off('click');
  137. this.testButton.node.on('click', () => {
  138. console.log('测谎按钮被点击');
  139. this.performLieTest();
  140. }, this);
  141. } else {
  142. console.error('测谎按钮未设置');
  143. }
  144. }
  145. /**
  146. * 显示测谎仪面板
  147. */
  148. public showDetectorPanel() {
  149. if (this.detectorPanel) {
  150. this.detectorPanel.active = true;
  151. // 确保面板在最前面
  152. this.detectorPanel.setSiblingIndex(999);
  153. // 隐藏结果显示,重置状态
  154. if (this.resultDisplay) {
  155. this.resultDisplay.node.active = false;
  156. }
  157. // 如果图片还没加载完成,再次尝试加载
  158. if (!this.imagesLoaded) {
  159. this.preloadResultImages();
  160. }
  161. } else {
  162. console.error('测谎仪面板未设置');
  163. }
  164. }
  165. /**
  166. * 执行测谎检测
  167. */
  168. private performLieTest() {
  169. if (!this.gameFlowManager || !this.dataManager || !this.resultDisplay) {
  170. console.error('游戏流程管理器、数据管理器或结果显示未设置');
  171. return;
  172. }
  173. // 获取当前NPC数据
  174. const currentNpc = this.gameFlowManager.getCurrentNpcData();
  175. if (!currentNpc) {
  176. console.error('无法获取当前NPC数据');
  177. return;
  178. }
  179. // 判断是否为真人
  180. const isRealHuman = currentNpc.type === 'real';
  181. console.log(`测谎结果: 角色${currentNpc.characterId} 是${isRealHuman ? '真人' : '伪人'}`);
  182. // 确保有结果图片可用
  183. if (isRealHuman && !this.trueResultImage) {
  184. console.error('真人结果图片未设置');
  185. return;
  186. }
  187. if (!isRealHuman && !this.fakeResultImage) {
  188. console.error('伪人结果图片未设置');
  189. return;
  190. }
  191. // 显示对应结果图片
  192. this.resultDisplay.spriteFrame = isRealHuman ? this.trueResultImage : this.fakeResultImage;
  193. this.resultDisplay.node.active = true;
  194. }
  195. /**
  196. * 隐藏测谎仪面板
  197. */
  198. public hideDetectorPanel() {
  199. if (this.detectorPanel) {
  200. this.detectorPanel.active = false;
  201. }
  202. }
  203. onDestroy() {
  204. // 移除按钮事件监听
  205. if (this.closeButton) {
  206. this.closeButton.node.off('click');
  207. }
  208. if (this.testButton) {
  209. this.testButton.node.off('click');
  210. }
  211. }
  212. }