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). On a belt it is on the Riding layer.
public Conveyor Belt
{
get => _belt;
set
{
bool was = !ReferenceEquals(_belt, null), now = !ReferenceEquals(value, null);
_belt = value;
if (was != now) Ride(now);
}
}
Conveyor _belt;
Transform[] _parts;
void Ride(bool riding)
{
if (_parts == null) _parts = GetComponentsInChildren(true);
int layer = riding ? Layers.Riding : Layers.Prop;
foreach (var t in _parts)
if (t && (t.gameObject.layer == Layers.Prop || t.gameObject.layer == Layers.Riding)) t.gameObject.layer = layer;
}
public Grabbable Grab => _g ? _g : (_g = GetComponent());
void Awake()
{
_g = GetComponent();
// what it looks like, for CargoTick to draw it with all the others
Look = GetComponentInChildren(true);
var mf = Look ? Look.GetComponent() : null;
LookMesh = mf ? mf.sharedMesh : null;
LookMats = Look ? Look.sharedMaterials : null;
}
public MeshRenderer Look { get; private set; }
public Mesh LookMesh { get; private set; }
public Material[] LookMats { get; private set; }
/// 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 (_belt) return;
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;
}
}
}