1
0

SharedSunShadowSystem.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Content.Shared.Light.Components;
  2. using Robust.Shared.Random;
  3. namespace Content.Shared.Light.EntitySystems;
  4. public abstract class SharedSunShadowSystem : EntitySystem
  5. {
  6. [Dependency] private readonly IRobustRandom _random = default!;
  7. public override void Initialize()
  8. {
  9. base.Initialize();
  10. SubscribeLocalEvent<SunShadowCycleComponent, MapInitEvent>(OnCycleMapInit);
  11. SubscribeLocalEvent<SunShadowCycleComponent, LightCycleOffsetEvent>(OnCycleOffset);
  12. }
  13. private void OnCycleOffset(Entity<SunShadowCycleComponent> ent, ref LightCycleOffsetEvent args)
  14. {
  15. // Okay so we synchronise with LightCycleComponent.
  16. // However, the offset is only set on MapInit and we have no guarantee which one is ran first so we make sure.
  17. ent.Comp.Offset = args.Offset;
  18. Dirty(ent);
  19. }
  20. private void OnCycleMapInit(Entity<SunShadowCycleComponent> ent, ref MapInitEvent args)
  21. {
  22. if (TryComp(ent.Owner, out LightCycleComponent? lightCycle))
  23. {
  24. ent.Comp.Duration = lightCycle.Duration;
  25. ent.Comp.Offset = lightCycle.Offset;
  26. }
  27. else
  28. {
  29. ent.Comp.Offset = _random.Next(ent.Comp.Duration);
  30. }
  31. Dirty(ent);
  32. }
  33. }