SharedBatteryDrainerSystem.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Content.Shared.Ninja.Components;
  2. using Content.Shared.DoAfter;
  3. using Robust.Shared.Serialization;
  4. namespace Content.Shared.Ninja.Systems;
  5. /// <summary>
  6. /// Basic draining prediction and API, all real logic is handled serverside.
  7. /// </summary>
  8. public abstract class SharedBatteryDrainerSystem : EntitySystem
  9. {
  10. public override void Initialize()
  11. {
  12. base.Initialize();
  13. SubscribeLocalEvent<BatteryDrainerComponent, DoAfterAttemptEvent<DrainDoAfterEvent>>(OnDoAfterAttempt);
  14. SubscribeLocalEvent<BatteryDrainerComponent, DrainDoAfterEvent>(OnDoAfter);
  15. }
  16. /// <summary>
  17. /// Cancel any drain doafters if the battery is removed or, on the server, gets filled.
  18. /// </summary>
  19. protected virtual void OnDoAfterAttempt(Entity<BatteryDrainerComponent> ent, ref DoAfterAttemptEvent<DrainDoAfterEvent> args)
  20. {
  21. if (ent.Comp.BatteryUid == null)
  22. args.Cancel();
  23. }
  24. /// <summary>
  25. /// Drain power from a power source (on server) and repeat if it succeeded.
  26. /// Client will predict always succeeding since power is serverside.
  27. /// </summary>
  28. private void OnDoAfter(Entity<BatteryDrainerComponent> ent, ref DrainDoAfterEvent args)
  29. {
  30. if (args.Cancelled || args.Handled || args.Target is not {} target)
  31. return;
  32. // repeat if there is still power to drain
  33. args.Repeat = TryDrainPower(ent, target);
  34. }
  35. /// <summary>
  36. /// Attempt to drain as much power as possible into the powercell.
  37. /// Client always predicts this as succeeding since power is serverside and it can only fail once, when the powercell is filled or the target is emptied.
  38. /// </summary>
  39. protected virtual bool TryDrainPower(Entity<BatteryDrainerComponent> ent, EntityUid target)
  40. {
  41. return true;
  42. }
  43. /// <summary>
  44. /// Sets the battery field on the drainer.
  45. /// </summary>
  46. public void SetBattery(Entity<BatteryDrainerComponent?> ent, EntityUid? battery)
  47. {
  48. if (!Resolve(ent, ref ent.Comp) || ent.Comp.BatteryUid == battery)
  49. return;
  50. ent.Comp.BatteryUid = battery;
  51. Dirty(ent, ent.Comp);
  52. }
  53. }
  54. /// <summary>
  55. /// DoAfter event for <see cref="BatteryDrainerComponent"/>.
  56. /// </summary>
  57. [Serializable, NetSerializable]
  58. public sealed partial class DrainDoAfterEvent : SimpleDoAfterEvent;