FarmSystem.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. for (let i = 0; i < len; i++) {
  65. if (this.farms[i].data.state == FarmState.Growing) {
  66. this.farms[i].process.onSpeed();
  67. this.farms[i].countDown.riped();
  68. }
  69. }
  70. }
  71. public canSpeedUp() {
  72. let can = false;
  73. let len = this.farms.length;
  74. for (let i = 0; i < len; i++) {
  75. if (this.farms[i].data.state == FarmState.Growing) {
  76. can = true;
  77. break;
  78. }
  79. }
  80. return can;
  81. }
  82. }