using System.Collections.Generic; using Unity.Netcode; using UnityEngine; namespace GrassCutter { /// A building that holds loose grass and sends it out of its ports as bundles, one every fifth of a second. public abstract class SiloPiece : NetworkBehaviour, IInteractable, IPorts, IPieceState { const float Every = 0.2f; /// Everything a mower can unload onto: garages and unload bays. public static readonly List Pads = new List(); /// Is this spot on the part a mower drives onto to unload? public virtual bool OnPad(Vector3 p) => false; /// The middle of that part, for markers. public virtual Vector3 PadSpot => transform.position; public virtual string PadName => Loc.T("Unload"); public static SiloPiece PadUnder(Vector3 p) { foreach (var s in Pads) if (s && s.IsSpawned && s.OnPad(p)) return s; return null; } public override void OnNetworkSpawn() { if (!Pads.Contains(this)) Pads.Add(this); } public override void OnNetworkDespawn() => Pads.Remove(this); public override void OnDestroy() { Pads.Remove(this); base.OnDestroy(); } public readonly NetworkVariable Silo = new NetworkVariable(); float _nextOut; int _turn; public abstract string Prompt { get; } public virtual bool CanInteract => TeamState.Instance; public abstract void Interact(); public abstract void Ports(List into); public float[] State { get => new[] { Silo.Value }; set { if (value != null && value.Length > 0) Silo.Value = value[0]; } } [Rpc(SendTo.Server)] public void TakeRpc(float kg) { if (kg > 0f) Silo.Value += kg; } protected virtual void Update() { if (!IsServer || !IsSpawned || Time.time < _nextOut) return; var piece = GetComponent(); _nextOut = Time.time + Every / (piece ? piece.TierMul : 1f); // hatches shut: it only fills up if (piece && piece.Switched) return; BeltNet.Drain(GetComponent(), Silo, ref _turn); } } }