FactorySystem.ts 10 KB

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