FarmSystem.ts 3.7 KB

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