FactoryIcon.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. @property({type: cc.Node, tooltip: '闲置动画'}) idleAni: cc.Node = null;
  11. public configID: number = 0;
  12. public sortID: number = 0;
  13. private tab: ProductType = ProductType.bmhc;
  14. private _state: FactroyState = -1;
  15. public get state(): FactroyState { return this._state; }
  16. data = null;
  17. onLoad() {
  18. let helper = this.getComponent(IDHelper);
  19. this.configID = helper.buildID;
  20. this.sortID = helper.sortID;
  21. switch (this.configID) {
  22. case 30004:
  23. this.tab = ProductType.bmhc;
  24. break;
  25. case 30005:
  26. this.tab = ProductType.gdp;
  27. break;
  28. case 30006:
  29. this.tab = ProductType.ztc;
  30. break;
  31. case 30007:
  32. this.tab = ProductType.lrc;
  33. break;
  34. case 30008:
  35. this.tab = ProductType.gfmg;
  36. break;
  37. case 30009:
  38. this.tab = ProductType.kcd;
  39. break;
  40. }
  41. this.countDown.node.active = false;
  42. this.freshState();
  43. }
  44. update() {
  45. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  46. if (index != -1) {
  47. this.freshState();
  48. gData.gameData.needFreshArr.splice(index, 1);
  49. }
  50. }
  51. freshState() {
  52. this.data = gData.gameData.getFactoryDataMap(this.configID);
  53. this.setState(this.data.state);
  54. }
  55. private setState(value: FactroyState): void {
  56. if (this.state != value) {
  57. this._state = value;
  58. this.countDown.node.active = false;
  59. switch (this.state) {
  60. case FactroyState.Lock:
  61. break;
  62. case FactroyState.Empty:
  63. if(this.idleAni && this.idleAni.active == false)
  64. {
  65. this.idleAni.active = true;
  66. }
  67. break;
  68. case FactroyState.Producting:
  69. this.countDown.node.active = true;
  70. this.countDown.setData(this.data);
  71. if(this.idleAni && this.idleAni.active == true)
  72. {
  73. this.idleAni.active = false;
  74. }
  75. break;
  76. case FactroyState.Ripe:
  77. this.countDown.node.active = true;
  78. this.countDown.setState(1);
  79. this.onProductComplete();
  80. break;
  81. case FactroyState.Sick:
  82. this.countDown.node.active = true;
  83. this.countDown.setState(0);
  84. break;
  85. }
  86. }
  87. }
  88. /**生产完成 */
  89. private async onProductComplete() {
  90. if (this.showGroup) {
  91. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  92. let item = cc.instantiate(prefab);
  93. let id = this.data.productID;
  94. item.getComponent(ProductShow).id = id;
  95. item.parent = this.showGroup;
  96. }
  97. }
  98. private clean(): void {
  99. this.showGroup.removeAllChildren();
  100. }
  101. public async onTouch() {
  102. switch (this.data.state) {
  103. case FactroyState.Lock:
  104. let arr = gData.gameData.getProductArrByType(this.tab);
  105. let lv = arr[0].value;
  106. mk.tip.pop(`农场达到${lv}级解锁`);
  107. break;
  108. case FactroyState.Empty:
  109. gData.factorySystem.currSelectFactory = this;
  110. gData.plantData.openPanel(this.tab, this.configID);
  111. break;
  112. case FactroyState.Ripe:
  113. gData.factorySystem.currSelectFactory = this;
  114. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  115. break;
  116. case FactroyState.Sick:
  117. // mk.tip.pop(`生虫了!`);
  118. gData.adClearSickData.openPanel(this.tab, this.configID);
  119. break;
  120. }
  121. }
  122. public make(id) {
  123. if (this.data.state == FactroyState.Empty) {
  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.gameData.setFactoryDataMap(this.configID, this.data);
  130. }
  131. }
  132. onRipe() {
  133. this._state = FactroyState.Ripe;
  134. this.data.state = FactroyState.Ripe;
  135. gData.gameData.setFactoryDataMap(this.configID, this.data);
  136. gData.gameData.addProductMakeTimesById(this.data.productID);
  137. }
  138. onSick() {
  139. this._state = FactroyState.Sick;
  140. this.data.state = FactroyState.Sick;
  141. gData.gameData.setFactoryDataMap(this.configID, this.data);
  142. }
  143. public onHarvest(): void {
  144. this.countDown.cleanRiped();
  145. this.clean();
  146. gData.factorySystem.currSelectFactory = null;
  147. this.data.state = FactroyState.Empty;
  148. this.data.productID = 0;
  149. this.data.growSpan = 0;
  150. gData.gameData.setFactoryDataMap(this.configID, this.data);
  151. }
  152. }