CreateIdiom.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { gui } from '../../core/ui/ui';
  5. import { UI_Main } from '../ui/UI_Main/UI_Main';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('CreateIdiom')
  8. export class CreateIdiom extends Component {
  9. count: number = 0;
  10. Container: Container_Manager;
  11. onLoad() {
  12. this.Container = find('Container').getComponent(Container_Manager);
  13. }
  14. update(deltaTime: number) {
  15. }
  16. nodeMoving() {
  17. this.node.setPosition(new Vec3(0, 1.6, 0));
  18. let startPos = this.node.position; // 起点,抛物线开始的坐标
  19. let middlePos = new Vec3(this.node.position.x, this.node.position.y + 2, 0); // 中间控制点
  20. let destPos = new Vec3(this.node.position.x, this.node.position.y + 4, 0); // 终点,抛物线上升顶点
  21. // 保持原来贝塞尔曲线和螺旋效果的计算
  22. let twoBezier = (t: number, p1: Vec3, cp: Vec3, p2: Vec3) => {
  23. // 贝塞尔曲线计算
  24. let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
  25. let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
  26. let z = (1 - t) * (1 - t) * p1.z + 2 * t * (1 - t) * cp.z + t * t * p2.z;
  27. // 螺旋运动计算
  28. let angle = t * Math.PI * 6; // 螺旋角度变化,增加圈数
  29. let radius = t * 3.5; // 螺旋半径逐渐增大
  30. let xOffset = Math.cos(angle) * radius; // x 轴偏移
  31. let zOffset = Math.sin(angle) * radius; // z 轴偏移
  32. // 返回最终点,带有螺旋效果
  33. return new Vec3(x + xOffset, y, z + zOffset);
  34. };
  35. let createdNodes = 0; // 已生成的节点计数
  36. let animationStopped = false; // 控制动画是否已经停止
  37. let t = 0; // 进度 t,0 为起点,1 为终点
  38. const updatePosition = () => {
  39. if (animationStopped) return; // 如果动画已停止,则不继续执行
  40. // 继续使用贝塞尔曲线计算,带螺旋效果
  41. this.node.position = twoBezier(t, startPos, middlePos, destPos);
  42. // 根据进度生成新节点
  43. let cube = this.Container.getCube();
  44. if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) {
  45. let newNode = cube;
  46. newNode.parent = director.getScene();
  47. newNode.setPosition(this.node.position);
  48. this.Container.nodeReferences.push(newNode);
  49. console.log(cube.getComponent(Cube_Infor).Text);
  50. createdNodes++; // 记录生成的方块
  51. }
  52. // 如果没有方块,停止动画
  53. if (cube == null && !animationStopped) {
  54. animationStopped = true; // 设置动画停止标志
  55. console.log("没有方块,停止动画");
  56. this.node.setPosition(this.node.position); // 防止继续更新位置
  57. }
  58. // 获取 Container 中的当前方块数量
  59. let totalCubes = this.Container.idioms.length * 2; // 假设 idioms 存储了所有方块的信息
  60. // 如果所有方块都已生成,停止动画
  61. if (createdNodes >= totalCubes) {
  62. animationStopped = true;
  63. this.Container.canTouch = true;
  64. console.log("所有方块已拿完,停止动画");
  65. }
  66. // 更新 t 和重新调用动画
  67. if (t < 1) {
  68. t += 0.01; // 每次更新进度
  69. requestAnimationFrame(updatePosition); // 使用 requestAnimationFrame 来继续更新位置
  70. }
  71. };
  72. // 启动动画
  73. updatePosition();
  74. }
  75. }