AreaClock2D.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine;
  2. namespace Chronos
  3. {
  4. /// <summary>
  5. /// A Clock that affects every Timeline within its 2D collider by multiplying its time scale with that of their observed clock.
  6. /// </summary>
  7. [AddComponentMenu("Time/Area Clock 2D")]
  8. [DisallowMultipleComponent]
  9. [HelpURL("http://ludiq.io/chronos/documentation#AreaClock")]
  10. public class AreaClock2D : AreaClock<Collider2D, Vector2>
  11. {
  12. protected virtual void OnTriggerEnter2D(Collider2D other)
  13. {
  14. if (!enabled)
  15. {
  16. return;
  17. }
  18. Timeline timeline = other.GetComponent<Timeline>();
  19. if (timeline != null)
  20. {
  21. // Store local coordinates to account for dynamic changes of the clock's transform
  22. Vector3 entry = transform.InverseTransformPoint(other.transform.position);
  23. Capture(timeline, entry);
  24. }
  25. }
  26. protected virtual void OnTriggerExit2D(Collider2D collider)
  27. {
  28. Timeline timeline = collider.GetComponent<Timeline>();
  29. if (timeline != null)
  30. {
  31. Release(timeline);
  32. }
  33. }
  34. protected override float PointToEdgeTimeScale(Vector3 position)
  35. {
  36. Vector2 center = transform.TransformPoint(this.center);
  37. Vector2 delta = (Vector2) position - center;
  38. Vector2 direction = delta.normalized;
  39. float distance = delta.magnitude;
  40. float noEffect = innerBlend == ClockBlend.Multiplicative ? 1 : 0;
  41. // For exact center...
  42. if (direction == Vector2.zero)
  43. {
  44. return Mathf.Lerp(noEffect, timeScale, curve.Evaluate(0));
  45. }
  46. // For circle colliders centered at origin...
  47. CircleCollider2D circleCollider = collider as CircleCollider2D;
  48. if (circleCollider != null && circleCollider.offset == this.center)
  49. {
  50. Vector3 scale = transform.lossyScale;
  51. float maxScale = Mathf.Max(scale.x, scale.y);
  52. float radius = circleCollider.radius * maxScale;
  53. Vector2 edge = center + (radius / 2 * direction);
  54. if (Timekeeper.instance.debug)
  55. {
  56. Debug.DrawLine(center, edge, Color.cyan);
  57. Debug.DrawLine(center, position, Color.magenta);
  58. }
  59. return Mathf.Lerp(noEffect, timeScale, curve.Evaluate(distance / radius));
  60. }
  61. // For any collider at any origin...
  62. // This ray length should always be sufficient -- the biggest distance within the AABB
  63. float rayLength = (collider.bounds.max - collider.bounds.min).magnitude;
  64. // Reverse the ray because a collider raycast doesn't work from within
  65. Vector2 origin = center + (rayLength * direction);
  66. direction = -direction;
  67. // Unfortunately, we have to use RaycastAll() because there is no Collider2D.Raycast()
  68. RaycastHit2D[] hits = Physics2D.RaycastAll(origin, direction, rayLength, 1 << gameObject.layer);
  69. foreach (RaycastHit2D hit in hits)
  70. {
  71. if (hit.collider == collider)
  72. {
  73. Vector2 edge = hit.point;
  74. float edgeDistance = (edge - center).magnitude;
  75. if (Timekeeper.instance.debug)
  76. {
  77. Debug.DrawLine(center, edge, Color.cyan);
  78. Debug.DrawLine(center, position, Color.magenta);
  79. }
  80. return Mathf.Lerp(noEffect, timeScale, curve.Evaluate(distance / edgeDistance));
  81. }
  82. }
  83. Debug.LogWarning("Area clock cannot find its collider's edge. Make sure the center is inside and the collider is convex.");
  84. return timeScale;
  85. }
  86. public override bool ContainsPoint(Vector3 point)
  87. {
  88. // Manually increase Z size in case the colliders aren't perfectly aligned.
  89. Bounds bounds = collider.bounds;
  90. Vector3 size = bounds.size;
  91. bounds.size = new Vector3(size.x, size.y, 999);
  92. return bounds.Contains(point);
  93. }
  94. /// <summary>
  95. /// The components used by the area clock are cached for performance optimization. If you add or remove the Collider2D on the GameObject, you need to call this method to update the area clock accordingly.
  96. /// </summary>
  97. public override void CacheComponents()
  98. {
  99. collider = GetComponent<Collider2D>();
  100. if (collider == null)
  101. {
  102. throw new ChronosException(string.Format("Missing collider for area clock."));
  103. }
  104. }
  105. }
  106. }