import { _decorator, Component, Label, Prefab, instantiate, UITransform, Vec3, find, v3, Node, Sprite, SpriteFrame, ProgressBar, ScrollView } from 'cc'; import { InfiniteList } from '../../core/InfiniteList/InfiniteList'; import { Http } from '../../core/net/Http'; import { ResourcesUtils } from '../../core/resourceManager/ResourcesUtils'; import { WindowManager } from '../../core/ui/window/WindowManager'; import { ConfigData } from '../../Data/ConfigData'; import { g } from '../../Data/g'; import { platform } from '../../Data/platform'; import { FactoryCountDown } from './FactoryCountDown'; import { FactoryPlus } from './FactoryPlus'; import { FactoryWindowItem } from './FactoryWindowItem'; const { ccclass, property } = _decorator; @ccclass('FactoryWindow') export class FactoryWindow extends Component { @property({ type: Label, tooltip: "title文本" }) titleLabel: Label; @property({ type: InfiniteList, tooltip: "产出列表" }) list: InfiniteList; @property({ type: Prefab, tooltip: "item预制体" }) itemPrefab: Prefab; // @property({ type: Prefab, tooltip: "maker预制体" }) makerPrefabs: Prefab; @property({ type: [FactoryPlus], tooltip: "加号组集合" }) plusArray: Array = []; @property({ type: [Sprite], tooltip: "生产队列集合" }) producingSpriteList: Array = []; @property({ type: Node, tooltip: "计时组" }) countDownGroup: Node; @property({ type: ProgressBar, tooltip: "计时进度条" }) progress: ProgressBar; @property({ type: Label, tooltip: "计时文字" }) progressLabel: Label; @property({ tooltip: "网络请求对象", type: Http }) http: Http; public buildID: number = 0; private config: any; private prefabWidth = 0; private listData = []; private maker: Node = null; // private scrollcount = 0; private countDown: FactoryCountDown; public countMap: Map = new Map(); start() { let prefab = instantiate(this.itemPrefab); this.prefabWidth = prefab.getComponent(UITransform).width; prefab.destroy(); } private lastProductingLength = 0; async update() { let producting = g.gameData.getProductingList(this.buildID); if (producting.length > 0 && this.countDownGroup.active == false) { this.countDownGroup.active = true; } else if (producting.length == 0 && this.countDownGroup.active) { this.countDownGroup.active = false; } if (this.lastProductingLength != producting.length) { this.lastProductingLength = producting.length; for (let i = 0; i < this.producingSpriteList.length; i++) { this.producingSpriteList[i].spriteFrame = null; } for (let i = 0; i < producting.length; i++) { this.producingSpriteList[i].spriteFrame = await ResourcesUtils.load("factory_icons/factory_" + producting[i].productID + "/spriteFrame", SpriteFrame, this.node); } } if (this.countDownGroup.active) { this.progress.progress = 1 - this.countDown.progress; this.progressLabel.string = this.countDown.timerString; } } public async onParams(id: number, count: FactoryCountDown) { this.buildID = id; this.countDown = count; this.config = ConfigData.configMap.get("build")[this.buildID]; this.titleLabel.string = this.config["name"]; let result = await this.http.send("/api/product/getProductTimes", { productIDList: this.config["product"] }); if (result.code == 0) { for (let i = 0; i < this.config["product"].length; i++) { let p_config = ConfigData.configMap.get("product")[this.config["product"][i]]; let obj = { id: this.config["product"][i], obj: this, count: result.data[this.config["product"][i]] } this.countMap.set(this.config["product"][i], p_config["reap"] - (result.data[this.config["product"][i]] ? result.data[this.config["product"][i]] : 0)); this.listData.push(obj); } this.list.Init(this); } for (let i = 0; i < this.plusArray.length; i++) { this.plusArray[i].setData(this.buildID); } this.reLoadList(); } public reLoadList(): void { this.listData = []; let noCountArray = [] for (let i = 0; i < this.config["product"].length; i++) { let obj = { id: this.config["product"][i], obj: this, count: this.countMap.get(this.config["product"][i]) } if (obj.count == 0) { noCountArray.push(obj); } else { this.listData.push(obj); } } this.listData = this.listData.concat(noCountArray); this.list.Reload(); this.list.getComponent(ScrollView).scrollToLeft(); } public onItemClick(pos: Vec3, productID: number): void { } public async make(productID: number) { let producting = g.gameData.getProductingList(this.buildID); if (producting.length < g.gameData.getUnlock(this.buildID)) { let result = await this.http.send("/api/product/createOrder", { buildID: this.buildID, productID: productID }); if (result.code == 0) { this.countMap.set(productID, this.countMap.get(productID) - 1); if (this.countMap.get(productID) == 0) { this.reLoadList(); } this.producingSpriteList[producting.length].spriteFrame = await ResourcesUtils.load("factory_icons/factory_" + productID + "/spriteFrame", SpriteFrame, this.node); let time = ConfigData.configMap.get("product")[productID]["time"]; producting.push({ productID: productID, ripeDate: producting.length == 0 ? Date.now() + time * 1000 : producting[producting.length - 1].ripeDate + time * 1000 }); if (result.data.productPrize) { g.gameData.productPrize = result.data.productPrize; } } else { WindowManager.showTips("请求失败"); } } } public async onSpeedButton() { if (g.userData.diamond >= 25) { let result = await this.http.send("/api/product/quickProduct", { buildID: this.buildID }); if (result.code == 0) { platform.reportThinking("diamond_decrease", JSON.stringify({ previous_number: g.userData.diamond, decrease_number: result.data, current_number: g.userData.diamond - result.data, reasons: "speed" })); g.userData.diamond -= result.data; this.countDown.ripe(); WindowManager.close(this.node); return; } } WindowManager.showTips("钻石不足"); } //#region IFDataSource public GetCellData(dataIndex: number): any { return this.listData[dataIndex]; } public GetCellNumber(): number { return this.listData.length; } public GetCellSize(index: number): number { return this.prefabWidth == 0 ? 126 : this.prefabWidth; } public GetCellView(index: number): FactoryWindowItem { let node = instantiate(this.itemPrefab); return node.getComponent(FactoryWindowItem); } public GetCellIdentifer(index: number): string { return "FactoryWindowItem"; } //#endregion }