FarmSystem.ts 4.2 KB

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