MapMove.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { EventTouch, tween, UITransform, v3, Vec2, Vec3 } from 'cc';
  2. import { _decorator, Component, Node, SystemEventType } from 'cc';
  3. import { GeometryUtils } from '../core/utils/GeometryUtils';
  4. import { Utils } from '../core/utils/Utils';
  5. const { ccclass, property } = _decorator;
  6. /**
  7. * @description 地图移动控制器
  8. * @author Soonsky
  9. */
  10. @ccclass('MapMove')
  11. export class MapMove extends Component {
  12. @property({ type: UITransform, tooltip: "画布,用与判断地图移动边界" }) canvas: UITransform;
  13. @property({ type: Node, tooltip: "地图容器" }) map: Node;
  14. private startPoint: Vec3;
  15. private transform: UITransform;
  16. start() {
  17. this.transform = this.map.getComponent("cc.UITransform") as UITransform;
  18. this.node.on(SystemEventType.TOUCH_START, this.onMapTouchBegin, this);
  19. }
  20. onDestroy() {
  21. this.node.off(SystemEventType.TOUCH_START, this.onMapTouchBegin, this);
  22. }
  23. private isDoubleTouch = false;
  24. private onMapTouchBegin(e: EventTouch): void {
  25. this.startPoint = GeometryUtils.V2ToV3(e.getUILocation());
  26. this.node.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  27. this.node.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  28. this.node.on(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
  29. }
  30. private onTouchMove(e: EventTouch): void {
  31. let touches = e.getAllTouches();
  32. if (touches.length == 1) {
  33. if (this.isDoubleTouch) {
  34. return;
  35. }
  36. let point = GeometryUtils.V2ToV3(e.getUILocation());
  37. let offsetX = point.x - this.startPoint.x;
  38. let offsetY = point.y - this.startPoint.y;
  39. this.move(new Vec3(offsetX, offsetY, 0));
  40. this.startPoint = point;
  41. } else if (touches.length == 2) {
  42. this.isDoubleTouch = true;
  43. let b_distance = Vec2.distance(touches[0].getStartLocation(), touches[1].getStartLocation());
  44. let e_distance = Vec2.distance(touches[0].getLocation(), touches[1].getLocation());
  45. let target = this.map.scale.x;
  46. if (b_distance > e_distance) {
  47. target = GeometryUtils.clamp(target - 0.01, 1.3, 1.7);
  48. } else {
  49. target = GeometryUtils.clamp(target + 0.01, 1.3, 1.7);
  50. }
  51. this.map.scale = v3(target, target, 1);
  52. // this.move(this.node.position);
  53. }
  54. }
  55. private onTouchEnd(e: EventTouch): void {
  56. this.startPoint = Vec3.ZERO;
  57. this.node.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  58. this.node.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  59. this.node.off(SystemEventType.TOUCH_CANCEL, this.onTouchEnd, this);
  60. if (e.getAllTouches().length == 0) {
  61. this.isDoubleTouch = false;
  62. }
  63. }
  64. update() {
  65. let target = this.map.scale.x;
  66. if (target < 1.5) {
  67. target += 0.001;
  68. } else {
  69. target -= 0.001;
  70. }
  71. this.map.scale = v3(target, target, 1);
  72. }
  73. private move(offset: Vec3): void {
  74. this.startPoint = Vec3.ZERO;
  75. this.node.setPosition(this.confirmPosition(new Vec3(this.node.position.x + offset.x, this.node.position.y + offset.y, 0)));
  76. }
  77. /**确认移动到的坐标是否合法 */
  78. private confirmPosition(pos: Vec3): Vec3 {
  79. let clampX = GeometryUtils.clamp(pos.x, -(this.transform.width * (this.map.scale.x - 0.2) / 2 - this.canvas.width / 2), this.transform.width * (this.map.scale.x - 0.2) / 2 - this.canvas.width / 2);
  80. let clampY = GeometryUtils.clamp(pos.y, -(this.transform.height * (this.map.scale.y - 0.2) / 2 - this.canvas.height / 2), this.transform.height * (this.map.scale.y - 0.2) / 2 - this.canvas.height / 2);
  81. return new Vec3(clampX, clampY, 0);
  82. }
  83. /**将视角中心移动到指定位置 */
  84. public moveTo(pos: Vec3): void {
  85. let toPoint = new Vec3(-pos.x * this.map.scale.x, -pos.y * this.map.scale.y, 0);
  86. tween(this.node).to(0.5, { position: this.confirmPosition(toPoint) }, { easing: "sineOut" }).start();
  87. }
  88. }