using UnityEngine; using UnityEngine.UIElements; namespace GrassCutter { /// /// The boosts you're on, as chips in the top left corner: the treat's picture, the boost's name and what it does, the time /// left and a bar running down. A chip pops in when the boost lands and blinks in its last ten seconds. Also the flash /// of colour over the whole screen at the moment a boost lands. /// public class TreatHud { static TreatHud _i; readonly VisualElement _flash; readonly Chip[] _chips = new Chip[2]; class Chip { public VisualElement root, bar; public Label time; public bool shown; } public TreatHud(VisualElement root) { _i = this; var col = new VisualElement { pickingMode = PickingMode.Ignore }; col.style.position = Position.Absolute; col.style.left = 34; col.style.top = 30; col.style.flexDirection = FlexDirection.Column; root.Add(col); for (int i = 0; i < 2; i++) _chips[i] = MakeChip(col, (TreatId)i); _flash = new VisualElement { pickingMode = PickingMode.Ignore }; _flash.style.position = Position.Absolute; _flash.style.left = 0; _flash.style.top = 0; _flash.style.right = 0; _flash.style.bottom = 0; _flash.style.opacity = 0f; root.Add(_flash); Treats.Started += OnStarted; col.schedule.Execute(Tick).Every(100); } public void Detach() { Treats.Started -= OnStarted; } Chip MakeChip(VisualElement parent, TreatId t) { var c = new Chip { root = new VisualElement { pickingMode = PickingMode.Ignore } }; var r = c.root; r.style.flexDirection = FlexDirection.Row; r.style.alignItems = Align.Center; r.style.marginBottom = 8; r.style.paddingLeft = 8; r.style.paddingRight = 14; r.style.paddingTop = 6; r.style.paddingBottom = 6; r.style.backgroundColor = new Color(0.08f, 0.1f, 0.06f, 0.62f); r.style.borderTopLeftRadius = r.style.borderTopRightRadius = r.style.borderBottomLeftRadius = r.style.borderBottomRightRadius = 14; var edge = t == TreatId.Beer ? new Color(1f, 0.8f, 0.3f, 0.9f) : new Color(1f, 0.58f, 0.25f, 0.9f); r.style.borderLeftWidth = r.style.borderRightWidth = r.style.borderTopWidth = r.style.borderBottomWidth = 2; r.style.borderLeftColor = r.style.borderRightColor = r.style.borderTopColor = r.style.borderBottomColor = edge; r.style.display = DisplayStyle.None; parent.Add(r); var icon = new VisualElement { pickingMode = PickingMode.Ignore }; icon.style.width = 54; icon.style.height = 54; icon.style.marginRight = 10; var tex = AssetBank.I ? AssetBank.I.Tex(Treats.Icon(t)) : null; if (tex) icon.style.backgroundImage = new StyleBackground(tex); r.Add(icon); var col = new VisualElement { pickingMode = PickingMode.Ignore }; r.Add(col); UIK.Text(Treats.Boost(t), "t-body t-outline", col); var line = new VisualElement { pickingMode = PickingMode.Ignore }; line.style.flexDirection = FlexDirection.Row; line.style.alignItems = Align.Center; col.Add(line); UIK.Text(Treats.Effect(t), "t-small t-outline", line); c.time = UIK.Text("", "t-small t-outline", line, false); c.time.style.marginLeft = 10; c.time.style.color = edge; var track = new VisualElement { pickingMode = PickingMode.Ignore }; track.style.height = 5; track.style.marginTop = 4; track.style.backgroundColor = new Color(1f, 1f, 1f, 0.15f); track.style.borderTopLeftRadius = track.style.borderTopRightRadius = track.style.borderBottomLeftRadius = track.style.borderBottomRightRadius = 3; col.Add(track); c.bar = new VisualElement { pickingMode = PickingMode.Ignore }; c.bar.style.height = Length.Percent(100); c.bar.style.backgroundColor = edge; c.bar.style.borderTopLeftRadius = c.bar.style.borderTopRightRadius = c.bar.style.borderBottomLeftRadius = c.bar.style.borderBottomRightRadius = 3; track.Add(c.bar); return c; } void OnStarted(TreatId t) { var c = _chips[(int)t]; c.root.style.display = DisplayStyle.Flex; c.shown = true; // a punch would kill the slide half way and leave the chip off the edge UIK.SlideIn(c.root, new Vector2(-260, 0), 0f, 0.45f); Tick(); } void Tick() { for (int i = 0; i < 2; i++) { var c = _chips[i]; float left = Treats.Left((TreatId)i); if (left <= 0f) { if (c.shown) { c.shown = false; UIK.Fade(c.root, 0f, 0.3f, () => { c.root.style.display = DisplayStyle.None; c.root.style.opacity = 1f; }); } continue; } int s = Mathf.CeilToInt(left); c.time.text = $"{s / 60}:{s % 60:00}"; c.bar.style.width = Length.Percent(100f * left / Treats.Seconds); c.root.style.opacity = left < 10f ? (Mathf.Repeat(Time.time * 2f, 1f) < 0.5f ? 1f : 0.45f) : 1f; } } /// The whole screen washes with a colour for a moment and fades back. public static void Flash(Color c) { if (_i == null) return; var f = _i._flash; f.style.backgroundColor = new Color(c.r, c.g, c.b, 0.34f); Tween.Run(f, 0.7f, k => f.style.opacity = 1f - k, Ease.OutQuad); } } }