Files
tbd-station-14/Content.Server/GameObjects/Components/Power/BatteryComponent.cs
SoulSloth df823d2245 Add Flashlight Visualizer/states (#1861)
* Add art assets for cloning

* Added a 'Scan DNA' button to the medical scanner

* Made the UI update unconditional for the medical scanner until checks for power changes are in place

* Update Medical scanner to reflect powered status and fix #1774

* added a 'scan dna' button the the medical scanner that will add the contained bodies Uid to a list in CloningSystem, fixed an issue with the menu not populating if the scanner starts in an unpowered state

* Add disabling logic to 'Scan DNA' button on medical scanner

* Removed un-used libraries

* Added playing parameter to radiatingLightComponent, changed it's animation key to radiatingLight

* refactored RadiatingLight into a visualizer

* Added different light animations for differnt power states of a flashlight

* split out the radiating light visualizer into two seperate visualizers

* further refactored and tweaked handheldlight animations

* further lantern light tweaks

* removed un-used attributes in flashlight and lantern prototypes

* fix null check in handheldlightcomponent
2020-08-24 12:32:18 +02:00

115 lines
3.2 KiB
C#

using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Server.GameObjects.Components.Power
{
[RegisterComponent]
public class BatteryComponent : Component
{
public override string Name => "Battery";
[ViewVariables(VVAccess.ReadWrite)] public int MaxCharge { get => _maxCharge; set => SetMaxCharge(value); }
private int _maxCharge;
[ViewVariables(VVAccess.ReadWrite)]
public float CurrentCharge { get => _currentCharge; set => SetCurrentCharge(value); }
private float _currentCharge;
[ViewVariables] public BatteryState BatteryState { get; private set; }
public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
serializer.DataField(ref _maxCharge, "maxCharge", 1000);
serializer.DataField(ref _currentCharge, "startingCharge", 500);
}
public override void Initialize()
{
base.Initialize();
UpdateStorageState();
}
/// <summary>
/// If sufficient charge is avaiable on the battery, use it. Otherwise, don't.
/// </summary>
public bool TryUseCharge(float chargeToUse)
{
if (chargeToUse >= CurrentCharge)
{
return false;
}
else
{
CurrentCharge -= chargeToUse;
return true;
}
}
public float UseCharge(float toDeduct)
{
var chargeChangedBy = Math.Min(CurrentCharge, toDeduct);
CurrentCharge -= chargeChangedBy;
return chargeChangedBy;
}
public void FillFrom(BatteryComponent battery)
{
var powerDeficit = MaxCharge - CurrentCharge;
if (battery.TryUseCharge(powerDeficit))
{
CurrentCharge += powerDeficit;
}
else
{
CurrentCharge += battery.CurrentCharge;
battery.CurrentCharge = 0;
}
}
protected virtual void OnChargeChanged() { }
private void UpdateStorageState()
{
if (CurrentCharge == MaxCharge)
{
BatteryState = BatteryState.Full;
}
else if (CurrentCharge == 0)
{
BatteryState = BatteryState.Empty;
}
else
{
BatteryState = BatteryState.PartlyFull;
}
}
private void SetMaxCharge(int newMax)
{
_maxCharge = Math.Max(newMax, 0);
_currentCharge = Math.Min(_currentCharge, MaxCharge);
UpdateStorageState();
OnChargeChanged();
}
private void SetCurrentCharge(float newChargeAmount)
{
_currentCharge = MathHelper.Clamp(newChargeAmount, 0, MaxCharge);
UpdateStorageState();
OnChargeChanged();
}
}
public enum BatteryState
{
Full,
PartlyFull,
Empty
}
}