Trajectory.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { _decorator, Component, Node, Vec3, tween } from 'cc';
  2. import { UIEffect } from './UIEffect';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('Trajectory')
  5. export class Trajectory extends Component {
  6. @property({ tooltip: "起点坐标", type: Vec3 })
  7. public startPoint: Vec3 = new Vec3(0, 0, 0);
  8. @property({ tooltip: "曲线控制点", type: Vec3 })
  9. public ctrl1Point: Vec3 = new Vec3(0, 0, 0);
  10. @property({ tooltip: "曲线控制点", type: Vec3 })
  11. public ctrl2Point: Vec3 = new Vec3(0, 0, 0);
  12. @property({ tooltip: "曲线终点", type: Vec3 })
  13. public endPoint: Vec3 = new Vec3(0, 0, 0);
  14. @property({ tooltip: "tween动画结束回调组件", type: Node })
  15. public backNode: Node;
  16. start() {
  17. tween<Trajectory>(this)
  18. .to(Math.random() * 1.2 + 0.6, { thirdOrderBeizer: 1 })
  19. .to(0.3, { scale: new Vec3(1.4, 1.4) })
  20. .to(0.3, { scale: new Vec3(1, 1) })
  21. .call(() => {
  22. this.backNode.getComponent(UIEffect).backFun();
  23. this.node.destroy();
  24. }).start();
  25. }
  26. public set thirdOrderBeizer(t: number) {
  27. 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;
  28. 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;
  29. this.node.position = new Vec3(beizerX, beizerY);
  30. }
  31. public get thirdOrderBeizer() {
  32. return 0;
  33. }
  34. public get scale(): Vec3 {
  35. return this.node.scale;
  36. }
  37. public set scale(value: Vec3) {
  38. this.node.scale = value;
  39. }
  40. }