Files
grass-cutters/Assets/Scripts/Gameplay/Cargo.cs
NiemandKG 57d0f4d31a Frame time on a big farm: batched grass, sleeping bundles, no light probes
Measured in a real 2560x1440 window on the player's Mommy save (RTX 4090):
the GPU took ~5 ms, the main thread the rest.

- Grass: a fully mown sector draws only its floor (one draw, not up to six);
  untouched and fully mown sectors share a mow map look, so the fur pile
  draws them all in one instanced call per pile (GC_SECTOR_BUFFER, sector
  frames from a structured buffer). Grass draws 1112 -> 337 at the base,
  876 -> 439 in the meadow.
- Bundles: loose ones sleep when they come to rest (our gravity kept ~600
  bodies awake), ones on belts skip the ground checks, and all of them are
  stepped from one CargoTick instead of two Unity messages each.
- Belts leave a jammed line alone and only look every few steps whether it
  can move; splitters test distance before transforming every bundle.
- Light probes off on everything (the planet has none) and the GPU Resident
  Drawer off: both cost CPU per moving renderer for nothing.
- -demogpu N -onscreen measures frame time with one feature off at a time.

Base view 17-19 ms -> 14-15 ms, meadow 14-16 ms -> 11.5-12 ms.

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

101 lines
4.2 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).</summary>
public Conveyor Belt { get; set; }
public Grabbable Grab => _g ? _g : (_g = GetComponent<Grabbable>());
void Awake() { _g = GetComponent<Grabbable>(); }
/// <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 (!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;
}
}
}