Container_Manager.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. import { _decorator, BoxCollider, Component, director, find, instantiate, Node, NodePool, Prefab, Quat, random, RigidBody, sp, tween, Vec3 } from 'cc';
  2. import ch_util from '../../ch/ch_util';
  3. import { gui } from '../../core/ui/ui';
  4. import { Layout_Main } from '../ui/UI_Main/Layout_Main';
  5. import { UI_Main } from '../ui/UI_Main/UI_Main';
  6. import { CreateIdiom } from './CreateIdiom';
  7. import { Cube_Infor, Cube_State } from './Cube_Infor';
  8. import { GameCtl } from './GameCtl';
  9. import { ch } from '../../ch/ch';
  10. import { table_idiom_order } from '../../module_extra/table_ts/table_idiom_order';
  11. import { table_idiom_unorder_1_3 } from '../../module_extra/table_ts/table_idiom_unorder_1_3';
  12. import { table_idiom_unorder_2_2 } from '../../module_extra/table_ts/table_idiom_unorder_2_2';
  13. import { table_idiom_unorder_3_1 } from '../../module_extra/table_ts/table_idiom_unorder_3_1';
  14. import { table_level } from '../../module_extra/table_ts/table_level';
  15. import { table_level_2 } from '../../module_extra/table_ts/table_level_2';
  16. import { GameState, Hall } from '../hall/Hall';
  17. import { UI_Idioms } from '../ui/UI_Idioms/UI_Idioms';
  18. const { ccclass, property } = _decorator;
  19. @ccclass('Container_Manager')
  20. export class Container_Manager extends Component {
  21. @property(Node)
  22. skeleton1: Node = null;
  23. @property(Node)
  24. skeleton2: Node = null;
  25. canTouch: boolean = false;
  26. @property([Node])
  27. Lock_node: Node[] = [];
  28. @property([Prefab])
  29. prefabs: Prefab[] = [];
  30. level_config: any = null;//关卡配置
  31. level2_config: any = null;//第二关配置
  32. config: any = null;//有规律的成语库
  33. config_1_3: any = null;//无规律1+3
  34. config_2_2: any = null;//无规律2+2
  35. config_3_1: any = null;//无规律3+1
  36. idioms: any[] = [];//生成的成语
  37. idioms_Copy: any[] = [];//生成的成语备份
  38. index: number = 0;
  39. @property(CreateIdiom)
  40. create_node: CreateIdiom = null;
  41. @property([Node])
  42. nodes: Node[] = [];//位置节点 用于成语放入
  43. node_isIdiom: boolean[] = new Array(9).fill(false);
  44. unlock_Num: number = 7;
  45. is_Show_UI_Lock = false;
  46. idiom_combine: Map<Cube_Infor, number> = new Map();
  47. Cube_Pool: NodePool = new NodePool();
  48. nodeReferences: Node[] = []; // 额外维护的节点引用数组
  49. count: number = 0;
  50. time: number = 0;
  51. private instantiateCube() {
  52. let num = 60;
  53. if (this.idioms.length < 30) {
  54. num = this.idioms.length * 2;
  55. }
  56. for (let i = 0; i < num; i++) {
  57. let idiomIndex = Math.floor(i / 2);
  58. let isPiece1 = i % 2 === 0;
  59. let Text_Length = isPiece1
  60. ? this.idioms[idiomIndex].piece_1_word.length
  61. : this.idioms[idiomIndex].piece_2_word.length;
  62. const newCube = instantiate(this.prefabs[Text_Length - 1]);
  63. newCube.active = true;
  64. // 按顺序为节点赋值文字内容
  65. newCube.getComponent(Cube_Infor).Text = isPiece1
  66. ? this.idioms[idiomIndex].piece_1_word
  67. : this.idioms[idiomIndex].piece_2_word;
  68. // console.log("生成第" + i + "个节点:" + newCube.getComponent(Cube_Infor).Text);
  69. this.Cube_Pool.put(newCube);
  70. }
  71. this.index = num;
  72. if (Hall.getInstance().player.get_max_floor() != 0)
  73. this.shufflePool();
  74. }
  75. public instantiateNewCube() {
  76. console.log("instantiateNewCube");
  77. if (this.index < (this.count * 2)) {
  78. console.log("idiomIndex:" + Math.floor(this.index / 2));
  79. let idiomIndex = Math.floor(this.index / 2);
  80. let isPiece1 = this.index % 2 === 0;
  81. let Text_Length = isPiece1
  82. ? this.idioms_Copy[idiomIndex].piece_1_word.length
  83. : this.idioms_Copy[idiomIndex].piece_2_word.length;
  84. const newCube = instantiate(this.prefabs[Text_Length - 1]);
  85. newCube.active = true;
  86. // 按顺序为节点赋值文字内容
  87. newCube.getComponent(Cube_Infor).Text = isPiece1
  88. ? this.idioms_Copy[idiomIndex].piece_1_word
  89. : this.idioms_Copy[idiomIndex].piece_2_word;
  90. //随机选择已在nodereference中的一个方块position
  91. newCube.parent = director.getScene();
  92. newCube.setPosition(this.nodeReferences[Math.floor(Math.random() * this.nodeReferences.length)].position.x, this.nodeReferences[Math.floor(Math.random() * this.nodeReferences.length)].position.y - 0.1, this.nodeReferences[Math.floor(Math.random() * this.nodeReferences.length)].position.z);
  93. this.nodeReferences.push(newCube);
  94. console.log("生成第" + this.index + "个节点:" + newCube.getComponent(Cube_Infor).Text);
  95. console.log("位置:" + newCube.position)
  96. this.index = this.index + 1;
  97. }
  98. }
  99. public getCube(): Node {
  100. // console.log(this.Cube_Pool.size());
  101. const cube = this.Cube_Pool.get();
  102. if (cube)
  103. cube.active = true;
  104. return cube;
  105. }
  106. // 将方块回收到对象池
  107. public recycleCube(cube: Node): void {
  108. cube.active = false; // 将方块设置为非激活状态
  109. this.Cube_Pool.put(cube);
  110. }
  111. //清空对象池
  112. public clearCubePool() {
  113. this.Cube_Pool.clear();
  114. }
  115. //合成规则导入
  116. start() {
  117. this.config = table_idiom_order.getList();
  118. this.config_1_3 = table_idiom_unorder_1_3.getList();
  119. this.config_2_2 = table_idiom_unorder_2_2.getList();
  120. this.config_3_1 = table_idiom_unorder_3_1.getList();
  121. console.log(this.config.length);
  122. console.log(this.config_1_3.length);
  123. console.log(this.config_2_2.length);
  124. console.log(this.config_3_1.length);
  125. this.level_config = table_level.getList();
  126. this.level2_config = table_level_2.getList();
  127. // if (this.level_config.length === 5) {
  128. // console.log("关卡配置导入成功");
  129. // }
  130. this.level_idioms();
  131. }
  132. update(deltaTime: number) {
  133. }
  134. checkIdiom_Combine(matchedcube2: Cube_Infor, outMatchedCubes: Cube_Infor[]): boolean {
  135. if (this.idiom_combine.size < 2) {
  136. return false; // 至少需要两个方块
  137. }
  138. for (let cube of this.idiom_combine.keys()) {
  139. // 遍历 idioms 列表,检查是否匹配成语
  140. const matchedIdiom = this.idioms.find(
  141. idiom =>
  142. idiom.piece_1_word === cube.Text && idiom.piece_2_word === matchedcube2.Text
  143. );
  144. if (matchedIdiom) {
  145. // 匹配成功
  146. outMatchedCubes.push(cube, matchedcube2);
  147. this.nodeReferences = this.nodeReferences.filter((el) => el !== matchedcube2.node && el !== cube.node);
  148. console.log("成功拼成成语: " + matchedIdiom.piece_1_word + matchedIdiom.piece_2_word);
  149. return true;
  150. }
  151. // 再检查逆序组合是否匹配
  152. const reverseMatchedIdiom = this.idioms.find(
  153. idiom =>
  154. idiom.piece_1_word === matchedcube2.Text && idiom.piece_2_word === cube.Text
  155. );
  156. if (reverseMatchedIdiom) {
  157. // 匹配成功
  158. outMatchedCubes.push(matchedcube2, cube);
  159. this.nodeReferences = this.nodeReferences.filter((el) => el !== matchedcube2.node && el !== cube.node);
  160. console.log(
  161. "成功拼成成语: " +
  162. reverseMatchedIdiom.piece_1_word +
  163. reverseMatchedIdiom.piece_2_word
  164. );
  165. return true;
  166. }
  167. }
  168. return false; // 没有匹配到成语
  169. }
  170. async level_idioms() {
  171. this.Lock_node.forEach(node => node.active = true);
  172. this.unlock_Num = 7;
  173. this.is_Show_UI_Lock = false;
  174. this.canTouch = false;
  175. this.index = 0;
  176. let level = Hall.getInstance().player.get_max_floor();
  177. Hall.getInstance().gameState = GameState.gameing;
  178. if (level > 0) {
  179. Hall.getInstance().firstEnter = false;
  180. }
  181. this.clearLevelData();
  182. this.idioms = [];
  183. this.node_isIdiom = new Array(9).fill(false);
  184. this.idiom_combine = new Map();
  185. // 获取当前关卡的成语配置
  186. if (level === 0) {
  187. this.setupLevel1();
  188. } else if (level === 1) {
  189. this.setupLevel2();
  190. } else {
  191. this.setupLevelDefault(level);
  192. }
  193. this.idioms_Copy = [...this.idioms];
  194. this.count = this.level_config[level].total;
  195. this.time = this.level_config[level].time;
  196. await this.instantiateCube();
  197. await gui.show(UI_Idioms);
  198. this.create_node.nodeMoving();
  199. gui.show(UI_Main);
  200. }
  201. clearLevelData() {
  202. for (let node of this.nodeReferences) {
  203. node.destroy();
  204. }
  205. this.nodeReferences = [];
  206. this.clearCubePool();
  207. }
  208. setupLevel1() {
  209. const idiom_type_2 = this.level_config[0].idiom_type_2.split("_");
  210. console.log("idiom_type_2:", idiom_type_2);
  211. idiom_type_2.forEach(rule => {
  212. const filteredIdioms = this.config.filter(item => item.piece_2_word === rule);
  213. console.log(`筛选2 ${rule} 后的成语:`, filteredIdioms);
  214. this.idioms.push(...filteredIdioms);
  215. });
  216. console.log("最终选中的成语:", this.idioms);
  217. }
  218. setupLevel2() {
  219. for (let i = 0; i < 30; i++) {
  220. this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config));
  221. this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config_3_1));
  222. }
  223. for (let i = 30; i < 40; i++) {
  224. this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config_2_2));
  225. }
  226. console.log("最终选中的成语:", this.idioms);
  227. }
  228. setupLevelDefault(level) {
  229. const dif = this.level_config[level].different_grade_level.split("_");
  230. const grade = dif[0];
  231. const idiom_type_1 = this.parseIdiomType(this.level_config[level].idiom_type_1);
  232. const idiom_type_2 = this.parseIdiomType(this.level_config[level].idiom_type_2);
  233. const count = this.level_config[level].count / (idiom_type_1.length + idiom_type_2.length);
  234. console.log("count:", count);
  235. let selectedIdioms = {};
  236. // 筛选成语并随机选择
  237. this.selectIdiomsByRules(idiom_type_1, grade, count, selectedIdioms, 'piece_1_word');
  238. this.selectIdiomsByRules(idiom_type_2, grade, count, selectedIdioms, 'piece_2_word');
  239. // 合并结果
  240. // 使用 Object.keys 来遍历 selectedIdioms
  241. for (let rule in selectedIdioms) {
  242. this.idioms.push(...selectedIdioms[rule]);
  243. }
  244. // 随机选择难度成语
  245. this.selectIdiomsByDifficulty(level, 'easy_1_3', this.config_1_3);
  246. this.selectIdiomsByDifficulty(level, 'hard_1_3', this.config_1_3);
  247. this.selectIdiomsByDifficulty(level, 'easy_2_2', this.config_2_2);
  248. this.selectIdiomsByDifficulty(level, 'hard_2_2', this.config_2_2);
  249. this.selectIdiomsByDifficulty(level, 'easy_3_1', this.config_3_1);
  250. this.selectIdiomsByDifficulty(level, 'hard_3_1', this.config_3_1);
  251. console.log("最终选中的成语:", this.idioms);
  252. }
  253. parseIdiomType(idiomType) {
  254. return idiomType.split("_").filter(item => item !== "");
  255. }
  256. selectIdiomsByRules(idiomType, grade, count, selectedIdioms, ruleKey) {
  257. idiomType.forEach(rule => {
  258. const filteredIdioms = this.config.filter(item => item[ruleKey] === rule && item.difficulty === grade);
  259. console.log(`筛选 ${rule} 后的成语:`, filteredIdioms);
  260. if (filteredIdioms.length < count) {
  261. console.error(`规律 ${rule} 的成语数量不足,仅有 ${filteredIdioms.length} 个`);
  262. } else {
  263. const selected = this.randomSelectIdioms(filteredIdioms, count);
  264. selectedIdioms[rule] = selected;
  265. }
  266. });
  267. }
  268. randomSelectIdioms(filteredIdioms, count) {
  269. let selected = [];
  270. for (let i = 0; i < count; i++) {
  271. const rand = ch_util.getRandomInt(0, filteredIdioms.length - 1);
  272. selected.push(filteredIdioms.splice(rand, 1)[0]);
  273. }
  274. return selected;
  275. }
  276. selectIdiomsByDifficulty(level, configKey, config) {
  277. if (this.level_config[level][configKey] > 0) {
  278. const filteredIdioms = config.filter(item => item.difficulty === "easy");
  279. for (let i = 0; i < this.level_config[level][configKey]; i++) {
  280. const rand = ch_util.getRandomInt(0, filteredIdioms.length - 1);
  281. const selectedIdiom = filteredIdioms.splice(rand, 1)[0];
  282. this.idioms.push(selectedIdiom);
  283. }
  284. }
  285. }
  286. filterIdioms(idiom, config) {
  287. return config.filter(item => item.idiom === idiom);
  288. }
  289. // 精简后的消除一组函数
  290. eliminate() {
  291. // 获取槽中的方块
  292. let cube = [...this.idiom_combine].find(([key, value]) => value === 0)?.[0];
  293. // 如果槽中有方块
  294. if (cube) {
  295. const originalReferences = [...this.nodeReferences];
  296. for (const element of originalReferences) {
  297. const elementCubeInfo = element.getComponent(Cube_Infor);
  298. const elementText = elementCubeInfo.Text;
  299. const cubeText = cube.Text;
  300. if ((this.idioms.some(c => c.idiom === elementText + cubeText))&& elementCubeInfo !== cube) {
  301. // 执行消除操作
  302. this.processElimination(element, cube);
  303. break;
  304. }
  305. else if (this.idioms.some(c => c.idiom === cubeText + elementText) && elementCubeInfo !== cube) {
  306. this.processElimination(cube, element);
  307. break;
  308. }
  309. }
  310. } else {
  311. let flag = false;
  312. const originalReferences = [...this.nodeReferences];
  313. for (const element1 of originalReferences) {
  314. for (const element2 of originalReferences) {
  315. const element1Text = element1.getComponent(Cube_Infor).Text;
  316. const element2Text = element2.getComponent(Cube_Infor).Text;
  317. if (this.idioms.some(c => c.idiom === element1Text + element2Text)) {
  318. // 执行消除操作
  319. this.processElimination(element1, element2);
  320. flag = true;
  321. break;
  322. }
  323. else if (this.idioms.some(c => c.idiom === element2Text + element1Text)) {
  324. // 执行消除操作
  325. this.processElimination(element2, element1);
  326. flag = true;
  327. break;
  328. }
  329. if (flag) {
  330. break;
  331. }
  332. }
  333. if (flag) {
  334. for (let i = 0; i < 2; i++) this.instantiateNewCube();
  335. break;
  336. }
  337. }
  338. if (!flag) {
  339. console.log("没有可消除");
  340. }
  341. }
  342. }
  343. // 消除操作
  344. processElimination(element1, element2) {
  345. this.nodeReferences = this.nodeReferences.filter(el => el !== element1 && el !== element2);
  346. const element1CubeInfo = element1.getComponent(Cube_Infor);
  347. const element2CubeInfo = element2.getComponent(Cube_Infor);
  348. [element1, element2].forEach(element => {
  349. element.getComponent(Cube_Infor).state = Cube_State.wait;
  350. element.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC;
  351. element.getComponent(BoxCollider).enabled = false;
  352. let targetRotation = new Quat();
  353. Quat.fromEuler(targetRotation, -90, 0, 0);
  354. element.rotation = targetRotation;
  355. });
  356. // 执行动画
  357. GameCtl.instance.combine_ani(element1CubeInfo, element2CubeInfo);
  358. }
  359. async shuffle() {
  360. // 回收所有非槽内的活跃节点
  361. this.nodeReferences.forEach(node => {
  362. const cubeInfo = node.getComponent(Cube_Infor);
  363. if (cubeInfo.state === Cube_State.live) {
  364. let targetRotation = new Quat();
  365. Quat.fromEuler(targetRotation, -90, 0, 0);
  366. node.rotation = targetRotation;
  367. this.recycleCube(node); // 回收到池中
  368. }
  369. });
  370. this.shufflePool();
  371. // 等待节点移动完成后执行后续逻辑
  372. this.create_node.nodeMoving();
  373. }
  374. //清空槽子
  375. Empty() {
  376. for (let idiom of this.idiom_combine.keys()) {
  377. idiom.state = Cube_State.live;
  378. let posX = ch.util.getRandom(-3, 3);
  379. let posZ = ch.util.getRandom(-3, 3);
  380. tween(idiom.node).to(0.2, { position: new Vec3(posX, this.create_node.node.position.y + 5, posZ) }).call(() => {
  381. idiom.rigidbody.type = RigidBody.Type.DYNAMIC;
  382. }).start();
  383. //idiom.node.position = ;
  384. }
  385. this.idiom_combine.clear();
  386. this.node_isIdiom.fill(false);
  387. gui.get(UI_Idioms).all_light_Hide();
  388. }
  389. AddTime() {
  390. const layout = gui.get(UI_Main).getLayout<Layout_Main>();
  391. layout.time += 120;
  392. }
  393. private shufflePool() {
  394. const poolSize = this.Cube_Pool.size();
  395. const tempArray = [];
  396. // 从池中取出所有节点到临时数组
  397. for (let i = 0; i < poolSize; i++) {
  398. tempArray.push(this.Cube_Pool.get());
  399. }
  400. // 只打乱部分节点(以 30% 为例,可调整比例)
  401. const shuffleCount = Math.ceil(poolSize * 0.3); // 只打乱前 30%
  402. for (let i = 0; i < shuffleCount; i++) {
  403. const randomIndex = Math.floor(Math.random() * poolSize);
  404. [tempArray[i], tempArray[randomIndex]] = [tempArray[randomIndex], tempArray[i]];
  405. }
  406. // 将打乱的节点放回池中
  407. tempArray.forEach(cube => this.Cube_Pool.put(cube));
  408. }
  409. }