FarmSystem.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(out?: any ,sel = true): FarmIcon | null {
  38. let nextFarm = this.getCanPlantFarm(out);
  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(out?: any): 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. let nextProduct = gData.gameData.getRandomPlantConfig(out);
  59. if(nextProduct)
  60. {
  61. gData.gameData.nextCanProduct = nextProduct
  62. gData.gameData.nextMake = this.farms[i];
  63. mk.console.logSingle('getCanPlantFarm ', this.farms[i].data);
  64. return this.farms[i];
  65. }else{
  66. return null;
  67. }
  68. }
  69. }
  70. return null;
  71. }
  72. public async btnMake() {
  73. let flyRed = await gData.gameData.nextMake.plant(gData.gameData.nextCanProduct.picture);
  74. return flyRed;
  75. }
  76. public setHarvest() {
  77. let len = this.farms.length;
  78. let sendToServer = false;
  79. for (let i = 0; i < len; i++) {
  80. if (this.farms[i].data.state == FarmState.Growing) {
  81. this.farms[i].process.onSpeed();
  82. this.farms[i].countDown.riped(false);
  83. sendToServer = true;
  84. }
  85. }
  86. if (sendToServer) {
  87. gData.gameData.freshSendToServer(1);
  88. }
  89. }
  90. public canSpeedUp() {
  91. let can = false;
  92. let len = this.farms.length;
  93. for (let i = 0; i < len; i++) {
  94. if (this.farms[i].data.state == FarmState.Growing) {
  95. can = true;
  96. break;
  97. }
  98. }
  99. return can;
  100. }
  101. public canHarvest() {
  102. let can = false;
  103. let len = this.farms.length;
  104. for (let i = 0; i < len; i++) {
  105. if (this.farms[i].data.state == FarmState.Ripe) {
  106. this.farms[i].canHarvest();
  107. can = true;
  108. break;
  109. }
  110. }
  111. return can;
  112. }
  113. public canClearSick() {
  114. let can = false;
  115. let len = this.farms.length;
  116. for (let i = 0; i < len; i++) {
  117. if (this.farms[i].data.state == FarmState.Sick) {
  118. this.farms[i].canClearSick();
  119. can = true;
  120. break;
  121. }
  122. }
  123. return can;
  124. }
  125. }