FarmSystem.ts 4.1 KB

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