using System.Collections.Generic; using Unity.Netcode; using UnityEngine; namespace GrassCutter { /// /// A solar panel feeds its network with what the sun gives it: a fixed panel less when the sun is low, a tracker its full /// power all day, nothing at night. Keeping power for the dark takes a battery. /// public class SolarPanel : PowerNode { public readonly NetworkVariable Output = new NetworkVariable(); Transform _head; Quaternion _rest; PanelDef Def => GameDefs.Panel(Piece.piece); public override float Radius => Def.radius; public override float Sunlight { get { float h = Sun.Height(Planet.Up(transform.position)); float kw = Def.kw * (Def.tracker ? Mathf.Clamp01(h * 8f) : Mathf.Clamp01(h * 1.4f)) * (TeamState.Instance ? WeatherFx.SolarOf(TeamState.Instance.WeatherNow) : 1f); if (IsServer) Write(Output, kw); return kw; } } public override string Prompt => Loc.F("{0}: {1:0.#} of {2:0.#} kW, reaches {3:0} m. {4}", Piece.Def.name, Output.Value, Def.kw, Def.radius, Network); void Awake() { _head = Build(transform, GetComponent().piece); _rest = Quaternion.Inverse(transform.rotation) * _head.rotation; var box = gameObject.AddComponent(); box.center = Vector3.up * 1.5f; box.size = new Vector3(1.2f, 3f, 1.2f); } void Update() { // the tracker's panel follows the sun, on every peer if (!Def.tracker || !_head) return; var up = Planet.Up(transform.position); var aim = Sun.Height(up) > 0f ? Sun.Dir : up; _head.rotation = Quaternion.RotateTowards(_head.rotation, Quaternion.FromToRotation(Vector3.up, aim) * _rest, 12f * Time.deltaTime); } /// The model, shared with the ghost. Returns the part that carries the cells. public static Transform Build(Transform root, PieceId id) { var inst = Instantiate(AssetBank.I.Model(id == PieceId.SolarS ? "SM_SolarPost" : id == PieceId.SolarM ? "SM_SolarPair" : "SM_SolarTracker"), root); inst.transform.localPosition = Vector3.zero; Transform head = inst.transform; foreach (var t in inst.GetComponentsInChildren()) { t.gameObject.layer = root.gameObject.layer; if (t.name == "Head") head = t; } return head; } } }