You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

74 lines
2.3KB

  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 relative coordinates.
  8. /// </summary>
  9. class ActionBezierRel : ActionInterval
  10. {
  11. protected Vector3 ep;
  12. protected Vector3 cp1;
  13. protected Vector3 cp2;
  14. protected Vector3 startPoint;
  15. protected Vector3 endPoint;
  16. protected Vector3 startControlPoint;
  17. protected Vector3 endControlPoint;
  18. public ActionBezierRel(Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
  19. : base(tgtDuration)
  20. {
  21. ep = tgtEnd;
  22. cp1 = tgtSCP;
  23. cp2 = tgtECP;
  24. }
  25. public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration)
  26. : this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration)
  27. {
  28. }
  29. /// <summary>
  30. /// Returns a copy of the action.
  31. /// </summary>
  32. public override ActionInstant clone()
  33. {
  34. return new ActionBezierRel(startControlPoint, endControlPoint, endPoint, duration);
  35. }
  36. /// <summary>
  37. /// Returns the reversed version of the action, if it is possible.
  38. /// </summary>
  39. public override ActionInstant reverse()
  40. {
  41. return new ActionBezierRel(-startControlPoint, -endControlPoint, -endPoint, duration);
  42. }
  43. /// <summary>
  44. /// This method is called at the action start.
  45. /// </summary>
  46. public override void start()
  47. {
  48. base.start();
  49. startPoint = target.transform.position;
  50. endPoint = startPoint + ep;
  51. startControlPoint = startPoint + cp1;
  52. endControlPoint = startControlPoint + cp2;
  53. }
  54. /// <summary>
  55. /// This method is called every frame update.
  56. /// </summary>
  57. public override void stepInterval(float dt)
  58. {
  59. float t = timer / duration;
  60. Vector3 newPosition = (((-startPoint
  61. + 3 * (startControlPoint - endControlPoint) + endPoint) * t
  62. + (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
  63. 3 * (startControlPoint - startPoint)) * t + startPoint;
  64. transform.position = newPosition;
  65. }
  66. }
  67. }