FactoryIcon.ts 8.4 KB

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