MapMove.ts 4.3 KB

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