FactoryIcon.ts 8.0 KB

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