123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- import { _decorator, Component, Node, Button, Sprite, SpriteFrame, resources } from 'cc';
- import { GameFlowManager } from './GameFlowManager';
- import { DataManager } from './DataManager';
- const { ccclass, property } = _decorator;
- /**
- * 测谎仪管理器,控制测谎仪UI的显示和隐藏,并处理测谎功能
- */
- @ccclass('LieDetectorManager')
- export class LieDetectorManager extends Component {
- @property({
- type: Node,
- tooltip: '测谎仪UI面板'
- })
- detectorPanel: Node = null;
-
- @property({
- type: Button,
- tooltip: '关闭按钮'
- })
- closeButton: Button = null;
-
- @property({
- type: Button,
- tooltip: '测谎按钮(红色按钮)'
- })
- testButton: Button = null;
-
- @property({
- type: Sprite,
- tooltip: '结果显示图片'
- })
- resultDisplay: Sprite = null;
-
- @property({
- type: GameFlowManager,
- tooltip: '游戏流程管理器引用'
- })
- gameFlowManager: GameFlowManager = null;
-
- @property({
- type: DataManager,
- tooltip: '数据管理器引用'
- })
- dataManager: DataManager = null;
-
- @property({
- type: SpriteFrame,
- tooltip: '真人结果图片'
- })
- trueResultImage: SpriteFrame = null;
-
- @property({
- type: SpriteFrame,
- tooltip: '伪人结果图片'
- })
- fakeResultImage: SpriteFrame = null;
-
- // 添加默认资源路径属性
- @property({
- tooltip: '真人结果图片资源路径(可选)',
- })
- trueResultPath: string = 'ui/liedetector/true_result';
-
- @property({
- tooltip: '伪人结果图片资源路径(可选)',
- })
- fakeResultPath: string = 'ui/liedetector/false_result';
-
- private imagesLoaded: boolean = false;
-
- start() {
- // 初始化隐藏测谎仪面板
- if (this.detectorPanel) {
- this.detectorPanel.active = false;
- }
-
- // 隐藏结果显示
- if (this.resultDisplay) {
- this.resultDisplay.node.active = false;
- }
-
- // 注册按钮点击事件
- this.setupButtons();
-
- // 预加载结果图片
- this.preloadResultImages();
- }
-
- /**
- * 预加载结果图片
- */
- private preloadResultImages() {
- // 如果已经直接设置了SpriteFrame,则不需要加载
- if (this.trueResultImage && this.fakeResultImage) {
- this.imagesLoaded = true;
- return;
- }
-
- let loadedCount = 0;
- const totalToLoad = 2;
-
- // 加载真人结果图片
- if (!this.trueResultImage && this.trueResultPath) {
- resources.load(this.trueResultPath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.error(`加载真人结果图片失败: ${this.trueResultPath}`, err);
- } else {
- console.log(`加载真人结果图片成功: ${this.trueResultPath}`);
- this.trueResultImage = spriteFrame;
- }
-
- loadedCount++;
- if (loadedCount >= totalToLoad) {
- this.imagesLoaded = true;
- }
- });
- } else {
- loadedCount++;
- }
-
- // 加载伪人结果图片
- if (!this.fakeResultImage && this.fakeResultPath) {
- resources.load(this.fakeResultPath, SpriteFrame, (err, spriteFrame) => {
- if (err) {
- console.error(`加载伪人结果图片失败: ${this.fakeResultPath}`, err);
- } else {
- console.log(`加载伪人结果图片成功: ${this.fakeResultPath}`);
- this.fakeResultImage = spriteFrame;
- }
-
- loadedCount++;
- if (loadedCount >= totalToLoad) {
- this.imagesLoaded = true;
- }
- });
- } else {
- loadedCount++;
- }
- }
-
- /**
- * 设置按钮事件
- */
- private setupButtons() {
- // 设置关闭按钮
- if (this.closeButton) {
- this.closeButton.node.off('click');
- this.closeButton.node.on('click', () => {
- console.log('测谎仪关闭按钮被点击');
- this.hideDetectorPanel();
- }, this);
- } else {
- console.error('测谎仪关闭按钮未设置');
- }
-
- // 设置测谎按钮
- if (this.testButton) {
- this.testButton.node.off('click');
- this.testButton.node.on('click', () => {
- console.log('测谎按钮被点击');
- this.performLieTest();
- }, this);
- } else {
- console.error('测谎按钮未设置');
- }
- }
-
- /**
- * 显示测谎仪面板
- */
- public showDetectorPanel() {
- if (this.detectorPanel) {
- this.detectorPanel.active = true;
-
- // 确保面板在最前面
- this.detectorPanel.setSiblingIndex(999);
-
- // 隐藏结果显示,重置状态
- if (this.resultDisplay) {
- this.resultDisplay.node.active = false;
- }
-
- // 如果图片还没加载完成,再次尝试加载
- if (!this.imagesLoaded) {
- this.preloadResultImages();
- }
- } else {
- console.error('测谎仪面板未设置');
- }
- }
-
- /**
- * 执行测谎检测
- */
- private performLieTest() {
- if (!this.gameFlowManager || !this.dataManager || !this.resultDisplay) {
- console.error('游戏流程管理器、数据管理器或结果显示未设置');
- return;
- }
-
- // 获取当前NPC数据
- const currentNpc = this.gameFlowManager.getCurrentNpcData();
-
- if (!currentNpc) {
- console.error('无法获取当前NPC数据');
- return;
- }
-
- // 判断是否为真人
- const isRealHuman = currentNpc.type === 'real';
-
- console.log(`测谎结果: 角色${currentNpc.characterId} 是${isRealHuman ? '真人' : '伪人'}`);
-
- // 确保有结果图片可用
- if (isRealHuman && !this.trueResultImage) {
- console.error('真人结果图片未设置');
- return;
- }
-
- if (!isRealHuman && !this.fakeResultImage) {
- console.error('伪人结果图片未设置');
- return;
- }
-
- // 显示对应结果图片
- this.resultDisplay.spriteFrame = isRealHuman ? this.trueResultImage : this.fakeResultImage;
- this.resultDisplay.node.active = true;
- }
-
- /**
- * 隐藏测谎仪面板
- */
- public hideDetectorPanel() {
- if (this.detectorPanel) {
- this.detectorPanel.active = false;
- }
- }
-
- onDestroy() {
- // 移除按钮事件监听
- if (this.closeButton) {
- this.closeButton.node.off('click');
- }
-
- if (this.testButton) {
- this.testButton.node.off('click');
- }
- }
- }
|