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 { Hall } from '../hall/Hall'; import { ch } from '../../ch/ch'; import { UI_Main } from '../ui/UI_Main/UI_Main'; import { gui } from '../../core/ui/ui'; import ch_audio from '../../ch/audio/audio'; const { ccclass, property } = _decorator; @ccclass('CreateIdiom') export class CreateIdiom extends Component { Container: Container_Manager; animationRunning: boolean = false; // 控制是否有动画正在运行 @property([Node]) startPos: Node[] = []; onLoad() { this.Container = find('Container').getComponent(Container_Manager); } update(deltaTime: number) { } nodeMoving() { if (Hall.getInstance().player.get_max_floor() !== 0 && !Hall.getInstance().bornRunning) { Hall.getInstance().bornRunning = true; this.node.setPosition(new Vec3(0, 1.6, -1.5)); this.Container.canTouch = false; let createdNodes = 0; let totalCubes = this.Container.Cube_Pool.size(); let animationStartTime = Date.now(); // 弹簧路径参数 const radius = 3; // 半径保持不变 const totalHeight = 5; // Y轴总上升高度 const totalRotations = totalCubes / 100 + 2; // 总旋转圈数 const centerZ = 1.0; // 圆心Z坐标 const startY = 1.6; // 初始Y坐标 const totalDuration = totalCubes * 30; // 总持续时间3秒 const updatePosition = () => { if (Hall.getInstance().sceneChanging || Hall.getInstance().bornRunning === false) { return; } const currentTime = Date.now() - animationStartTime; const progress = Math.min(currentTime / totalDuration, 1); // 计算应该生成的方块数量 const expectedCreated = Math.floor(progress * totalCubes); // 生成方块 while (createdNodes < expectedCreated && createdNodes < totalCubes) { const s = createdNodes / totalCubes; // 计算弹簧路径坐标 const y = startY + s * totalHeight; const angle = s * totalRotations * 2 * Math.PI; const x = radius * Math.cos(angle); const z = radius * Math.sin(angle) + centerZ; const cube = this.Container.getCube(); if (cube && cube.getComponent(Cube_Infor).state === Cube_State.live) { cube.parent = director.getScene(); cube.setPosition(new Vec3(x, y, z)); cube.setRotationFromEuler(-90, ch.util.getRandomInt(-30, 30), ch.util.getRandomInt(-30, 30)); this.Container.nodeReferences.push(cube); ch_audio.getInstance().playOneShot('sound/dealing',0.3); createdNodes++; }else if(!cube){ break; } console.log(createdNodes); } // 更新节点位置 const s = progress; const currentY = startY + s * totalHeight; const currentAngle = s * totalRotations * 2 * Math.PI; const currentX = radius * Math.cos(currentAngle); const currentZ = radius * Math.sin(currentAngle) + centerZ; this.node.setPosition(new Vec3(currentX, currentY, currentZ)); // 结束判断 if (progress < 1) { requestAnimationFrame(updatePosition); } else { this.Container.canTouch = true; Hall.getInstance().bornRunning = false; gui.get(UI_Main).running = true; this.node.setPosition(new Vec3(0, 1.6, -1.5)); } }; updatePosition(); } else if (Hall.getInstance().player.get_max_floor() == 0) { // 原有初始生成逻辑保持不变 for (let i = 0; i < this.Container.config.length * 2; i++) { let cube = this.Container.getCube(); if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) { let newNode = cube; newNode.parent = director.getScene(); let randomXOffset = Math.random() * 1 - 0.5; newNode.setPosition(this.startPos[i].position.x, this.startPos[i].position.y, this.startPos[i].position.z); this.Container.nodeReferences.push(newNode); if (i > 1) { newNode.getComponent(Cube_Infor).state = Cube_State.wait; } } } this.Container.canTouch = true; } } }