PastureSystem.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { PastureState, ProductType } from "../../game/data/GameData";
  2. import PastureData from "./PastureData";
  3. import PastureIcon from "./PastureIcon";
  4. /** 动物移动管理系统 */
  5. const { ccclass } = cc._decorator;
  6. @ccclass
  7. export default class PastureSystem {
  8. private pastureDataMap: Map<number, PastureData> = new Map<number, PastureData>();
  9. public pastureIcons: Array<PastureIcon> = [];
  10. setPastureData(configID) {
  11. if (this.getPastureData(configID)) {
  12. return;
  13. }
  14. let data = new PastureData();
  15. this.pastureDataMap.set(configID, data);
  16. return data;
  17. }
  18. getPastureData(configID) {
  19. return this.pastureDataMap.get(configID);
  20. }
  21. addPastureIcon(pasture) {
  22. this.pastureIcons.push(pasture);
  23. }
  24. /** 下一个空置饲养场 */
  25. nextPasture(out?: any) {
  26. let data = gData.gameData.playerProp.orderData;
  27. //let copyData = gData.gameData.playerProp.copyOrderData
  28. if (data && data.orderTaskList) {
  29. let orderData = data.orderTaskList;
  30. let productJson = gData.gameData.configs.Product;
  31. for (let index = 0; index != orderData.length; ++index) {
  32. let dataE = orderData[index];
  33. if (dataE.completeCount < dataE.taskCount && dataE.Id <= productJson.length) {
  34. let productData = productJson[dataE.Id - 1];
  35. let len = this.pastureIcons.length;
  36. for (var i = 0; i < len; i++) {
  37. if (productData.picture == this.pastureIcons[i].data.productID && this.pastureIcons[i].data.state == PastureState.Empty) {
  38. gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID);
  39. gData.gameData.nextMake = this.pastureIcons[i];
  40. gData.gameData.nextType = 2;
  41. return this.pastureIcons[i];
  42. }
  43. }
  44. } else {
  45. break;
  46. }
  47. }
  48. //工厂有匹配的
  49. if (out.state == 1) {
  50. return null;
  51. } else
  52. {
  53. return this.doSameCheckLogic(out);
  54. }
  55. } else {
  56. return this.doSameCheckLogic(out);
  57. }
  58. return null;
  59. }
  60. nextPastureExtra() {
  61. let factoryArray = [];
  62. let animalArray = [];
  63. let data = gData.gameData.playerProp.orderData;
  64. if (data) {
  65. let orderData = data.orderTaskList;
  66. let productJson = gData.gameData.configs.Product;
  67. for (let i = 0; i != orderData.length; ++i) {
  68. let dataE = orderData[i];
  69. if (dataE.completeCount < dataE.taskCount) {
  70. if (dataE.Id <= productJson.length) {
  71. let productData = productJson[dataE.Id - 1];
  72. if (productData.tab == ProductType.nzw) {
  73. continue;
  74. } else if (productData.tab == ProductType.dw) {
  75. animalArray.push(productData.picture);
  76. } else {
  77. factoryArray.push(productData);
  78. }
  79. }
  80. }
  81. }
  82. //从两种类型随机选择逻辑
  83. let typeArr = [];
  84. let matchSuccessFactorys = null;
  85. let matchSuccessPastures = null;
  86. if (factoryArray.length > 0) {
  87. matchSuccessFactorys = gData.factorySystem.isHaveEmptyFactory(factoryArray);
  88. if(matchSuccessFactorys.length > 0){
  89. typeArr.push(1);
  90. }
  91. }
  92. if (animalArray.length > 0) {
  93. matchSuccessPastures = this.isHaveEmptyPasture(animalArray);
  94. if(matchSuccessPastures.length > 0){
  95. typeArr.push(2);
  96. }
  97. }
  98. if(typeArr.length > 0)
  99. {
  100. let rand = mk.math.random(0, typeArr.length-1);
  101. let type = typeArr[rand];
  102. if (type == 1) {
  103. let randFactory = mk.math.random(0, matchSuccessFactorys.length-1);
  104. let factory = matchSuccessFactorys[randFactory];
  105. gData.gameData.nextCanProduct = gData.gameData.getProductMap(factory.picture);
  106. gData.gameData.nextMake = factory.key;
  107. gData.gameData.nextType = 3;
  108. return gData.gameData.nextMake;
  109. } else if (type == 2) {
  110. let randPasture = mk.math.random(0, matchSuccessPastures.length-1);
  111. let pasture = matchSuccessPastures[randPasture];
  112. gData.gameData.nextCanProduct = gData.gameData.getProductMap(pasture.data.productID);
  113. gData.gameData.nextMake = pasture
  114. gData.gameData.nextType = 2;
  115. return pasture;
  116. }
  117. }else{
  118. let len = this.pastureIcons.length;
  119. for (var i = 0; i < len; i++) {
  120. if (this.pastureIcons[i].data.state == PastureState.Empty) {
  121. gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID);
  122. gData.gameData.nextMake = this.pastureIcons[i];
  123. gData.gameData.nextType = 2;
  124. return this.pastureIcons[i];
  125. }
  126. }
  127. }
  128. } else {
  129. let len = this.pastureIcons.length;
  130. for (var i = 0; i < len; i++) {
  131. if (this.pastureIcons[i].data.state == PastureState.Empty) {
  132. gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID);
  133. gData.gameData.nextMake = this.pastureIcons[i];
  134. gData.gameData.nextType = 2;
  135. return this.pastureIcons[i];
  136. }
  137. }
  138. }
  139. return null;
  140. }
  141. doSameCheckLogic(out:any)
  142. {
  143. if (out.state == 0) {
  144. let len = this.pastureIcons.length;
  145. for (var i = 0; i < len; i++) {
  146. if (this.pastureIcons[i].data.state == PastureState.Empty) {
  147. gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID);
  148. gData.gameData.nextMake = this.pastureIcons[i];
  149. gData.gameData.nextType = 2;
  150. return this.pastureIcons[i];
  151. }
  152. }
  153. }
  154. else {
  155. // let isHaveEmpty = {state: false};
  156. // let picId = gData.gameData.doFarmMapDataCheck(1, isHaveEmpty);
  157. // if (picId) {
  158. // return picId;
  159. // } else
  160. {
  161. //if(isHaveEmpty.state)
  162. {
  163. let len = this.pastureIcons.length;
  164. for (var i = 0; i < len; i++) {
  165. if (this.pastureIcons[i].data.state == PastureState.Empty) {
  166. gData.gameData.nextCanProduct = gData.gameData.getProductMap(this.pastureIcons[i].data.productID);
  167. gData.gameData.nextMake = this.pastureIcons[i];
  168. gData.gameData.nextType = 2;
  169. return this.pastureIcons[i];
  170. }
  171. }
  172. }
  173. }
  174. }
  175. return null;
  176. }
  177. async btnMake() {
  178. let f = await gData.gameData.nextMake.onEat();
  179. if (f) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. setHarvest() {
  186. let len = this.pastureIcons.length;
  187. let sendToServer = false;
  188. for (var i = 0; i < len; i++) {
  189. if (this.pastureIcons[i].data.state == PastureState.Growing) {
  190. this.pastureIcons[i].countDown.riped(false);
  191. sendToServer = true;
  192. }
  193. }
  194. if (sendToServer) {
  195. gData.gameData.freshSendToServer(2);
  196. }
  197. }
  198. canSpeedUp() {
  199. let can = false;
  200. let len = this.pastureIcons.length;
  201. for (var i = 0; i < len; i++) {
  202. if (this.pastureIcons[i].data.state == PastureState.Growing) {
  203. can = true;
  204. break;
  205. }
  206. }
  207. return can;
  208. }
  209. canHarvest() {
  210. let can = false;
  211. let len = this.pastureIcons.length;
  212. for (var i = 0; i < len; i++) {
  213. if (this.pastureIcons[i].data.state == PastureState.Ripe) {
  214. this.pastureIcons[i].canHarvest();
  215. can = true;
  216. break;
  217. }
  218. }
  219. return can;
  220. }
  221. canClearSick() {
  222. let can = false;
  223. let len = this.pastureIcons.length;
  224. for (var i = 0; i < len; i++) {
  225. if (this.pastureIcons[i].data.state == PastureState.Sick) {
  226. this.pastureIcons[i].canClearSick();
  227. can = true;
  228. break;
  229. }
  230. }
  231. return can;
  232. }
  233. isHaveEmptyPasture(animalIdArray: any[]) {
  234. let pastureArr = [];
  235. for (let i = 0; i != animalIdArray.length; ++i) {
  236. let len = this.pastureIcons.length;
  237. for (var j = 0; j < len; j++) {
  238. if (this.pastureIcons[j].data.productID == animalIdArray[i] && this.pastureIcons[j].data.state == PastureState.Empty) {
  239. pastureArr.push(this.pastureIcons[j]);
  240. }
  241. }
  242. }
  243. return pastureArr;
  244. }
  245. }