FarmIcon.ts 7.3 KB

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