FactoryIcon.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { FactroyState, ProductType } from "../../game/data/GameData";
  2. import ProductShow from "../view/ProductShow";
  3. import { FarmCountDown } from "./FarmCountDown";
  4. import { IDHelper } from "./IDHelper";
  5. const { ccclass, property } = cc._decorator;
  6. @ccclass
  7. export class FactoryIcon extends cc.Component {
  8. @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null;
  9. @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null;
  10. public configID: number = 0;
  11. public sortID: number = 0;
  12. private tab: ProductType = ProductType.bmhc;
  13. private _state: FactroyState = -1;
  14. public get state(): FactroyState { return this._state; }
  15. data = null;
  16. onLoad() {
  17. let helper = this.getComponent(IDHelper);
  18. this.configID = helper.buildID;
  19. this.sortID = helper.sortID;
  20. switch (this.configID) {
  21. case 30004:
  22. this.tab = ProductType.bmhc;
  23. break;
  24. case 30005:
  25. this.tab = ProductType.gdp;
  26. break;
  27. case 30006:
  28. this.tab = ProductType.ztc;
  29. break;
  30. case 30007:
  31. this.tab = ProductType.lrc;
  32. break;
  33. case 30008:
  34. this.tab = ProductType.gfmg;
  35. break;
  36. case 30009:
  37. this.tab = ProductType.kcd;
  38. break;
  39. }
  40. this.countDown.node.active = false;
  41. this.freshState();
  42. }
  43. update() {
  44. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  45. if (index != -1) {
  46. this.freshState();
  47. gData.gameData.needFreshArr.splice(index, 1);
  48. }
  49. }
  50. freshState() {
  51. this.data = gData.gameData.getFactoryDataMap(this.configID);
  52. this.setState(this.data.state);
  53. }
  54. private setState(value: FactroyState): void {
  55. if (this.state != value) {
  56. this._state = value;
  57. this.countDown.node.active = false;
  58. switch (this.state) {
  59. case FactroyState.Lock:
  60. break;
  61. case FactroyState.Empty:
  62. break;
  63. case FactroyState.Producting:
  64. this.countDown.node.active = true;
  65. this.countDown.setData(this.data);
  66. break;
  67. case FactroyState.Ripe:
  68. this.countDown.node.active = true;
  69. this.countDown.setState(1);
  70. this.onProductComplete();
  71. break;
  72. case FactroyState.Sick:
  73. this.countDown.node.active = true;
  74. this.countDown.setState(0);
  75. break;
  76. }
  77. }
  78. }
  79. /**生产完成 */
  80. private async onProductComplete() {
  81. if (this.showGroup) {
  82. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  83. let item = cc.instantiate(prefab);
  84. let id = this.data.productID;
  85. item.getComponent(ProductShow).id = id;
  86. item.parent = this.showGroup;
  87. }
  88. }
  89. private clean(): void {
  90. this.showGroup.removeAllChildren();
  91. }
  92. public async onTouch() {
  93. switch (this.data.state) {
  94. case FactroyState.Lock:
  95. let arr = gData.gameData.getProductArrByType(this.tab);
  96. let lv = arr[0].value;
  97. mk.tip.pop(`农场达到${lv}级解锁`);
  98. break;
  99. case FactroyState.Empty:
  100. gData.factorySystem.currSelectFactory = this;
  101. gData.plantData.openPanel(this.tab, this.configID);
  102. break;
  103. case FactroyState.Ripe:
  104. gData.factorySystem.currSelectFactory = this;
  105. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  106. break;
  107. case FactroyState.Sick:
  108. // mk.tip.pop(`生虫了!`);
  109. gData.adClearSickData.openPanel(this.tab, this.configID);
  110. break;
  111. }
  112. }
  113. public make(id) {
  114. if (this.data.state == FactroyState.Empty) {
  115. let config = gData.gameData.getProductMap(id);
  116. let growSpan = Date.now() + config.time * 1000;
  117. this.data.state = FactroyState.Producting;
  118. this.data.productID = config.picture;
  119. this.data.growSpan = growSpan;
  120. gData.gameData.setFactoryDataMap(this.configID, this.data);
  121. }
  122. }
  123. onRipe() {
  124. this._state = FactroyState.Ripe;
  125. this.data.state = FactroyState.Ripe;
  126. gData.gameData.setFactoryDataMap(this.configID, this.data);
  127. gData.gameData.addProductMakeTimesById(this.data.productID);
  128. }
  129. onSick() {
  130. this._state = FactroyState.Sick;
  131. this.data.state = FactroyState.Sick;
  132. gData.gameData.setFactoryDataMap(this.configID, this.data);
  133. }
  134. public onHarvest(): void {
  135. this.countDown.cleanRiped();
  136. this.clean();
  137. gData.factorySystem.currSelectFactory = null;
  138. this.data.state = FactroyState.Empty;
  139. this.data.productID = 0;
  140. this.data.growSpan = 0;
  141. gData.gameData.setFactoryDataMap(this.configID, this.data);
  142. }
  143. }