using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
///
/// A battery takes what the panels have to spare and gives it back when they fall short, so machines keep running through
/// the night at whatever the battery can deliver. It charges and empties over time, at no more than its rated power.
///
public class PowerBattery : PowerNode, IPieceState
{
public Transform gauge;
public Renderer lamp;
public readonly NetworkVariable Stored = new NetworkVariable();
/// Kilowatts going in, or out if below zero.
public readonly NetworkVariable Flow = new NetworkVariable();
MaterialPropertyBlock _mpb;
Vector3 _gaugeScale = Vector3.one;
AudioSource _hum;
bool _wasFull;
static readonly int Emission = Shader.PropertyToID("_EmissionColor");
BatteryDef Def => GameDefs.Battery(Piece.piece);
public override float Radius => Def.radius;
public float Capacity => Def.kwh;
public float Rate => Def.kw;
/// How many times its output rating a battery may charge at.
public const float ChargeBoost = 6f;
public bool Full => Stored.Value >= Capacity - 0.001f;
public float[] State
{
get => new[] { Stored.Value };
set { if (value != null && value.Length > 0) Stored.Value = Mathf.Min(value[0], Capacity); }
}
public override string Prompt
{
get
{
string doing = Flow.Value > 0.01f ? Loc.F("charging at {0:0.#} kW", Flow.Value) : Flow.Value < -0.01f ? Loc.F("giving {0:0.#} kW, {1:0} min left at this rate", -Flow.Value, Stored.Value / -Flow.Value * 60f) : Full ? Loc.T("full") : Loc.T("resting");
return Loc.F("{0}: {1:0.0} of {2:0} kWh, {3}. {4}", Piece.Def.name, Stored.Value, Capacity, doing, Network);
}
}
/// Server: moves kilowatts for dt seconds, in or out if below zero. Returns what really moved. Kilowatt-hours are real hours.
public float Charge(float kw, float dt)
{
float can = kw >= 0f ? (Capacity - Stored.Value) * 3600f / dt : Stored.Value * 3600f / dt;
// they take charge much faster than they give it: a rack fills from spare sun in a few minutes, not half an hour
float done = Mathf.Sign(kw) * Mathf.Min(Mathf.Abs(kw), kw >= 0f ? Rate * ChargeBoost : Rate, can);
if (done != 0f) Stored.Value = Mathf.Clamp(Stored.Value + done * dt / 3600f, 0f, Capacity);
Write(Flow, done);
return done;
}
void Awake()
{
_mpb = new MaterialPropertyBlock();
if (gauge) _gaugeScale = gauge.localScale;
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
_hum = Sfx.Loop("battery_hum_loop", transform, 0f);
_wasFull = Full;
}
void Update()
{
float full = Capacity > 0f ? Stored.Value / Capacity : 0f;
if (_hum) _hum.volume = Mathf.MoveTowards(_hum.volume, Mathf.Abs(Flow.Value) > 0.01f ? 0.3f * Sfx.SfxVolume : 0f, Time.deltaTime * 0.6f);
if (Full && !_wasFull) Sfx.PlayAt("battery_full", transform.position + transform.up, 0.5f);
_wasFull = Full;
if (gauge)
{
var s = _gaugeScale;
s.y *= Mathf.Max(0.02f, full);
gauge.localScale = s;
}
if (!lamp) return;
var c = Flow.Value > 0.01f ? new Color(0.2f, 1f, 0.35f) : Flow.Value < -0.01f ? new Color(1f, 0.7f, 0.15f) : full > 0.02f ? new Color(0.2f, 0.5f, 1f) : new Color(1f, 0.15f, 0.1f);
lamp.GetPropertyBlock(_mpb);
_mpb.SetColor(Emission, c * 2.2f);
lamp.SetPropertyBlock(_mpb);
}
}
}