EnergySwordSystem.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Content.Shared.Interaction;
  2. using Content.Shared.Light;
  3. using Content.Shared.Light.Components;
  4. using Content.Shared.Toggleable;
  5. using Content.Shared.Tools.Systems;
  6. using Robust.Shared.Random;
  7. namespace Content.Shared.Weapons.Melee.EnergySword;
  8. public sealed class EnergySwordSystem : EntitySystem
  9. {
  10. [Dependency] private readonly SharedRgbLightControllerSystem _rgbSystem = default!;
  11. [Dependency] private readonly SharedAppearanceSystem _appearance = default!;
  12. [Dependency] private readonly IRobustRandom _random = default!;
  13. [Dependency] private readonly SharedToolSystem _toolSystem = default!;
  14. public override void Initialize()
  15. {
  16. base.Initialize();
  17. SubscribeLocalEvent<EnergySwordComponent, MapInitEvent>(OnMapInit);
  18. SubscribeLocalEvent<EnergySwordComponent, InteractUsingEvent>(OnInteractUsing);
  19. }
  20. // Used to pick a random color for the blade on map init.
  21. private void OnMapInit(Entity<EnergySwordComponent> entity, ref MapInitEvent args)
  22. {
  23. if (entity.Comp.ColorOptions.Count != 0)
  24. {
  25. entity.Comp.ActivatedColor = _random.Pick(entity.Comp.ColorOptions);
  26. Dirty(entity);
  27. }
  28. if (!TryComp(entity, out AppearanceComponent? appearanceComponent))
  29. return;
  30. _appearance.SetData(entity, ToggleableLightVisuals.Color, entity.Comp.ActivatedColor, appearanceComponent);
  31. }
  32. // Used to make the blade multicolored when using a multitool on it.
  33. private void OnInteractUsing(Entity<EnergySwordComponent> entity, ref InteractUsingEvent args)
  34. {
  35. if (args.Handled)
  36. return;
  37. if (!_toolSystem.HasQuality(args.Used, SharedToolSystem.PulseQuality))
  38. return;
  39. args.Handled = true;
  40. entity.Comp.Hacked = !entity.Comp.Hacked;
  41. if (entity.Comp.Hacked)
  42. {
  43. var rgb = EnsureComp<RgbLightControllerComponent>(entity);
  44. _rgbSystem.SetCycleRate(entity, entity.Comp.CycleRate, rgb);
  45. }
  46. else
  47. RemComp<RgbLightControllerComponent>(entity);
  48. Dirty(entity);
  49. }
  50. }