using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UIElements;
namespace GrassCutter
{
///
/// The supply wall: hand tools laid out with price tags, belt slots and gear, the selected item on the right. Tools
/// and gear are personal, paid for from the crew wallet.
///
public class SupplyScreen
{
const int SlotItem = GameDefs.ToolCount, JetItem = SlotItem + 1, BootItem = JetItem + 1, NightItem = BootItem + 1;
public VisualElement Card { get; }
readonly Label _money;
readonly VisualElement _grid, _detail;
int _selected;
TeamState Ts => TeamState.Instance;
PlayerAgent Me => PlayerAgent.Local;
public SupplyScreen(VisualElement layer, Action close)
{
Card = UIK.El("card shop", layer);
Card.pickingMode = PickingMode.Position;
var header = UIK.El("card-header", Card);
UIK.Text(Loc.T("SUPPLY WALL"), "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 new tool lands on the belt a frame after the server says it's yours
NetworkVariable.OnValueChangedDelegate owned = (a, b) => Card.schedule.Execute(() => Rebuild(false)).ExecuteLater(60);
NetworkVariable.OnValueChangedDelegate slots = (a, b) => Card.schedule.Execute(() => Rebuild(false)).ExecuteLater(60);
var me = Me;
if (me) { me.Owned.OnValueChanged += owned; me.BeltSlots.OnValueChanged += slots; }
TeamState.Changed += Refresh;
Card.RegisterCallback(_ =>
{
TeamState.Changed -= Refresh;
if (me) { me.Owned.OnValueChanged -= owned; me.BeltSlots.OnValueChanged -= slots; }
});
// open on the next thing to get
foreach (var o in GameDefs.ToolOrder)
if (me && !me.Owns(o)) { _selected = (int)o; break; }
Rebuild(true);
// the wallet moves all the time: it recolours the prices and the pill, it doesn't rebuild the card
UIK.FollowWallet(Card);
Card.schedule.Execute(() => { if (Ts) _money.text = UIK.Money(Ts.Money.Value); }).Every(250);
}
void Refresh() => Rebuild(false);
void Rebuild(bool animate)
{
var ts = Ts;
var me = Me;
if (!ts || !me) return;
_money.text = UIK.Money(ts.Money.Value);
_grid.Clear();
foreach (var t in GameDefs.ToolOrder)
{
int i = (int)t;
var d = GameDefs.Tool(t);
string sub = GameDefs.IsBlade(t) ? Loc.F("{0:0.#} cm · {1:0.##} kg", ts.ToolWidth(t) * 100f, ts.ToolCap(t)) : Loc.T("Build menu");
double price = ts.ToolPrice(d);
string tag = me.Owns(t) ? (me.Tools.Belt.Contains(t) ? Loc.T("ON BELT") : Loc.T("OWNED")) : price <= 0d ? Loc.T("FREE") : UIK.Money(price);
Item(i, d.icon, d.name, sub, tag, me.Owns(t) ? -1d : price, animate);
}
int slots = me.BeltSlots.Value;
bool maxed = slots >= GameDefs.MaxBeltSlots;
long slotPrice = GameDefs.BeltSlotPrice(slots);
Item(SlotItem, "icon_perk_release", Loc.T("Belt Slot"), Loc.F("{0} / {1} slots", slots, GameDefs.MaxBeltSlots), maxed ? Loc.T("MAXED") : UIK.Money(slotPrice), maxed ? -1d : slotPrice, animate);
Gear(JetItem, GearId.Jetpack, "icon_perk_jetpack", Loc.T("Jetpack"), Loc.T("Dash in the air"), animate);
Gear(BootItem, GearId.Boots, "icon_perk_boots", Loc.T("Gravity Boots"), Loc.T("G: Earth's pull"), animate);
Gear(NightItem, GearId.NightVision, "icon_perk_pure", Loc.T("Night Vision"), Loc.T("N: see in the dark"), animate);
BuildDetail(animate);
}
void Gear(int id, GearId g, string icon, string name, string sub, bool animate)
{
bool has = Me.HasGear(g);
long price = GameDefs.GearPrice(g);
Item(id, icon, name, sub, has ? Loc.T("OWNED") : UIK.Money(price), has ? -1d : price, animate);
}
void Item(int id, string icon, string name, string sub, string tag, double cost, bool animate)
{
var e = UIK.El("supply-item", _grid);
e.pickingMode = PickingMode.Position;
e.EnableInClassList("supply-item-on", id == _selected);
UIK.Img(AssetBank.I.Tex(icon), "supply-icon", e).pickingMode = PickingMode.Ignore;
UIK.Text(name, "supply-name", e, false).pickingMode = PickingMode.Ignore;
UIK.Text(sub, "supply-sub", e, false).pickingMode = PickingMode.Ignore;
var t = UIK.Text(tag, "supply-tag", e, false);
t.pickingMode = PickingMode.Ignore;
if (cost >= 0d) UIK.Costs(t, cost, "supply-tag-afford", null);
e.RegisterCallback(_ => { _selected = id; Sfx.Play("ui_click", 0.6f, 1.1f); Rebuild(false); });
e.RegisterCallback(_ => Sfx.Play("ui_hover", 0.35f, 1.2f));
if (animate) UIK.PopIn(e, 0.03f * id, 0.3f);
}
void BuildDetail(bool animate)
{
var ts = Ts;
var me = Me;
_detail.Clear();
string icon, title, desc;
string[] stats;
if (_selected == SlotItem)
{
icon = "icon_perk_release"; title = Loc.T("Belt Slot");
desc = Loc.T("One more tool on your belt. Keys 1-6 or the wheel switch between them.");
stats = new[] { Loc.F("NOW {0} of {1} slots", me.BeltSlots.Value, GameDefs.MaxBeltSlots) };
}
else if (_selected == JetItem)
{
icon = "icon_perk_jetpack"; title = Loc.T("Jetpack");
desc = Loc.T("Double-tap SPACE in the air for a dash forward. Powered by... grass, obviously.");
stats = new[] { Loc.F("Dash every {0:0.#} s", GameDefs.JetCooldown) };
}
else if (_selected == BootItem)
{
icon = "icon_perk_boots"; title = Loc.T("Gravity Boots");
desc = Loc.T("Herbaria-7 pulls weakly. G clamps you down with Earth's gravity: you land fast, but you won't jump as high.");
stats = new[] { Loc.F("Fall {0:0.#} instead of {1:0.#} m/s²", -me.gravity, GameDefs.LowGravity) };
}
else if (_selected == NightItem)
{
icon = "icon_perk_pure"; title = Loc.T("Night Vision");
desc = Loc.T("N goes from the headlamp to night vision to nothing. Everything turns green, and you can see all the way to the horizon.");
stats = new[] { Loc.T("Works as far as you can see") };
}
else
{
var t = (ToolId)_selected;
var d = GameDefs.Tool(t);
icon = d.icon; title = d.name; desc = d.desc;
stats = GameDefs.IsBlade(t)
? new[] { Loc.F("Strip width {0:0.#} cm", ts.ToolWidth(t) * 100f), Loc.F("Container {0:0.##} kg", ts.ToolCap(t)) }
: new[] { Loc.T("Opens the build menu (B)") };
}
var top = UIK.El("detail-top", _detail);
top.style.backgroundColor = new Color(0.9f, 0.8f, 0.55f);
UIK.Img(AssetBank.I.Tex(icon), "detail-icon", top);
UIK.Text(title, "t-h1 t-dark t-center detail-title", _detail);
var dt = UIK.Text(desc, "item-desc t-center", _detail, false);
dt.style.whiteSpace = WhiteSpace.Normal;
var fx = UIK.El("detail-effect", _detail);
foreach (var line in stats) UIK.Text(line, "t-body t-dark", fx, false);
var spacer = UIK.El(null, _detail);
spacer.style.flexGrow = 1;
Action();
if (animate) UIK.SlideIn(_detail, new Vector2(40, 0), 0.05f, 0.3f);
}
/// The button of the selected item: buy it, put it on the belt, or take it in hand.
void Action()
{
var ts = Ts;
var me = Me;
if (_selected == SlotItem)
{
int slots = me.BeltSlots.Value;
if (slots >= GameDefs.MaxBeltSlots) { Done(Loc.T("MAXED OUT!")); return; }
long price = GameDefs.BeltSlotPrice(slots);
Buy(Loc.F("BUY {0}", UIK.Money(price)), price, () => ts.BuyBeltSlotRpc());
}
else if (_selected >= JetItem)
{
var g = _selected == JetItem ? GearId.Jetpack : _selected == BootItem ? GearId.Boots : GearId.NightVision;
if (me.HasGear(g)) { Done(Loc.T("OWNED")); return; }
long price = GameDefs.GearPrice(g);
Buy(Loc.F("BUY {0}", UIK.Money(price)), price, () => ts.BuyGearRpc((byte)g));
}
else
{
var t = (ToolId)_selected;
var tools = me.Tools;
if (!me.Owns(t))
{
double price = ts.ToolPrice(GameDefs.Tool(t));
Buy(price <= 0d ? Loc.T("TAKE IT") : Loc.F("BUY {0}", UIK.Money(price)), price, () => ts.BuyToolRpc((byte)t));
}
else if (tools.Current == t) Done(Loc.T("IN YOUR HANDS"));
else
{
var b = UIK.Btn(tools.Belt.Contains(t) ? Loc.T("TAKE IN HAND") : Loc.T("PUT ON BELT"), () => { tools.Equip(t); Sfx.Play("plastic_grab", 0.6f); Rebuild(false); }, "btn btn-green", _detail);
b.style.alignSelf = Align.Stretch;
}
}
}
void Done(string text) => UIK.Text(text, "price t-green t-outline t-center", _detail);
void Buy(string label, double cost, Action buy)
{
var b = UIK.Btn(label, () =>
{
if (!UIK.CanPay(cost))
{
Sfx.Play("error", 0.7f);
Assistant.Say("asst_nocash"); Hud.Toast(Loc.T("Not enough cash: mow more!"), Hud.ToastKind.Warn);
return;
}
buy();
Sfx.Play("cash_register", 0.9f);
}, "btn", _detail);
UIK.Costs(b, cost);
b.style.alignSelf = Align.Stretch;
}
}
}