MapMove.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Util from "../util/Util";
  2. const { ccclass, property } = cc._decorator;
  3. /**
  4. * @description 地图移动控制器
  5. * @author Soonsky
  6. */
  7. @ccclass
  8. export class MapMove extends cc.Component {
  9. @property({ type: cc.Node, tooltip: "地图容器" }) map: cc.Node = null;
  10. private canvas: cc.Node = null;
  11. private startPoint: cc.Vec2;
  12. private tarScale = 1.5;
  13. start() {
  14. this.canvas = cc.find('Canvas');
  15. this.node.on(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
  16. //初始地图位置
  17. this.node.setPosition(cc.v2(72, 348));
  18. }
  19. onDestroy() {
  20. this.node.off(cc.Node.EventType.TOUCH_START, this.onMapTouchBegin, this);
  21. }
  22. private isDoubleTouch = false;
  23. private onMapTouchBegin(e): void {
  24. this.startPoint = e.touch._point;
  25. this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  26. this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  27. this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  28. }
  29. private onTouchMove(e: cc.Event.EventTouch): void {
  30. let touches = e.getTouches();
  31. if (touches.length == 1) {
  32. if (this.isDoubleTouch) {
  33. return;
  34. }
  35. let point = e.touch.getLocation();
  36. let offsetX = point.x - this.startPoint.x;
  37. let offsetY = point.y - this.startPoint.y;
  38. this.move(new cc.Vec2(offsetX, offsetY));
  39. this.startPoint = point;
  40. } else if (touches.length == 2) {
  41. this.isDoubleTouch = true;
  42. let b_distance = cc.Vec2.distance(touches[0].getStartLocation(), touches[1].getStartLocation());
  43. let e_distance = cc.Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
  44. // let target = this.map.scaleX;
  45. // if (b_distance > e_distance) {
  46. // target = Util.clamp(target - 0.01, 1.3, 1.7);
  47. // } else {
  48. // target = Util.clamp(target + 0.01, 1.3, 1.7);
  49. // }
  50. // this.map.scale = target
  51. // this.move(this.node.position);
  52. this.tarScale = this.map.scale;
  53. if (b_distance > e_distance) {
  54. this.tarScale = Util.clamp(this.tarScale - 0.1, 1.3, 1.7);
  55. } else {
  56. this.tarScale = Util.clamp(this.tarScale + 0.1, 1.3, 1.7);
  57. }
  58. }
  59. }
  60. private onTouchEnd(e: cc.Event.EventTouch): void {
  61. this.startPoint = cc.Vec2.ZERO;
  62. this.node.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  63. this.node.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  64. this.node.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  65. this.isDoubleTouch = false;
  66. // if (e.getTouches().length == 0) {
  67. // }
  68. }
  69. update() {
  70. let target = this.map.scale;
  71. if (target < this.tarScale) {
  72. target += 0.005;
  73. if (target >= this.tarScale) {
  74. target = this.tarScale;
  75. }
  76. } else {
  77. target -= 0.005;
  78. if (target <= this.tarScale) {
  79. target = this.tarScale;
  80. }
  81. }
  82. this.map.scale = target
  83. }
  84. private move(offset: cc.Vec2): void {
  85. this.startPoint = cc.Vec2.ZERO;
  86. this.node.setPosition(this.confirmPosition(new cc.Vec2(this.node.position.x + offset.x, this.node.position.y + offset.y)));
  87. }
  88. /**确认移动到的坐标是否合法 */
  89. private confirmPosition(pos: cc.Vec2): cc.Vec3 {
  90. 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);
  91. 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);
  92. return new cc.Vec3(clampX, clampY, 0);
  93. }
  94. /**将视角中心移动到指定位置 */
  95. public moveTo(pos: cc.Vec3): void {
  96. let toPoint = new cc.Vec2(-pos.x * this.map.scaleX, -pos.y * this.map.scaleY);
  97. cc.tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
  98. }
  99. private clickMapBookBtn() {
  100. mk.ui.openPanel('module/farmMap/farmMap');
  101. }
  102. }