Grass Cutters Together: the project as of moon-sweepers f3666f0
A clean start for the shared repository: the Unity project (Assets, Packages, ProjectSettings), ArtSource, Docs and Tools as they stood at f3666f0 in the old local repository, with its generated prefabs and scene brought up to date. Images, audio, models and binaries go through Git LFS (.gitattributes). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
143
Assets/Scripts/UI/GarageScreen.cs
Normal file
143
Assets/Scripts/UI/GarageScreen.cs
Normal file
@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
namespace GrassCutter
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
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
|
||||
Card.schedule.Execute(Refresh).Every(250);
|
||||
Refresh();
|
||||
}
|
||||
|
||||
void Refresh()
|
||||
{
|
||||
var ts = Ts;
|
||||
var me = Me;
|
||||
if (!ts || !me) return;
|
||||
_money.text = UIK.Money(ts.Money.Value);
|
||||
var key = $"{_selected} {Math.Floor(ts.Money.Value)}";
|
||||
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];
|
||||
key += v.Tank.Value.ToString("0");
|
||||
}
|
||||
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;
|
||||
t.EnableInClassList("supply-tag-afford", !v && Ts.Money.Value >= d.price);
|
||||
e.RegisterCallback<ClickEvent>(_ => { _selected = d.kind; Sfx.Play("ui_click", 0.6f, 1.1f); Refresh(); });
|
||||
e.RegisterCallback<MouseEnterEvent>(_ => 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);
|
||||
_detail.Clear();
|
||||
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) UIK.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)), "t-small t-dark t-center", _detail, false).style.whiteSpace = WhiteSpace.Normal;
|
||||
if (!v)
|
||||
{
|
||||
bool can = ts.Money.Value >= d.price;
|
||||
var b = UIK.Btn(Loc.F("BUY {0}", UIK.Money(d.price)), () =>
|
||||
{
|
||||
if (!can) { 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);
|
||||
}, can ? "btn btn-green" : "btn btn-grey", _detail);
|
||||
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 = new ScrollView(ScrollViewMode.Vertical);
|
||||
list.AddToClassList("tune-list");
|
||||
_detail.Add(list);
|
||||
for (int i = 0; i < (int)TuneId.Count; i++) Tune(list.contentContainer, v, (TuneId)i);
|
||||
}
|
||||
|
||||
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);
|
||||
bool can = !max && Ts.Money.Value >= price;
|
||||
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);
|
||||
UIK.Btn(max ? Loc.T("MAX") : UIK.Money(price), () =>
|
||||
{
|
||||
if (!can) { Sfx.Play("error", 0.7f); return; }
|
||||
Ts.TuneVehicleRpc(v.NetworkObjectId, (byte)id);
|
||||
Sfx.Play("cash_register", 0.9f);
|
||||
}, (can ? "btn btn-green" : "btn btn-grey") + " btn-small", row);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user