MapMove.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. this.node.setPosition(cc.v2(72, 348));
  20. }
  21. onEnable() {
  22. this.scheduleOnce(() => {
  23. this.onScale = true;
  24. }, 0.3)
  25. }
  26. onDestroy() {
  27. this.node.off(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
  28. }
  29. private isDoubleTouch = false;
  30. private onMapTouchBegin(e): void {
  31. this.startPoint = e.touch._point;
  32. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  33. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  34. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  35. }
  36. private onTouchMove(e: cc.Event.EventTouch): void {
  37. let touches = e.getTouches();
  38. if (touches.length == 1) {
  39. if (this.isDoubleTouch) {
  40. return;
  41. }
  42. let point = e.touch.getLocation();
  43. let offsetX = point.x - this.startPoint.x;
  44. let offsetY = point.y - this.startPoint.y;
  45. this.move(new cc.Vec2(offsetX, offsetY));
  46. this.startPoint = point;
  47. } else if (touches.length == 2) {
  48. this.isDoubleTouch = true;
  49. let b_distance = cc.Vec2.distance(touches[0].getStartLocation(), touches[1].getStartLocation());
  50. let e_distance = cc.Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
  51. // let target = this.map.scaleX;
  52. // if (b_distance > e_distance) {
  53. // target = Util.clamp(target - 0.01, 1.3, 1.7);
  54. // } else {
  55. // target = Util.clamp(target + 0.01, 1.3, 1.7);
  56. // }
  57. // this.map.scale = target
  58. // this.move(this.node.position);
  59. this.tarScale = this.map.scale;
  60. if (b_distance > e_distance) {
  61. this.tarScale = Util.clamp(this.tarScale - 0.1, 1.3, 1.7);
  62. } else {
  63. this.tarScale = Util.clamp(this.tarScale + 0.1, 1.3, 1.7);
  64. }
  65. }
  66. }
  67. private onTouchEnd(e: cc.Event.EventTouch): void {
  68. this.startPoint = cc.Vec2.ZERO;
  69. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  70. this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  71. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  72. this.isDoubleTouch = false;
  73. // if (e.getTouches().length == 0) {
  74. // }
  75. }
  76. update() {
  77. if (!this.onScale) {
  78. return;
  79. }
  80. let target = this.map.scale;
  81. if (target < this.tarScale) {
  82. target += 0.005;
  83. if (target >= this.tarScale) {
  84. target = this.tarScale;
  85. }
  86. } else {
  87. target -= 0.005;
  88. if (target <= this.tarScale) {
  89. target = this.tarScale;
  90. }
  91. }
  92. this.map.scale = target
  93. }
  94. private move(offset: cc.Vec2): void {
  95. this.startPoint = cc.Vec2.ZERO;
  96. this.node.setPosition(this.confirmPosition(new cc.Vec2(this.node.position.x + offset.x, this.node.position.y + offset.y)));
  97. }
  98. /**确认移动到的坐标是否合法 */
  99. private confirmPosition(pos: cc.Vec2): cc.Vec3 {
  100. 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);
  101. 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);
  102. return new cc.Vec3(clampX, clampY, 0);
  103. }
  104. /**将视角中心移动到指定位置 */
  105. public moveTo(pos: cc.Vec3): void {
  106. let toPoint = new cc.Vec2(-pos.x * this.map.scaleX, -pos.y * this.map.scaleY);
  107. cc.tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
  108. }
  109. private clickMapBookBtn() {
  110. mk.ui.openPanel('module/farmMap/farmMap');
  111. mk.data.sendDataEvent(DataEventId.button_click, "图鉴icon");
  112. }
  113. private clickSpeedup() {
  114. mk.data.sendDataEvent(DataEventId.button_click, "全体加速icon");
  115. }
  116. }