Files
grass-cutters/Assets/Scripts/UI/PieceScreen.cs
NiemandKG 86c8403c2a Train lines that grow, station yards, a cab to ride, a wagon per upgrade; autopilot unloads where it can
- The grass train lays on to the next station by itself once its newest stretch is mown as far as the cutters reach,
  rounding each station at the edge of its yard, until no station is left within reach; it runs three times as fast
  over the stretches already mown.
- Every station on a line has a yard at the end of the rails: machines (the autopilot too) unload there, the train
  picks it up as it passes and it is paid when the train gets home. With nothing left to cut it carries for the yards.
- E on the locomotive puts you in the cab: W on up the line, S back home, E to get off; it carries on by itself after.
- Three wagons, one more per upgrade (six at Mk IV), 2 t each; the heaps grow as they fill. The upgrade text says so.
- The autopilot only drives to places a machine can unload (garage pads, unload bays, the yard, train yards), not to
  drone pads, binders or catchers, where it used to stop and stand; at a spot that won't take it, it tries the next.
  The HUD unload markers skip those too.
- -demotrain checks the line growing, a yard filling and emptying, the cab ride and six wagons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 22:31:45 +06:00

125 lines
5.7 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UIElements;
namespace GrassCutter
{
/// <summary>
/// T on anything built: its upgrade level and what the next one does and costs, its mode if it is a splitter or a merger,
/// and its switch if it has one (a belt stopped, a machine paused). Everything here is for the whole crew, paid from the wallet.
/// </summary>
public class PieceScreen
{
public VisualElement Card { get; }
readonly BuildPiece _p;
readonly VisualElement _body;
readonly Action _close;
public PieceScreen(VisualElement layer, Action close, BuildPiece piece)
{
_p = piece;
_close = close;
Card = UIK.El("card", layer);
Card.pickingMode = PickingMode.Position;
Card.style.width = 680;
var header = UIK.El("card-header", Card);
UIK.Text(piece.Def.name.ToUpper(Loc.Culture), "t-h1 t-dark", header);
UIK.Btn("X", close, "btn btn-red btn-small", header);
_body = UIK.El(null, Card);
Refresh();
// the card follows the piece while it is up; the wallet only recolours the price
int was = Stamp();
Card.schedule.Execute(() =>
{
if (!_p || !_p.IsSpawned) { _close(); return; }
int now = Stamp();
if (now != was) { was = now; Refresh(); }
}).Every(200);
UIK.FollowWallet(Card);
}
int Stamp() => !_p ? 0 : _p.Setting;
void Refresh()
{
_body.Clear();
var ts = TeamState.Instance;
if (!_p || !ts) return;
var kind = _p.Upgrades;
int tier = _p.Tier.Value;
if (kind != UpgradeKind.None)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text($"{GameDefs.TierName(tier)}", "t-small t-dark", names, false);
UIK.Text(tier == 0 ? Loc.T("As built.") : Loc.F("Now {0}.", GameDefs.TierEffect(_p.piece, kind, tier)), "tune-effect", names, false);
if (tier < GameDefs.MaxTier)
{
double price = GameDefs.UpgradePrice(_p, tier);
var up = UIK.El("tune-row", _body);
var upNames = UIK.El("tune-names", up);
UIK.Text(Loc.F("Upgrade to {0}", GameDefs.TierName(tier + 1)), "t-small t-dark", upNames, false);
UIK.Text(Loc.F("{0}.", Cap(GameDefs.TierEffect(_p.piece, kind, tier + 1))), "tune-effect", upNames, false);
UIK.Costs(UIK.Btn(UIK.Money(price), () =>
{
if (!UIK.CanPay(price)) { Sfx.Play("error", 0.7f); return; }
_p.UpgradeRpc();
Sfx.Play("cash_register", 0.9f);
}, "btn btn-small", up), price);
}
else UIK.Text(Loc.T("Fully upgraded."), "tune-effect", _body, false);
}
var junction = _p.GetComponentInChildren<IJunction>();
if (junction != null)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text(Loc.T("Mode"), "t-small t-dark", names, false);
UIK.Text(Cap(junction.ModeName(_p.Mode.Value)), "tune-effect", names, false);
UIK.Btn(Loc.T("CHANGE"), () => { _p.NextModeRpc(junction.ModeCount); Sfx.Play("ui_click", 0.7f); }, "btn btn-blue btn-small", row);
}
string switchName = SwitchName();
if (switchName != null)
{
var row = UIK.El("tune-row", _body);
var names = UIK.El("tune-names", row);
UIK.Text(switchName, "t-small t-dark", names, false);
UIK.Text(_p.Switched ? SwitchOff() : _p.Upgrades == UpgradeKind.Silo ? Loc.T("Open: bundles go out to the belts.") : Loc.T("Running."), "tune-effect", names, false);
UIK.Btn(_p.Switched ? (_p.Upgrades == UpgradeKind.Silo ? Loc.T("OPEN") : Loc.T("START")) : (_p.Upgrades == UpgradeKind.Silo ? Loc.T("SHUT") : Loc.T("STOP")), () => { _p.SwitchRpc(); Sfx.Play("ui_click", 0.7f); }, (_p.Switched ? "btn btn-green" : "btn btn-red") + " btn-small", row);
}
if (kind == UpgradeKind.None && junction == null && switchName == null)
UIK.Text(Loc.T("Nothing to set on this one."), "tune-effect", _body, false);
UIK.Text(Loc.T("Upgrades are for the whole crew and come out of the shared wallet."), "t-small t-muted wrap", _body, false).style.marginTop = 10;
}
string SwitchName()
{
switch (_p.Upgrades)
{
case UpgradeKind.Belt: return Loc.T("Belt");
case UpgradeKind.Machine: return Loc.T("Machine");
case UpgradeKind.Robot: return _p.GetComponent<Mowbot>() ? Loc.T("Robot") : null;
case UpgradeKind.Silo: return Loc.T("Hatches");
default: return null;
}
}
string SwitchOff()
{
switch (_p.Upgrades)
{
case UpgradeKind.Belt: return Loc.T("Stopped: what's on it waits where it is.");
case UpgradeKind.Robot: return Loc.T("Stopped: it waits where it is.");
case UpgradeKind.Silo: return Loc.T("Shut: everything stays in the silo.");
default: return Loc.T("Paused: it takes grass in but doesn't work it.");
}
}
static string Cap(string s) => string.IsNullOrEmpty(s) ? s : char.ToUpper(s[0], Loc.Culture) + s.Substring(1);
}
}