path.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import gameUI from "../game/UIFace/gameUI";
  2. import GameConfig from "./GameConfig";
  3. const {ccclass, property} = cc._decorator;
  4. @ccclass
  5. export default class path extends cc.Component {
  6. private anim :cc.Animation = null;
  7. private pathdata:cc.Vec3[] = [];
  8. public static instance:path = null;
  9. onLoad () {
  10. path.instance = this;
  11. //获取当前节点上的动画组件
  12. this.anim = this.node.getComponent(cc.Animation);
  13. // this.anim.play("path_1");
  14. }
  15. /**编辑路径 */
  16. public changeLevelData(): void
  17. {
  18. //获取动画片段
  19. let clips = this.anim.getClips();
  20. //获取当前片段
  21. let clip = clips[gameUI.instance.myDataObj.LevelData.scene_path-1];
  22. //获取贝塞尔曲线路径
  23. this.pathdata = [];
  24. if (clip) {
  25. //获取贝塞尔曲线路径
  26. let frames = clip.curveData.props.position;
  27. this.genPathData(frames);
  28. }
  29. }
  30. private genPathData(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.pathdata = data;
  76. }
  77. /**获取路径 */
  78. public getPathData(){
  79. return this.pathdata;
  80. }
  81. }