CarpRiftsConditionSystem.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Content.Server.Objectives.Components;
  2. using Content.Server.Roles;
  3. using Content.Shared.Objectives.Components;
  4. namespace Content.Server.Objectives.Systems;
  5. public sealed class CarpRiftsConditionSystem : EntitySystem
  6. {
  7. [Dependency] private readonly NumberObjectiveSystem _number = default!;
  8. public override void Initialize()
  9. {
  10. base.Initialize();
  11. SubscribeLocalEvent<CarpRiftsConditionComponent, ObjectiveGetProgressEvent>(OnGetProgress);
  12. }
  13. private void OnGetProgress(EntityUid uid, CarpRiftsConditionComponent comp, ref ObjectiveGetProgressEvent args)
  14. {
  15. args.Progress = GetProgress(comp, _number.GetTarget(uid));
  16. }
  17. private float GetProgress(CarpRiftsConditionComponent comp, int target)
  18. {
  19. // prevent divide-by-zero
  20. if (target == 0)
  21. return 1f;
  22. if (comp.RiftsCharged >= target)
  23. return 1f;
  24. return (float) comp.RiftsCharged / (float) target;
  25. }
  26. /// <summary>
  27. /// Increments RiftsCharged, called after a rift fully charges.
  28. /// </summary>
  29. public void RiftCharged(EntityUid uid, CarpRiftsConditionComponent? comp = null)
  30. {
  31. if (!Resolve(uid, ref comp))
  32. return;
  33. comp.RiftsCharged++;
  34. }
  35. /// <summary>
  36. /// Resets RiftsCharged to 0, called after rifts get destroyed.
  37. /// </summary>
  38. public void ResetRifts(EntityUid uid, CarpRiftsConditionComponent? comp = null)
  39. {
  40. if (!Resolve(uid, ref comp))
  41. return;
  42. comp.RiftsCharged = 0;
  43. }
  44. }