| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- 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<number, PastureData> = new Map<number, PastureData>();
- public pastureIcons: Array<PastureIcon> = [];
- 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;
- }
- }
|