BezierComponent.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { _decorator, Component, Vec3, tween } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('BezierComponent')
  4. export class BezierComponent extends Component {
  5. @property({ tooltip: "曲线起点", type: Vec3 })
  6. public startPoint: Vec3 = new Vec3(0, 0, 0);
  7. @property({ tooltip: "曲线控制点", type: Vec3 })
  8. public ctrl1Point: Vec3 = new Vec3(0, 0, 0);
  9. @property({ tooltip: "曲线控制点", type: Vec3 })
  10. public ctrl2Point: Vec3 = new Vec3(0, 0, 0);
  11. @property({ tooltip: "曲线终点", type: Vec3 })
  12. public endPoint: Vec3 = new Vec3(0, 0, 0);
  13. start() {
  14. tween<BezierComponent>(this)
  15. .to(Math.random() * 1.2 + 0.6, { thirdOrderBeizer: 1 })
  16. .to(0.3, { scale: new Vec3(1.4, 1.4) })
  17. .to(0.3, { scale: new Vec3(1, 1) })
  18. .call(() => {
  19. //执行完销毁节点
  20. this.node.destroy();
  21. })
  22. .start();
  23. }
  24. public set thirdOrderBeizer(t: number) {
  25. let beizerX: number = this.startPoint.x * Math.pow((1 - t), 3) + 3 * this.ctrl1Point.x * t * Math.pow((1 - t), 2) + 3 * this.ctrl2Point.x * t * t * (1 - t) + this.endPoint.x * t * t * t;
  26. let beizerY: number = this.startPoint.y * Math.pow((1 - t), 3) + 3 * this.ctrl1Point.y * t * Math.pow((1 - t), 2) + 3 * this.ctrl1Point.y * t * t * (1 - t) + this.endPoint.y * t * t * t;
  27. this.node.position = new Vec3(beizerX, beizerY);
  28. }
  29. public get thirdOrderBeizer() {
  30. return 0;
  31. }
  32. public get scale(): Vec3 {
  33. return this.node.scale;
  34. }
  35. public set scale(value: Vec3) {
  36. this.node.scale = value;
  37. }
  38. }