SunShadowSystem.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Diagnostics.Contracts;
  2. using System.Numerics;
  3. using Content.Client.GameTicking.Managers;
  4. using Content.Shared.Light.Components;
  5. using Content.Shared.Light.EntitySystems;
  6. using Robust.Shared.Timing;
  7. using Robust.Shared.Utility;
  8. namespace Content.Client.Light.EntitySystems;
  9. public sealed class SunShadowSystem : SharedSunShadowSystem
  10. {
  11. [Dependency] private readonly ClientGameTicker _ticker = default!;
  12. [Dependency] private readonly IGameTiming _timing = default!;
  13. [Dependency] private readonly MetaDataSystem _metadata = default!;
  14. public override void Update(float frameTime)
  15. {
  16. base.Update(frameTime);
  17. if (!_timing.IsFirstTimePredicted)
  18. return;
  19. var mapQuery = AllEntityQuery<SunShadowCycleComponent, SunShadowComponent>();
  20. while (mapQuery.MoveNext(out var uid, out var cycle, out var shadow))
  21. {
  22. if (!cycle.Running || cycle.Directions.Count == 0)
  23. continue;
  24. var pausedTime = _metadata.GetPauseTime(uid);
  25. var time = (float)(_timing.CurTime
  26. .Add(cycle.Offset)
  27. .Subtract(_ticker.RoundStartTimeSpan)
  28. .Subtract(pausedTime)
  29. .TotalSeconds % cycle.Duration.TotalSeconds);
  30. var (direction, alpha) = GetShadow((uid, cycle), time);
  31. shadow.Direction = direction;
  32. shadow.Alpha = alpha;
  33. }
  34. }
  35. [Pure]
  36. public (Vector2 Direction, float Alpha) GetShadow(Entity<SunShadowCycleComponent> entity, float time)
  37. {
  38. // So essentially the values are stored as the percentages of the total duration just so it adjusts the speed
  39. // dynamically and we don't have to manually handle it.
  40. // It will lerp from each value to the next one with angle and length handled separately
  41. var ratio = (float) (time / entity.Comp.Duration.TotalSeconds);
  42. for (var i = entity.Comp.Directions.Count - 1; i >= 0; i--)
  43. {
  44. var dir = entity.Comp.Directions[i];
  45. if (ratio > dir.Ratio)
  46. {
  47. var next = entity.Comp.Directions[(i + 1) % entity.Comp.Directions.Count];
  48. float nextRatio;
  49. // Last entry
  50. if (i == entity.Comp.Directions.Count - 1)
  51. {
  52. nextRatio = next.Ratio + 1f;
  53. }
  54. else
  55. {
  56. nextRatio = next.Ratio;
  57. }
  58. var range = nextRatio - dir.Ratio;
  59. var diff = (ratio - dir.Ratio) / range;
  60. DebugTools.Assert(diff is >= 0f and <= 1f);
  61. // We lerp angle + length separately as we don't want a straight-line lerp and want the rotation to be consistent.
  62. var currentAngle = dir.Direction.ToAngle();
  63. var nextAngle = next.Direction.ToAngle();
  64. var angle = Angle.Lerp(currentAngle, nextAngle, diff);
  65. // This is to avoid getting weird issues where the angle gets pretty close but length still noticeably catches up.
  66. var lengthDiff = MathF.Pow(diff, 1f / 2f);
  67. var length = float.Lerp(dir.Direction.Length(), next.Direction.Length(), lengthDiff);
  68. var vector = angle.ToVec() * length;
  69. var alpha = float.Lerp(dir.Alpha, next.Alpha, diff);
  70. return (vector, alpha);
  71. }
  72. }
  73. throw new InvalidOperationException();
  74. }
  75. }