import { _decorator, Component, debug, director, find, instantiate, Layers, Node, NodePool, Prefab, tween, Vec3 } from 'cc'; import { Cube_Infor, Cube_State } from './Cube_Infor'; import { Container_Manager } from './Container_Manager'; import { gui } from '../../core/ui/ui'; import { UI_Main } from '../ui/UI_Main/UI_Main'; const { ccclass, property } = _decorator; @ccclass('CreateIdiom') export class CreateIdiom extends Component { count: number = 0; Container: Container_Manager; onLoad() { this.Container = find('Container').getComponent(Container_Manager); } update(deltaTime: number) { } nodeMoving() { this.node.setPosition(new Vec3(0, 1.6, 0)); let startPos = this.node.position; // 起点,抛物线开始的坐标 let middlePos = new Vec3(this.node.position.x, this.node.position.y + 2, 0); // 中间控制点 let destPos = new Vec3(this.node.position.x, this.node.position.y + 4, 0); // 终点,抛物线上升顶点 // 保持原来贝塞尔曲线和螺旋效果的计算 let twoBezier = (t: number, p1: Vec3, cp: Vec3, p2: Vec3) => { // 贝塞尔曲线计算 let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x; let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y; let z = (1 - t) * (1 - t) * p1.z + 2 * t * (1 - t) * cp.z + t * t * p2.z; // 螺旋运动计算 let angle = t * Math.PI * 6; // 螺旋角度变化,增加圈数 let radius = t * 3.5; // 螺旋半径逐渐增大 let xOffset = Math.cos(angle) * radius; // x 轴偏移 let zOffset = Math.sin(angle) * radius; // z 轴偏移 // 返回最终点,带有螺旋效果 return new Vec3(x + xOffset, y, z + zOffset); }; let createdNodes = 0; // 已生成的节点计数 let animationStopped = false; // 控制动画是否已经停止 let t = 0; // 进度 t,0 为起点,1 为终点 const updatePosition = () => { if (animationStopped) return; // 如果动画已停止,则不继续执行 // 继续使用贝塞尔曲线计算,带螺旋效果 this.node.position = twoBezier(t, startPos, middlePos, destPos); // 根据进度生成新节点 let cube = this.Container.getCube(); if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) { let newNode = cube; newNode.parent = director.getScene(); newNode.setPosition(this.node.position); this.Container.nodeReferences.push(newNode); console.log(cube.getComponent(Cube_Infor).Text); createdNodes++; // 记录生成的方块 } // 如果没有方块,停止动画 if (cube == null && !animationStopped) { animationStopped = true; // 设置动画停止标志 console.log("没有方块,停止动画"); this.node.setPosition(this.node.position); // 防止继续更新位置 } // 获取 Container 中的当前方块数量 let totalCubes = this.Container.idioms.length * 2; // 假设 idioms 存储了所有方块的信息 // 如果所有方块都已生成,停止动画 if (createdNodes >= totalCubes) { animationStopped = true; this.Container.canTouch = true; console.log("所有方块已拿完,停止动画"); } // 更新 t 和重新调用动画 if (t < 1) { t += 0.01; // 每次更新进度 requestAnimationFrame(updatePosition); // 使用 requestAnimationFrame 来继续更新位置 } }; // 启动动画 updatePosition(); } }