joy_stick.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import AccountModel from "../../data/Account/AccountModel";
  2. import UIHelp from "../../framework/ui/UIHelp";
  3. import Utils from "../../framework/utils/utils";
  4. import UIPauseView from "../../ui/uiview/Interface/UIPauseView";
  5. import { EVENT_TYPE, JOY_STATE } from "./gameEnum";
  6. import gameEventManager from "./gameEventManager";
  7. const { ccclass, property } = cc._decorator;
  8. /**遥感类 */
  9. @ccclass
  10. export default class JoyStick extends cc.Component {
  11. @property(cc.Node)
  12. /**中心节点 */
  13. btn: cc.Node = null;
  14. bJoy: boolean = true;
  15. bguide: boolean = true; //是否可以引导
  16. isPause = false;
  17. protected static _instance = null;
  18. static getInstance(): JoyStick {
  19. return JoyStick._instance;
  20. }
  21. static setInstance(instance: JoyStick) {
  22. JoyStick._instance = instance;
  23. }
  24. start() {
  25. this.bguide = AccountModel.getInstance().is_new == 1 ? true : false;
  26. }
  27. onEnable() {
  28. JoyStick.setInstance(this);
  29. this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this);
  30. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
  31. this.node.on(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
  32. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
  33. gameEventManager.on(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this);
  34. gameEventManager.on(EVENT_TYPE.CANT_JOY, this.cantJoy, this);
  35. }
  36. onDisable() {
  37. this.node.off(cc.Node.EventType.TOUCH_START, this.touchStart, this);
  38. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMove, this);
  39. this.node.off(cc.Node.EventType.TOUCH_END, this.touchEnd, this);
  40. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEnd, this);
  41. gameEventManager.off(EVENT_TYPE.CHANGE_STATUS, this.changeStatue, this);
  42. gameEventManager.off(EVENT_TYPE.CANT_JOY, this.cantJoy, this);
  43. }
  44. changeStatue(flag) {
  45. //维护遥感状态机
  46. if (this.isPause && !flag){
  47. this.touchStart();
  48. }
  49. this.isPause = flag;
  50. }
  51. cantJoy(bJoy) {
  52. this.bJoy = bJoy;
  53. }
  54. update() {
  55. if (this.bguide == false) {
  56. if (this.node.$hand && this.node.$guideLb) {
  57. this.node.$hand.destroy();
  58. this.node.$guideLb.destroy();
  59. }
  60. }
  61. }
  62. /**
  63. * 触摸开始
  64. * @param event 触摸信息
  65. */
  66. private touchStart() {
  67. //发送遥感开始事件
  68. gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.start, 0, 0);
  69. if (this.bguide) {
  70. this.bguide = false;
  71. }
  72. }
  73. /**
  74. * 触摸移动
  75. * @param event 触摸信息
  76. */
  77. private touchMove(event: cc.Event.EventTouch) {
  78. if (this.isPause) {
  79. if (this.btn.x != 0){
  80. //强行归位
  81. this.touchEnd();
  82. }
  83. return;
  84. }
  85. if (!this.bJoy) { return };
  86. let pos = event.getLocation();
  87. pos = this.node.convertToNodeSpaceAR(pos);
  88. if (pos.mag() > 0) {
  89. let rad = Math.acos(pos.x / pos.mag());
  90. if (pos.y < 0) {
  91. rad = Math.PI * 2 - rad;
  92. }
  93. let angle = rad / Math.PI * 180;
  94. let len = (this.node.width - this.btn.width) / 2;
  95. let power = pos.mag() / len;
  96. if (power > 1) {
  97. power = 1;
  98. pos = pos.normalize().mul(len);
  99. }
  100. this.btn.setPosition(pos);
  101. //发送遥感移动事件
  102. gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.move, angle, power);
  103. }
  104. }
  105. /**
  106. * 触摸结束
  107. * @param event 触摸信息
  108. */
  109. private touchEnd() {
  110. this.btn.setPosition(0, 0);
  111. //发送遥感结束事件
  112. gameEventManager.emit(EVENT_TYPE.joy_stick, JOY_STATE.end, 0, 0);
  113. }
  114. }