SharedPowerSwitchableSystem.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using Content.Shared.Examine;
  2. namespace Content.Shared.Power.Generator;
  3. /// <summary>
  4. /// Shared logic for power-switchable devices.
  5. /// </summary>
  6. /// <seealso cref="PowerSwitchableComponent"/>
  7. public abstract class SharedPowerSwitchableSystem : EntitySystem
  8. {
  9. public override void Initialize()
  10. {
  11. SubscribeLocalEvent<PowerSwitchableComponent, ExaminedEvent>(OnExamined);
  12. }
  13. private void OnExamined(EntityUid uid, PowerSwitchableComponent comp, ExaminedEvent args)
  14. {
  15. // Show which voltage is currently selected.
  16. var voltage = VoltageColor(GetVoltage(uid, comp));
  17. args.PushMarkup(Loc.GetString(comp.ExamineText, ("voltage", voltage)));
  18. }
  19. /// <summary>
  20. /// Helper to get the colored markup string for a voltage type.
  21. /// </summary>
  22. public string VoltageColor(SwitchableVoltage voltage)
  23. {
  24. return Loc.GetString("power-switchable-voltage", ("voltage", VoltageString(voltage)));
  25. }
  26. /// <summary>
  27. /// Converts from "hv" to "HV" since for some reason the enum gets made lowercase???
  28. /// </summary>
  29. public string VoltageString(SwitchableVoltage voltage)
  30. {
  31. return voltage.ToString().ToUpper();
  32. }
  33. /// <summary>
  34. /// Returns index of the next cable type index to cycle to.
  35. /// </summary>
  36. public int NextIndex(EntityUid uid, PowerSwitchableComponent? comp = null)
  37. {
  38. if (!Resolve(uid, ref comp))
  39. return 0;
  40. // loop back at the end
  41. return (comp.ActiveIndex + 1) % comp.Cables.Count;
  42. }
  43. /// <summary>
  44. /// Returns the current cable voltage being used by a power-switchable device.
  45. /// </summary>
  46. public SwitchableVoltage GetVoltage(EntityUid uid, PowerSwitchableComponent? comp = null)
  47. {
  48. if (!Resolve(uid, ref comp))
  49. return default;
  50. return comp.Cables[comp.ActiveIndex].Voltage;
  51. }
  52. /// <summary>
  53. /// Returns the cable's next voltage to cycle to being used by a power-switchable device.
  54. /// </summary>
  55. public SwitchableVoltage GetNextVoltage(EntityUid uid, PowerSwitchableComponent? comp = null)
  56. {
  57. if (!Resolve(uid, ref comp))
  58. return default;
  59. return comp.Cables[NextIndex(uid, comp)].Voltage;
  60. }
  61. }