using System; using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// /// The garage: the five machines down the left, the selected one on the right with what it can do, the button to buy or /// call it, and its upgrades. Machines and their upgrades are personal, paid for from the crew wallet. /// public class GarageScreen { public VisualElement Card { get; } readonly Label _money; readonly VisualElement _grid, _detail; readonly ulong _garage; VehicleKind _selected = VehicleKind.Push; string _shown; TeamState Ts => TeamState.Instance; PlayerAgent Me => PlayerAgent.Local; Vehicle Mine(VehicleKind k) => Me ? Vehicle.Mine(k, Me.SaveId) : null; public GarageScreen(VisualElement layer, Action close, Garage garage, VehicleKind? select = null) { _garage = garage ? garage.NetworkObjectId : 0UL; if (select.HasValue) _selected = select.Value; Card = UIK.El("card shop", layer); Card.pickingMode = PickingMode.Position; var header = UIK.El("card-header", Card); UIK.Text(Loc.T("GARAGE"), "t-h1 t-dark", header); var right = UIK.El(null, header); right.style.flexDirection = FlexDirection.Row; right.style.alignItems = Align.Center; var wallet = UIK.El("pill", right); UIK.Img(AssetBank.I.Tex("icon_coin"), "check-icon", wallet); _money = UIK.Text("$0", "t-h2 t-dark", wallet); wallet.style.marginRight = 16; UIK.Btn("X", close, "btn btn-red btn-small", right); var body = UIK.El("tree-body", Card); _grid = UIK.El("tree-graph supply-grid", body); _detail = UIK.El("tree-detail", body); // a machine or an upgrade arrives a moment after the server says so; the wallet only recolours the prices Card.schedule.Execute(Refresh).Every(250); UIK.FollowWallet(Card); Refresh(); } void Refresh() { var ts = Ts; var me = Me; if (!ts || !me) return; _money.text = UIK.Money(ts.Money.Value); Status(); // rebuilt only when what you own changes: not for the money, not for the hopper var key = $"{_selected}"; foreach (var d in GameDefs.Vehicles) { var v = Mine(d.kind); key += v ? "1" : "0"; if (!v) continue; for (int i = 0; i < v.Tuned.Count; i++) key += v.Tuned[i]; } if (key == _shown) return; bool first = _shown == null; _shown = key; _grid.Clear(); int n = 0; foreach (var d in GameDefs.Vehicles) Item(d, n++, first); Detail(); } void Item(VehicleDef d, int index, bool animate) { var v = Mine(d.kind); var e = UIK.El("supply-item", _grid); e.pickingMode = PickingMode.Position; e.EnableInClassList("supply-item-on", d.kind == _selected); UIK.Img(AssetBank.I.Tex(d.icon), "supply-icon", e).pickingMode = PickingMode.Ignore; UIK.Text(d.name, "supply-name", e, false).pickingMode = PickingMode.Ignore; UIK.Text(d.bed > 0 ? Loc.F("{0} bundles", d.bed) : Loc.F("{0:0.##} m · {1:0} kg", d.width, d.hopper), "supply-sub", e, false).pickingMode = PickingMode.Ignore; var t = UIK.Text(v ? Loc.T("OWNED") : UIK.Money(d.price), "supply-tag", e, false); t.pickingMode = PickingMode.Ignore; if (!v) UIK.Costs(t, d.price, "supply-tag-afford", null); e.RegisterCallback(_ => { _selected = d.kind; Sfx.Play("ui_click", 0.6f, 1.1f); Refresh(); }); e.RegisterCallback(_ => Sfx.Play("ui_hover", 0.35f, 1.2f)); if (animate) UIK.PopIn(e, 0.03f * index, 0.3f); } void Detail() { var ts = Ts; var d = GameDefs.Vehicle(_selected); var v = Mine(_selected); // the same machine again (an upgrade landed): stay where the list was scrolled to var keep = _tunes != null && _tunesFor == _selected ? _tunes.scrollOffset : Vector2.zero; _detail.Clear(); _tunes = null; _status = null; var top = UIK.El("detail-top", _detail); top.style.backgroundColor = new Color(0.9f, 0.8f, 0.55f); UIK.Img(AssetBank.I.Tex(d.icon), "detail-icon", top); UIK.Text(d.name, "t-h1 t-dark t-center detail-title", _detail); UIK.Text(d.desc, "item-desc t-center", _detail, false).style.whiteSpace = WhiteSpace.Normal; if (v) { _status = UIK.Text("", "t-small t-dark t-center", _detail, false); _status.style.whiteSpace = WhiteSpace.Normal; Status(); } if (!v) { var b = UIK.Btn(Loc.F("BUY {0}", UIK.Money(d.price)), () => { if (!UIK.CanPay(d.price)) { Sfx.Play("error", 0.7f); Assistant.Say("asst_nocash"); Hud.Toast(Loc.T("Not enough cash: mow more!"), Hud.ToastKind.Warn); return; } ts.BuyVehicleRpc((byte)d.kind, _garage); Sfx.Play("cash_register", 0.9f); }, "btn", _detail); UIK.Costs(b, d.price); b.style.alignSelf = Align.Stretch; return; } var call = UIK.Btn(Loc.T("CALL IT HERE"), () => { ts.CallVehicleRpc(v.NetworkObjectId, _garage); Sfx.Play("ui_click", 0.7f); }, "btn btn-green", _detail); call.style.alignSelf = Align.Stretch; var list = _tunes = new ScrollView(ScrollViewMode.Vertical); _tunesFor = _selected; list.AddToClassList("tune-list"); _detail.Add(list); for (int i = 0; i < (int)TuneId.Count; i++) Tune(list.contentContainer, v, (TuneId)i); UIK.KeepScroll(list, keep); } ScrollView _tunes; VehicleKind _tunesFor; Label _status; /// The machine's line of numbers, kept up to date in place (its hopper fills while the card is open). void Status() { if (_status == null) return; var d = GameDefs.Vehicle(_selected); var v = Mine(_selected); if (!v) return; _status.text = d.bed > 0 ? Loc.F("Bed: {0} of {1}", v.Bed.Count, d.bed) : Loc.F("Strip {0:0.##} m · top speed {1:0} km/h · {2} of {3}", v.CutWidth, v.TopSpeed * 3.6f, GameDefs.Kilos(v.Tank.Value), GameDefs.Kilos(v.Capacity)); } void Tune(VisualElement parent, Vehicle v, TuneId id) { var d = GameDefs.Tune(id); if (!GameDefs.TuneFits(v.kind, id)) return; int level = v.Level(id); bool max = level >= d.prices.Length; double price = max ? 0d : GameDefs.TunePrice(v.kind, id, level); var row = UIK.El("tune-row", parent); var names = UIK.El("tune-names", row); UIK.Text($"{d.name} {level}/{d.prices.Length}", "t-small t-dark", names, false); UIK.Text(max ? d.effect(level) : d.effect(level + 1), "tune-effect", names, false); var b = UIK.Btn(max ? Loc.T("MAX") : UIK.Money(price), () => { if (max || !UIK.CanPay(price)) { Sfx.Play("error", 0.7f); return; } Ts.TuneVehicleRpc(v.NetworkObjectId, (byte)id); Sfx.Play("cash_register", 0.9f); }, max ? "btn btn-grey btn-small" : "btn btn-small", row); if (!max) UIK.Costs(b, price); } } }