FarmSystem.ts 4.1 KB

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