using UnityEngine; namespace GrassCutter { /// /// 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. /// 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; /// Bundles on different ways don't queue behind each other once they're past the middle. 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; // the host has its own items in a list; a joined peer looks round every few frames bool mine = Server; if (!mine && (Time.frameCount + _phase) % 4 != 0) { Paddle(); return; } int count = mine ? Items.Count : Cargo.All.Count; for (int k = 0; k < count; k++) { var c = mine ? Items[k].c : Cargo.All[k]; 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); } Paddle(); } static int s_phases; readonly int _phase = s_phases++; void Paddle() { if (!paddle) return; _paddleAngle = Mathf.MoveTowards(_paddleAngle, _paddleTarget, Time.deltaTime * 260f); paddle.localRotation = Quaternion.AngleAxis(_paddleAngle, paddle.parent ? paddle.parent.InverseTransformDirection(transform.up) : Vector3.up) * _paddleRest; } } }