116 lines
4.8 KiB
C#
116 lines
4.8 KiB
C#
using System.Reflection;
|
|
using HarmonyLib;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The bird's wind, as a bar under the player's stamina.
|
|
///
|
|
/// It is a CLONE of vanilla's own mount stamina bar, not a bar drawn from scratch, for two
|
|
/// reasons: it is already skinned, sized and animated like every other bar in the game, and it
|
|
/// is semantically the same thing - Hud.UpdateMount drives that bar from Sadle.GetStamina for
|
|
/// the asksvin. Ours is the same idea with a different animal underneath.
|
|
///
|
|
/// Vanilla's copy cannot simply be borrowed where it stands: Hud.UpdateMount switches the whole
|
|
/// mount panel off on every frame in which the player is not on a Sadle, and it would take the
|
|
/// bar with it. The clone is reparented out of that panel, so vanilla never touches it again.
|
|
///
|
|
/// Everything here is reflection because GuiBar lives in assembly_guiutils, which this project
|
|
/// does not reference - and adding a reference to the csproj is a bigger change than a bar is
|
|
/// worth. Every step is null-checked: a bar that fails to build costs the bar and nothing else,
|
|
/// which is the right trade for an ornament on a flying mount.
|
|
/// </summary>
|
|
public class WarVultureHud
|
|
{
|
|
private const float BelowStaminaPixels = 24f;
|
|
|
|
private readonly WarVultureMount mount;
|
|
private readonly GameObject barObject;
|
|
private readonly Component guiBar;
|
|
private readonly MethodInfo setValue;
|
|
|
|
private WarVultureHud(WarVultureMount mount, GameObject barObject, Component guiBar, MethodInfo setValue)
|
|
{
|
|
this.mount = mount;
|
|
this.barObject = barObject;
|
|
this.guiBar = guiBar;
|
|
this.setValue = setValue;
|
|
}
|
|
|
|
public static WarVultureHud Create(WarVultureMount mount)
|
|
{
|
|
Hud hud = Hud.instance;
|
|
if (hud == null) return null;
|
|
|
|
Component template = Traverse.Create(hud).Field("m_mountStaminaBar").GetValue<Component>();
|
|
RectTransform staminaRoot = hud.m_staminaBar2Root;
|
|
if (template == null || staminaRoot == null || staminaRoot.parent == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[WarVulture] The vanilla mount stamina bar or the stamina bar root " +
|
|
"could not be found, so the bird flies without a gauge.");
|
|
return null;
|
|
}
|
|
|
|
GameObject clone = Object.Instantiate(template.gameObject, staminaRoot.parent);
|
|
clone.name = "WarVultureWindBar";
|
|
|
|
// Anchored off the stamina bar rather than off the screen: the stamina bar moves with
|
|
// the player's HUD layout, and anything positioned against the screen would drift away
|
|
// from it the moment that layout changes.
|
|
RectTransform rect = clone.GetComponent<RectTransform>();
|
|
if (rect != null)
|
|
{
|
|
rect.anchorMin = staminaRoot.anchorMin;
|
|
rect.anchorMax = staminaRoot.anchorMax;
|
|
rect.pivot = staminaRoot.pivot;
|
|
rect.anchoredPosition = staminaRoot.anchoredPosition + new Vector2(0f, -BelowStaminaPixels);
|
|
rect.localScale = staminaRoot.localScale;
|
|
}
|
|
|
|
clone.SetActive(true);
|
|
|
|
Component bar = clone.GetComponent("GuiBar");
|
|
MethodInfo setter = bar != null
|
|
? bar.GetType().GetMethod("SetValue", new[] { typeof(float) })
|
|
: null;
|
|
|
|
if (setter == null)
|
|
{
|
|
Jotunn.Logger.LogWarning("[WarVulture] The cloned bar has no GuiBar.SetValue(float) to drive, " +
|
|
"so it would have sat there full forever; it was removed.");
|
|
Object.Destroy(clone);
|
|
return null;
|
|
}
|
|
|
|
TintBrown(bar);
|
|
|
|
return new WarVultureHud(mount, clone, bar, setter);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Optional, and treated as such: if GuiBar ever loses SetColor the bar simply keeps
|
|
/// vanilla's colour instead of costing an exception every summon.
|
|
/// </summary>
|
|
private static void TintBrown(Component bar)
|
|
{
|
|
MethodInfo setColor = bar.GetType().GetMethod("SetColor", new[] { typeof(Color) });
|
|
if (setColor == null) return;
|
|
|
|
setColor.Invoke(bar, new object[] { new Color(0.72f, 0.55f, 0.32f, 1f) });
|
|
}
|
|
|
|
public void Refresh()
|
|
{
|
|
if (barObject == null || guiBar == null || setValue == null) return;
|
|
if (mount == null) return;
|
|
|
|
setValue.Invoke(guiBar, new object[] { mount.WindFraction });
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (barObject != null) Object.Destroy(barObject);
|
|
}
|
|
}
|
|
}
|