import { PastureState, ProductType } from "../../game/data/GameData"; import PastureData from "./PastureData"; import PastureIcon from "./PastureIcon"; /** 动物移动管理系统 */ const { ccclass } = cc._decorator; @ccclass export default class PastureSystem { private pastureDataMap: Map = new Map(); public pastureIcons: Array = []; setPastureData(configID) { if (this.getPastureData(configID)) { return; } let data = new PastureData(); this.pastureDataMap.set(configID, data); return data; } getPastureData(configID) { return this.pastureDataMap.get(configID); } addPastureIcon(pasture) { this.pastureIcons.push(pasture); } /** 下一个空置饲养场 */ nextPasture(out?: any) { let data = gData.gameData.playerProp.orderData; //let copyData = gData.gameData.playerProp.copyOrderData if (data && data.orderTaskList) { let orderData = data.orderTaskList; let productJson = gData.gameData.configs.Product; for (let index = 0; index != orderData.length; ++index) { let dataE = orderData[index]; if (dataE.completeCount < dataE.taskCount && dataE.Id <= productJson.length) { let productData = productJson[dataE.Id - 1]; let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (productData.picture == this.pastureIcons[i].data.productID && this.pastureIcons[i].data.state == PastureState.Empty) { gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID); gData.gameData.nextMake = this.pastureIcons[i]; gData.gameData.nextType = 2; return this.pastureIcons[i]; } } } else { break; } } //工厂有匹配的 if (out.state == 1) { return null; } else { return this.doSameCheckLogic(out); } } else { return this.doSameCheckLogic(out); } return null; } nextPastureExtra() { let factoryArray = []; let animalArray = []; let data = gData.gameData.playerProp.orderData; if (data) { let orderData = data.orderTaskList; let productJson = gData.gameData.configs.Product; for (let i = 0; i != orderData.length; ++i) { let dataE = orderData[i]; if (dataE.completeCount < dataE.taskCount) { if (dataE.Id <= productJson.length) { let productData = productJson[dataE.Id - 1]; if (productData.tab == ProductType.nzw) { continue; } else if (productData.tab == ProductType.dw) { animalArray.push(productData.picture); } else { factoryArray.push(productData); } } } } //从两种类型随机选择逻辑 let typeArr = []; let matchSuccessFactorys = null; let matchSuccessPastures = null; if (factoryArray.length > 0) { matchSuccessFactorys = gData.factorySystem.isHaveEmptyFactory(factoryArray); if(matchSuccessFactorys.length > 0){ typeArr.push(1); } } if (animalArray.length > 0) { matchSuccessPastures = this.isHaveEmptyPasture(animalArray); if(matchSuccessPastures.length > 0){ typeArr.push(2); } } if(typeArr.length > 0) { let rand = mk.math.random(0, typeArr.length-1); let type = typeArr[rand]; if (type == 1) { let randFactory = mk.math.random(0, matchSuccessFactorys.length-1); let factory = matchSuccessFactorys[randFactory]; gData.gameData.nextCanProduct = gData.gameData.getProductMap(factory.picture); gData.gameData.nextMake = factory.key; gData.gameData.nextType = 3; return gData.gameData.nextMake; } else if (type == 2) { let randPasture = mk.math.random(0, matchSuccessPastures.length-1); let pasture = matchSuccessPastures[randPasture]; gData.gameData.nextCanProduct = gData.gameData.getProductMap(pasture.data.productID); gData.gameData.nextMake = pasture gData.gameData.nextType = 2; return pasture; } }else{ let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Empty) { gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID); gData.gameData.nextMake = this.pastureIcons[i]; gData.gameData.nextType = 2; return this.pastureIcons[i]; } } } } else { let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Empty) { gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID); gData.gameData.nextMake = this.pastureIcons[i]; gData.gameData.nextType = 2; return this.pastureIcons[i]; } } } return null; } doSameCheckLogic(out:any) { if (out.state == 0) { let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Empty) { gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID); gData.gameData.nextMake = this.pastureIcons[i]; gData.gameData.nextType = 2; return this.pastureIcons[i]; } } } else { // let isHaveEmpty = {state: false}; // let picId = gData.gameData.doFarmMapDataCheck(1, isHaveEmpty); // if (picId) { // return picId; // } else { //if(isHaveEmpty.state) { let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Empty) { gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID); gData.gameData.nextMake = this.pastureIcons[i]; gData.gameData.nextType = 2; return this.pastureIcons[i]; } } } } } return null; } async btnMake() { let f = await gData.gameData.nextMake.onEat(); if (f) { return true; } else { return false; } } setHarvest() { let len = this.pastureIcons.length; let sendToServer = false; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Growing) { this.pastureIcons[i].countDown.riped(false); sendToServer = true; } } if (sendToServer) { gData.gameData.freshSendToServer(2); } } canSpeedUp() { let can = false; let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Growing) { can = true; break; } } return can; } canHarvest() { let can = false; let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Ripe) { this.pastureIcons[i].canHarvest(); can = true; break; } } return can; } canClearSick() { let can = false; let len = this.pastureIcons.length; for (var i = 0; i < len; i++) { if (this.pastureIcons[i].data.state == PastureState.Sick) { this.pastureIcons[i].canClearSick(); can = true; break; } } return can; } isHaveEmptyPasture(animalIdArray: any[]) { let pastureArr = []; for (let i = 0; i != animalIdArray.length; ++i) { let len = this.pastureIcons.length; for (var j = 0; j < len; j++) { if (this.pastureIcons[j].data.productID == animalIdArray[i] && this.pastureIcons[j].data.state == PastureState.Empty) { pastureArr.push(this.pastureIcons[j]); } } } return pastureArr; } }