FarmSystem.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { FarmState } from '../../game/data/GameData';
  2. import Util from '../util/Util';
  3. import { FarmIcon } from './FarmIcon';
  4. import { TouchHelper } from './TouchHelper';
  5. const { ccclass } = cc._decorator;
  6. @ccclass
  7. export class FarmSystem {
  8. public farms: Array<FarmIcon> = [];
  9. private _currSelectFarm: FarmIcon = null;
  10. public plantWindowIsOpening = false;
  11. /** 种植冷却 */
  12. public plantIsRequesting = false;
  13. public set currSelectFarm(farm: FarmIcon) {
  14. if (this._currSelectFarm) {
  15. this._currSelectFarm.selectFarm(false);
  16. }
  17. this._currSelectFarm = farm;
  18. if (this._currSelectFarm) {
  19. let icon = this._currSelectFarm;
  20. icon.selectFarm(true);
  21. }
  22. }
  23. public get currSelectFarm(): FarmIcon {
  24. return this._currSelectFarm;
  25. }
  26. private canvas: cc.Node;
  27. start() {
  28. this.canvas = cc.find("Canvas");
  29. this.canvas.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  30. this.canvas.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
  31. this.canvas.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  32. }
  33. private isMoved = false;
  34. private onTouchEnd(e) {
  35. if (!this.plantWindowIsOpening) {
  36. let isInPoly = false;
  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._point;
  43. let localPoint = frame.node.convertToNodeSpaceAR(touchPoint);
  44. isInPoly = Util.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: cc.Event.EventTouch) {
  63. // this.closeWindow();
  64. if (e.getTouches().length > 1) { this.isMoved = true; return }
  65. this.isMoved = cc.Vec2.distance(e.getStartLocation(), e.getLocation()) > 20;
  66. }
  67. private closeWindow() {
  68. //暂时注释
  69. // WindowManager.close();
  70. this.currSelectFarm = null;
  71. }
  72. private onTouchCancel(e) {
  73. this.isMoved = false;
  74. }
  75. public addFram(fram: FarmIcon) {
  76. this.farms[fram.sortID] = fram;
  77. }
  78. public plant(id: number): void {
  79. this.currSelectFarm && this.currSelectFarm.plant(id);
  80. }
  81. public selectNextFarm(): FarmIcon | null {
  82. let nextFarm = this.getCanPlantFarm();
  83. if (nextFarm) {
  84. this.currSelectFarm = nextFarm;
  85. } else {
  86. //暂时注释
  87. // WindowManager.close();
  88. }
  89. return nextFarm;
  90. }
  91. /**获取可种植农田 */
  92. private getCanPlantFarm(): FarmIcon | null {
  93. for (let i = 0; i < this.farms.length; i++) {
  94. if (this.farms[i] != this.currSelectFarm && this.farms[i].state == FarmState.Empty) {
  95. return this.farms[i];
  96. }
  97. }
  98. return null;
  99. }
  100. /**触摸事件的同级传递 */
  101. public next(node: FarmIcon, e): void {
  102. let index = this.farms.indexOf(node);
  103. if (index % 3 < 2) {
  104. this.farms[index + 1].node.emit(cc.Node.EventType.TOUCH_END, e);
  105. }
  106. }
  107. onDestroy() {
  108. this.canvas.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
  109. this.canvas.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
  110. this.canvas.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
  111. }
  112. public novice_plant(): void {
  113. let frame = this.farms[0];
  114. frame.novice_plant();
  115. }
  116. public novice_clean(): void {
  117. let frame = this.farms[0];
  118. frame.novice_clean();
  119. }
  120. }