MapMove.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { DataEventId } from "../../game/data/GameData";
  2. import Util from "../util/Util";
  3. const { ccclass, property } = cc._decorator;
  4. /**
  5. * @description 地图移动控制器
  6. * @author Soonsky
  7. */
  8. @ccclass
  9. export class MapMove extends cc.Component {
  10. @property({ type: cc.Node, tooltip: "地图容器" }) map: cc.Node = null;
  11. private canvas: cc.Node = null;
  12. private startPoint: cc.Vec2;
  13. private onScale = false;
  14. private tarScale = 1.5;
  15. start() {
  16. this.canvas = cc.find('Canvas');
  17. // this.node.on(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
  18. this.map.scale = 1.2;
  19. //地图初始位置
  20. // this.node.setPosition(cc.v2(72, 348));
  21. // this.node.setPosition(cc.v2(195.9, 23.6));
  22. this.node.setPosition(cc.v2(0, 800));
  23. }
  24. onEnable() {
  25. this.scheduleOnce(() => {
  26. this.onScale = true;
  27. }, 0.3)
  28. }
  29. onDestroy() {
  30. this.node.off(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
  31. }
  32. private isDoubleTouch = false;
  33. private onMapTouchBegin(e): void {
  34. this.startPoint = e.touch._point;
  35. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  36. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  37. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  38. }
  39. private onTouchMove(e: cc.Event.EventTouch): void {
  40. let touches = e.getTouches();
  41. if (touches.length == 1) {
  42. if (this.isDoubleTouch) {
  43. return;
  44. }
  45. let point = e.touch.getLocation();
  46. let offsetX = point.x - this.startPoint.x;
  47. let offsetY = point.y - this.startPoint.y;
  48. this.move(new cc.Vec2(offsetX, offsetY));
  49. this.startPoint = point;
  50. } else if (touches.length == 2) {
  51. this.isDoubleTouch = true;
  52. let b_distance = cc.Vec2.distance(touches[0].getStartLocation(), touches[1].getStartLocation());
  53. let e_distance = cc.Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
  54. // let target = this.map.scaleX;
  55. // if (b_distance > e_distance) {
  56. // target = Util.clamp(target - 0.01, 1.3, 1.7);
  57. // } else {
  58. // target = Util.clamp(target + 0.01, 1.3, 1.7);
  59. // }
  60. // this.map.scale = target
  61. // this.move(this.node.position);
  62. this.tarScale = this.map.scale;
  63. if (b_distance > e_distance) {
  64. this.tarScale = Util.clamp(this.tarScale - 0.1, 1.3, 1.7);
  65. } else {
  66. this.tarScale = Util.clamp(this.tarScale + 0.1, 1.3, 1.7);
  67. }
  68. }
  69. }
  70. private onTouchEnd(e: cc.Event.EventTouch): void {
  71. this.startPoint = cc.Vec2.ZERO;
  72. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  73. this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  74. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  75. this.isDoubleTouch = false;
  76. // if (e.getTouches().length == 0) {
  77. // }
  78. }
  79. update() {
  80. if (!this.onScale) {
  81. return;
  82. }
  83. let target = this.map.scale;
  84. if (target < this.tarScale) {
  85. target += 0.005;
  86. if (target >= this.tarScale) {
  87. target = this.tarScale;
  88. }
  89. } else {
  90. target -= 0.005;
  91. if (target <= this.tarScale) {
  92. target = this.tarScale;
  93. }
  94. }
  95. this.map.scale = target
  96. }
  97. private move(offset: cc.Vec2): void {
  98. this.startPoint = cc.Vec2.ZERO;
  99. this.node.setPosition(this.confirmPosition(new cc.Vec2(this.node.position.x + offset.x, this.node.position.y + offset.y)));
  100. }
  101. /**确认移动到的坐标是否合法 */
  102. private confirmPosition(pos: cc.Vec2): cc.Vec3 {
  103. let clampX = Util.clamp(pos.x, -(this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2), this.map.width * (this.map.scaleX - 0.2) / 2 - this.canvas.width / 2);
  104. let clampY = Util.clamp(pos.y, -(this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2), this.map.height * (this.map.scaleY - 0.2) / 2 - this.canvas.height / 2);
  105. return new cc.Vec3(clampX, clampY, 0);
  106. }
  107. /**将视角中心移动到指定位置 */
  108. public moveTo(pos: cc.Vec3): void {
  109. let toPoint = new cc.Vec2(-pos.x * this.map.scaleX, -pos.y * this.map.scaleY);
  110. cc.tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
  111. }
  112. private clickMapBookBtn() {
  113. return;
  114. mk.ui.openPanel('module/farmMap/farmMap');
  115. mk.data.sendDataEvent(DataEventId.button_click, "图鉴icon");
  116. }
  117. private clickSpeedup() {
  118. mk.data.sendDataEvent(DataEventId.button_click, "全体加速icon");
  119. }
  120. private clickCenter() {
  121. return;
  122. mk.data.sendDataEvent(DataEventId.button_click, "农场等级领奖按钮");
  123. mk.ui.openPanel('game/prefab/uiPanel/FarmLevelPanel');
  124. }
  125. }