123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- import { _decorator, BoxCollider, Camera, Component, director, EventTouch, find, Game, geometry, Layers, Node, PhysicsSystem, Quat, RigidBody, Size, tween, UITransform, v3, Vec3 } from 'cc';
- import { Cube_Infor, Cube_State } from './Cube_Infor';
- import { Container_Manager } from './Container_Manager';
- import { UI_Idioms } from '../module_game/ui/UI_Idioms/UI_Idioms';
- import { gui } from '../core/ui/ui';
- const { ccclass, property } = _decorator;
- @ccclass('GameCtl')
- export class GameCtl extends Component {
- static instance: GameCtl = null;
- @property(Camera)
- camera: Camera = null;
- @property([Node])
- Ani: Node[] = [];
- canTouch: boolean = true;
- onLoad() {
- GameCtl.instance = this;
- console.log(find('Ani'));
- console.log('Camera value in onLoad:', this.camera);
- this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
- this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
- this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
- }
- update(deltaTime: number) {
- }
- onTouchStart(event: EventTouch) {
- }
- onTouchMove(event: EventTouch) {
- }
- onTouchEnd(event: EventTouch) {
- if (this.canTouch) {
- this.shootRay(event);
- }
- }
- //发射射线检测判断物体是否可消除
- shootRay(event: EventTouch) {
- if (!this.camera) return;
- let ray = new geometry.Ray();
- this.camera.screenPointToRay(event.getLocationX(), event.getLocationY(), ray);
- const index = Layers.nameToLayer('Cube');
- const cubeMask = 2 << index;
- const maxDistance = 100000;
- const queryTrigger = false;
- if (PhysicsSystem.instance.raycastClosest(ray, cubeMask, maxDistance, queryTrigger)) {
- const raycastClosestResult = PhysicsSystem.instance.raycastClosestResult;
- const hitPoint = raycastClosestResult.hitPoint;
- const hitNormal = raycastClosestResult.hitNormal;
- const collider = raycastClosestResult.collider;
- const distance = raycastClosestResult.distance;
- console.log(collider.node.name);
- //当前其余点击无效
- if (collider.node.getComponent(Cube_Infor).state === Cube_State.live) {
- this.entryContainer(collider.node);
- }
- }
- console.log('发射了射线');
- }
- entryContainer(node: Node) {
- // 判断容器剩余容量
- let startIndex = -1;
- let targetPos = new Vec3();
- let txt_length = node.getComponent(Cube_Infor).Text.length;
- console.log(txt_length);
- // 判断字长 并 判断是否还有空间可放置
- switch (txt_length) {
- case 1:
- for (let i = 0; i < Container_Manager.instance.node_isIdiom.length; i++) {
- if (Container_Manager.instance.node_isIdiom[i] === false) {
- startIndex = i; // 找到连续的三个 false,记录起始位置
- break; // 找到第一个符合条件的位置后停止
- }
- }
- if (startIndex !== -1) { // 如果找到了一个值为 false 的元素
- targetPos = Container_Manager.instance.nodes[startIndex].getWorldPosition().clone();
- Container_Manager.instance.node_isIdiom[startIndex] = true;
- console.log(targetPos);
- } else {
- console.log("没有空间了");
- }
- break;
- case 2: {
- for (let i = 0; i < Container_Manager.instance.node_isIdiom.length - 2; i++) {
- if (Container_Manager.instance.node_isIdiom[i] === false &&
- Container_Manager.instance.node_isIdiom[i + 1] === false) {
- startIndex = i; // 找到连续的两个 false,记录起始位置
- break; // 找到第一个符合条件的位置后停止
- }
- }
- if (startIndex !== -1) {
- console.log("找到连续的两个 false,起始索引是:", startIndex);
- // 可以在此使用 startIndex 进行后续操作,例如:
- let pos1 = Container_Manager.instance.nodes[startIndex].getWorldPosition();
- let pos2 = Container_Manager.instance.nodes[startIndex + 1].getWorldPosition();
- Container_Manager.instance.node_isIdiom[startIndex] = true;
- Container_Manager.instance.node_isIdiom[startIndex + 1] = true;
- targetPos.set(
- (pos1.x + pos2.x) / 2,
- (pos1.y + pos2.y) / 2,
- (pos1.z + pos2.z) / 2
- );
- console.log(targetPos);
- }
- break;
- }
- case 3: {
- for (let i = 0; i < Container_Manager.instance.node_isIdiom.length - 2; i++) {
- if (Container_Manager.instance.node_isIdiom[i] === false &&
- Container_Manager.instance.node_isIdiom[i + 1] === false &&
- Container_Manager.instance.node_isIdiom[i + 2] === false) {
- startIndex = i; // 找到连续的三个 false,记录起始位置
- break; // 找到第一个符合条件的位置后停止
- }
- }
- if (startIndex !== -1) {
- // 找到连续三个 false,startIndex 即为最前面的索引
- console.log("找到连续的三个 false,起始索引是:", startIndex);
- // 可以在此使用 startIndex 进行后续操作,例如:
- let pos1 = Container_Manager.instance.nodes[startIndex].getWorldPosition();
- let pos2 = Container_Manager.instance.nodes[startIndex + 1].getWorldPosition();
- let pos3 = Container_Manager.instance.nodes[startIndex + 2].getWorldPosition();
- Container_Manager.instance.node_isIdiom[startIndex] = true;
- Container_Manager.instance.node_isIdiom[startIndex + 1] = true;
- Container_Manager.instance.node_isIdiom[startIndex + 2] = true;
- targetPos.set(
- (pos1.x + pos2.x + pos3.x) / 3,
- (pos1.y + pos2.y + pos3.y) / 3,
- (pos1.z + pos2.z + pos3.z) / 3
- );
- console.log(targetPos);
- } else {
- console.log("没有找到连续的三个 false");
- }
- break;
- }
- default:
- return;
- }
- if (startIndex !== -1) {
- // node.getComponent(Cube_Infor).lock = true;
- node.getComponent(Cube_Infor).state = Cube_State.wait;
- node.getComponent(Cube_Infor).rigidbody.type = RigidBody.Type.STATIC; // 禁用重力
- let targetRotation = new Quat();
- Quat.fromEuler(targetRotation, -90, 0, 0);
- this.canTouch = false;
- tween(node)
- .to(0.5, { position: new Vec3(targetPos.x, targetPos.y, targetPos.z + 0.4), rotation: targetRotation })
- .call(() => {
- Container_Manager.instance.idiom_combine.set(node.getComponent(Cube_Infor), startIndex);
- // 执行判断成语合成逻辑
- let matchedCubes: Cube_Infor[] = [];
- let flag = Container_Manager.instance.checkIdiom_Combine(node.getComponent(Cube_Infor), matchedCubes);
- if (flag) {
- // 执行合成动画
- console.log("匹配的成语方块:", matchedCubes);
- this.combine_ani(matchedCubes[0], matchedCubes[1]);
- } else {
- //高亮
- gui.get(UI_Idioms).light_Show(node.getComponent(Cube_Infor));
- this.canTouch = true;
- }
- })
- .start();
- }
- }
- //合成动画
- combine_ani(cube1: Cube_Infor, cube2: Cube_Infor) {
- // 创建第一个 tween 动画
- const tween1 = tween(cube1.node)
- .to(0.5, { position: new Vec3(this.Ani[0].position.x, this.Ani[0].position.y, this.Ani[0].position.z + 0.4) })
- .call(() => {
- for (let i = Container_Manager.instance.idiom_combine.get(cube1); i < Container_Manager.instance.idiom_combine.get(cube1) + cube1.Text.length; i++) {
- Container_Manager.instance.node_isIdiom[i] = false;
- }
- Container_Manager.instance.idiom_combine.delete(cube1);
- setTimeout(() => {
- cube1.state=Cube_State.dead;
- Container_Manager.instance.recycleCube(cube1.node);
- }, 1000.0);
- });
- // 创建第二个 tween 动画
- const tween2 = tween(cube2.node)
- .to(0.5, { position: new Vec3(this.Ani[1].position.x, this.Ani[1].position.y, this.Ani[1].position.z + 0.4) })
- .call(() => {
- for (let i = Container_Manager.instance.idiom_combine.get(cube2); i < Container_Manager.instance.idiom_combine.get(cube2) + cube2.Text.length; i++) {
- Container_Manager.instance.node_isIdiom[i] = false;
- }
- Container_Manager.instance.idiom_combine.delete(cube2);
- setTimeout(() => {
- cube2.state=Cube_State.dead;
- //在nodeReferences中修改该节点属性
- // Container_Manager.instance.nodeReferences.forEach((node, index) => {
- // if (node === cube2.node) {
- // Container_Manager.instance.nodeReferences[index].getComponent(Cube_Infor).state = Cube_State.dead;
- // }
- // });
- Container_Manager.instance.recycleCube(cube2.node);
- }, 1000.0);
- });
- // 使用 tween 的并行组合功能
- tween(cube1.node)
- .parallel(tween1, tween2) // 并行执行 tween1 和 tween2
- .call(() => {
- // 取消相关字的高亮显示
- gui.get(UI_Idioms).light_Hide(cube1, cube2);
- this.adjustContainer();
- // 检测容器中哪些字需要高亮显示
- console.log(Container_Manager.instance.node_isIdiom); // 在所有动画结束后执行
- Container_Manager.instance.idioms = Container_Manager.instance.idioms.filter(c => c.idiom !== cube1.Text + cube2.Text);
- this.canTouch = true;
- })
- .start();
- }
- adjustContainer() {
- const container = Container_Manager.instance;
- // 新的 node_isIdiom 状态数组
- const newNodeIsIdiom = Array(container.node_isIdiom.length).fill(false);
- // 新的 idiom_combine 映射表
- const newIdiomCombine = new Map<Cube_Infor, number>();
- // 遍历 idiom_combine,重新计算位置
- for (const [cube, startIndex] of container.idiom_combine.entries()) {
- const txtLength = cube.Text.length; // 获取字长
- let targetIndex = -1; // 目标位置起始索引
- let targetPos = new Vec3();
- switch (txtLength) {
- case 1: {
- // 找到一个空位
- for (let i = 0; i < container.node_isIdiom.length; i++) {
- if (!newNodeIsIdiom[i]) {
- targetIndex = i;
- break;
- }
- }
- if (targetIndex !== -1) {
- targetPos = container.nodes[targetIndex].getWorldPosition().clone();
- newNodeIsIdiom[targetIndex] = true; // 占用位置
- } else {
- console.log("没有空位容纳单字 Cube_Infor");
- }
- break;
- }
- case 2: {
- // 找到连续两个空位
- for (let i = 0; i < container.node_isIdiom.length - 1; i++) {
- if (!newNodeIsIdiom[i] && !newNodeIsIdiom[i + 1]) {
- targetIndex = i;
- break;
- }
- }
- if (targetIndex !== -1) {
- const pos1 = container.nodes[targetIndex].getWorldPosition();
- const pos2 = container.nodes[targetIndex + 1].getWorldPosition();
- targetPos.set(
- (pos1.x + pos2.x) / 2,
- (pos1.y + pos2.y) / 2,
- (pos1.z + pos2.z) / 2
- );
- newNodeIsIdiom[targetIndex] = true;
- newNodeIsIdiom[targetIndex + 1] = true;
- } else {
- console.log("没有连续两个空位容纳双字 Cube_Infor");
- }
- break;
- }
- case 3: {
- // 找到连续三个空位
- for (let i = 0; i < container.node_isIdiom.length - 2; i++) {
- if (
- !newNodeIsIdiom[i] &&
- !newNodeIsIdiom[i + 1] &&
- !newNodeIsIdiom[i + 2]
- ) {
- targetIndex = i;
- break;
- }
- }
- if (targetIndex !== -1) {
- const pos1 = container.nodes[targetIndex].getWorldPosition();
- const pos2 = container.nodes[targetIndex + 1].getWorldPosition();
- const pos3 = container.nodes[targetIndex + 2].getWorldPosition();
- targetPos.set(
- (pos1.x + pos2.x + pos3.x) / 3,
- (pos1.y + pos2.y + pos3.y) / 3,
- (pos1.z + pos2.z + pos3.z) / 3
- );
- newNodeIsIdiom[targetIndex] = true;
- newNodeIsIdiom[targetIndex + 1] = true;
- newNodeIsIdiom[targetIndex + 2] = true;
- } else {
- console.log("没有连续三个空位容纳三字 Cube_Infor");
- }
- break;
- }
- default:
- return;
- }
- // 移动 Cube_Infor 到目标位置
- if (targetIndex !== -1) {
- tween(cube.node)
- .to(0.3, { position: new Vec3(targetPos.x, targetPos.y, targetPos.z + 0.4) })
- .start();
- newIdiomCombine.set(cube, targetIndex); // 更新新映射
- }
- }
- // 更新 container 状态
- container.node_isIdiom = newNodeIsIdiom;
- container.idiom_combine = newIdiomCombine;
- console.log("调整后的容器状态:", container.node_isIdiom);
- console.log("更新后的 idiom_combine:", [...container.idiom_combine.entries()]);
- }
- }
|