| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- import { _decorator, Component, Widget, tween, UITransform, Prefab, instantiate, ScrollView } from 'cc';
- import { IFDataSource, InfiniteList } from '../../core/InfiniteList/InfiniteList';
- import { Http } from '../../core/net/Http';
- import { Window } from '../../core/ui/window/Window';
- import { ConfigData } from '../../Data/ConfigData';
- import { FarmManager } from '../../Main/FarmManager';
- import { NoviceWindow } from '../Novice/NoviceWindow';
- import { PlantWindowItem } from './PlantWindowItem';
- const { ccclass, property } = _decorator;
- @ccclass('PlantWindow')
- export class PlantWindow extends Component implements IFDataSource {
- public static needReLoad = false;
- @property({ type: Widget, tooltip: "布局约束控件" }) widget: Widget;
- @property({ type: InfiniteList, tooltip: "list节点" }) list: InfiniteList;
- @property({ type: Prefab, tooltip: "Item预制体" }) itemPrefab: Prefab;
- @property({ tooltip: "网络请求对象", type: Http }) http: Http;
- private plantData: Array<{ id: number, plantcount: number }> = [];
- private prefabWidth = 0;
- private configID = 0;
- private isLoaded = false;
- start() {
- FarmManager.plantWindowIsOpening = true;
- tween(this.widget).to(0.5, { editorBottom: -50 }, { easing: "bounceOut" }).call(() => {
- FarmManager.plantWindowIsOpening = false;
- }).start();
- let prefab = instantiate(this.itemPrefab);
- this.prefabWidth = prefab.getComponent(UITransform).width;
- prefab.destroy();
- }
- public async onParams(configID: number = 31000) {
- if (!this.isLoaded) {
- this.configID = configID;
- let config = ConfigData.configMap.get("build")[this.configID]["product"];
- let result = await this.http.send("/api/product/getProductTimes", { productIDList: config });
- if (result.code == 0) {
- this.plantData = config;
- for (let i = 0; i < config.length; i++) {
- let count = ConfigData.configMap.get("product")[config[i]]["reap"];
- FarmManager.plantCountMap.set(config[i], count - (result.data[config[i]] ? result.data[config[i]] : 0));
- }
- this.list.Init(this);
- PlantWindow.needReLoad = true;
- }
- this.isLoaded = true;
- }
- }
- public async onClosing(cb: () => void) {
- FarmManager.plantWindowIsOpening = true;
- tween(this.widget).to(0.3, { editorBottom: -500 }, { easing: "backIn" }).call(() => {
- FarmManager.plantWindowIsOpening = false;
- }).call(cb).start();
- }
- update() {
- if (!NoviceWindow.isNovicing && !FarmManager.getCurrFarm()) this.getComponent(Window).close();
- if (PlantWindow.needReLoad) {
- PlantWindow.needReLoad = false;
- let config = ConfigData.configMap.get("build")[this.configID]["product"];
- let noCountArray = [];
- this.plantData = [];
- for (let i = 0; i < config.length; i++) {
- let count = FarmManager.plantCountMap.get(config[i]);
- if (count == 0) {
- noCountArray.push(config[i]);
- } else {
- this.plantData.push(config[i]);
- }
- }
- this.plantData = this.plantData.concat(noCountArray);
- this.list.Reload();
- this.list.getComponent(ScrollView).scrollToLeft();
- }
- }
- //#region IFDataSource
- public GetCellData(dataIndex: number): any {
- return this.plantData[dataIndex];
- }
- public GetCellNumber(): number {
- return this.plantData.length;
- }
- public GetCellSize(index: number): number {
- return this.prefabWidth == 0 ? 181 : this.prefabWidth;
- }
- public GetCellView(index: number): PlantWindowItem {
- let node = instantiate(this.itemPrefab);
- return node.getComponent(PlantWindowItem);
- }
- public GetCellIdentifer(index: number): string {
- return "PlantWindowItem";
- }
- //#endregion
- }
|