import { _decorator, Component, Node } from 'cc'; import { PhoneManager } from './PhoneManager'; import { GameFlowManager } from './GameFlowManager'; const { ccclass, property } = _decorator; /** * 电话触发器,用于点击场景中的电话物体时激活电话UI和对话 */ @ccclass('PhoneTrigger') export class PhoneTrigger extends Component { @property({ type: PhoneManager, tooltip: '电话管理器引用 (已弃用,请使用GameFlowManager中的电话管理器)' }) phoneManager: PhoneManager = null; @property({ type: Node, tooltip: '游戏流程管理器所在的节点' }) gameFlowManagerNode: Node = null; @property({ tooltip: '是否启用触发器' }) isEnabled: boolean = true; // 游戏流程管理器引用 private gameFlowManager: GameFlowManager = null; start() { // 注册节点点击事件 this.node.on(Node.EventType.TOUCH_END, this.onPhoneClicked, this); // 从节点获取GameFlowManager组件 if (this.gameFlowManagerNode) { this.gameFlowManager = this.gameFlowManagerNode.getComponent(GameFlowManager); if (!this.gameFlowManager) { console.error('游戏流程管理器节点上没有GameFlowManager组件'); } } else { // 尝试在场景中查找 this.gameFlowManager = this.node.scene.getComponentInChildren(GameFlowManager); if (!this.gameFlowManager) { console.error('无法在场景中找到GameFlowManager组件'); } } } /** * 电话被点击时的处理 */ private onPhoneClicked() { if (!this.isEnabled) { return; } console.log('电话被点击'); // 优先通过GameFlowManager获取电话管理器 if (this.gameFlowManager) { this.gameFlowManager.showPhoneUI(); } else if (this.phoneManager) { // 兼容:如果直接设置了phoneManager属性,则使用它 this.phoneManager.showPhonePanel(); } else { console.error('未设置电话管理器引用,无法显示电话UI'); } } /** * 启用触发器 */ public enable() { this.isEnabled = true; } /** * 禁用触发器 */ public disable() { this.isEnabled = false; } onDestroy() { // 移除事件监听 this.node.off(Node.EventType.TOUCH_END, this.onPhoneClicked, this); } }