CreateIdiom.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. const { ccclass, property } = _decorator;
  5. @ccclass('CreateIdiom')
  6. export class CreateIdiom extends Component {
  7. count: number = 0;
  8. Container: Container_Manager;
  9. onLoad() {
  10. this.Container = find('Container').getComponent(Container_Manager);
  11. }
  12. update(deltaTime: number) {
  13. }
  14. nodeMoving() {
  15. this.node.setPosition(new Vec3(0,0.3,0));
  16. let startPos = this.node.position; // 起点,抛物线开始的坐标
  17. let middlePos = new Vec3(this.node.position.x, this.node.position.y, 0); // 中间控制点
  18. let destPos = new Vec3(this.node.position.x, this.node.position.y, 0); // 终点,抛物线上升顶点
  19. let twoBezier = (t: number, p1: Vec3, cp: Vec3, p2: Vec3) => {
  20. // 贝塞尔曲线计算
  21. let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
  22. let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
  23. // 螺旋运动计算
  24. let angle = t * Math.PI * 6; // 螺旋角度变化,增加圈数
  25. let radius = 0.5 + t * 2.5; // 螺旋半径逐渐增大
  26. let xOffset = Math.cos(angle) * radius; // x 轴偏移
  27. let zOffset = Math.sin(angle) * radius; // z 轴偏移
  28. // 返回最终点,带有螺旋效果
  29. return new Vec3(x + xOffset, y, zOffset);
  30. };
  31. let tweenDuration: number = 1.0; // 动画时长
  32. let createdNodes = 0; // 已生成的节点计数
  33. tween(this.node.position)
  34. .to(tweenDuration, destPos, {
  35. onUpdate: (target: Vec3, ratio: number) => {
  36. // 计算新的位置,带螺旋上升效果
  37. this.node.position = twoBezier(ratio, startPos, middlePos, destPos);
  38. // 根据进度生成新节点
  39. let cube =this.Container.getCube();
  40. if (cube != null&&cube.getComponent(Cube_Infor).state === Cube_State.live) {
  41. let newNode = cube;
  42. newNode.parent = director.getScene();
  43. newNode.setPosition(this.node.position);
  44. this.Container.nodeReferences.push(newNode);
  45. console.log(cube.getComponent(Cube_Infor).Text);
  46. }
  47. }
  48. }).start();
  49. }
  50. }