MapMove.ts 4.6 KB

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