Files
OldThink/Content.Server/Light/EntitySystems/HandheldLightSystem.cs

207 lines
7.9 KiB
C#
Raw Normal View History

2021-12-27 18:15:16 +11:00
using Content.Server.Popups;
using Content.Server.PowerCell;
2021-12-27 18:15:16 +11:00
using Content.Shared.Examine;
using Content.Shared.Interaction;
using Content.Shared.Light;
2021-12-27 18:15:16 +11:00
using Content.Shared.Rounding;
using Content.Shared.Toggleable;
2021-12-27 18:15:16 +11:00
using Content.Shared.Verbs;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
2021-12-27 18:15:16 +11:00
using Robust.Shared.Utility;
namespace Content.Server.Light.EntitySystems
{
[UsedImplicitly]
public sealed class HandheldLightSystem : SharedHandheldLightSystem
2021-12-27 18:15:16 +11:00
{
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly PowerCellSystem _powerCell = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
2023-01-10 07:01:57 -05:00
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
2021-12-27 18:15:16 +11:00
// TODO: Ideally you'd be able to subscribe to power stuff to get events at certain percentages.. or something?
// But for now this will be better anyway.
private readonly HashSet<HandheldLightComponent> _activeLights = new();
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<HandheldLightComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<HandheldLightComponent, ExaminedEvent>(OnExamine);
SubscribeLocalEvent<HandheldLightComponent, GetVerbsEvent<ActivationVerb>>(AddToggleLightVerb);
SubscribeLocalEvent<HandheldLightComponent, ActivateInWorldEvent>(OnActivate);
SubscribeLocalEvent<HandheldLightComponent, ToggleActionEvent>(OnToggleAction);
}
private void OnToggleAction(EntityUid uid, HandheldLightComponent component, ToggleActionEvent args)
{
if (args.Handled)
return;
if (component.Activated)
TurnOff(uid, component);
else
TurnOn(args.Performer, uid, component);
args.Handled = true;
2021-12-27 18:15:16 +11:00
}
private void OnGetState(EntityUid uid, HandheldLightComponent component, ref ComponentGetState args)
{
args.State = new HandheldLightComponent.HandheldLightComponentState(component.Activated, GetLevel(uid, component));
2021-12-27 18:15:16 +11:00
}
private byte? GetLevel(EntityUid uid, HandheldLightComponent component)
2021-12-27 18:15:16 +11:00
{
// Curently every single flashlight has the same number of levels for status and that's all it uses the charge for
// Thus we'll just check if the level changes.
if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery))
return null;
2021-12-27 18:15:16 +11:00
if (MathHelper.CloseToPercent(battery.CurrentCharge, 0) || component.Wattage > battery.CurrentCharge)
2021-12-27 18:15:16 +11:00
return 0;
return (byte?) ContentHelpers.RoundToNearestLevels(battery.CurrentCharge / battery.MaxCharge * 255, 255, HandheldLightComponent.StatusLevels);
2021-12-27 18:15:16 +11:00
}
private void OnActivate(EntityUid uid, HandheldLightComponent component, ActivateInWorldEvent args)
2021-12-27 18:15:16 +11:00
{
if (args.Handled)
return;
2021-12-27 18:15:16 +11:00
if (ToggleStatus(args.User, uid, component))
2021-12-27 18:15:16 +11:00
args.Handled = true;
}
/// <summary>
/// Illuminates the light if it is not active, extinguishes it if it is active.
/// </summary>
/// <returns>True if the light's status was toggled, false otherwise.</returns>
public bool ToggleStatus(EntityUid user, EntityUid uid, HandheldLightComponent component)
2021-12-27 18:15:16 +11:00
{
return component.Activated ? TurnOff(uid, component) : TurnOn(user, uid, component);
2021-12-27 18:15:16 +11:00
}
private void OnExamine(EntityUid uid, HandheldLightComponent component, ExaminedEvent args)
{
args.PushMarkup(component.Activated
? Loc.GetString("handheld-light-component-on-examine-is-on-message")
: Loc.GetString("handheld-light-component-on-examine-is-off-message"));
}
private void AddToggleLightVerb(EntityUid uid, HandheldLightComponent component, GetVerbsEvent<ActivationVerb> args)
2021-12-27 18:15:16 +11:00
{
if (!args.CanAccess || !args.CanInteract)
return;
2021-12-27 18:15:16 +11:00
ActivationVerb verb = new()
2021-12-27 18:15:16 +11:00
{
Text = Loc.GetString("verb-common-toggle-light"),
Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/light.svg.192dpi.png")),
2021-12-27 18:15:16 +11:00
Act = component.Activated
? () => TurnOff(uid, component)
: () => TurnOn(args.User, uid, component)
2021-12-27 18:15:16 +11:00
};
args.Verbs.Add(verb);
}
public bool TurnOff(EntityUid uid, HandheldLightComponent component, bool makeNoise = true)
2021-12-27 18:15:16 +11:00
{
if (!component.Activated || !TryComp<PointLightComponent>(uid, out var pointLightComponent))
{
return false;
}
2021-12-27 18:15:16 +11:00
pointLightComponent.Enabled = false;
SetActivated(uid, false, component, makeNoise);
component.Level = null;
2021-12-27 18:15:16 +11:00
_activeLights.Remove(component);
return true;
}
public bool TurnOn(EntityUid user, EntityUid uid, HandheldLightComponent component)
2021-12-27 18:15:16 +11:00
{
if (component.Activated || !TryComp<PointLightComponent>(uid, out var pointLightComponent))
{
return false;
}
2021-12-27 18:15:16 +11:00
if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery) &&
!TryComp(uid, out battery))
2021-12-27 18:15:16 +11:00
{
_audio.PlayPvs(_audio.GetSound(component.TurnOnFailSound), uid);
_popup.PopupEntity(Loc.GetString("handheld-light-component-cell-missing-message"), uid, user);
2021-12-27 18:15:16 +11:00
return false;
}
// To prevent having to worry about frame time in here.
// Let's just say you need a whole second of charge before you can turn it on.
// Simple enough.
if (component.Wattage > battery.CurrentCharge)
2021-12-27 18:15:16 +11:00
{
_audio.PlayPvs(_audio.GetSound(component.TurnOnFailSound), uid);
_popup.PopupEntity(Loc.GetString("handheld-light-component-cell-dead-message"), uid, user);
2021-12-27 18:15:16 +11:00
return false;
}
pointLightComponent.Enabled = true;
SetActivated(uid, true, component, true);
2021-12-27 18:15:16 +11:00
_activeLights.Add(component);
2021-12-27 18:15:16 +11:00
return true;
}
public void TryUpdate(EntityUid uid, HandheldLightComponent component, float frameTime)
2021-12-27 18:15:16 +11:00
{
if (!_powerCell.TryGetBatteryFromSlot(uid, out var battery) &&
!TryComp(uid, out battery))
2021-12-27 18:15:16 +11:00
{
TurnOff(uid, component, false);
2021-12-27 18:15:16 +11:00
return;
}
var appearanceComponent = EntityManager.GetComponentOrNull<AppearanceComponent>(uid);
2021-12-27 18:15:16 +11:00
var fraction = battery.CurrentCharge / battery.MaxCharge;
if (fraction >= 0.30)
2021-12-27 18:15:16 +11:00
{
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.FullPower, appearanceComponent);
2021-12-27 18:15:16 +11:00
}
else if (fraction >= 0.10)
2021-12-27 18:15:16 +11:00
{
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.LowPower, appearanceComponent);
2021-12-27 18:15:16 +11:00
}
else
{
_appearance.SetData(uid, HandheldLightVisuals.Power, HandheldLightPowerStates.Dying, appearanceComponent);
2021-12-27 18:15:16 +11:00
}
if (component.Activated && !battery.TryUseCharge(component.Wattage * frameTime))
TurnOff(uid, component, false);
2021-12-27 18:15:16 +11:00
UpdateLevel(uid, component);
}
2021-12-27 18:15:16 +11:00
private void UpdateLevel(EntityUid uid, HandheldLightComponent comp)
{
var level = GetLevel(uid, comp);
if (level == comp.Level)
return;
comp.Level = level;
Dirty(comp);
2021-12-27 18:15:16 +11:00
}
}
}