FarmSystem.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { _decorator, Component, Node, SystemEventType, EventTouch, find, UITransform, Vec2 } from 'cc';
  2. import { WindowManager } from '../core/ui/window/WindowManager';
  3. import { GeometryUtils } from '../core/utils/GeometryUtils';
  4. import { FarmIcon, FarmState } from './FarmIcon';
  5. import { FarmManager } from './FarmManager';
  6. import { TouchHelper } from './TouchHelper';
  7. const { ccclass, property } = _decorator;
  8. @ccclass('FarmSystem')
  9. export class FarmSystem extends Component {
  10. public farms: Array<FarmIcon> = [];
  11. private _currSelectFarm: FarmIcon;
  12. public set currSelectFarm(farm: FarmIcon) {
  13. if (this._currSelectFarm) {
  14. this._currSelectFarm.selectFarm(false);
  15. }
  16. this._currSelectFarm = farm;
  17. if (this._currSelectFarm) {
  18. let icon = this._currSelectFarm;
  19. icon.selectFarm(true);
  20. }
  21. }
  22. public get currSelectFarm(): FarmIcon {
  23. return this._currSelectFarm;
  24. }
  25. private canvas: Node;
  26. start() {
  27. this.canvas = find("Canvas");
  28. this.canvas.on(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  29. this.canvas.on(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
  30. this.canvas.on(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  31. }
  32. private isMoved = false;
  33. private onTouchEnd(e: EventTouch) {
  34. if (!FarmManager.plantWindowIsOpening) {
  35. let isInPoly = false;
  36. this.hideAllSelectProgressGroup();
  37. for (let i = 0; i < this.farms.length; i++) {
  38. const frame = this.farms[i];
  39. const touchHelper = frame.getComponent(TouchHelper);
  40. if (!this.isMoved && touchHelper.points.length >= 3) {
  41. this.isMoved = false;
  42. let touchPoint = e.getUILocation();
  43. let localPoint = frame.node.getComponent(UITransform).convertToNodeSpaceAR(GeometryUtils.V2ToV3(touchPoint));
  44. isInPoly = GeometryUtils.pointInPoly(localPoint, touchHelper.points);
  45. if (isInPoly) {//农场被点击
  46. frame.onTouch.call(frame);
  47. if (frame.state == FarmState.Empty) {
  48. this.currSelectFarm = frame;
  49. } else {
  50. this.closeWindow();
  51. }
  52. return;
  53. }
  54. }
  55. }
  56. if (!isInPoly && this.currSelectFarm) {
  57. this.closeWindow();
  58. }
  59. this.isMoved = false;
  60. }
  61. }
  62. private onTouchMove(e: EventTouch) {
  63. // this.closeWindow();
  64. if (e.getAllTouches().length > 1) { this.isMoved = true; return }
  65. this.isMoved = Vec2.distance(e.getStartLocation(), e.getLocation()) > 20;
  66. }
  67. private closeWindow() {
  68. WindowManager.close();
  69. this.currSelectFarm = null;
  70. }
  71. private onTouchCancel(e: EventTouch) {
  72. this.isMoved = false;
  73. }
  74. public addFram(fram: FarmIcon) {
  75. this.farms[fram.sortID] = fram;
  76. }
  77. public plant(id: number): void {
  78. this.currSelectFarm && this.currSelectFarm.plant(id);
  79. }
  80. public selectNextFarm(): FarmIcon | null {
  81. let nextFarm = this.getCanPlantFarm();
  82. if (nextFarm) {
  83. this.currSelectFarm = nextFarm;
  84. } else {
  85. WindowManager.close();
  86. }
  87. return nextFarm;
  88. }
  89. public hideAllSelectProgressGroup(): void {
  90. for (let i = 0; i < this.farms.length; i++) {
  91. this.farms[i].countDown.setCountActive(false);
  92. }
  93. }
  94. /**获取可种植农田 */
  95. private getCanPlantFarm(): FarmIcon | null {
  96. for (let i = 0; i < this.farms.length; i++) {
  97. if (this.farms[i] != this.currSelectFarm && this.farms[i].state == FarmState.Empty) {
  98. return this.farms[i];
  99. }
  100. }
  101. return null;
  102. }
  103. /**触摸事件的同级传递 */
  104. public next(node: FarmIcon, e: EventTouch): void {
  105. let index = this.farms.indexOf(node);
  106. if (index % 3 < 2) {
  107. this.farms[index + 1].node.emit(SystemEventType.TOUCH_END, e);
  108. }
  109. }
  110. onDestroy() {
  111. this.canvas.off(SystemEventType.TOUCH_MOVE, this.onTouchMove, this);
  112. this.canvas.off(SystemEventType.TOUCH_CANCEL, this.onTouchCancel, this);
  113. this.canvas.off(SystemEventType.TOUCH_END, this.onTouchEnd, this);
  114. }
  115. public novice_plant(): void {
  116. let frame = this.farms[0];
  117. frame.novice_plant();
  118. }
  119. public novice_clean(): void {
  120. let frame = this.farms[0];
  121. frame.novice_clean();
  122. }
  123. }