import { DataEventId, ExpAddType, FactroyState, GameProp, ProductType } from "../../game/data/GameData"; import { MoveToCenter } from "../map/MoveToCenter"; import ProductShow from "../view/ProductShow"; import { FarmCountDown } from "./FarmCountDown"; import { IDHelper } from "./IDHelper"; const { ccclass, property } = cc._decorator; @ccclass export class FactoryIcon extends cc.Component { @property({ type: cc.Node, tooltip: "渲染节点" }) buildNode: cc.Node = null; @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null; @property({ type: cc.Node, tooltip: "完成品展示节点" }) showGroup: cc.Node = null; @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null; @property({ type: cc.Node, tooltip: '锁住节点' }) node_lock: cc.Node = null; public configID: number = 0; public sortID: number = 0; public tab: ProductType = ProductType.bmhc; private _state: FactroyState = -1; public get state(): FactroyState { return this._state; } data = null; onLoad() { let helper = this.getComponent(IDHelper); this.configID = helper.buildID; this.sortID = helper.sortID; this.tab = gData.gameData.getTabByConfigID(this.configID); this.countDown.node.active = false; this.freshState(); gData.factorySystem.addFactory(this); } update() { let index = gData.gameData.needFreshArr.indexOf(this.configID); if (index != -1) { this.freshState(); gData.gameData.needFreshArr.splice(index, 1); } } freshState() { this.data = gData.gameData.getFactoryDataMap(this.configID); this.setState(this.data.state); } private setState(value: FactroyState): void { if (this.state != value) { this._state = value; this.countDown.node.active = false; this.node_lock.active = false; switch (this.state) { case FactroyState.Lock: this.buildNode.color = cc.Color.GRAY; this.node_lock.active = true; break; case FactroyState.Empty: this.buildNode.color = cc.Color.WHITE; if (this.idleAni && this.idleAni.active == false) { this.idleAni.active = true; } break; case FactroyState.Producting: this.countDown.node.active = true; this.countDown.setData(this.data); if (this.idleAni && this.idleAni.active == true) { this.idleAni.active = false; } break; case FactroyState.Ripe: this.countDown.node.active = true; this.countDown.setState(1); this.onProductComplete(); break; case FactroyState.Sick: this.countDown.node.active = true; this.countDown.setState(0); break; } } } /**生产完成 */ private async onProductComplete() { if (this.showGroup) { let prefab = await mk.loader.load("game/prefab/ProductShow", cc.Prefab); let item = cc.instantiate(prefab); let id = this.data.productID; item.getComponent(ProductShow).id = id; item.parent = this.showGroup; } } private clean(): void { this.showGroup.removeAllChildren(); } isCanTouch = true; public async onTouch() { if (!this.isCanTouch) { return; } switch (this.data.state) { case FactroyState.Lock: let arr = gData.gameData.getProductArrByType(this.tab); if (gData.gameData.playerProp.orderData) { let lv = arr[0].value - gData.gameData.playerProp.orderData.overTimes; mk.tip.pop(`订单提现${lv}次后解锁`); } else { mk.tip.pop(`提现订单后解锁`); } break; case FactroyState.Empty: gData.factorySystem.currSelectFactory = this; gData.plantData.openPanel(this.tab, this.configID); break; case FactroyState.Producting: mk.ui.openPanel('module/speedUpUI/speedUp'); break; case FactroyState.Ripe: gData.factorySystem.currSelectFactory = this; gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 2); this.isCanTouch = false; setTimeout(() => { this.isCanTouch = true; }, 1000); break; case FactroyState.Sick: // mk.tip.pop(`生虫了!`); mk.data.sendDataEvent(DataEventId.button_click, "停电icon"); gData.adClearSickData.openPanel(this.tab, this.configID); mk.console.logSingle('VVVV ', '停电icon'); break; } } public async make(id, parentNode = null) { if (this.data.state == FactroyState.Empty) { gData.gameData.isProducting = true; let config = gData.gameData.getProductMap(id); let growSpan = Date.now() + config.time * 1000; this.data.state = FactroyState.Producting; this.data.productID = config.picture; this.data.growSpan = growSpan; gData.adData.checkPopRed(); this.getComponent(MoveToCenter).move(); try { gData.gameData.setFactoryDataMap(this.configID, this.data); gData.gameData.changeLeftTimes(-1); gData.gameData.addProductMakeTimesById(this.data.productID); await gData.gameData.gameStyle.doCropFlyLogic(this.data.productID, this.node, parentNode); } catch (error) { console.log("========factoryIcon"); } //gData.gameData.updateOrderTaskCopyData(this.data.productID); gData.gameData.nextMake = null; gData.gameData.setNextProduct(false); gData.gameData.isProducting = false; let flyRed = await gData.gameData.updateNewTaskProgress(); return flyRed; // if (flyRed) { // //mk.audio.playEffect('redmoney'); // gData.gameData.gameStyle.dpFlyRedAni(); // } } else { mk.tip.pop('商品已满!') return false; } } clickSpeedUp() { mk.ui.openPanel('module/speedUpUI/speedUp'); } onRipe(sendToServer) { this.data.state = FactroyState.Ripe; gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer); //gData.gameData.addProductMakeTimesById(this.data.productID); } onSick(sendToServer) { this.data.state = FactroyState.Sick; gData.gameData.setFactoryDataMap(this.configID, this.data, sendToServer); } public async onHarvest() { this.countDown.cleanRiped(); this.clean(); gData.factorySystem.currSelectFactory = null; let productID = this.data.productID; this.data.state = FactroyState.Empty; this.data.productID = 0; this.data.growSpan = 0; gData.gameData.setFactoryDataMap(this.configID, this.data); //await gData.gameData.gameStyle.doCropFlyLogic(productID, this.node); //gData.gameData.nextMake = null; gData.gameData.setNextProduct(false); if (gData.harvestData.getType == 0) { gData.farmGradeData.addGradeExp(ExpAddType.EAT_harvest, this.node); } else { gData.farmGradeData.addGradeExp(ExpAddType.EAT_others, this.node); } } public canHarvest() { this.getComponent(MoveToCenter).move(); gData.factorySystem.currSelectFactory = this; gData.harvestData.openPanel(this.configID, this.data.productID, this.onHarvest.bind(this), this.node, 2); } public canClearSick() { this.getComponent(MoveToCenter).move(); mk.data.sendDataEvent(DataEventId.button_click, "停电icon"); gData.adClearSickData.openPanel(this.tab, this.configID); } }