APC GUI. (#107)
This commit is contained in:
committed by
GitHub
parent
bb5a278fdb
commit
d414ea55f5
@@ -1,23 +1,42 @@
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using Content.Server.GameObjects.EntitySystems;
|
||||
using Content.Shared.GameObjects.Components.Power;
|
||||
using SS14.Server.GameObjects;
|
||||
using SS14.Shared.GameObjects;
|
||||
using SS14.Server.GameObjects.Components.UserInterface;
|
||||
using SS14.Server.Interfaces.GameObjects;
|
||||
using SS14.Shared.GameObjects.Components.UserInterface;
|
||||
using SS14.Shared.Interfaces.GameObjects;
|
||||
|
||||
namespace Content.Server.GameObjects.Components.Power
|
||||
{
|
||||
public class ApcComponent : Component
|
||||
public sealed class ApcComponent : SharedApcComponent, IAttackHand
|
||||
{
|
||||
public override string Name => "Apc";
|
||||
|
||||
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(BoundUserInterfaceMessage obj)
|
||||
{
|
||||
if (obj is ApcToggleMainBreakerMessage)
|
||||
{
|
||||
_provider.MainBreaker = !_provider.MainBreaker;
|
||||
_uiDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnUpdate()
|
||||
@@ -28,23 +47,68 @@ namespace Content.Server.GameObjects.Components.Power
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
ApcChargeState CalcChargeState()
|
||||
private ApcChargeState CalcChargeState()
|
||||
{
|
||||
var storageCharge = Storage.GetChargeState();
|
||||
if (storageCharge == ChargeState.Discharging)
|
||||
switch (storageCharge)
|
||||
{
|
||||
return ApcChargeState.Lack;
|
||||
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;
|
||||
}
|
||||
|
||||
if (storageCharge == ChargeState.Charging)
|
||||
var net = node.Parent;
|
||||
if (net.LastTotalAvailable <= 0)
|
||||
{
|
||||
return ApcChargeState.Charging;
|
||||
return ApcExternalPowerState.None;
|
||||
}
|
||||
|
||||
// Still.
|
||||
return Storage.Full ? ApcChargeState.Full : ApcChargeState.Lack;
|
||||
return net.Lack > 0 ? ApcExternalPowerState.Low : ApcExternalPowerState.Good;
|
||||
}
|
||||
|
||||
bool IAttackHand.Attackhand(IEntity user)
|
||||
{
|
||||
if (!user.TryGetComponent(out IActorComponent actor))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_userInterface.Open(actor.playerSession);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user