A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
70 lines
2.9 KiB
C#
70 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// A piece of the power network: a panel, a battery or a pole. Nodes join up with whatever lies inside the longer of
|
|
/// their radii, machines with any node that reaches them, and everything joined shares one supply.
|
|
/// </summary>
|
|
public abstract class PowerNode : NetworkBehaviour, IInteractable
|
|
{
|
|
public static readonly List<PowerNode> All = new List<PowerNode>();
|
|
|
|
/// <summary>What the whole network this node is on is doing, in kilowatts and kilowatt-hours.</summary>
|
|
public readonly NetworkVariable<float> Supply = new NetworkVariable<float>();
|
|
public readonly NetworkVariable<float> Asked = new NetworkVariable<float>();
|
|
public readonly NetworkVariable<float> Held = new NetworkVariable<float>();
|
|
public readonly NetworkVariable<float> Cap = new NetworkVariable<float>();
|
|
|
|
BuildPiece _piece;
|
|
|
|
protected BuildPiece Piece => _piece ? _piece : (_piece = GetComponent<BuildPiece>());
|
|
public abstract float Radius { get; }
|
|
/// <summary>Where a wire ties on.</summary>
|
|
public virtual float WireHeight => 1.6f;
|
|
/// <summary>Kilowatts the sun gives it right now.</summary>
|
|
public virtual float Sunlight => 0f;
|
|
|
|
public abstract string Prompt { get; }
|
|
public bool CanInteract => false;
|
|
public void Interact() { }
|
|
|
|
/// <summary>The network in a few words, for the prompt.</summary>
|
|
protected string Network
|
|
{
|
|
get
|
|
{
|
|
bool batteries = Cap.Value > 0f;
|
|
if (Asked.Value <= 0f)
|
|
return batteries ? Loc.F("nothing to run, {0:0.#} kW free, batteries {1:0.0} of {2:0} kWh", Supply.Value, Held.Value, Cap.Value) : Loc.F("nothing to run, {0:0.#} kW free", Supply.Value);
|
|
float at = Mathf.Clamp01(Supply.Value / Asked.Value) * 100f;
|
|
return batteries ? Loc.F("machines at {0:0}%, {1:0.#} of {2:0.#} kW, batteries {3:0.0} of {4:0} kWh", at, Supply.Value, Asked.Value, Held.Value, Cap.Value) : Loc.F("machines at {0:0}%, {1:0.#} of {2:0.#} kW", at, Supply.Value, Asked.Value);
|
|
}
|
|
}
|
|
|
|
public override void OnNetworkSpawn() => All.Add(this);
|
|
public override void OnNetworkDespawn() => All.Remove(this);
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
All.Remove(this);
|
|
base.OnDestroy();
|
|
}
|
|
|
|
public void Report(float supply, float asked, float held, float cap)
|
|
{
|
|
Write(Supply, supply);
|
|
Write(Asked, asked);
|
|
Write(Held, held);
|
|
Write(Cap, cap);
|
|
}
|
|
|
|
protected static void Write(NetworkVariable<float> v, float value)
|
|
{
|
|
if (Mathf.Abs(v.Value - value) > 0.01f || (value == 0f && v.Value != 0f)) v.Value = value;
|
|
}
|
|
}
|
|
}
|