* Refactor battery/powercell assets and add new ones. * committing before I fuck things up * slot component doned I think * dictionary update * Fixes * Moving flashlight to powerslotcomponent * har har i am using the message tubes * Better documentation comment * Reverting this overengineered garbage. * Off with ye I said * Examine texts. * Some minor fixes to IDE complaints * slot size from yaml * Ignored component + removing a useless typo entry * Making stunbatons use this * Handle the message and remove some unnecessary dirtiness * actionblocker checks * remove unused file * remove updatevisual * make these nullable * make these nullable too * Unrename sprite folder * check itemcomponent on insertion * Use SendMessage over Owner.SendMessage * Add support for auto-recharging batteries, an auto-recharging cell, and make flashlight status update correctly if one is inserted in it. * get rid of public fields which are Bad * add a description for the stun baton while i'm in here * one more public field * Add the blinky animation to the atomic cell * Fix the charge indicator being STUPID * better comments * this is a better function * add pause for flashlight, remove unnecessary imports from battery * potato battery copyright link * WHO DID THAT * mr clean has come * Random pitch * pausing * round to nearest levels
138 lines
4.1 KiB
C#
138 lines
4.1 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";
|
|
|
|
/// <summary>
|
|
/// Maximum charge of the battery in joules (ie. watt seconds)
|
|
/// </summary>
|
|
[ViewVariables(VVAccess.ReadWrite)] public int MaxCharge { get => _maxCharge; set => SetMaxCharge(value); }
|
|
private int _maxCharge;
|
|
|
|
/// <summary>
|
|
/// Current charge of the battery in joules (ie. watt seconds)
|
|
/// </summary>
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
public float CurrentCharge { get => _currentCharge; set => SetCurrentCharge(value); }
|
|
private float _currentCharge;
|
|
|
|
/// <summary>
|
|
/// True if the battery is fully charged.
|
|
/// </summary>
|
|
[ViewVariables] public bool IsFullyCharged => MathHelper.CloseTo(CurrentCharge, MaxCharge);
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public bool AutoRecharge { get; set; }
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public float AutoRechargeRate { get; set; }
|
|
|
|
[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);
|
|
serializer.DataField(this, x => x.AutoRecharge, "autoRecharge", false);
|
|
serializer.DataField(this, x => x.AutoRechargeRate, "autoRechargeRate", 0);
|
|
}
|
|
|
|
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 (IsFullyCharged)
|
|
{
|
|
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 void OnUpdate(float frameTime)
|
|
{
|
|
if (!AutoRecharge) return;
|
|
if (IsFullyCharged) return;
|
|
CurrentCharge += AutoRechargeRate * frameTime;
|
|
}
|
|
}
|
|
|
|
public enum BatteryState
|
|
{
|
|
Full,
|
|
PartlyFull,
|
|
Empty
|
|
}
|
|
}
|