FactoryIcon.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. public 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. if(gData.gameData.playerProp.orderData)
  87. {
  88. let lv = arr[0].value - gData.gameData.playerProp.orderData.overTimes;
  89. mk.tip.pop(`订单提现${lv}次后解锁`);
  90. }else{
  91. mk.tip.pop(`提现订单后解锁`);
  92. }
  93. break;
  94. case FactroyState.Empty:
  95. gData.factorySystem.currSelectFactory = this;
  96. gData.plantData.openPanel(this.tab, this.configID);
  97. break;
  98. case FactroyState.Producting:
  99. mk.ui.openPanel('module/speedUpUI/speedUp');
  100. break;
  101. case FactroyState.Ripe:
  102. gData.factorySystem.currSelectFactory = this;
  103. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  104. break;
  105. case FactroyState.Sick:
  106. // mk.tip.pop(`生虫了!`);
  107. mk.data.sendDataEvent(DataEventId.button_click, "停电icon");
  108. gData.adClearSickData.openPanel(this.tab, this.configID);
  109. mk.console.logSingle('VVVV ', '停电icon');
  110. break;
  111. }
  112. }
  113. public async make(id, parentNode = null) {
  114. if (this.data.state == FactroyState.Empty) {
  115. gData.gameData.isProducting = true;
  116. let config = gData.gameData.getProductMap(id);
  117. let growSpan = Date.now() + config.time * 1000;
  118. this.data.state = FactroyState.Producting;
  119. this.data.productID = config.picture;
  120. this.data.growSpan = growSpan;
  121. gData.adData.checkPopRed();
  122. this.getComponent(MoveToCenter).move();
  123. try {
  124. gData.gameData.setFactoryDataMap(this.configID, this.data);
  125. gData.gameData.changeLeftTimes(-1);
  126. gData.gameData.addProductMakeTimesById(this.data.productID);
  127. await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node, parentNode);
  128. } catch (error) {
  129. console.log("========factoryIcon");
  130. }
  131. //gData.gameData.updateOrderTaskCopyData(this.data.productID);
  132. gData.gameData.nextMake = null;
  133. gData.gameData.setNextProduct(false);
  134. gData.gameData.isProducting = false;
  135. let flyRed = await gData.gameData.updateNewTaskProgress();
  136. return flyRed;
  137. // if (flyRed) {
  138. // //mk.audio.playEffect('redmoney');
  139. // gData.gameData.gameStyle.dpFlyRedAni();
  140. // }
  141. }
  142. else {
  143. mk.tip.pop('商品已满!')
  144. return false;
  145. }
  146. }
  147. clickSpeedUp() {
  148. mk.ui.openPanel('module/speedUpUI/speedUp');
  149. }
  150. onRipe(sendToServer) {
  151. this.data.state = FactroyState.Ripe;
  152. gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
  153. //gData.gameData.addProductMakeTimesById(this.data.productID);
  154. }
  155. onSick(sendToServer) {
  156. this.data.state = FactroyState.Sick;
  157. gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer);
  158. }
  159. public async onHarvest() {
  160. this.countDown.cleanRiped();
  161. this.clean();
  162. gData.factorySystem.currSelectFactory = null;
  163. let productID = this.data.productID;
  164. this.data.state = FactroyState.Empty;
  165. this.data.productID = 0;
  166. this.data.growSpan = 0;
  167. gData.gameData.setFactoryDataMap(this.configID, this.data);
  168. //await gData.gameData.gameStyle.doCropFlyLogic(productID, this.node);
  169. //gData.gameData.nextMake = null;
  170. gData.gameData.setNextProduct(false);
  171. if (gData.harvestData.getType == 0) {
  172. gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node);
  173. }
  174. else {
  175. gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node);
  176. }
  177. }
  178. public canHarvest() {
  179. this.getComponent(MoveToCenter).move();
  180. gData.factorySystem.currSelectFactory = this;
  181. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node);
  182. }
  183. public canClearSick() {
  184. this.getComponent(MoveToCenter).move();
  185. mk.data.sendDataEvent(DataEventId.button_click, "停电icon");
  186. gData.adClearSickData.openPanel(this.tab, this.configID);
  187. }
  188. }