Files
tbd-station-14/Content.Shared/Weapons/Melee/EnergySword/EnergySwordSystem.cs
Plykiya 0d9f9e9615 Make hacking energy swords predicted (#34877)
* Make hacking energy swords predicted

* Fix up component, add dirty call

* access

* Dirty Entity<T>

* false
2025-02-05 20:48:28 +01:00

62 lines
2.1 KiB
C#

using Content.Shared.Interaction;
using Content.Shared.Light;
using Content.Shared.Light.Components;
using Content.Shared.Toggleable;
using Content.Shared.Tools.Systems;
using Robust.Shared.Random;
namespace Content.Shared.Weapons.Melee.EnergySword;
public sealed class EnergySwordSystem : EntitySystem
{
[Dependency] private readonly SharedRgbLightControllerSystem _rgbSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedToolSystem _toolSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EnergySwordComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<EnergySwordComponent, InteractUsingEvent>(OnInteractUsing);
}
// Used to pick a random color for the blade on map init.
private void OnMapInit(Entity<EnergySwordComponent> entity, ref MapInitEvent args)
{
if (entity.Comp.ColorOptions.Count != 0)
{
entity.Comp.ActivatedColor = _random.Pick(entity.Comp.ColorOptions);
Dirty(entity);
}
if (!TryComp(entity, out AppearanceComponent? appearanceComponent))
return;
_appearance.SetData(entity, ToggleableLightVisuals.Color, entity.Comp.ActivatedColor, appearanceComponent);
}
// Used to make the blade multicolored when using a multitool on it.
private void OnInteractUsing(Entity<EnergySwordComponent> entity, ref InteractUsingEvent args)
{
if (args.Handled)
return;
if (!_toolSystem.HasQuality(args.Used, SharedToolSystem.PulseQuality))
return;
args.Handled = true;
entity.Comp.Hacked = !entity.Comp.Hacked;
if (entity.Comp.Hacked)
{
var rgb = EnsureComp<RgbLightControllerComponent>(entity);
_rgbSystem.SetCycleRate(entity, entity.Comp.CycleRate, rgb);
}
else
RemComp<RgbLightControllerComponent>(entity);
Dirty(entity);
}
}