FarmSystem.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { FarmState } from '../../game/data/GameData';
  2. import { FarmIcon } from './FarmIcon';
  3. const { ccclass } = cc._decorator;
  4. @ccclass
  5. export class FarmSystem {
  6. public farms: Array<FarmIcon> = [];
  7. private _currSelectFarm: FarmIcon = null;
  8. public plantWindowIsOpening = false;
  9. public _lastSeletFarm: FarmIcon = null;
  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. public addFram(fram: FarmIcon) {
  24. this.farms[fram.sortID] = fram;
  25. }
  26. public async plant(id: number, parentNode = null) {
  27. if (this.currSelectFarm) {
  28. return await this.currSelectFarm.plant(id, parentNode);
  29. }
  30. else {
  31. return false;
  32. }
  33. // this._lastSeletFarm = this._currSelectFarm;
  34. }
  35. public selectNextFarm(out?: any, sel = true): FarmIcon | null {
  36. let nextFarm = this.getCanPlantFarm(out);
  37. if (this._lastSeletFarm) {
  38. this._lastSeletFarm.selectFarm(false);
  39. }
  40. if (nextFarm) {
  41. if (sel) {
  42. this.currSelectFarm = nextFarm;
  43. }
  44. else {
  45. this._lastSeletFarm = this._currSelectFarm;
  46. this._currSelectFarm = nextFarm;
  47. }
  48. }
  49. return nextFarm;
  50. }
  51. /**获取可种植农田 */
  52. private getCanPlantFarm(out?: any): FarmIcon | null {
  53. let len = this.farms.length;
  54. for (let i = 0; i < len; i++) {
  55. if (this.farms[i] != this.currSelectFarm && this.farms[i].data.state == FarmState.Empty) {
  56. out.state = 0;
  57. //let nextProduct = gData.gameData.getRandomPlantConfig(out);
  58. let nextProduct = gData.gameData.getRandomPlantConfigExtra(out);
  59. if (nextProduct) {
  60. if (gData.gameData.nextType == 1) {
  61. gData.gameData.nextCanProduct = nextProduct
  62. gData.gameData.nextMake = this.farms[i];
  63. }
  64. mk.console.logSingle('getCanPlantFarm ', this.farms[i].data);
  65. return this.farms[i];
  66. } else {
  67. return null;
  68. }
  69. }
  70. }
  71. return null;
  72. }
  73. public async btnMake() {
  74. let flyRed = await gData.gameData.nextMake.plant(gData.gameData.nextCanProduct.picture);
  75. return flyRed;
  76. }
  77. public setHarvest() {
  78. let len = this.farms.length;
  79. let sendToServer = false;
  80. for (let i = 0; i < len; i++) {
  81. if (this.farms[i].data.state == FarmState.Growing) {
  82. this.farms[i].process.onSpeed();
  83. this.farms[i].countDown.riped(false);
  84. sendToServer = true;
  85. }
  86. }
  87. if (sendToServer) {
  88. gData.gameData.freshSendToServer(1);
  89. }
  90. }
  91. public canSpeedUp() {
  92. let can = false;
  93. let len = this.farms.length;
  94. for (let i = 0; i < len; i++) {
  95. if (this.farms[i].data.state == FarmState.Growing) {
  96. can = true;
  97. break;
  98. }
  99. }
  100. return can;
  101. }
  102. public canHarvest() {
  103. let can = false;
  104. let len = this.farms.length;
  105. for (let i = 0; i < len; i++) {
  106. if (this.farms[i].data.state == FarmState.Ripe) {
  107. this.farms[i].canHarvest();
  108. can = true;
  109. break;
  110. }
  111. }
  112. return can;
  113. }
  114. public canClearSick() {
  115. let can = false;
  116. let len = this.farms.length;
  117. for (let i = 0; i < len; i++) {
  118. if (this.farms[i].data.state == FarmState.Sick) {
  119. this.farms[i].canClearSick();
  120. can = true;
  121. break;
  122. }
  123. }
  124. return can;
  125. }
  126. }