GravitySystem.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Content.Shared.Gravity;
  2. using JetBrains.Annotations;
  3. using Robust.Shared.Map.Components;
  4. namespace Content.Server.Gravity
  5. {
  6. [UsedImplicitly]
  7. public sealed class GravitySystem : SharedGravitySystem
  8. {
  9. public override void Initialize()
  10. {
  11. base.Initialize();
  12. SubscribeLocalEvent<GravityComponent, ComponentInit>(OnGravityInit);
  13. }
  14. /// <summary>
  15. /// Iterates gravity components and checks if this entity can have gravity applied.
  16. /// </summary>
  17. public void RefreshGravity(EntityUid uid, GravityComponent? gravity = null)
  18. {
  19. if (!Resolve(uid, ref gravity))
  20. return;
  21. if (gravity.Inherent)
  22. return;
  23. var enabled = false;
  24. foreach (var (comp, xform) in EntityQuery<GravityGeneratorComponent, TransformComponent>(true))
  25. {
  26. if (!comp.GravityActive || xform.ParentUid != uid)
  27. continue;
  28. enabled = true;
  29. break;
  30. }
  31. if (enabled != gravity.Enabled)
  32. {
  33. gravity.Enabled = enabled;
  34. var ev = new GravityChangedEvent(uid, enabled);
  35. RaiseLocalEvent(uid, ref ev, true);
  36. Dirty(uid, gravity);
  37. if (HasComp<MapGridComponent>(uid))
  38. {
  39. StartGridShake(uid);
  40. }
  41. }
  42. }
  43. private void OnGravityInit(EntityUid uid, GravityComponent component, ComponentInit args)
  44. {
  45. RefreshGravity(uid);
  46. }
  47. /// <summary>
  48. /// Enables gravity. Note that this is a fast-path for GravityGeneratorSystem.
  49. /// This means it does nothing if Inherent is set and it might be wiped away with a refresh
  50. /// if you're not supposed to be doing whatever you're doing.
  51. /// </summary>
  52. public void EnableGravity(EntityUid uid, GravityComponent? gravity = null)
  53. {
  54. if (!Resolve(uid, ref gravity))
  55. return;
  56. if (gravity.Enabled || gravity.Inherent)
  57. return;
  58. gravity.Enabled = true;
  59. var ev = new GravityChangedEvent(uid, true);
  60. RaiseLocalEvent(uid, ref ev, true);
  61. Dirty(uid, gravity);
  62. if (HasComp<MapGridComponent>(uid))
  63. {
  64. StartGridShake(uid);
  65. }
  66. }
  67. }
  68. }