FactoryIcon.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { DataEventId, ExpAddType, FactroyState, GameProp, ProductType } from "../../game/data/GameData";
  2. import { MoveToCenter } from "../map/MoveToCenter";
  3. import ProductShow from "../view/ProductShow";
  4. import { FarmCountDown } from "./FarmCountDown";
  5. import { IDHelper } from "./IDHelper";
  6. const { ccclass, property } = cc._decorator;
  7. @ccclass
  8. export class FactoryIcon extends cc.Component {
  9. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  10. @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null;
  11. @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null;
  12. public configID: number = 0;
  13. public sortID: number = 0;
  14. private tab: ProductType = ProductType.bmhc;
  15. private _state: FactroyState = -1;
  16. public get state(): FactroyState { return this._state; }
  17. data = null;
  18. onLoad() {
  19. let helper = this.getComponent(IDHelper);
  20. this.configID = helper.buildID;
  21. this.sortID = helper.sortID;
  22. this.tab = gData.gameData.getTabByConfigID(this.configID);
  23. this.countDown.node.active = false;
  24. this.freshState();
  25. gData.factorySystem.addFactory(this);
  26. }
  27. update() {
  28. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  29. if (index != -1) {
  30. this.freshState();
  31. gData.gameData.needFreshArr.splice(index, 1);
  32. }
  33. }
  34. freshState() {
  35. this.data = gData.gameData.getFactoryDataMap(this.configID);
  36. this.setState(this.data.state);
  37. }
  38. private setState(value: FactroyState): void {
  39. if (this.state != value) {
  40. this._state = value;
  41. this.countDown.node.active = false;
  42. switch (this.state) {
  43. case FactroyState.Lock:
  44. break;
  45. case FactroyState.Empty:
  46. if (this.idleAni && this.idleAni.active == false) {
  47. this.idleAni.active = true;
  48. }
  49. break;
  50. case FactroyState.Producting:
  51. this.countDown.node.active = true;
  52. this.countDown.setData(this.data);
  53. if (this.idleAni && this.idleAni.active == true) {
  54. this.idleAni.active = false;
  55. }
  56. break;
  57. case FactroyState.Ripe:
  58. this.countDown.node.active = true;
  59. this.countDown.setState(1);
  60. this.onProductComplete();
  61. break;
  62. case FactroyState.Sick:
  63. this.countDown.node.active = true;
  64. this.countDown.setState(0);
  65. break;
  66. }
  67. }
  68. }
  69. /**生产完成 */
  70. private async onProductComplete() {
  71. if (this.showGroup) {
  72. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  73. let item = cc.instantiate(prefab);
  74. let id = this.data.productID;
  75. item.getComponent(ProductShow).id = id;
  76. item.parent = this.showGroup;
  77. }
  78. }
  79. private clean(): void {
  80. this.showGroup.removeAllChildren();
  81. }
  82. public async onTouch() {
  83. switch (this.data.state) {
  84. case FactroyState.Lock:
  85. let arr = gData.gameData.getProductArrByType(this.tab);
  86. let lv = arr[0].value - gData.gameData.playerProp.completeFarmTaskTimes;
  87. mk.tip.pop(`完成${lv}次任务红包后解锁`);
  88. break;
  89. case FactroyState.Empty:
  90. gData.factorySystem.currSelectFactory = this;
  91. gData.plantData.openPanel(this.tab, this.configID);
  92. break;
  93. case FactroyState.Producting:
  94. mk.ui.openPanel('module/speedUpUI/speedUp');
  95. break;
  96. case FactroyState.Ripe:
  97. gData.factorySystem.currSelectFactory = this;
  98. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  99. break;
  100. case FactroyState.Sick:
  101. // mk.tip.pop(`生虫了!`);
  102. mk.data.sendDataEvent(DataEventId.button_click, "停电icon");
  103. gData.adClearSickData.openPanel(this.tab, this.configID);
  104. mk.console.logSingle('VVVV ', '停电icon');
  105. break;
  106. }
  107. }
  108. public async make(id) {
  109. if (this.data.state == FactroyState.Empty) {
  110. gData.gameData.isProducting = true;
  111. let config = gData.gameData.getProductMap(id);
  112. let growSpan = Date.now() + config.time * 1000;
  113. this.data.state = FactroyState.Producting;
  114. this.data.productID = config.picture;
  115. this.data.growSpan = growSpan;
  116. gData.gameData.setFactoryDataMap(this.configID, this.data);
  117. this.getComponent(MoveToCenter).move();
  118. gData.gameData.changeLeftTimes(-1);
  119. gData.adData.checkPopRed();
  120. gData.gameData.addProductMakeTimesById(this.data.productID);
  121. gData.gameData.isProducting = false;
  122. gData.gameData.nextMake = null;
  123. gData.gameData.setNextProduct(false);
  124. let flyRed = await gData.gameData.updateNewTaskProgress();
  125. return flyRed;
  126. // if (flyRed) {
  127. // //mk.audio.playEffect('redmoney');
  128. // gData.gameData.gameStyle.dpFlyRedAni();
  129. // }
  130. }
  131. else {
  132. mk.tip.pop('商品已满!')
  133. return false;
  134. }
  135. }
  136. clickSpeedUp() {
  137. mk.ui.openPanel('module/speedUpUI/speedUp');
  138. }
  139. onRipe(sendToServer) {
  140. this.data.state = FactroyState.Ripe;
  141. gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
  142. //gData.gameData.addProductMakeTimesById(this.data.productID);
  143. }
  144. onSick(sendToServer) {
  145. this.data.state = FactroyState.Sick;
  146. gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
  147. }
  148. public onHarvest(): void {
  149. this.countDown.cleanRiped();
  150. this.clean();
  151. gData.factorySystem.currSelectFactory = null;
  152. this.data.state = FactroyState.Empty;
  153. this.data.productID = 0;
  154. this.data.growSpan = 0;
  155. gData.gameData.setFactoryDataMap(this.configID, this.data);
  156. gData.gameData.setNextProduct(false);
  157. if (gData.harvestData.getType == 0) {
  158. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  159. }
  160. else {
  161. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  162. }
  163. }
  164. public canHarvest() {
  165. this.getComponent(MoveToCenter).move();
  166. gData.factorySystem.currSelectFactory = this;
  167. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  168. }
  169. public canClearSick() {
  170. this.getComponent(MoveToCenter).move();
  171. mk.data.sendDataEvent(DataEventId.button_click, "停电icon");
  172. gData.adClearSickData.openPanel(this.tab, this.configID);
  173. }
  174. }