1
0

SharedWeightExamineInfoSystem.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using Content.Shared.Examine;
  2. namespace Content.Shared._Stalker.Weight;
  3. public sealed class SharedWeightExamineInfoSystem : EntitySystem
  4. {
  5. public override void Initialize()
  6. {
  7. base.Initialize();
  8. SubscribeLocalEvent<STWeightComponent, ExaminedEvent>(OnWeightExamine);
  9. }
  10. /// <summary>
  11. /// Adds a colour-coded weight description to the examination event based on the entity's total weight.
  12. /// </summary>
  13. private void OnWeightExamine(EntityUid uid, STWeightComponent component, ExaminedEvent args)
  14. {
  15. if (!args.IsInDetailsRange)
  16. return;
  17. var r = HexFromId(255);
  18. var g = HexFromId(255 - 255 / 30 * ((int)component.Total - 50));
  19. if (component.Total < 50f)
  20. {
  21. r = HexFromId(255 / 50 * (int)component.Total);
  22. g = HexFromId(255);
  23. }
  24. var colorString = $"#{r}{g}00";
  25. var str = $"Weighs [color={colorString}]{component.Total:0.00}[/color] kg";
  26. args.PushMarkup(str);
  27. }
  28. /// <summary>
  29. /// Converts an integer to a two-character hexadecimal string, clamping values below 0 to "00" and above 255 to "FF".
  30. /// </summary>
  31. /// <param name="id">The integer value to convert.</param>
  32. /// <returns>A two-character hexadecimal string representing the clamped value.</returns>
  33. private string HexFromId(int id)
  34. {
  35. switch (id)
  36. {
  37. case < 0:
  38. return "00";
  39. case < 16:
  40. return "0" + id.ToString("X");
  41. case > 255:
  42. id = 255;
  43. return id.ToString("X");
  44. default:
  45. return id.ToString("X");
  46. }
  47. }
  48. }