Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

77 lines
2.5KB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace coa4u
  5. {
  6. /// <summary>
  7. /// Moves the target with a cubic Bezier in absolute coordinates (if the target is not in the start point, it'll be positioned there when the action starts).
  8. /// </summary>
  9. class ActionBezierAbs : ActionInterval
  10. {
  11. protected Vector3 startPoint;
  12. protected Vector3 endPoint;
  13. protected Vector3 startControlPoint;
  14. protected Vector3 endControlPoint;
  15. public ActionBezierAbs(Vector3 tgtStart, Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
  16. : base(tgtDuration)
  17. {
  18. startPoint = tgtStart;
  19. endPoint = tgtEnd;
  20. startControlPoint = tgtSCP;
  21. endControlPoint = tgtECP;
  22. }
  23. public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration)
  24. : this((Vector3)tgtStart, (Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration)
  25. {
  26. is2d = true;
  27. }
  28. /// <summary>
  29. /// Returns a copy of the action.
  30. /// </summary>
  31. public override ActionInstant clone()
  32. {
  33. return new ActionBezierAbs(startPoint, startControlPoint, endControlPoint, endPoint, duration);
  34. }
  35. /// <summary>
  36. /// Returns the reversed version of the action, if it is possible.
  37. /// </summary>
  38. public override ActionInstant reverse()
  39. {
  40. return new ActionBezierAbs(endPoint, endControlPoint, startControlPoint, startPoint, duration);
  41. }
  42. /// <summary>
  43. /// This method is called at the action start.
  44. /// </summary>
  45. public override void start()
  46. {
  47. base.start();
  48. float z = transform.position.z;
  49. if (is2d)
  50. {
  51. startPoint.z = z;
  52. endPoint.z = z;
  53. startControlPoint.z = z;
  54. endControlPoint.z = z;
  55. }
  56. }
  57. /// <summary>
  58. /// This method is called every frame update.
  59. /// </summary>
  60. public override void stepInterval(float dt)
  61. {
  62. float t = timer / duration;
  63. Vector3 newPosition = (((-startPoint
  64. + 3 * (startControlPoint - endControlPoint) + endPoint) * t
  65. + (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
  66. 3 * (startControlPoint - startPoint)) * t + startPoint;
  67. transform.position = newPosition;
  68. }
  69. }
  70. }