Files
grass-cutters/Assets/Scripts/Gameplay/Cargo.cs
NiemandKG bbf73a6b96 Frame time on a big farm, round two: riding layer, instanced bundles
- Bundles on a belt move to a Riding layer that ignores the belts and the
  static world: every belt's catch trigger called OnTriggerStay (with a
  GetComponent) for each bundle riding it, every physics step.
- Bundles draw as instanced batches from CargoTick (their own renderers off,
  one in someone's hands draws itself), not as 2000 renderers to cull, sort
  and draw in the view and every shadow cascade.
- The main camera skips front-to-back sorting: the GPU has time to spare.
- BeltNet.Belted and DronePad.Belted are cached until the build changes;
  splitters on the host look only at their own items.

On the player's Mommy save, 2560x1440: base 14-15 ms -> 7.7 ms (~130 fps),
meadow 11.5-12 ms -> 6.8-7.5 ms (~135-145 fps).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 20:35:35 +06:00

133 lines
5.4 KiB
C#

using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// 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.
/// </summary>
[RequireComponent(typeof(Grabbable))]
public class Cargo : NetworkBehaviour
{
public static readonly List<Cargo> All = new List<Cargo>();
public CargoKind kind;
/// <summary>Half height (ground contact checks).</summary>
public float halfHeight = 0.33f;
public readonly NetworkVariable<float> Kg = new NetworkVariable<float>(GameDefs.BundleKg);
Grabbable _g;
bool _wasInGrass;
public bool Loaded { get; set; }
/// <summary>Server: the belt this item is locked onto (null when loose). On a belt it is on the Riding layer.</summary>
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<Transform>(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<Grabbable>());
void Awake()
{
_g = GetComponent<Grabbable>();
// what it looks like, for CargoTick to draw it with all the others
Look = GetComponentInChildren<MeshRenderer>(true);
var mf = Look ? Look.GetComponent<MeshFilter>() : 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; }
/// <summary>Server: unlock from the belt and become a loose physics body again.</summary>
public void ServerLeaveBelt(Vector3 velocity)
{
if (Belt) Belt.ServerRemove(this, velocity);
}
/// <summary>E at the depot or a machine with this bundle in your arms.</summary>
[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<ICargoSink>();
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;
/// <summary>One physics step, called by <see cref="CargoTick"/> for every bundle at once: two thousand Unity
/// FixedUpdate messages cost more than the work in them.</summary>
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;
}
}
}