import { _decorator, BoxCollider, Component, instantiate, Node, NodePool, Prefab, Quat, random, RigidBody } from 'cc'; import { TableUtil } from '../module_extra/table_ts/TableUtil'; import { table_idiom } from '../module_extra/table_ts/table_idiom'; import TableLoadUtil from '../core/util/TableLoadUtil'; import { ModuleDef } from './ModuleDef'; import ch_util from '../ch/ch_util'; import { CreateIdiom } from './CreateIdiom'; import { Cube_Infor, Cube_State } from './Cube_Infor'; import { UI_Idioms } from '../module_game/ui/UI_Idioms/UI_Idioms'; import { gui } from '../core/ui/ui'; import { GameCtl } from './GameCtl'; const { ccclass, property } = _decorator; @ccclass('Container_Manager') export class Container_Manager extends Component { static instance: Container_Manager = null; @property(Prefab) prefab: Prefab = null; config: any = null;//成语库 idioms: any[] = [];//生成的成语 index: number[] = [];//生成的成语角标,避免生成重复成语 @property(CreateIdiom) create_node: CreateIdiom = null; @property([Node]) nodes: Node[] = [];//位置节点 用于成语放入 node_isIdiom: boolean[] = new Array(10).fill(false); idiom_combine: Map = new Map(); Cube_Pool: NodePool = new NodePool(); nodeReferences: Node[] = []; // 额外维护的节点引用数组 private instantiateCube() { for (let i = 0; i < 10; i++) { const newCube = instantiate(this.prefab); newCube.active = true; // 初始时设置为非激活状态 // 按顺序为节点赋值文字内容 let idiomIndex = Math.floor(i / 2); let isPiece1 = i % 2 === 0; 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); } } public getCube(): Node { 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); } //合成规则导入 start() { if (!Container_Manager.instance) { Container_Manager.instance = this; } this.config = table_idiom.getList(); if (this.config.length === 95) { 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() { // 筛选满足条件的元素 let validConfig = this.config.filter(item => (item.idiom_type === "一" || item.idiom_type === "二") && // 可以加多个条件 (item.piece_1_word === "一" || item.piece_2_word === "二") // 可以加多个筛选条件 ); if (validConfig.length < 5) { console.error("满足条件的成语数量不足 5 个"); } else { // 从筛选结果中随机选择 for (let i = 0; i < 5;) { let rand = ch_util.getRandomInt(0, validConfig.length - 1); // 使用 indexOf 检查是否已存在 if (this.index.indexOf(rand) === -1) { this.index[i] = rand; this.idioms[i] = validConfig[rand]; i++; } } } await gui.show(UI_Idioms); await this.instantiateCube(); await this.create_node.nodeMoving(); } //消除一组 eliminate() { //先判断槽内是否有方块,如果有,匹配槽内最前面一个与散落方块中的 let cube; for (const [key, value] of this.idiom_combine) { if (value === 0) { cube = key; break; } } //槽中有方块 if (cube) { const originalReferences = [...this.nodeReferences]; for (const element of originalReferences) { if (this.idioms.find(c => c.idiom == element.getComponent(Cube_Infor).Text + cube.Text) && element.getComponent(Cube_Infor) !== cube) { console.log(element.getComponent(Cube_Infor).Text); 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; // 使用filter过滤掉当前元素 GameCtl.instance.combine_ani(element.getComponent(Cube_Infor), cube); this.nodeReferences = this.nodeReferences.filter((el) => el !== element && el !== cube.node); break; } else if (this.idioms.find(c => c.idiom == cube.Text + element.getComponent(Cube_Infor).Text) && element.getComponent(Cube_Infor) !== cube) { console.log(element.getComponent(Cube_Infor).Text); 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(cube, element.getComponent(Cube_Infor)); this.nodeReferences = this.nodeReferences.filter((el) => el !== element && el !== cube.node); break; } } } //槽中没有方块 else{ let flag:boolean=false; const originalReferences1 = [...this.nodeReferences]; const originalReferences2 = [...this.nodeReferences]; for (const element1 of originalReferences1) { for (const element2 of originalReferences2) { if (this.idioms.find(c => c.idiom == element1.getComponent(Cube_Infor).Text + element2.getComponent(Cube_Infor).Text)) { element1.getComponent(Cube_Infor).state = Cube_State.wait; element1.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC; let targetRotation = new Quat(); Quat.fromEuler(targetRotation, -90, 0, 0); element1.rotation = targetRotation; element2.getComponent(Cube_Infor).state = Cube_State.wait; element2.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC; element2.getComponent(BoxCollider).enabled = false; element2.rotation = targetRotation; // 使用filter过滤掉当前元素 GameCtl.instance.combine_ani(element1.getComponent(Cube_Infor), element2.getComponent(Cube_Infor)); this.nodeReferences = this.nodeReferences.filter((el) => el !== element1 && el !== element2); flag=true; break; } if (this.idioms.find(c => c.idiom == element2.getComponent(Cube_Infor).Text + element1.getComponent(Cube_Infor).Text)) { element1.getComponent(Cube_Infor).state = Cube_State.wait; element1.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC; let targetRotation = new Quat(); Quat.fromEuler(targetRotation, -90, 0, 0); element1.rotation = targetRotation; element2.getComponent(Cube_Infor).state = Cube_State.wait; element2.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC; element2.getComponent(BoxCollider).enabled = false; element2.rotation = targetRotation; // 使用filter过滤掉当前元素 GameCtl.instance.combine_ani(element2.getComponent(Cube_Infor), element1.getComponent(Cube_Infor)); this.nodeReferences = this.nodeReferences.filter((el) => el !== element1 && el !== element2); flag=true; break; } } if(flag===true) { break; } } } } async shuffle() { // 回收所有非槽内的活跃节点 this.nodeReferences.forEach(node => { const cubeInfo = node.getComponent(Cube_Infor); if (cubeInfo.state === Cube_State.live) { this.recycleCube(node); // 回收到池中 } }); // 等待节点移动完成后执行后续逻辑 await this.create_node.nodeMoving(); } //清空槽子 Empty() { for (let idiom of this.idiom_combine.keys()) { idiom.rigidbody.type = RigidBody.Type.DYNAMIC; idiom.state = Cube_State.live; idiom.node.position = this.create_node.node.position; } this.idiom_combine.clear(); this.node_isIdiom.fill(false); gui.get(UI_Idioms).all_light_Hide(); } }