path1.ts 3.0 KB

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