import { DataEventId, ExpAddType, FarmState, GameProp, ProductType } from '../../game/data/GameData'; import { MoveToCenter } from '../map/MoveToCenter'; import ProductShow from '../view/ProductShow'; import { FarmCountDown } from './FarmCountDown'; import { FarmGrowProcess } from './FarmGrowProcess'; import { IDHelper } from './IDHelper'; const { ccclass, property } = cc._decorator; @ccclass export class FarmIcon extends cc.Component { @property({ type: cc.Node, tooltip: "未解锁时显示节点" }) lockNode: cc.Node = null; @property({ type: cc.Node, tooltip: "可解锁节点" }) canUnlockNode: cc.Node = null; @property({ type: cc.Node, tooltip: "选中闪烁节点" }) selectNode: cc.Node = null; @property({ type: cc.Sprite, tooltip: "种植动画图标" }) plantIcon: cc.Sprite = null; @property({ type: FarmCountDown, tooltip: "倒计时组件" }) countDown: FarmCountDown = null; @property({ type: FarmGrowProcess, tooltip: "成长控制组件" }) process: FarmGrowProcess = null; @property({ type: cc.Node, tooltip: '闲置动画' }) idleAni: cc.Node = null; public configID: number = 0; public sortID: number = 0; public plantID: number = 0; private _state: FarmState = -1; public get state(): FarmState { return this._state; } public data = null; onLoad() { let helper = this.getComponent(IDHelper); this.configID = helper.buildID; this.sortID = helper.sortID; gData.farmSystem.addFram(this); } start() { this.freshState(); } update(dt) { let index = gData.gameData.needFreshArr.indexOf(this.configID); if (index != -1) { this.freshState(); gData.gameData.needFreshArr.splice(index, 1); } } freshState() { this.data = gData.gameData.getFarmDataMap(this.configID); this.setState(this.data.state); } private async setState(value: FarmState) { if (this.state != value) { this._state = value; this.lockNode.active = false; this.canUnlockNode.active = false; this.plantIcon.node.active = false; this.countDown.node.active = false; this.process.node.active = false; switch (this.state) { case FarmState.Lock: this.lockNode.active = true; break; case FarmState.CanUnlock: this.lockNode.active = true; this.canUnlockNode.active = true; break; case FarmState.Empty: if (this.idleAni && this.idleAni.active == false) { this.idleAni.active = true; } break; case FarmState.Growing: this.countDown.node.active = true; this.process.node.active = true; this.countDown.setData(this.data); this.process.setData(this.data); if (this.idleAni && this.idleAni.active == true) { this.idleAni.active = false; } break; case FarmState.Ripe: this.plantIcon.node.active = true; this.countDown.node.active = true; this.process.node.active = true; this.countDown.setState(1); this.process.setData(this.data); this.plantID = this.data.productID; let prefab = await mk.loader.load("game/prefab/ProductShow1", cc.Prefab); let item = cc.instantiate(prefab); item.name = 'hb'; item.parent = this.node; break; case FarmState.Sick: this.plantIcon.node.active = true; this.countDown.node.active = true; this.process.node.active = true; this.countDown.setState(0); this.process.setData(this.data); break; } } } public activeFram(): void { this.lockNode.active = false; } public selectFarm(isSelect = false): void { this.selectNode.active = isSelect; if (isSelect) { this.getComponent(MoveToCenter).move(); } } public async onTouch() { switch (this.data.state) { case FarmState.Lock: mk.tip.pop(`点击拓建即可解锁农田`); break; case FarmState.CanUnlock: mk.data.sendDataEvent(DataEventId.button_click, "拓建icon"); gData.adUnlockData.openPanel(this.configID); break; case FarmState.Empty: gData.farmSystem.currSelectFarm = this; gData.plantData.openPanel(ProductType.nzw, this.configID); break; case FarmState.Growing: mk.ui.openPanel('module/speedUpUI/speedUp'); break; case FarmState.Ripe: gData.farmSystem.currSelectFarm = this; gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node); break; case FarmState.Sick: // mk.tip.pop(`生虫了!`); mk.data.sendDataEvent(DataEventId.button_click, "生虫icon"); gData.adClearSickData.openPanel(ProductType.nzw, this.configID); break; } } public onHarvest(): void { this.countDown.cleanRiped(); this.process.cleanProcess(); gData.farmSystem.currSelectFarm = null; this.data.state = FarmState.Empty; this.data.productID = 0; this.data.growSpan = 0; gData.gameData.setFarmDataMap(this.configID, this.data); 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); } let child = this.node.getChildByName('hb'); this.node.removeChild(child); } public async plant(id: number) { if (this.state == FarmState.Empty) { this.plantID = id; gData.gameData.isProducting = true; this.plantIcon.node.active = true; gData.gameData.changeLeftTimes(-1); gData.farmSystem.currSelectFarm = this; gData.adData.checkPopRed(); gData.gameData.addProductMakeTimesById(this.plantID); gData.gameData.nextMake = null; gData.gameData.setNextProduct(false); this.plantIcon.spriteFrame = await mk.loader.load("game/coregame/texture/plant_icons/plantIcon_" + this.plantID, cc.SpriteFrame); this.plantIcon.getComponent(cc.Animation).play(); this.plantIcon.getComponent(cc.Animation).on(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this); let flyRed = await gData.gameData.updateNewTaskProgress(); return flyRed; } else { mk.tip.pop('农田已满,无法继续生产'); return false; } } /** 成熟 */ public async onRiped(sendToServer) { this.data.state = FarmState.Ripe; gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer); } /** 生虫 */ public onSick(sendToServer) { this.data.state = FarmState.Sick; gData.gameData.setFarmDataMap(this.configID, this.data, sendToServer); } private async onPlantAnimationFinish() { this.plantIcon.getComponent(cc.Animation).off(cc.Animation.EventType.FINISHED, this.onPlantAnimationFinish, this); this.plantIcon.node.active = false; let config = gData.gameData.getProductMap(this.plantID); let growSpan = Date.now() + config.time * 1000; this.data.state = FarmState.Growing; this.data.productID = this.plantID; this.data.growSpan = growSpan; gData.gameData.setFarmDataMap(this.configID, this.data); gData.gameData.isProducting = false; } public novice_plant(): void { this.process.setData(this.data); } public novice_clean(): void { this.process.cleanProcess(); } public canHarvest() { gData.farmSystem.currSelectFarm = this; gData.harvestData.openPanel(this.configID, this.plantID, this.onHarvest.bind(this), this.node); } public canClearSick() { gData.farmSystem.currSelectFarm = this; mk.data.sendDataEvent(DataEventId.button_click, "生虫icon"); gData.adClearSickData.openPanel(ProductType.nzw, this.configID); } }