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>
128 lines
5.7 KiB
C#
128 lines
5.7 KiB
C#
using UnityEngine;
|
|
|
|
namespace GrassCutter
|
|
{
|
|
/// <summary>
|
|
/// A belt comes in at the back and every bundle picks its way in the middle: right, left or straight on, only ways that
|
|
/// have something joined to them, and never one that's full, so one blocked belt doesn't hold up the others. E turns it into
|
|
/// a sorter: one kind of cargo goes left and the rest go the other ways. The flap swings toward the way a bundle is taking.
|
|
/// </summary>
|
|
public class BeltSplitter : Conveyor, IJunction
|
|
{
|
|
const float Middle = 0.5f;
|
|
|
|
public Transform paddle;
|
|
int _next;
|
|
float _paddleAngle, _paddleTarget;
|
|
Quaternion _paddleRest;
|
|
Cargo _lastSeen;
|
|
|
|
protected override void Start()
|
|
{
|
|
base.Start();
|
|
if (paddle) _paddleRest = paddle.localRotation;
|
|
}
|
|
|
|
public const int Overflow = 4;
|
|
public int ModeCount => 5;
|
|
public string ModeName(int mode) => mode == 0 ? Loc.T("deals bundles out in turn") : mode == Overflow ? Loc.T("straight on, to the sides only when that's full") : (CargoKind)(mode - 1) == CargoKind.Grass ? Loc.T("Grass goes left, the rest the other ways") : (CargoKind)(mode - 1) == CargoKind.Hay ? Loc.T("Hay goes left, the rest the other ways") : Loc.T("Brick goes left, the rest the other ways");
|
|
public Color ModeColor(int mode) => mode == 1 ? new Color(0.35f, 0.8f, 0.25f) : mode == 2 ? new Color(0.95f, 0.75f, 0.2f) : mode == 3 ? new Color(0.75f, 0.3f, 0.15f) : mode == Overflow ? new Color(0.25f, 0.7f, 0.95f) : Color.white;
|
|
|
|
// route 0 = out to the right, route 1 = out to the left, route 2 = straight on
|
|
const int Left = 1;
|
|
protected override int RouteCount => 3;
|
|
protected override float Turn => Middle;
|
|
static float Side(int route) => route == 0 ? 1f : route == Left ? -1f : 0f;
|
|
|
|
protected override Vector2 PathAt(float d, int route) =>
|
|
d < Middle || route == 2 ? new Vector2(0f, d - Middle) : new Vector2(Side(route) * (d - Middle), 0f);
|
|
|
|
protected override int EntryRoute(Cargo c, int port) => 2;
|
|
|
|
protected override int RouteAt(Vector2 local, Cargo c) => local.y > -0.1f && Mathf.Abs(local.x) > 0.1f ? (local.x > 0f ? 0 : Left) : 2;
|
|
|
|
/// <summary>Bundles on different ways don't queue behind each other once they're past the middle.</summary>
|
|
protected override bool Apart(Slot ahead, Slot s) => ahead.set && (s.set ? ahead.route != s.route : ahead.d > Middle + 0.1f);
|
|
|
|
bool Free(int route)
|
|
{
|
|
if (!ExitOpen(route)) return false;
|
|
foreach (var o in Items) if (o.set && o.route == route && o.d < Middle + Spacing) return false;
|
|
return true;
|
|
}
|
|
|
|
bool Send(Slot s, int route)
|
|
{
|
|
if (!Free(route)) return false;
|
|
s.route = route;
|
|
return true;
|
|
}
|
|
|
|
protected override bool Choose(Slot s)
|
|
{
|
|
int mode = Mode;
|
|
if (mode == Overflow)
|
|
{
|
|
// straight on first; only a full or missing way ahead spills to the sides
|
|
if (Connected(2) && Free(2)) { s.route = 2; return true; }
|
|
for (int k = 0; k < 2; k++)
|
|
{
|
|
int r = (_next + k) % 2;
|
|
if (Connected(r) && Free(r)) { _next = (r + 1) % 2; s.route = r; return true; }
|
|
}
|
|
return !Connected(0) && !Connected(Left) && !Connected(2) && Send(s, 2);
|
|
}
|
|
if (mode > 0 && (int)s.c.kind == mode - 1) return Send(s, Left);
|
|
bool joined = false;
|
|
for (int k = 0; k < 3; k++)
|
|
{
|
|
int r = (_next + k) % 3;
|
|
if (mode > 0 && r == Left) continue;
|
|
if (!Connected(r)) continue;
|
|
joined = true;
|
|
if (!Free(r)) continue;
|
|
_next = (r + 1) % 3;
|
|
s.route = r;
|
|
return true;
|
|
}
|
|
// nothing to send it down: it waits if something is joined, and falls off the end if nothing is
|
|
return !joined && Send(s, mode > 0 ? 0 : 2);
|
|
}
|
|
|
|
protected override void Update()
|
|
{
|
|
base.Update();
|
|
// every peer: watch the junction and swing the flap toward the item crossing it
|
|
bool seen = false;
|
|
Cargo inJunction = null;
|
|
float side = 0f;
|
|
var here = transform.position;
|
|
foreach (var c in Cargo.All)
|
|
{
|
|
if (!c) continue;
|
|
var at = c.transform.position;
|
|
// most of a farm's bundles are nowhere near: the cheap test first
|
|
if ((at - here).sqrMagnitude > 4f) continue;
|
|
var l = transform.InverseTransformPoint(at);
|
|
if (Mathf.Abs(l.x) > 0.55f || Mathf.Abs(l.z) > 0.55f || l.y < -0.2f || l.y > 1.4f) continue;
|
|
if (l.z < -0.2f) continue;
|
|
inJunction = c;
|
|
side = Mathf.Abs(l.x) > 0.08f ? Mathf.Sign(l.x) : 0f;
|
|
seen = Mathf.Abs(l.x) > 0.08f || l.z > 0.08f;
|
|
if (seen) break;
|
|
}
|
|
if (seen && inJunction != _lastSeen)
|
|
{
|
|
_lastSeen = inJunction;
|
|
_paddleTarget = -side * 38f;
|
|
Sfx.PlayAt("splitter_flip", transform.position + transform.up * 0.4f, 0.55f, 1f, 0.08f);
|
|
}
|
|
if (paddle)
|
|
{
|
|
_paddleAngle = Mathf.MoveTowards(_paddleAngle, _paddleTarget, Time.deltaTime * 260f);
|
|
paddle.localRotation = Quaternion.AngleAxis(_paddleAngle, paddle.parent ? paddle.parent.InverseTransformDirection(transform.up) : Vector3.up) * _paddleRest;
|
|
}
|
|
}
|
|
}
|
|
}
|