RacesOfValheim/JotunnModStub/Furling/FurlingKingUI.cs
2026-07-30 19:00:13 +02:00

149 lines
6.6 KiB
C#

using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using Jotunn.Managers;
namespace RacesOfValheim
{
/// <summary>
/// Displays a small "King / Queen" status indicator in the top-right corner of the HUD
/// when the local Furling player is inside the Plains biome and has unlocked the Yagluth ability (state >= 8).
/// Shows the unmodified Midsummer Crown icon above the gendered title.
/// </summary>
public static class FurlingKingUI
{
private static GameObject _hudRoot; // root panel anchored top-right
private static Image _crownImage; // the crown sprite
private static Text _titleText; // "King" / "Queen"
private const float UPDATE_INTERVAL = 1.0f;
private static float _timer = 0f;
// ─── Harmony patch: hook onto Hud.Update ─────────────────────────────
[HarmonyPatch(typeof(Hud), "Update")]
public static class FurlingKingUI_Hud_Update_Patch
{
public static void Postfix(Hud __instance)
{
_timer += Time.deltaTime;
if (_timer < UPDATE_INTERVAL) return;
_timer = 0f;
Tick(__instance);
}
}
// ─── Core update logic ────────────────────────────────────────────────
private static void Tick(Hud hud)
{
bool shouldShow = ShouldShowKingStatus();
if (!shouldShow)
{
if (_hudRoot != null) _hudRoot.SetActive(false);
return;
}
// Build the UI the first time it's needed
if (_hudRoot == null)
{
BuildUI(hud);
if (_hudRoot == null) return; // build failed (prefab not ready yet)
}
_hudRoot.SetActive(true);
// Update label based on player model (0 = male → King, 1 = female → Queen)
if (_titleText != null && Player.m_localPlayer != null)
{
int modelIndex = HarmonyLib.Traverse.Create(
Player.m_localPlayer.GetComponent<VisEquipment>()
).Field<int>("m_modelIndex").Value;
_titleText.text = (modelIndex == 1) ? "Queen" : "King";
}
}
// ─── Condition check ──────────────────────────────────────────────────
private static bool ShouldShowKingStatus()
{
if (Player.m_localPlayer == null) return false;
if (!FurlingRace.IsFurling()) return false;
if (BossProgressionManager.GetCurrentProgressionState() < 8) return false;
// Check Plains biome
Heightmap.Biome biome = Player.m_localPlayer.GetCurrentBiome();
return biome == Heightmap.Biome.Plains;
}
// ─── Build the HUD panel ──────────────────────────────────────────────
private static void BuildUI(Hud hud)
{
// Fetch the crown item icon
Sprite crownSprite = null;
var crownPrefab = PrefabManager.Cache.GetPrefab<ItemDrop>("HelmetMidsummerCrown");
if (crownPrefab != null)
crownSprite = crownPrefab.m_itemData.m_shared.m_icons.Length > 0
? crownPrefab.m_itemData.m_shared.m_icons[0]
: null;
// Parent to the Hud root canvas
Canvas canvas = hud.GetComponentInChildren<Canvas>();
if (canvas == null) return;
// Root panel
_hudRoot = new GameObject("FurlingKingStatus", typeof(RectTransform));
_hudRoot.transform.SetParent(canvas.transform, false);
RectTransform rt = _hudRoot.GetComponent<RectTransform>();
rt.anchorMin = new Vector2(1f, 1f);
rt.anchorMax = new Vector2(1f, 1f);
rt.pivot = new Vector2(1f, 1f);
rt.sizeDelta = new Vector2(90f, 110f);
rt.anchoredPosition = new Vector2(-10f, -10f); // 10px from top-right edge
// ── Crown icon ──────────────────────────────────────────────────
GameObject iconGO = new GameObject("CrownIcon", typeof(RectTransform), typeof(Image));
iconGO.transform.SetParent(_hudRoot.transform, false);
RectTransform iconRt = iconGO.GetComponent<RectTransform>();
iconRt.anchorMin = new Vector2(0.5f, 1f);
iconRt.anchorMax = new Vector2(0.5f, 1f);
iconRt.pivot = new Vector2(0.5f, 1f);
iconRt.sizeDelta = new Vector2(72f, 72f);
iconRt.anchoredPosition = Vector2.zero;
_crownImage = iconGO.GetComponent<Image>();
_crownImage.preserveAspect = true;
if (crownSprite != null)
_crownImage.sprite = crownSprite;
else
_crownImage.color = new Color(1f, 0.9f, 0.2f, 1f); // gold fallback
// ── Title text ──────────────────────────────────────────────────
GameObject textGO = new GameObject("TitleText", typeof(RectTransform), typeof(Text));
textGO.transform.SetParent(_hudRoot.transform, false);
RectTransform textRt = textGO.GetComponent<RectTransform>();
textRt.anchorMin = new Vector2(0f, 0f);
textRt.anchorMax = new Vector2(1f, 0f);
textRt.pivot = new Vector2(0.5f, 0f);
textRt.sizeDelta = new Vector2(0f, 28f);
textRt.anchoredPosition = Vector2.zero;
_titleText = textGO.GetComponent<Text>();
_titleText.text = "King";
_titleText.alignment = TextAnchor.MiddleCenter;
_titleText.fontSize = 18;
_titleText.fontStyle = FontStyle.Bold;
_titleText.color = new Color(1f, 0.92f, 0.4f, 1f); // gold text
// Try to match Valheim's font
_titleText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
// ── Subtle dark backing for readability ─────────────────────────
CanvasGroup cg = _hudRoot.AddComponent<CanvasGroup>();
cg.alpha = 0.92f;
}
}
}