MapMove.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { _decorator, Component, Node, Size, Vec3, UITransform, EventTouch, view, Vec2, Enum, Camera, Widget, v2, v3 } from 'cc';
  2. import { UIHelper } from '../../framework/common/UIHelper';
  3. import { Framework } from '../../framework/Framework';
  4. const { ccclass, property } = _decorator;
  5. class MapRange {
  6. up: number = 0;
  7. down: number = 0;
  8. left: number = 0;
  9. right: number = 0;
  10. }
  11. class MapData {
  12. size: Size = null;
  13. design: Size = null;
  14. pos: Vec3 = null;
  15. range = new MapRange;
  16. }
  17. enum DragDir {
  18. none = 0,
  19. up_and_down,
  20. left_and_right,
  21. all,
  22. }
  23. @ccclass('MapMove')
  24. export class MapMove extends Component {
  25. @property({ type: Enum(DragDir), displayName: "拖动方向" })
  26. private drag_dir = DragDir.none;
  27. @property({ type: Camera, displayName: "摄像机" })
  28. private cur_camera: Camera = null;
  29. private _map_data = new MapData;
  30. private _ui_transform: UITransform = null;
  31. onLoad() {
  32. Framework.event.addEvent("GAME_RESIZE", () => {
  33. if (this.node.active) {
  34. ((this.cur_camera) ? this.cur_camera.node : this.node).setPosition(0, 0);
  35. this._updateMap();
  36. }
  37. }, this);
  38. this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
  39. this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  40. this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
  41. this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
  42. this._ui_transform = this.getComponent(UITransform);
  43. this._updateMap();
  44. }
  45. onDestroy() {
  46. Framework.event.removeEvent(this);
  47. }
  48. private onTouchStart(event: EventTouch) {
  49. }
  50. private onTouchMove(event: EventTouch) {
  51. let events = event.getTouches();
  52. //拖动地图
  53. let oldPos = this._ui_transform.convertToNodeSpaceAR(this._convertToVec3(events[0].getStartLocation()));
  54. let newPos = this._ui_transform.convertToNodeSpaceAR(this._convertToVec3(events[0].getLocation()));
  55. let subPos = oldPos.subtract(newPos);
  56. let curPos = new Vec3(this._map_data.pos.x + subPos.x * (this.cur_camera ? 1 : -1), this._map_data.pos.y + subPos.y * (this.cur_camera ? 1 : -1), 0);
  57. if (curPos.x < this._map_data.range.left) {
  58. curPos.x = this._map_data.range.left;
  59. } else if (curPos.x > this._map_data.range.right) {
  60. curPos.x = this._map_data.range.right;
  61. }
  62. if (curPos.y > this._map_data.range.up) {
  63. curPos.y = this._map_data.range.up;
  64. } else if (curPos.y < this._map_data.range.down) {
  65. curPos.y = this._map_data.range.down;
  66. }
  67. let temp = ((this.cur_camera) ? this.cur_camera.node : this.node).getPosition();
  68. if (this.drag_dir == DragDir.left_and_right) {
  69. ((this.cur_camera) ? this.cur_camera.node : this.node).setPosition(curPos.x, temp.y);
  70. } else if (this.drag_dir == DragDir.up_and_down) {
  71. ((this.cur_camera) ? this.cur_camera.node : this.node).setPosition(temp.x, curPos.y);
  72. }
  73. }
  74. private onTouchEnd(event: EventTouch) {
  75. (this.drag_dir !== DragDir.none) && (this._map_data.pos = ((this.cur_camera) ? this.cur_camera.node : this.node).getPosition());
  76. //console.log(this._map_data.pos);
  77. }
  78. private _updateMap() {
  79. this._map_data.design = view.getDesignResolutionSize();
  80. this._map_data.size = this._ui_transform.contentSize;
  81. this._map_data.range.left = -(this._map_data.size.width - this._map_data.design.width) / 2;
  82. this._map_data.range.right = (this._map_data.size.width - this._map_data.design.width) / 2;
  83. this._map_data.range.up = (this._map_data.size.height - this._map_data.design.height) / 2;
  84. this._map_data.range.down = -(this._map_data.size.height - this._map_data.design.height) / 2;
  85. let pos = this.node.getPosition();
  86. this._map_data.range.up += pos.y;
  87. this._map_data.range.down += pos.y;
  88. this._map_data.range.right += pos.x;
  89. this._map_data.range.left += pos.x;
  90. this._map_data.pos = ((this.cur_camera) ? this.cur_camera.node : this.node).getPosition();
  91. }
  92. private _convertToVec3(pos: Vec2) {
  93. return new Vec3(pos.x, pos.y);
  94. }
  95. setCameraPostion(pos: Vec2) {
  96. if (this.cur_camera) {
  97. let temp = this.cur_camera.node.getPosition();
  98. this.cur_camera.node.setPosition(pos.x, temp.y);
  99. this._map_data.pos = this.cur_camera.node.getPosition().clone();
  100. } else {
  101. let temp = this.node.getPosition();
  102. this.node.setPosition(pos.x, temp.y);
  103. this._map_data.pos = this.node.getPosition().clone();
  104. }
  105. }
  106. // update() {
  107. // this.cur_camera && console.error(this.cur_camera.node.getPosition());
  108. // }
  109. }