Files
tbd-station-14/Content.Server/Light/EntitySystems/UnpoweredFlashlightSystem.cs
2023-05-07 16:08:03 +10:00

104 lines
4.0 KiB
C#

using Content.Server.Light.Events;
using Content.Server.Mind.Components;
using Content.Shared.Actions;
using Content.Shared.Decals;
using Content.Shared.Emag.Systems;
using Content.Shared.Light;
using Content.Shared.Light.Component;
using Content.Shared.Toggleable;
using Content.Shared.Verbs;
using Robust.Server.GameObjects;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Utility;
namespace Content.Server.Light.EntitySystems
{
public sealed class UnpoweredFlashlightSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedActionsSystem _actionsSystem = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<UnpoweredFlashlightComponent, GetVerbsEvent<ActivationVerb>>(AddToggleLightVerbs);
SubscribeLocalEvent<UnpoweredFlashlightComponent, GetItemActionsEvent>(OnGetActions);
SubscribeLocalEvent<UnpoweredFlashlightComponent, ToggleActionEvent>(OnToggleAction);
SubscribeLocalEvent<UnpoweredFlashlightComponent, MindAddedMessage>(OnMindAdded);
SubscribeLocalEvent<UnpoweredFlashlightComponent, GotEmaggedEvent>(OnGotEmagged);
}
private void OnToggleAction(EntityUid uid, UnpoweredFlashlightComponent component, ToggleActionEvent args)
{
if (args.Handled)
return;
ToggleLight(uid, component);
args.Handled = true;
}
private void OnGetActions(EntityUid uid, UnpoweredFlashlightComponent component, GetItemActionsEvent args)
{
args.Actions.Add(component.ToggleAction);
}
private void AddToggleLightVerbs(EntityUid uid, UnpoweredFlashlightComponent component, GetVerbsEvent<ActivationVerb> args)
{
if (!args.CanAccess || !args.CanInteract)
return;
ActivationVerb verb = new()
{
Text = Loc.GetString("toggle-flashlight-verb-get-data-text"),
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
Act = () => ToggleLight(uid, component),
Priority = -1 // For things like PDA's, Open-UI and other verbs that should be higher priority.
};
args.Verbs.Add(verb);
}
private void OnMindAdded(EntityUid uid, UnpoweredFlashlightComponent component, MindAddedMessage args)
{
_actionsSystem.AddAction(uid, component.ToggleAction, null);
}
private void OnGotEmagged(EntityUid uid, UnpoweredFlashlightComponent component, ref GotEmaggedEvent args)
{
if (!TryComp<PointLightComponent>(uid, out var light))
return;
if (_prototypeManager.TryIndex<ColorPalettePrototype>(component.EmaggedColorsPrototype, out var possibleColors))
{
var pick = _random.Pick(possibleColors.Colors.Values);
light.Color = pick;
}
args.Repeatable = true;
args.Handled = true;
}
public void ToggleLight(EntityUid uid, UnpoweredFlashlightComponent flashlight)
{
if (!TryComp<PointLightComponent>(uid, out var light))
return;
flashlight.LightOn = !flashlight.LightOn;
light.Enabled = flashlight.LightOn;
_appearance.SetData(uid, UnpoweredFlashlightVisuals.LightOn, flashlight.LightOn);
_audioSystem.PlayPvs(flashlight.ToggleSound, uid);
RaiseLocalEvent(uid, new LightToggleEvent(flashlight.LightOn), true);
_actionsSystem.SetToggled(flashlight.ToggleAction, flashlight.LightOn);
}
}
}