CodeConditionSystem.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using Content.Server.Objectives.Components;
  2. using Content.Shared.Objectives.Components;
  3. using Content.Shared.Mind;
  4. using Content.Shared.Mind.Components;
  5. namespace Content.Server.Objectives.Systems;
  6. /// <summary>
  7. /// Handles <see cref="CodeConditionComponent"/> progress and provides API for systems to use.
  8. /// </summary>
  9. public sealed class CodeConditionSystem : EntitySystem
  10. {
  11. [Dependency] private readonly SharedMindSystem _mind = default!;
  12. public override void Initialize()
  13. {
  14. base.Initialize();
  15. SubscribeLocalEvent<CodeConditionComponent, ObjectiveGetProgressEvent>(OnGetProgress);
  16. }
  17. private void OnGetProgress(Entity<CodeConditionComponent> ent, ref ObjectiveGetProgressEvent args)
  18. {
  19. args.Progress = ent.Comp.Completed ? 1f : 0f;
  20. }
  21. /// <summary>
  22. /// Returns whether an objective is completed.
  23. /// </summary>
  24. public bool IsCompleted(Entity<CodeConditionComponent?> ent)
  25. {
  26. if (!Resolve(ent, ref ent.Comp))
  27. return false;
  28. return ent.Comp.Completed;
  29. }
  30. /// <summary>
  31. /// Sets an objective's completed field.
  32. /// </summary>
  33. public void SetCompleted(Entity<CodeConditionComponent?> ent, bool completed = true)
  34. {
  35. if (!Resolve(ent, ref ent.Comp))
  36. return;
  37. ent.Comp.Completed = completed;
  38. }
  39. /// <summary>
  40. /// Sets a mob's objective to complete.
  41. /// </summary>
  42. public void SetCompleted(Entity<MindContainerComponent?> mob, string prototype, bool completed = true)
  43. {
  44. if (_mind.GetMind(mob, mob.Comp) is not {} mindId)
  45. return;
  46. if (!_mind.TryFindObjective(mindId, prototype, out var obj))
  47. return;
  48. SetCompleted(obj.Value, completed);
  49. }
  50. }