MapMove.ts 3.6 KB

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