CreateIdiom.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. const { ccclass, property } = _decorator;
  6. @ccclass('CreateIdiom')
  7. export class CreateIdiom extends Component {
  8. Container: Container_Manager;
  9. animationRunning: boolean = false; // 控制是否有动画正在运行
  10. @property([Node])
  11. startPos:Node[]=[];
  12. onLoad() {
  13. this.Container = find('Container').getComponent(Container_Manager);
  14. }
  15. update(deltaTime: number) {
  16. }
  17. nodeMoving() {
  18. if (Hall.getInstance().player.get_max_floor() !== 0) {
  19. this.node.setPosition(new Vec3(0, 1.6, -0.5));
  20. let startPos = this.node.position; // 起点,抛物线开始的坐标
  21. let middlePos = new Vec3(this.node.position.x, this.node.position.y + 1, -0.5); // 中间控制点
  22. let destPos = new Vec3(this.node.position.x, this.node.position.y + 2, -0.5); // 终点,抛物线上升顶点
  23. let lastGeneratedPosition = null; // 记录上一次生成方块的位置
  24. // 保持原来贝塞尔曲线和螺旋效果的计算
  25. let twoBezier = (t: number, p1: Vec3, cp: Vec3, p2: Vec3) => {
  26. let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
  27. let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
  28. let z = (1 - t) * (1 - t) * p1.z + 2 * t * (1 - t) * cp.z + t * t * p2.z;
  29. let angle = t * Math.PI * 10;
  30. // 缓动的半径计算方法,调整第一圈半径的变化速度
  31. let radius = 1.2 - Math.pow(t, 2); // 使用平方函数,t小的时候变化较大,t大时变化变缓
  32. let xOffset = Math.cos(angle) * radius;
  33. let zOffset = Math.sin(angle) * radius;
  34. return new Vec3(x + xOffset, y, z + zOffset);
  35. };
  36. let createdNodes = 0; // 已生成的节点计数
  37. let animationStopped = false; // 控制动画是否已经停止
  38. let t = 0; // 进度 t,0 为起点,1 为终点
  39. const updatePosition = () => {
  40. if (animationStopped) return;
  41. this.node.position = twoBezier(t, startPos, middlePos, destPos);
  42. let cube = this.Container.getCube();
  43. if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) {
  44. let newNode = cube;
  45. newNode.parent = director.getScene();
  46. // 随机左右偏移量,范围设定为 [-0.5, 0.5]
  47. let randomXOffset = Math.random() * 1 - 0.5; // 生成一个[-0.5, 0.5]之间的随机值
  48. newNode.setPosition(this.node.position.x + randomXOffset, this.node.position.y, this.node.position.z);
  49. this.Container.nodeReferences.push(newNode);
  50. lastGeneratedPosition = this.node.position.clone(); // 更新上次生成位置
  51. console.log(cube.getComponent(Cube_Infor).Text);
  52. createdNodes++;
  53. }
  54. let totalCubes = 60;
  55. // 设置生成方块的间隔,可以调整生成速度
  56. let interval = 0.01; // 控制生成间隔的速率,可以根据需要调整
  57. if (createdNodes >= totalCubes) {
  58. animationStopped = true;
  59. this.Container.canTouch = true;
  60. console.log("所有方块已拿完或没有方块了,停止动画");
  61. }
  62. // 这里控制 t 增加的速率
  63. t += interval;
  64. if (!animationStopped) {
  65. requestAnimationFrame(updatePosition);
  66. }
  67. };
  68. updatePosition();
  69. }else{
  70. for(let i=0;i<this.Container.idioms.length*2;i++){
  71. let cube = this.Container.getCube();
  72. if (cube != null && cube.getComponent(Cube_Infor).state === Cube_State.live) {
  73. let newNode = cube;
  74. newNode.parent = director.getScene();
  75. // 随机左右偏移量,范围设定为 [-0.5, 0.5]
  76. let randomXOffset = Math.random() * 1 - 0.5; // 生成一个[-0.5, 0.5]之间的随机值
  77. newNode.setPosition(this.startPos[i].position.x, this.startPos[i].position.y, this.startPos[i].position.z);
  78. this.Container.nodeReferences.push(newNode);
  79. if(i>1)
  80. {
  81. newNode.getComponent(Cube_Infor).state=Cube_State.wait;
  82. }
  83. }
  84. }
  85. this.Container.canTouch = true;
  86. }
  87. }
  88. }