1
0

NumberObjectiveSystem.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using Content.Server.Objectives.Components;
  2. using Content.Shared.Objectives.Components;
  3. using Robust.Shared.GameObjects;
  4. using Robust.Shared.Random;
  5. namespace Content.Server.Objectives.Systems;
  6. /// <summary>
  7. /// Provides API for other components, handles picking the count and setting the title and description.
  8. /// </summary>
  9. public sealed class NumberObjectiveSystem : EntitySystem
  10. {
  11. [Dependency] private readonly IRobustRandom _random = default!;
  12. [Dependency] private readonly MetaDataSystem _metaData = default!;
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<NumberObjectiveComponent, ObjectiveAssignedEvent>(OnAssigned);
  17. SubscribeLocalEvent<NumberObjectiveComponent, ObjectiveAfterAssignEvent>(OnAfterAssign);
  18. }
  19. private void OnAssigned(EntityUid uid, NumberObjectiveComponent comp, ref ObjectiveAssignedEvent args)
  20. {
  21. comp.Target = _random.Next(comp.Min, comp.Max);
  22. }
  23. private void OnAfterAssign(EntityUid uid, NumberObjectiveComponent comp, ref ObjectiveAfterAssignEvent args)
  24. {
  25. if (comp.Title != null)
  26. _metaData.SetEntityName(uid, Loc.GetString(comp.Title, ("count", comp.Target)), args.Meta);
  27. if (comp.Description != null)
  28. _metaData.SetEntityDescription(uid, Loc.GetString(comp.Description, ("count", comp.Target)), args.Meta);
  29. }
  30. /// <summary>
  31. /// Gets the objective's target count.
  32. /// </summary>
  33. public int GetTarget(EntityUid uid, NumberObjectiveComponent? comp = null)
  34. {
  35. if (!Resolve(uid, ref comp))
  36. return 0;
  37. return comp.Target;
  38. }
  39. }