path3.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import gameUI from "../game/UIFace/gameUI";
  2. const {ccclass, property} = cc._decorator;
  3. @ccclass
  4. export default class path3 extends cc.Component {
  5. private anim: cc.Animation = null;
  6. private path3data: cc.Vec3[] = [];
  7. public static instance: path3 = null;
  8. onLoad() {
  9. path3.instance = this;
  10. //获取当前节点上的动画组件
  11. this.anim = this.node.getComponent(cc.Animation);
  12. // this.anim.play("path3_1");
  13. }
  14. /**编辑路径 */
  15. public changeLevelData(): void {
  16. this.path3data = [];
  17. if (this.anim == null) {
  18. return;
  19. }
  20. //获取动画片段
  21. let clips = this.anim.getClips();
  22. //获取当前片段
  23. let clip = clips[gameUI.instance.myDataObj.LevelData.scene_path - 1];
  24. if (clip) {
  25. //获取贝塞尔曲线路径
  26. let frames = clip.curveData.props.position;
  27. this.genpath3Data(frames);
  28. }
  29. }
  30. private genpath3Data(frames): void {
  31. let bezier = [];
  32. //贝塞尔曲线定义四个点:起点、终点、两个相互分离中间控制点点 [begin, ctrl1, ctrl2, end]
  33. let begin = null, ctrl1 = null, ctrl2 = null, end = null;
  34. //遍历关键帧获取贝塞尔曲线
  35. for (let i = 0; i < frames.length; i++) {
  36. const frame = frames[i];
  37. //获取控制起点
  38. if (ctrl1 !== null) {
  39. bezier.push([begin, ctrl1, ctrl1, cc.v3(frame.value[0], frame.value[1])]);
  40. }
  41. //获取起点坐标
  42. begin = cc.v3(frame.value[0], frame.value[1]);
  43. //获取关键帧的运动路径
  44. const motionPath = frame.motionPath;
  45. //遍历关键帧运动路径获取贝塞尔曲线的四个点
  46. for (let j = 0; j < motionPath.length; j++) {
  47. const item = motionPath[j];
  48. //获取终点
  49. end = cc.v3(item[0], item[1]);
  50. //获取控制终点
  51. ctrl2 = cc.v3(item[2], item[3]);
  52. //判断控制起点是否为空
  53. if (ctrl1 === null) {
  54. ctrl1 = ctrl2;
  55. }
  56. //添加贝塞尔曲线中各点坐标
  57. bezier.push([begin, ctrl1, ctrl2, end]);
  58. //获取控制起点
  59. ctrl1 = cc.v3(item[4], item[5]);
  60. //设置下一个节点的起点为上一个节点的终点
  61. begin = end;
  62. }
  63. }
  64. // //将贝塞尔曲线转换为坐标点
  65. // const first:cc.Vec3 = bezier[0][0];//获取起始点坐标
  66. // let data = [first];
  67. let data = [];
  68. for (let k = 0; k < bezier.length; k++) {
  69. const item = bezier[k];
  70. data.push(item[0]);
  71. if (k == bezier.length - 1) {
  72. data.push(item[3]);
  73. }
  74. }
  75. this.path3data = data;
  76. }
  77. /**获取路径 */
  78. public getpathData() {
  79. return this.path3data;
  80. }
  81. }