123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- import { _decorator, BoxCollider, Component, director, find, instantiate, Node, NodePool, Prefab, Quat, random, RigidBody, sp, tween, Vec3 } from 'cc';
- import ch_util from '../../ch/ch_util';
- import { gui } from '../../core/ui/ui';
- import { Layout_Main } from '../ui/UI_Main/Layout_Main';
- import { UI_Main } from '../ui/UI_Main/UI_Main';
- import { CreateIdiom } from './CreateIdiom';
- import { Cube_Infor, Cube_State } from './Cube_Infor';
- import { GameCtl } from './GameCtl';
- import { ch } from '../../ch/ch';
- import { table_idiom_order } from '../../module_extra/table_ts/table_idiom_order';
- import { table_idiom_unorder_1_3 } from '../../module_extra/table_ts/table_idiom_unorder_1_3';
- import { table_idiom_unorder_2_2 } from '../../module_extra/table_ts/table_idiom_unorder_2_2';
- import { table_idiom_unorder_3_1 } from '../../module_extra/table_ts/table_idiom_unorder_3_1';
- import { table_level } from '../../module_extra/table_ts/table_level';
- import { table_level_2 } from '../../module_extra/table_ts/table_level_2';
- import { GameState, Hall } from '../hall/Hall';
- import { UI_Idioms } from '../ui/UI_Idioms/UI_Idioms';
- const { ccclass, property } = _decorator;
- @ccclass('Container_Manager')
- export class Container_Manager extends Component {
- @property(Node)
- skeleton1: Node = null;
- @property(Node)
- skeleton2: Node = null;
- canTouch: boolean = false;
- @property([Node])
- Lock_node: Node[] = [];
- @property([Prefab])
- prefabs: Prefab[] = [];
- level_config: any = null;//关卡配置
- level2_config: any = null;//第二关配置
- config: any = null;//有规律的成语库
- config_1_3: any = null;//无规律1+3
- config_2_2: any = null;//无规律2+2
- config_3_1: any = null;//无规律3+1
- idioms: any[] = [];//生成的成语
- idioms_Copy: any[] = [];//生成的成语备份
- index: number = 0;
- @property(CreateIdiom)
- create_node: CreateIdiom = null;
- @property([Node])
- nodes: Node[] = [];//位置节点 用于成语放入
- node_isIdiom: boolean[] = new Array(9).fill(false);
- unlock_Num: number = 7;
- is_Show_UI_Lock = false;
- idiom_combine: Map<Cube_Infor, number> = new Map();
- Cube_Pool: NodePool = new NodePool();
- nodeReferences: Node[] = []; // 额外维护的节点引用数组
- count: number = 0;
- time: number = 0;
- private instantiateCube() {
- let num = 60;
- if (this.idioms.length < 30) {
- num = this.idioms.length * 2;
- }
- for (let i = 0; i < num; i++) {
- let idiomIndex = Math.floor(i / 2);
- let isPiece1 = i % 2 === 0;
- let Text_Length = isPiece1
- ? this.idioms[idiomIndex].piece_1_word.length
- : this.idioms[idiomIndex].piece_2_word.length;
- const newCube = instantiate(this.prefabs[Text_Length - 1]);
- newCube.active = true;
- // 按顺序为节点赋值文字内容
- newCube.getComponent(Cube_Infor).Text = isPiece1
- ? this.idioms[idiomIndex].piece_1_word
- : this.idioms[idiomIndex].piece_2_word;
- // console.log("生成第" + i + "个节点:" + newCube.getComponent(Cube_Infor).Text);
- this.Cube_Pool.put(newCube);
- }
- this.index = num;
- if (Hall.getInstance().player.get_max_floor() != 0)
- this.shufflePool();
- }
- public instantiateNewCube() {
- console.log("instantiateNewCube");
- if (this.index < (this.count * 2)) {
- console.log("idiomIndex:" + Math.floor(this.index / 2));
- let idiomIndex = Math.floor(this.index / 2);
- let isPiece1 = this.index % 2 === 0;
- let Text_Length = isPiece1
- ? this.idioms_Copy[idiomIndex].piece_1_word.length
- : this.idioms_Copy[idiomIndex].piece_2_word.length;
- const newCube = instantiate(this.prefabs[Text_Length - 1]);
- newCube.active = true;
- // 按顺序为节点赋值文字内容
- newCube.getComponent(Cube_Infor).Text = isPiece1
- ? this.idioms_Copy[idiomIndex].piece_1_word
- : this.idioms_Copy[idiomIndex].piece_2_word;
- //随机选择已在nodereference中的一个方块position
- newCube.parent = director.getScene();
- 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);
- this.nodeReferences.push(newCube);
- console.log("生成第" + this.index + "个节点:" + newCube.getComponent(Cube_Infor).Text);
- console.log("位置:" + newCube.position)
- this.index = this.index + 1;
- }
- }
- public getCube(): Node {
- // console.log(this.Cube_Pool.size());
- const cube = this.Cube_Pool.get();
- if (cube)
- cube.active = true;
- return cube;
- }
- // 将方块回收到对象池
- public recycleCube(cube: Node): void {
- cube.active = false; // 将方块设置为非激活状态
- this.Cube_Pool.put(cube);
- }
- //清空对象池
- public clearCubePool() {
- this.Cube_Pool.clear();
- }
- //合成规则导入
- start() {
- this.config = table_idiom_order.getList();
- this.config_1_3 = table_idiom_unorder_1_3.getList();
- this.config_2_2 = table_idiom_unorder_2_2.getList();
- this.config_3_1 = table_idiom_unorder_3_1.getList();
- console.log(this.config.length);
- console.log(this.config_1_3.length);
- console.log(this.config_2_2.length);
- console.log(this.config_3_1.length);
- this.level_config = table_level.getList();
- this.level2_config = table_level_2.getList();
- // if (this.level_config.length === 5) {
- // console.log("关卡配置导入成功");
- // }
- this.level_idioms();
- }
- update(deltaTime: number) {
- }
- checkIdiom_Combine(matchedcube2: Cube_Infor, outMatchedCubes: Cube_Infor[]): boolean {
- if (this.idiom_combine.size < 2) {
- return false; // 至少需要两个方块
- }
- for (let cube of this.idiom_combine.keys()) {
- // 遍历 idioms 列表,检查是否匹配成语
- const matchedIdiom = this.idioms.find(
- idiom =>
- idiom.piece_1_word === cube.Text && idiom.piece_2_word === matchedcube2.Text
- );
- if (matchedIdiom) {
- // 匹配成功
- outMatchedCubes.push(cube, matchedcube2);
- this.nodeReferences = this.nodeReferences.filter((el) => el !== matchedcube2.node && el !== cube.node);
- console.log("成功拼成成语: " + matchedIdiom.piece_1_word + matchedIdiom.piece_2_word);
- return true;
- }
- // 再检查逆序组合是否匹配
- const reverseMatchedIdiom = this.idioms.find(
- idiom =>
- idiom.piece_1_word === matchedcube2.Text && idiom.piece_2_word === cube.Text
- );
- if (reverseMatchedIdiom) {
- // 匹配成功
- outMatchedCubes.push(matchedcube2, cube);
- this.nodeReferences = this.nodeReferences.filter((el) => el !== matchedcube2.node && el !== cube.node);
- console.log(
- "成功拼成成语: " +
- reverseMatchedIdiom.piece_1_word +
- reverseMatchedIdiom.piece_2_word
- );
- return true;
- }
- }
- return false; // 没有匹配到成语
- }
- async level_idioms() {
- this.Lock_node.forEach(node => node.active = true);
- this.unlock_Num = 7;
- this.is_Show_UI_Lock = false;
- this.canTouch = false;
- this.index = 0;
- let level = Hall.getInstance().player.get_max_floor();
- Hall.getInstance().gameState = GameState.gameing;
- if (level > 0) {
- Hall.getInstance().firstEnter = false;
- }
- this.clearLevelData();
- this.idioms = [];
- this.node_isIdiom = new Array(9).fill(false);
- this.idiom_combine = new Map();
- // 获取当前关卡的成语配置
- if (level === 0) {
- this.setupLevel1();
- } else if (level === 1) {
- this.setupLevel2();
- } else {
- this.setupLevelDefault(level);
- }
- this.idioms_Copy = [...this.idioms];
- this.count = this.level_config[level].total;
- this.time = this.level_config[level].time;
- await this.instantiateCube();
- await gui.show(UI_Idioms);
- this.create_node.nodeMoving();
- gui.show(UI_Main);
- }
- clearLevelData() {
- for (let node of this.nodeReferences) {
- node.destroy();
- }
- this.nodeReferences = [];
- this.clearCubePool();
- }
- setupLevel1() {
- const idiom_type_2 = this.level_config[0].idiom_type_2.split("_");
- console.log("idiom_type_2:", idiom_type_2);
- idiom_type_2.forEach(rule => {
- const filteredIdioms = this.config.filter(item => item.piece_2_word === rule);
- console.log(`筛选2 ${rule} 后的成语:`, filteredIdioms);
- this.idioms.push(...filteredIdioms);
- });
- console.log("最终选中的成语:", this.idioms);
- }
- setupLevel2() {
- for (let i = 0; i < 30; i++) {
- this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config));
- this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config_3_1));
- }
- for (let i = 30; i < 40; i++) {
- this.idioms.push(...this.filterIdioms(this.level2_config[i].idiom, this.config_2_2));
- }
- console.log("最终选中的成语:", this.idioms);
- }
- setupLevelDefault(level) {
- const dif = this.level_config[level].different_grade_level.split("_");
- const grade = dif[0];
- const idiom_type_1 = this.parseIdiomType(this.level_config[level].idiom_type_1);
- const idiom_type_2 = this.parseIdiomType(this.level_config[level].idiom_type_2);
- const count = this.level_config[level].count / (idiom_type_1.length + idiom_type_2.length);
- console.log("count:", count);
- let selectedIdioms = {};
- // 筛选成语并随机选择
- this.selectIdiomsByRules(idiom_type_1, grade, count, selectedIdioms, 'piece_1_word');
- this.selectIdiomsByRules(idiom_type_2, grade, count, selectedIdioms, 'piece_2_word');
- // 合并结果
- // 使用 Object.keys 来遍历 selectedIdioms
- for (let rule in selectedIdioms) {
- this.idioms.push(...selectedIdioms[rule]);
- }
- // 随机选择难度成语
- this.selectIdiomsByDifficulty(level, 'easy_1_3', this.config_1_3);
- this.selectIdiomsByDifficulty(level, 'hard_1_3', this.config_1_3);
- this.selectIdiomsByDifficulty(level, 'easy_2_2', this.config_2_2);
- this.selectIdiomsByDifficulty(level, 'hard_2_2', this.config_2_2);
- this.selectIdiomsByDifficulty(level, 'easy_3_1', this.config_3_1);
- this.selectIdiomsByDifficulty(level, 'hard_3_1', this.config_3_1);
- console.log("最终选中的成语:", this.idioms);
- }
- parseIdiomType(idiomType) {
- return idiomType.split("_").filter(item => item !== "");
- }
- selectIdiomsByRules(idiomType, grade, count, selectedIdioms, ruleKey) {
- idiomType.forEach(rule => {
- const filteredIdioms = this.config.filter(item => item[ruleKey] === rule && item.difficulty === grade);
- console.log(`筛选 ${rule} 后的成语:`, filteredIdioms);
- if (filteredIdioms.length < count) {
- console.error(`规律 ${rule} 的成语数量不足,仅有 ${filteredIdioms.length} 个`);
- } else {
- const selected = this.randomSelectIdioms(filteredIdioms, count);
- selectedIdioms[rule] = selected;
- }
- });
- }
- randomSelectIdioms(filteredIdioms, count) {
- let selected = [];
- for (let i = 0; i < count; i++) {
- const rand = ch_util.getRandomInt(0, filteredIdioms.length - 1);
- selected.push(filteredIdioms.splice(rand, 1)[0]);
- }
- return selected;
- }
- selectIdiomsByDifficulty(level, configKey, config) {
- if (this.level_config[level][configKey] > 0) {
- const filteredIdioms = config.filter(item => item.difficulty === "easy");
- for (let i = 0; i < this.level_config[level][configKey]; i++) {
- const rand = ch_util.getRandomInt(0, filteredIdioms.length - 1);
- const selectedIdiom = filteredIdioms.splice(rand, 1)[0];
- this.idioms.push(selectedIdiom);
- }
- }
- }
- filterIdioms(idiom, config) {
- return config.filter(item => item.idiom === idiom);
- }
- // 精简后的消除一组函数
- eliminate() {
- // 获取槽中的方块
- let cube = [...this.idiom_combine].find(([key, value]) => value === 0)?.[0];
- // 如果槽中有方块
- if (cube) {
- const originalReferences = [...this.nodeReferences];
- for (const element of originalReferences) {
- const elementCubeInfo = element.getComponent(Cube_Infor);
- const elementText = elementCubeInfo.Text;
- const cubeText = cube.Text;
- if ((this.idioms.some(c => c.idiom === elementText + cubeText))&& elementCubeInfo !== cube) {
- // 执行消除操作
- this.processElimination(element, cube);
- break;
- }
- else if (this.idioms.some(c => c.idiom === cubeText + elementText) && elementCubeInfo !== cube) {
- this.processElimination(cube, element);
- break;
- }
- }
- } else {
- let flag = false;
- const originalReferences = [...this.nodeReferences];
- for (const element1 of originalReferences) {
- for (const element2 of originalReferences) {
- const element1Text = element1.getComponent(Cube_Infor).Text;
- const element2Text = element2.getComponent(Cube_Infor).Text;
- if (this.idioms.some(c => c.idiom === element1Text + element2Text)) {
- // 执行消除操作
- this.processElimination(element1, element2);
- flag = true;
- break;
- }
- else if (this.idioms.some(c => c.idiom === element2Text + element1Text)) {
- // 执行消除操作
- this.processElimination(element2, element1);
- flag = true;
- break;
- }
- if (flag) {
- break;
- }
- }
- if (flag) {
- for (let i = 0; i < 2; i++) this.instantiateNewCube();
- break;
- }
- }
- if (!flag) {
- console.log("没有可消除");
- }
- }
- }
- // 消除操作
- processElimination(element1, element2) {
- this.nodeReferences = this.nodeReferences.filter(el => el !== element1 && el !== element2);
- const element1CubeInfo = element1.getComponent(Cube_Infor);
- const element2CubeInfo = element2.getComponent(Cube_Infor);
- [element1, element2].forEach(element => {
- element.getComponent(Cube_Infor).state = Cube_State.wait;
- element.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC;
- element.getComponent(BoxCollider).enabled = false;
- let targetRotation = new Quat();
- Quat.fromEuler(targetRotation, -90, 0, 0);
- element.rotation = targetRotation;
- });
- // 执行动画
- GameCtl.instance.combine_ani(element1CubeInfo, element2CubeInfo);
- }
- async shuffle() {
- // 回收所有非槽内的活跃节点
- this.nodeReferences.forEach(node => {
- const cubeInfo = node.getComponent(Cube_Infor);
- if (cubeInfo.state === Cube_State.live) {
- let targetRotation = new Quat();
- Quat.fromEuler(targetRotation, -90, 0, 0);
- node.rotation = targetRotation;
- this.recycleCube(node); // 回收到池中
- }
- });
- this.shufflePool();
- // 等待节点移动完成后执行后续逻辑
- this.create_node.nodeMoving();
- }
- //清空槽子
- Empty() {
- for (let idiom of this.idiom_combine.keys()) {
- idiom.state = Cube_State.live;
- let posX = ch.util.getRandom(-3, 3);
- let posZ = ch.util.getRandom(-3, 3);
- tween(idiom.node).to(0.2, { position: new Vec3(posX, this.create_node.node.position.y + 5, posZ) }).call(() => {
- idiom.rigidbody.type = RigidBody.Type.DYNAMIC;
- }).start();
- //idiom.node.position = ;
- }
- this.idiom_combine.clear();
- this.node_isIdiom.fill(false);
- gui.get(UI_Idioms).all_light_Hide();
- }
- AddTime() {
- const layout = gui.get(UI_Main).getLayout<Layout_Main>();
- layout.time += 120;
- }
- private shufflePool() {
- const poolSize = this.Cube_Pool.size();
- const tempArray = [];
- // 从池中取出所有节点到临时数组
- for (let i = 0; i < poolSize; i++) {
- tempArray.push(this.Cube_Pool.get());
- }
- // 只打乱部分节点(以 30% 为例,可调整比例)
- const shuffleCount = Math.ceil(poolSize * 0.3); // 只打乱前 30%
- for (let i = 0; i < shuffleCount; i++) {
- const randomIndex = Math.floor(Math.random() * poolSize);
- [tempArray[i], tempArray[randomIndex]] = [tempArray[randomIndex], tempArray[i]];
- }
- // 将打乱的节点放回池中
- tempArray.forEach(cube => this.Cube_Pool.put(cube));
- }
- }
|