FarmSystem.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 set currSelectFarm(farm: FarmIcon) {
  10. if (this._currSelectFarm) {
  11. this._currSelectFarm.selectFarm(false);
  12. }
  13. this._currSelectFarm = farm;
  14. if (this._currSelectFarm) {
  15. let icon = this._currSelectFarm;
  16. icon.selectFarm(true);
  17. }
  18. }
  19. public get currSelectFarm(): FarmIcon {
  20. return this._currSelectFarm;
  21. }
  22. public addFram(fram: FarmIcon) {
  23. this.farms[fram.sortID] = fram;
  24. }
  25. public plant(id: number): void {
  26. this.currSelectFarm && this.currSelectFarm.plant(id);
  27. }
  28. public selectNextFarm(sel = true): FarmIcon | null {
  29. let nextFarm = this.getCanPlantFarm();
  30. if (nextFarm) {
  31. if (sel) {
  32. this.currSelectFarm = nextFarm;
  33. }
  34. else {
  35. this._currSelectFarm = nextFarm;
  36. }
  37. }
  38. return nextFarm;
  39. }
  40. /**获取可种植农田 */
  41. private getCanPlantFarm(): FarmIcon | null {
  42. for (let i = 0; i < this.farms.length; i++) {
  43. if (this.farms[i] != this.currSelectFarm && this.farms[i].data.state == FarmState.Empty) {
  44. gData.gameData.nextCanProduct = gData.gameData.getRandomPlantConfig();
  45. gData.gameData.nextMake = this.farms[i];
  46. mk.console.logSingle('getCanPlantFarm ', this.farms[i].data);
  47. return this.farms[i];
  48. }
  49. }
  50. return null;
  51. }
  52. public btnMake() {
  53. gData.gameData.nextMake.plant(gData.gameData.nextCanProduct.picture);
  54. }
  55. }