MapMove.ts 4.2 KB

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