Files
tbd-station-14/Content.Server/GameObjects/Components/Power/ApcComponent.cs
FL-OZ 53900b79e9 Rename SoundComponent and refactor its wrong usages. (#1036)
* Rename `SoundComponent` and refactor its wrong usages.

* Replace verbose IoC grabs with EntitySysetm.Get

* unused depend

Co-authored-by: FL-OZ <anotherscuffed@gmail.com>
Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
2020-05-31 19:40:36 +02:00

131 lines
4.3 KiB
C#

using Content.Server.GameObjects.EntitySystems;
using Content.Shared.GameObjects.Components.Power;
using Robust.Server.GameObjects;
using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Audio;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Systems;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.GameObjects.Components.Power
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public sealed class ApcComponent : SharedApcComponent, IActivate
{
#pragma warning disable 649
[Dependency] private readonly IEntitySystemManager _entitySystemManager;
#pragma warning restore 649
PowerStorageComponent Storage;
AppearanceComponent Appearance;
private PowerProviderComponent _provider;
ApcChargeState LastChargeState;
private float _lastCharge = 0f;
private ApcExternalPowerState _lastExternalPowerState;
private BoundUserInterface _userInterface;
private bool _uiDirty = true;
public override void Initialize()
{
base.Initialize();
Storage = Owner.GetComponent<PowerStorageComponent>();
Appearance = Owner.GetComponent<AppearanceComponent>();
_provider = Owner.GetComponent<PowerProviderComponent>();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>().GetBoundUserInterface(ApcUiKey.Key);
_userInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage serverMsg)
{
var obj = serverMsg.Message;
if (obj is ApcToggleMainBreakerMessage)
{
_provider.MainBreaker = !_provider.MainBreaker;
_uiDirty = true;
_clickSound();
}
}
public void OnUpdate()
{
var newState = CalcChargeState();
if (newState != LastChargeState)
{
LastChargeState = newState;
Appearance.SetData(ApcVisuals.ChargeState, newState);
}
var newCharge = Storage.Charge;
if (newCharge != _lastCharge)
{
_lastCharge = newCharge;
_uiDirty = true;
}
var extPowerState = CalcExtPowerState();
if (extPowerState != _lastExternalPowerState)
{
_lastExternalPowerState = extPowerState;
_uiDirty = true;
}
if (_uiDirty)
{
_userInterface.SetState(new ApcBoundInterfaceState(_provider.MainBreaker, extPowerState,
newCharge / Storage.Capacity));
_uiDirty = false;
}
}
private ApcChargeState CalcChargeState()
{
var storageCharge = Storage.GetChargeState();
switch (storageCharge)
{
case ChargeState.Discharging:
return ApcChargeState.Lack;
case ChargeState.Charging:
return ApcChargeState.Charging;
default:
// Still.
return Storage.Full ? ApcChargeState.Full : ApcChargeState.Lack;
}
}
private ApcExternalPowerState CalcExtPowerState()
{
if (!Owner.TryGetComponent(out PowerNodeComponent node) || node.Parent == null)
{
return ApcExternalPowerState.None;
}
var net = node.Parent;
if (net.LastTotalAvailable <= 0)
{
return ApcExternalPowerState.None;
}
return net.Lack > 0 ? ApcExternalPowerState.Low : ApcExternalPowerState.Good;
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out IActorComponent actor))
{
return;
}
_userInterface.Open(actor.playerSession);
}
private void _clickSound()
{
EntitySystem.Get<AudioSystem>().Play("/Audio/machines/machine_switch.ogg", AudioParams.Default.WithVolume(-2f));
}
}
}