PlantWindow.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { _decorator, Component, Widget, tween, UITransform, Prefab, instantiate, ScrollView } from 'cc';
  2. import { IFDataSource, InfiniteList } from '../../core/InfiniteList/InfiniteList';
  3. import { Http } from '../../core/net/Http';
  4. import { Window } from '../../core/ui/window/Window';
  5. import { ConfigData } from '../../Data/ConfigData';
  6. import { FarmManager } from '../../Main/FarmManager';
  7. import { NoviceWindow } from '../Novice/NoviceWindow';
  8. import { PlantWindowItem } from './PlantWindowItem';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('PlantWindow')
  11. export class PlantWindow extends Component implements IFDataSource {
  12. public static needReLoad = false;
  13. @property({ type: Widget, tooltip: "布局约束控件" }) widget: Widget;
  14. @property({ type: InfiniteList, tooltip: "list节点" }) list: InfiniteList;
  15. @property({ type: Prefab, tooltip: "Item预制体" }) itemPrefab: Prefab;
  16. @property({ tooltip: "网络请求对象", type: Http }) http: Http;
  17. private plantData: Array<{ id: number, plantcount: number }> = [];
  18. private prefabWidth = 0;
  19. private configID = 0;
  20. private isLoaded = false;
  21. start() {
  22. FarmManager.plantWindowIsOpening = true;
  23. tween(this.widget).to(0.5, { editorBottom: -50 }, { easing: "bounceOut" }).call(() => {
  24. FarmManager.plantWindowIsOpening = false;
  25. }).start();
  26. let prefab = instantiate(this.itemPrefab);
  27. this.prefabWidth = prefab.getComponent(UITransform).width;
  28. prefab.destroy();
  29. }
  30. public async onParams(configID: number = 31000) {
  31. if (!this.isLoaded) {
  32. this.configID = configID;
  33. let config = ConfigData.configMap.get("build")[this.configID]["product"];
  34. let result = await this.http.send("/api/product/getProductTimes", { productIDList: config });
  35. if (result.code == 0) {
  36. this.plantData = config;
  37. for (let i = 0; i < config.length; i++) {
  38. let count = ConfigData.configMap.get("product")[config[i]]["reap"];
  39. FarmManager.plantCountMap.set(config[i], count - (result.data[config[i]] ? result.data[config[i]] : 0));
  40. }
  41. this.list.Init(this);
  42. PlantWindow.needReLoad = true;
  43. }
  44. this.isLoaded = true;
  45. }
  46. }
  47. public async onClosing(cb: () => void) {
  48. FarmManager.plantWindowIsOpening = true;
  49. tween(this.widget).to(0.3, { editorBottom: -500 }, { easing: "backIn" }).call(() => {
  50. FarmManager.plantWindowIsOpening = false;
  51. }).call(cb).start();
  52. }
  53. update() {
  54. if (!NoviceWindow.isNovicing && !FarmManager.getCurrFarm()) this.getComponent(Window).close();
  55. if (PlantWindow.needReLoad) {
  56. PlantWindow.needReLoad = false;
  57. let config = ConfigData.configMap.get("build")[this.configID]["product"];
  58. let noCountArray = [];
  59. this.plantData = [];
  60. for (let i = 0; i < config.length; i++) {
  61. let count = FarmManager.plantCountMap.get(config[i]);
  62. if (count == 0) {
  63. noCountArray.push(config[i]);
  64. } else {
  65. this.plantData.push(config[i]);
  66. }
  67. }
  68. this.plantData = this.plantData.concat(noCountArray);
  69. this.list.Reload();
  70. this.list.getComponent(ScrollView).scrollToLeft();
  71. }
  72. }
  73. //#region IFDataSource
  74. public GetCellData(dataIndex: number): any {
  75. return this.plantData[dataIndex];
  76. }
  77. public GetCellNumber(): number {
  78. return this.plantData.length;
  79. }
  80. public GetCellSize(index: number): number {
  81. return this.prefabWidth == 0 ? 181 : this.prefabWidth;
  82. }
  83. public GetCellView(index: number): PlantWindowItem {
  84. let node = instantiate(this.itemPrefab);
  85. return node.getComponent(PlantWindowItem);
  86. }
  87. public GetCellIdentifer(index: number): string {
  88. return "PlantWindowItem";
  89. }
  90. //#endregion
  91. }