Trajectory.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { _decorator, Component, Node, Vec3, tween, EventHandler } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('Trajectory')
  4. export class Trajectory 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. @property({ tooltip: "tween动画结束回调组件", type: EventHandler })
  14. public backFn: EventHandler;
  15. start() {
  16. tween<Trajectory>(this)
  17. .to(Math.random() * 1.2 + 0.6, { thirdOrderBeizer: 1 })
  18. .to(0.3, { scale: new Vec3(1.4, 1.4) })
  19. .to(0.3, { scale: new Vec3(1, 1) })
  20. .call(() => {
  21. this.backFn && this.backFn.emit([]);
  22. this.node.destroy();
  23. }).start();
  24. }
  25. public set thirdOrderBeizer(t: number) {
  26. 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;
  27. let beizerY: number = this.startPoint.y * Math.pow((1 - t), 3) + 3 * this.ctrl1Point.y * t * Math.pow((1 - t), 2) + 3 * this.ctrl2Point.y * t * t * (1 - t) + this.endPoint.y * t * t * t;
  28. this.node.position = new Vec3(beizerX, beizerY);
  29. }
  30. public get thirdOrderBeizer() {
  31. return 0;
  32. }
  33. public get scale(): Vec3 {
  34. return this.node.scale;
  35. }
  36. public set scale(value: Vec3) {
  37. this.node.scale = value;
  38. }
  39. }