FarmIcon.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { FarmState, ProductType } from '../../game/data/GameData';
  2. import { Data } from '../../mk/data/Data';
  3. import { MoveToCenter } from '../map/MoveToCenter';
  4. import { FarmCountDown } from './FarmCountDown';
  5. import { FarmGrowProcess } from './FarmGrowProcess';
  6. import { FarmSystem } from './FarmSystem';
  7. import { IDHelper } from './IDHelper';
  8. const { ccclass, property } = cc._decorator;
  9. @ccclass
  10. export class FarmIcon extends cc.Component {
  11. @property({ type: cc.Node, tooltip: "未解锁时显示节点" }) lockNode: cc.Node = null;
  12. @property({ type: cc.Node, tooltip: "可解锁节点" }) canUnlockNode: cc.Node = null;
  13. @property({ type: cc.Node, tooltip: "选中闪烁节点" }) selectNode: cc.Node = null;
  14. @property({ type: cc.Sprite, tooltip: "种植动画图标" }) plantIcon: cc.Sprite = null;
  15. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  16. @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess = null;
  17. public configID: number = 0;
  18. public sortID: number = 0;
  19. private plantID: number = 0;
  20. private _state: FarmState = -1;
  21. public get state(): FarmState { return this._state; }
  22. private data = null;
  23. onLoad() {
  24. this.configID = this.getComponent(IDHelper).buildID;
  25. this.sortID = this.getComponent(IDHelper).sortID;
  26. gData.farmSystem.addFram(this);
  27. }
  28. start() {
  29. this.freshState();
  30. }
  31. update(dt) {
  32. // if (this.state == FarmState.Lock && gData.gameData.hasBuildData(this.configID)) {
  33. // this.activeFram();
  34. // }
  35. // let producting = gData.gameData.getProductingList(this.configID);
  36. // if (producting.length > 0) {
  37. // if (producting[0].ripeDate <= Date.now()) {
  38. // this.setState(FarmState.Ripe);
  39. // } else {
  40. // this.setState(FarmState.Growing);
  41. // }
  42. // this.countDown.setData(this.configID, false);
  43. // this.process.setData(this.configID);
  44. // } else {
  45. // this.setState(FarmState.Empty);
  46. // this.countDown.cleanRiped();
  47. // this.process.cleanProcess();
  48. // }
  49. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  50. if (index != -1) {
  51. this.freshState();
  52. gData.gameData.needFreshArr.splice(index, 1);
  53. }
  54. }
  55. freshState() {
  56. this.data = gData.gameData.getFarmDataMap(this.configID);
  57. this.setState(this.data.state);
  58. }
  59. private setState(value: FarmState): void {
  60. if (this.state != value) {
  61. this._state = value;
  62. this.lockNode.active = false;
  63. this.canUnlockNode.active = false;
  64. this.plantIcon.node.active = false;
  65. this.countDown.node.active = false;
  66. switch (this.state) {
  67. case FarmState.Lock:
  68. this.lockNode.active = true;
  69. break;
  70. case FarmState.CanUnlock:
  71. this.lockNode.active = true;
  72. this.canUnlockNode.active = true;
  73. break;
  74. case FarmState.Empty:
  75. break;
  76. case FarmState.Growing:
  77. this.plantIcon.node.active = true;
  78. this.countDown.node.active = true;
  79. break;
  80. case FarmState.Ripe:
  81. this.plantIcon.node.active = true;
  82. this.countDown.node.active = true;
  83. break;
  84. }
  85. }
  86. }
  87. public activeFram(): void {
  88. this.lockNode.active = false;
  89. }
  90. public selectFarm(isSelect = false): void {
  91. this.selectNode.active = isSelect;
  92. if (isSelect) {
  93. this.getComponent(MoveToCenter).move();
  94. }
  95. }
  96. public async onTouch() {
  97. switch (this.data.state) {
  98. case FarmState.Lock:
  99. mk.tip.pop(`请先解锁扩建农田`);
  100. break;
  101. case FarmState.CanUnlock:
  102. gData.adUnlockData.openPanel(this.configID);
  103. break;
  104. case FarmState.Empty:
  105. gData.farmSystem.currSelectFarm = this;
  106. gData.plantData.openPanel(ProductType.nzw, this.configID);
  107. break;
  108. case FarmState.Growing:
  109. break;
  110. case FarmState.Ripe:
  111. gData.farmSystem.currSelectFarm = this;
  112. gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this));
  113. break;
  114. }
  115. }
  116. public onHarvest(): void {
  117. this.countDown.cleanRiped();
  118. this.process.cleanProcess();
  119. gData.farmSystem.currSelectFarm = null;
  120. this.data.state = FarmState.Empty;
  121. this.data.productID = 0;
  122. this.data.growSpan = 0;
  123. gData.gameData.setFarmDataMap(this.configID, this.data);
  124. }
  125. public async plant(id: number) {
  126. if (this.state == FarmState.Empty && !gData.farmSystem.plantIsRequesting) {
  127. this.plantID = id;
  128. gData.farmSystem.plantIsRequesting = true;
  129. let config = gData.gameData.getProductMap(this.plantID);
  130. let growSpan = Date.now() + config.time * 1000;
  131. this.data.state = FarmState.Growing;
  132. this.data.productID = this.plantID;
  133. this.data.growSpan = growSpan;
  134. gData.gameData.setFarmDataMap(this.configID, this.data);
  135. this.plantIcon.node.active = true;
  136. this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame);
  137. this.plantIcon.getComponent(cc.Animation).play();
  138. // this.plantIcon.getComponent(Sound).play();
  139. this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  140. gData.farmSystem.selectNextFarm();
  141. gData.farmSystem.plantIsRequesting = false;
  142. }
  143. }
  144. public onRiped(): void {
  145. this._state = FarmState.Ripe;
  146. this.data.state = FarmState.Ripe;
  147. gData.gameData.setFarmDataMap(this.configID, this.data);
  148. gData.gameData.addProductMakeTimesById(this.plantID);
  149. }
  150. private async onPlantAnimationFinish() {
  151. this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this);
  152. this.plantIcon.node.active = false;
  153. this.countDown.setData(this.data);
  154. this.process.setData(this.data);
  155. }
  156. public novice_plant(): void {
  157. this.process.setData(this.data);
  158. }
  159. public novice_clean(): void {
  160. this.process.cleanProcess();
  161. }
  162. }