using System.Collections.Generic; using Unity.Netcode; using UnityEngine; namespace GrassCutter { /// /// A shippable item: grass bundle, hay or pressed brick. Physics + Grabbable carry it around; when it tumbles off the /// deck it lands in the meadow with a puff of clippings. A depot takes it the moment it drops in. /// [RequireComponent(typeof(Grabbable))] public class Cargo : NetworkBehaviour { public static readonly List All = new List(); public CargoKind kind; /// Half height (ground contact checks). public float halfHeight = 0.33f; public readonly NetworkVariable Kg = new NetworkVariable(GameDefs.BundleKg); Grabbable _g; bool _wasInGrass; public bool Loaded { get; set; } /// Server: the belt this item is locked onto (null when loose). public Conveyor Belt { get; set; } public Grabbable Grab => _g ? _g : (_g = GetComponent()); void Awake() { _g = GetComponent(); } /// Server: unlock from the belt and become a loose physics body again. public void ServerLeaveBelt(Vector3 velocity) { if (Belt) Belt.ServerRemove(this, velocity); } /// E at the depot or a machine with this bundle in your arms. [Rpc(SendTo.Server)] public void HandInRpc(ulong piece, RpcParams rpc = default) { if (Grab.Holder.Value != rpc.Receive.SenderClientId || Grab.Consumed) return; if (!NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(piece, out var no)) return; var sink = no.GetComponentInChildren(); if (sink == null || (transform.position - no.transform.position).sqrMagnitude > 49f) return; Grab.ServerForceRelease(); sink.ServerAccept(this, transform.position, Vector3.down); } public override void OnNetworkSpawn() { All.Add(this); CargoTick.Ensure(); if (Time.timeSinceLevelLoad > 3f) Tween.Scale(transform, Vector3.one * 0.3f, Vector3.one, 0.35f, Ease.OutBack); } public override void OnNetworkDespawn() { All.Remove(this); if (Belt) { Belt.ServerRemove(this, Vector3.zero); Belt = null; } } public override void OnDestroy() { All.Remove(this); base.OnDestroy(); } float _fallV; /// One physics step, called by for every bundle at once: two thousand Unity /// FixedUpdate messages cost more than the work in them. internal void Step() { if (!IsSpawned) return; var rb = _g.Body; // riding a belt or lying still: nothing below needs looking at (most of a big farm's bundles, every step) if (Belt || (rb && !rb.isKinematic && rb.IsSleeping())) { _fallV = 0f; _wasInGrass = !Belt; return; } var p = transform.position; // tossed into a depot if (IsServer && !_g.IsHeld && !_g.Consumed && !Belt) foreach (var d in Depot.All) if (d && (p - d.Mouth).sqrMagnitude < 1.1f) { d.ServerAccept(this, p, Vector3.down); return; } var up = Planet.Up(p); bool inGrass = Planet.Altitude(p) - halfHeight < 0.25f; // thump into the meadow: a puff of clippings if (inGrass && !_wasInGrass && _fallV < -2.5f) { Fx.Splash(Planet.Surface(p), Mathf.Clamp01(-_fallV / 10f) + 0.3f); Sfx.PlayAt(_fallV < -6f ? "splash_big" : "splash_small", p, 0.8f, 1.1f, 0.1f); } _wasInGrass = inGrass; _fallV = rb.isKinematic ? 0f : Vector3.Dot(rb.linearVelocity, up); if (!IsOwner || rb.isKinematic || _g.IsHeld || !inGrass) return; // the thick grass brakes rolling cargo rb.AddForce(-Vector3.ProjectOnPlane(rb.linearVelocity, up) * 2.2f, ForceMode.Acceleration); rb.angularVelocity *= 0.96f; } } }