import AccountModel from "../../data/Account/AccountModel"; import UIHelp from "../../framework/ui/UIHelp"; import Utils from "../../framework/utils/utils"; import UIPauseView from "../../ui/uiview/Interface/UIPauseView"; import { EVENT_TYPE, JOY_STATE } from "./gameEnum"; import gameEventManager from "./gameEventManager"; const { ccclass, property } = cc._decorator; /**遥感类 */ @ccclass export default class JoyStick extends cc.Component { @property(cc.Node) /**中心节点 */ btn: cc.Node = null; bJoy: boolean = true; bguide: boolean = true; //是否可以引导 isPause = false; protected static _instance = null; static getInstance(): JoyStick { return JoyStick._instance; } static setInstance(instance: JoyStick) { JoyStick._instance = instance; } start() { this.bguide = AccountModel.getInstance().is_new == 1 ? true : false; } onEnable() { JoyStick.setInstance(this); this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this); this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this); this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this); this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this); gameEventManager.on(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this); gameEventManager.on(EVENT_TYPE.CANT_JOY, this.cantJoy, this); } onDisable() { this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this); this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this); this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this); this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this); gameEventManager.off(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this); gameEventManager.off(EVENT_TYPE.CANT_JOY, this.cantJoy, this); } changeStatue(flag) { //维护遥感状态机 if (this.isPause && !flag){ this.touchStart(); } this.isPause = flag; } cantJoy(bJoy) { this.bJoy = bJoy; } update() { if (this.bguide == false) { if (this.node.$hand && this.node.$guideLb) { this.node.$hand.destroy(); this.node.$guideLb.destroy(); } } } /** * 触摸开始 * @param event 触摸信息 */ private touchStart() { //发送遥感开始事件 gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.start, 0, 0); if (this.bguide) { this.bguide = false; } } /** * 触摸移动 * @param event 触摸信息 */ private touchMove(event: cc.Event.EventTouch) { if (this.isPause) { if (this.btn.x != 0){ //强行归位 this.touchEnd(); } return; } if (!this.bJoy) { return }; let pos = event.getLocation(); pos = this.node.convertToNodeSpaceAR(pos); if (pos.mag() > 0) { let rad = Math.acos(pos.x / pos.mag()); if (pos.y < 0) { rad = Math.PI * 2 - rad; } let angle = rad / Math.PI * 180; let len = (this.node.width - this.btn.width) / 2; let power = pos.mag() / len; if (power > 1) { power = 1; pos = pos.normalize().mul(len); } this.btn.setPosition(pos); //发送遥感移动事件 gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.move, angle, power); } } /** * 触摸结束 * @param event 触摸信息 */ private touchEnd() { this.btn.setPosition(0, 0); //发送遥感结束事件 gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.end, 0, 0); } }