CreateIdiom.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { _decorator, Component, debug, director, find, instantiate, Layers, Node, NodePool, Prefab, tween, Vec3 } from 'cc';
  2. import { Cube_Infor, Cube_State } from './Cube_Infor';
  3. import { Container_Manager } from './Container_Manager';
  4. import { Hall } from '../hall/Hall';
  5. import { ch } from '../../ch/ch';
  6. import { UI_Main } from '../ui/UI_Main/UI_Main';
  7. import { gui } from '../../core/ui/ui';
  8. import ch_audio from '../../ch/audio/audio';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('CreateIdiom')
  11. export class CreateIdiom extends Component {
  12. Container: Container_Manager;
  13. animationRunning: boolean = false; // 控制是否有动画正在运行
  14. @property([Node])
  15. startPos: Node[] = [];
  16. onLoad() {
  17. this.Container = find('Container').getComponent(Container_Manager);
  18. }
  19. update(deltaTime: number) {
  20. }
  21. nodeMoving() {
  22. if (Hall.getInstance().player.get_max_floor() !== 0 && !Hall.getInstance().bornRunning) {
  23. Hall.getInstance().bornRunning = true;
  24. this.node.setPosition(new Vec3(0, 1.6, -1.5));
  25. this.Container.canTouch = false;
  26. let createdNodes = 0;
  27. let totalCubes = this.Container.Cube_Pool.size();
  28. let animationStartTime = Date.now();
  29. // 弹簧路径参数
  30. const radius = 3; // 半径保持不变
  31. const totalHeight = 5; // Y轴总上升高度
  32. const totalRotations = totalCubes / 100 + 2; // 总旋转圈数
  33. const centerZ = 1.0; // 圆心Z坐标
  34. const startY = 1.6; // 初始Y坐标
  35. const totalDuration = totalCubes * 30; // 总持续时间3秒
  36. const updatePosition = () => {
  37. if (Hall.getInstance().sceneChanging || Hall.getInstance().bornRunning === false) {
  38. return;
  39. }
  40. const currentTime = Date.now() - animationStartTime;
  41. const progress = Math.min(currentTime / totalDuration, 1);
  42. // 计算应该生成的方块数量
  43. const expectedCreated = Math.floor(progress * totalCubes);
  44. // 生成方块
  45. while (createdNodes < expectedCreated && createdNodes < totalCubes) {
  46. const s = createdNodes / totalCubes;
  47. // 计算弹簧路径坐标
  48. const y = startY + s * totalHeight;
  49. const angle = s * totalRotations * 2 * Math.PI;
  50. const x = radius * Math.cos(angle);
  51. const z = radius * Math.sin(angle) + centerZ;
  52. const cube = this.Container.getCube();
  53. if (cube && cube.getComponent(Cube_Infor).state === Cube_State.live) {
  54. cube.parent = director.getScene();
  55. cube.setPosition(new Vec3(x, y, z));
  56. cube.setRotationFromEuler(-90, ch.util.getRandomInt(-30, 30), ch.util.getRandomInt(-30, 30));
  57. this.Container.nodeReferences.push(cube);
  58. ch_audio.getInstance().playOneShot('sound/dealing',0.3);
  59. createdNodes++;
  60. }else if(!cube){
  61. break;
  62. }
  63. console.log(createdNodes);
  64. }
  65. // 更新节点位置
  66. const s = progress;
  67. const currentY = startY + s * totalHeight;
  68. const currentAngle = s * totalRotations * 2 * Math.PI;
  69. const currentX = radius * Math.cos(currentAngle);
  70. const currentZ = radius * Math.sin(currentAngle) + centerZ;
  71. this.node.setPosition(new Vec3(currentX, currentY, currentZ));
  72. // 结束判断
  73. if (progress < 1) {
  74. requestAnimationFrame(updatePosition);
  75. } else {
  76. this.Container.canTouch = true;
  77. Hall.getInstance().bornRunning = false;
  78. gui.get(UI_Main).running = true;
  79. this.node.setPosition(new Vec3(0, 1.6, -1.5));
  80. }
  81. };
  82. updatePosition();
  83. } else if (Hall.getInstance().player.get_max_floor() == 0) {
  84. // 原有初始生成逻辑保持不变
  85. for (let i = 0; i < this.Container.config.length * 2; i++) {
  86. let cube = this.Container.getCube();
  87. if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) {
  88. let newNode = cube;
  89. newNode.parent = director.getScene();
  90. let randomXOffset = Math.random() * 1 - 0.5;
  91. newNode.setPosition(this.startPos[i].position.x, this.startPos[i].position.y, this.startPos[i].position.z);
  92. this.Container.nodeReferences.push(newNode);
  93. if (i > 1) {
  94. newNode.getComponent(Cube_Infor).state = Cube_State.wait;
  95. }
  96. }
  97. }
  98. this.Container.canTouch = true;
  99. }
  100. }
  101. }