FactoryIcon.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. this.tab = gData.gameData.getTabByConfigID(this.configID);
  22. this.countDown.node.active = false;
  23. this.freshState();
  24. }
  25. update() {
  26. let index = gData.gameData.needFreshArr.indexOf(this.configID);
  27. if (index != -1) {
  28. this.freshState();
  29. gData.gameData.needFreshArr.splice(index, 1);
  30. }
  31. }
  32. freshState() {
  33. this.data = gData.gameData.getFactoryDataMap(this.configID);
  34. this.setState(this.data.state);
  35. }
  36. private setState(value: FactroyState): void {
  37. if (this.state != value) {
  38. this._state = value;
  39. this.countDown.node.active = false;
  40. switch (this.state) {
  41. case FactroyState.Lock:
  42. break;
  43. case FactroyState.Empty:
  44. if (this.idleAni && this.idleAni.active == false) {
  45. this.idleAni.active = true;
  46. }
  47. break;
  48. case FactroyState.Producting:
  49. this.countDown.node.active = true;
  50. this.countDown.setData(this.data);
  51. if (this.idleAni && this.idleAni.active == true) {
  52. this.idleAni.active = false;
  53. }
  54. break;
  55. case FactroyState.Ripe:
  56. this.countDown.node.active = true;
  57. this.countDown.setState(1);
  58. this.onProductComplete();
  59. break;
  60. case FactroyState.Sick:
  61. this.countDown.node.active = true;
  62. this.countDown.setState(0);
  63. break;
  64. }
  65. }
  66. }
  67. /**生产完成 */
  68. private async onProductComplete() {
  69. if (this.showGroup) {
  70. let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab);
  71. let item = cc.instantiate(prefab);
  72. let id = this.data.productID;
  73. item.getComponent(ProductShow).id = id;
  74. item.parent = this.showGroup;
  75. }
  76. }
  77. private clean(): void {
  78. this.showGroup.removeAllChildren();
  79. }
  80. public async onTouch() {
  81. switch (this.data.state) {
  82. case FactroyState.Lock:
  83. let arr = gData.gameData.getProductArrByType(this.tab);
  84. let lv = arr[0].value;
  85. mk.tip.pop(`农场达到${lv}级解锁`);
  86. break;
  87. case FactroyState.Empty:
  88. gData.factorySystem.currSelectFactory = this;
  89. gData.plantData.openPanel(this.tab, this.configID);
  90. break;
  91. case FactroyState.Ripe:
  92. gData.factorySystem.currSelectFactory = this;
  93. gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this));
  94. break;
  95. case FactroyState.Sick:
  96. // mk.tip.pop(`生虫了!`);
  97. gData.adClearSickData.openPanel(this.tab, this.configID);
  98. break;
  99. }
  100. }
  101. public make(id) {
  102. if (this.data.state == FactroyState.Empty) {
  103. let config = gData.gameData.getProductMap(id);
  104. let growSpan = Date.now() + config.time * 1000;
  105. this.data.state = FactroyState.Producting;
  106. this.data.productID = config.picture;
  107. this.data.growSpan = growSpan;
  108. gData.gameData.setFactoryDataMap(this.configID, this.data);
  109. }
  110. }
  111. onRipe() {
  112. this._state = FactroyState.Ripe;
  113. this.data.state = FactroyState.Ripe;
  114. gData.gameData.setFactoryDataMap(this.configID, this.data);
  115. gData.gameData.addProductMakeTimesById(this.data.productID);
  116. }
  117. onSick() {
  118. this._state = FactroyState.Sick;
  119. this.data.state = FactroyState.Sick;
  120. gData.gameData.setFactoryDataMap(this.configID, this.data);
  121. }
  122. public onHarvest(): void {
  123. this.countDown.cleanRiped();
  124. this.clean();
  125. gData.factorySystem.currSelectFactory = null;
  126. this.data.state = FactroyState.Empty;
  127. this.data.productID = 0;
  128. this.data.growSpan = 0;
  129. gData.gameData.setFactoryDataMap(this.configID, this.data);
  130. }
  131. }