1
0

SharedPowerCellSystem.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Content.Shared.Containers.ItemSlots;
  2. using Content.Shared.PowerCell.Components;
  3. using Content.Shared.Rejuvenate;
  4. using Robust.Shared.Containers;
  5. using Robust.Shared.Timing;
  6. namespace Content.Shared.PowerCell;
  7. public abstract class SharedPowerCellSystem : EntitySystem
  8. {
  9. [Dependency] protected readonly IGameTiming Timing = default!;
  10. [Dependency] private readonly ItemSlotsSystem _itemSlots = default!;
  11. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<PowerCellDrawComponent, MapInitEvent>(OnMapInit);
  16. SubscribeLocalEvent<PowerCellSlotComponent, RejuvenateEvent>(OnRejuvenate);
  17. SubscribeLocalEvent<PowerCellSlotComponent, EntInsertedIntoContainerMessage>(OnCellInserted);
  18. SubscribeLocalEvent<PowerCellSlotComponent, EntRemovedFromContainerMessage>(OnCellRemoved);
  19. SubscribeLocalEvent<PowerCellSlotComponent, ContainerIsInsertingAttemptEvent>(OnCellInsertAttempt);
  20. }
  21. private void OnMapInit(Entity<PowerCellDrawComponent> ent, ref MapInitEvent args)
  22. {
  23. QueueUpdate((ent, ent.Comp));
  24. }
  25. private void OnRejuvenate(EntityUid uid, PowerCellSlotComponent component, RejuvenateEvent args)
  26. {
  27. if (!_itemSlots.TryGetSlot(uid, component.CellSlotId, out var itemSlot) || !itemSlot.Item.HasValue)
  28. return;
  29. // charge entity batteries and remove booby traps.
  30. RaiseLocalEvent(itemSlot.Item.Value, args);
  31. }
  32. private void OnCellInsertAttempt(EntityUid uid, PowerCellSlotComponent component, ContainerIsInsertingAttemptEvent args)
  33. {
  34. if (!component.Initialized)
  35. return;
  36. if (args.Container.ID != component.CellSlotId)
  37. return;
  38. if (!HasComp<PowerCellComponent>(args.EntityUid))
  39. {
  40. args.Cancel();
  41. }
  42. }
  43. private void OnCellInserted(EntityUid uid, PowerCellSlotComponent component, EntInsertedIntoContainerMessage args)
  44. {
  45. if (!component.Initialized)
  46. return;
  47. if (args.Container.ID != component.CellSlotId)
  48. return;
  49. _appearance.SetData(uid, PowerCellSlotVisuals.Enabled, true);
  50. RaiseLocalEvent(uid, new PowerCellChangedEvent(false), false);
  51. }
  52. protected virtual void OnCellRemoved(EntityUid uid, PowerCellSlotComponent component, EntRemovedFromContainerMessage args)
  53. {
  54. if (args.Container.ID != component.CellSlotId)
  55. return;
  56. _appearance.SetData(uid, PowerCellSlotVisuals.Enabled, false);
  57. RaiseLocalEvent(uid, new PowerCellChangedEvent(true), false);
  58. }
  59. /// <summary>
  60. /// Makes the draw logic update in the next tick.
  61. /// </summary>
  62. public void QueueUpdate(Entity<PowerCellDrawComponent?> ent)
  63. {
  64. if (Resolve(ent, ref ent.Comp))
  65. ent.Comp.NextUpdateTime = Timing.CurTime;
  66. }
  67. public void SetDrawEnabled(Entity<PowerCellDrawComponent?> ent, bool enabled)
  68. {
  69. if (!Resolve(ent, ref ent.Comp, false) || ent.Comp.Enabled == enabled)
  70. return;
  71. ent.Comp.Enabled = enabled;
  72. Dirty(ent, ent.Comp);
  73. }
  74. /// <summary>
  75. /// Returns whether the entity has a slotted battery and <see cref="PowerCellDrawComponent.UseRate"/> charge.
  76. /// </summary>
  77. /// <param name="uid"></param>
  78. /// <param name="battery"></param>
  79. /// <param name="cell"></param>
  80. /// <param name="user">Popup to this user with the relevant detail if specified.</param>
  81. public abstract bool HasActivatableCharge(
  82. EntityUid uid,
  83. PowerCellDrawComponent? battery = null,
  84. PowerCellSlotComponent? cell = null,
  85. EntityUid? user = null);
  86. /// <summary>
  87. /// Whether the power cell has any power at all for the draw rate.
  88. /// </summary>
  89. public abstract bool HasDrawCharge(
  90. EntityUid uid,
  91. PowerCellDrawComponent? battery = null,
  92. PowerCellSlotComponent? cell = null,
  93. EntityUid? user = null);
  94. }