FactoryWindow.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { _decorator, Component, Label, Prefab, instantiate, UITransform, Vec3, find, v3, Node, Sprite, SpriteFrame, ProgressBar, ScrollView } from 'cc';
  2. import { InfiniteList } from '../../core/InfiniteList/InfiniteList';
  3. import { Http } from '../../core/net/Http';
  4. import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils';
  5. import { WindowManager } from '../../core/ui/window/WindowManager';
  6. import { ConfigData } from '../../Data/ConfigData';
  7. import { g } from '../../Data/g';
  8. import { platform } from '../../Data/platform';
  9. import { FactoryCountDown } from './FactoryCountDown';
  10. import { FactoryPlus } from './FactoryPlus';
  11. import { FactoryWindowItem } from './FactoryWindowItem';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('FactoryWindow')
  14. export class FactoryWindow extends Component {
  15. @property({ type: Label, tooltip: "title文本" }) titleLabel: Label;
  16. @property({ type: InfiniteList, tooltip: "产出列表" }) list: InfiniteList;
  17. @property({ type: Prefab, tooltip: "item预制体" }) itemPrefab: Prefab;
  18. // @property({ type: Prefab, tooltip: "maker预制体" }) makerPrefabs: Prefab;
  19. @property({ type: [FactoryPlus], tooltip: "加号组集合" }) plusArray: Array<FactoryPlus> = [];
  20. @property({ type: [Sprite], tooltip: "生产队列集合" }) producingSpriteList: Array<Sprite> = [];
  21. @property({ type: Node, tooltip: "计时组" }) countDownGroup: Node;
  22. @property({ type: ProgressBar, tooltip: "计时进度条" }) progress: ProgressBar;
  23. @property({ type: Label, tooltip: "计时文字" }) progressLabel: Label;
  24. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  25. public buildID: number = 0;
  26. private config: any;
  27. private prefabWidth = 0;
  28. private listData = [];
  29. private maker: Node = null;
  30. // private scrollcount = 0;
  31. private countDown: FactoryCountDown;
  32. public countMap: Map<number, number> = new Map();
  33. start() {
  34. let prefab = instantiate(this.itemPrefab);
  35. this.prefabWidth = prefab.getComponent(UITransform).width;
  36. prefab.destroy();
  37. }
  38. private lastProductingLength = 0;
  39. async update() {
  40. let producting = g.gameData.getProductingList(this.buildID);
  41. if (producting.length > 0 && this.countDownGroup.active == false) {
  42. this.countDownGroup.active = true;
  43. } else if (producting.length == 0 && this.countDownGroup.active) {
  44. this.countDownGroup.active = false;
  45. }
  46. if (this.lastProductingLength != producting.length) {
  47. this.lastProductingLength = producting.length;
  48. for (let i = 0; i < this.producingSpriteList.length; i++) {
  49. this.producingSpriteList[i].spriteFrame = null;
  50. }
  51. for (let i = 0; i < producting.length; i++) {
  52. this.producingSpriteList[i].spriteFrame = await ResourcesUtils.load<SpriteFrame>("factory_icons/factory_" + producting[i].productID + "/spriteFrame", SpriteFrame, this.node);
  53. }
  54. }
  55. if (this.countDownGroup.active) {
  56. this.progress.progress = 1 - this.countDown.progress;
  57. this.progressLabel.string = this.countDown.timerString;
  58. }
  59. }
  60. public async onParams(id: number, count: FactoryCountDown) {
  61. this.buildID = id;
  62. this.countDown = count;
  63. this.config = ConfigData.configMap.get("build")[this.buildID];
  64. this.titleLabel.string = this.config["name"];
  65. let result = await this.http.send("/api/product/getProductTimes", { productIDList: this.config["product"] });
  66. if (result.code == 0) {
  67. for (let i = 0; i < this.config["product"].length; i++) {
  68. let p_config = ConfigData.configMap.get("product")[this.config["product"][i]];
  69. let obj = { id: this.config["product"][i], obj: this, count: result.data[this.config["product"][i]] }
  70. this.countMap.set(this.config["product"][i], p_config["reap"] - (result.data[this.config["product"][i]] ? result.data[this.config["product"][i]] : 0));
  71. this.listData.push(obj);
  72. }
  73. this.list.Init(this);
  74. }
  75. for (let i = 0; i < this.plusArray.length; i++) {
  76. this.plusArray[i].setData(this.buildID);
  77. }
  78. this.reLoadList();
  79. }
  80. public reLoadList(): void {
  81. this.listData = [];
  82. let noCountArray = []
  83. for (let i = 0; i < this.config["product"].length; i++) {
  84. let obj = { id: this.config["product"][i], obj: this, count: this.countMap.get(this.config["product"][i]) }
  85. if (obj.count == 0) {
  86. noCountArray.push(obj);
  87. } else {
  88. this.listData.push(obj);
  89. }
  90. }
  91. this.listData = this.listData.concat(noCountArray);
  92. this.list.Reload();
  93. this.list.getComponent(ScrollView).scrollToLeft();
  94. }
  95. public onItemClick(pos: Vec3, productID: number): void {
  96. }
  97. public async make(productID: number) {
  98. let producting = g.gameData.getProductingList(this.buildID);
  99. if (producting.length < g.gameData.getUnlock(this.buildID)) {
  100. let result = await this.http.send("/api/product/createOrder", { buildID: this.buildID, productID: productID });
  101. if (result.code == 0) {
  102. this.countMap.set(productID, this.countMap.get(productID) - 1);
  103. if (this.countMap.get(productID) == 0) {
  104. this.reLoadList();
  105. }
  106. this.producingSpriteList[producting.length].spriteFrame = await ResourcesUtils.load<SpriteFrame>("factory_icons/factory_" + productID + "/spriteFrame", SpriteFrame, this.node);
  107. let time = ConfigData.configMap.get("product")[productID]["time"];
  108. producting.push({ productID: productID, ripeDate: producting.length == 0 ? Date.now() + time * 1000 : producting[producting.length - 1].ripeDate + time * 1000 });
  109. if (result.data.productPrize) {
  110. g.gameData.productPrize = result.data.productPrize;
  111. }
  112. } else {
  113. WindowManager.showTips("请求失败");
  114. }
  115. }
  116. }
  117. public async onSpeedButton() {
  118. if (g.userData.diamond >= 25) {
  119. let result = await this.http.send("/api/product/quickProduct", { buildID: this.buildID });
  120. if (result.code == 0) {
  121. platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" }));
  122. g.userData.diamond -= result.data;
  123. this.countDown.ripe();
  124. WindowManager.close(this.node);
  125. return;
  126. }
  127. }
  128. WindowManager.showTips("钻石不足");
  129. }
  130. //#region IFDataSource
  131. public GetCellData(dataIndex: number): any {
  132. return this.listData[dataIndex];
  133. }
  134. public GetCellNumber(): number {
  135. return this.listData.length;
  136. }
  137. public GetCellSize(index: number): number {
  138. return this.prefabWidth == 0 ? 126 : this.prefabWidth;
  139. }
  140. public GetCellView(index: number): FactoryWindowItem {
  141. let node = instantiate(this.itemPrefab);
  142. return node.getComponent(FactoryWindowItem);
  143. }
  144. public GetCellIdentifer(index: number): string {
  145. return "FactoryWindowItem";
  146. }
  147. //#endregion
  148. }