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