FarmSystem.ts 3.5 KB

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