Merge branch 'fix-form-headlamp': Lights follow the form's head

This commit is contained in:
kanym 2026-10-29 13:22:45 +01:00
commit ed7483568c

View file

@ -0,0 +1,167 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The lights a player wears - the Dvergr circlet's headlamp, a torch in hand - follow the form
/// the player has taken.
///
/// A form hides the human body and leaves it standing where it was: PlayerVisualHider turns its
/// renderers off, the Muspell shrinks the Visual to nothing. Either way the gear stays on the
/// body's bones, and a Light is not a renderer, so the headlamp kept shining from the hidden
/// human's head: at a person's height inside a troll's chest, from the feet of a Muspell. A
/// tester saw it stuck where the head had been before the transformation.
///
/// While the body is hidden, every Light under it is put at the form's head bone, a little in
/// front of it. A form with no bone called "head" gets the top front of its own model instead,
/// and with no form model at all the light sits centre-forward at a person's head height. The
/// lights go back to their own places the moment the body shows again.
///
/// Run for every player, not only the local one: the headlamp lights the world for onlookers too.
/// </summary>
public class FormLightFollow : MonoBehaviour
{
// Lights and the form's head are looked up again this often: VisEquipment rebuilds gear,
// and a form can be built a frame after the body is hidden.
private const float RescanInterval = 0.25f;
// RaceEyeHeight's reading of a Visual this small: hidden, not small.
private const float HiddenScale = 0.05f;
// Ahead of the head bone, so the lamp does not light the skull from inside.
private const float AheadOfHead = 0.3f;
// No form model at all: centre-forward, where a person's headlamp would be.
private static readonly Vector3 FallbackOffset = new Vector3(0f, 1.6f, 0.4f);
private static readonly AccessTools.FieldRef<Character, GameObject> VisualRef =
AccessTools.FieldRefAccess<Character, GameObject>("m_visual");
// Each light moved, with the local position it goes back to.
private readonly Dictionary<Light, Vector3> moved = new Dictionary<Light, Vector3>();
private readonly List<Light> dead = new List<Light>();
private float nextScan;
private Transform head;
private Vector3 anchorOffset = FallbackOffset; // player-local, used when there is no head
public static void Tick(Player player)
{
GameObject visual = VisualRef(player);
if (visual == null) return;
bool hidden = PlayerVisualHider.IsHidden(visual.transform) || visual.transform.localScale.y < HiddenScale;
FormLightFollow follow = visual.GetComponent<FormLightFollow>();
if (!hidden)
{
if (follow != null && follow.moved.Count > 0) follow.Restore();
return;
}
if (follow == null) follow = visual.AddComponent<FormLightFollow>();
follow.Follow(player);
}
private void Follow(Player player)
{
if (Time.time >= nextScan)
{
nextScan = Time.time + RescanInterval;
Scan(player);
}
Transform body = player.transform;
Vector3 anchor = head != null
? head.position + body.forward * AheadOfHead
: body.TransformPoint(anchorOffset);
foreach (Light light in moved.Keys)
{
if (light != null) light.transform.position = anchor;
}
}
private void Scan(Player player)
{
dead.Clear();
foreach (Light light in moved.Keys)
{
if (light == null) dead.Add(light);
}
foreach (Light light in dead) moved.Remove(light);
foreach (Light light in GetComponentsInChildren<Light>(true))
{
if (!moved.ContainsKey(light)) moved.Add(light, light.transform.localPosition);
}
if (moved.Count == 0) return;
if (head == null || !head.gameObject.activeInHierarchy) FindForm(player);
}
/// <summary>
/// The form's model is a child of the player beside the Visual (every form builds it that
/// way). Its head bone, if it has one; otherwise the top front of its renderers.
/// </summary>
private void FindForm(Player player)
{
head = null;
anchorOffset = FallbackOffset;
Transform body = player.transform;
foreach (Transform child in body)
{
if (child == transform || !child.gameObject.activeInHierarchy) continue;
if (child.GetComponentInChildren<Animator>() == null) continue;
head = FindHead(child);
if (head != null) return;
Bounds bounds = default;
bool any = false;
foreach (Renderer r in child.GetComponentsInChildren<Renderer>())
{
if (!(r is SkinnedMeshRenderer) && !(r is MeshRenderer)) continue;
if (any) bounds.Encapsulate(r.bounds); else { bounds = r.bounds; any = true; }
}
if (!any) continue;
Vector3 local = body.InverseTransformPoint(bounds.center);
Vector3 size = body.InverseTransformVector(bounds.extents);
anchorOffset = new Vector3(local.x, local.y + Mathf.Abs(size.y) * 0.6f, local.z + Mathf.Abs(size.z) * 0.6f);
return;
}
}
private static Transform FindHead(Transform root)
{
foreach (Transform t in root.GetComponentsInChildren<Transform>())
{
if (string.Equals(t.name, "head", System.StringComparison.OrdinalIgnoreCase)) return t;
}
return null;
}
private void Restore()
{
foreach (KeyValuePair<Light, Vector3> entry in moved)
{
if (entry.Key != null) entry.Key.transform.localPosition = entry.Value;
}
moved.Clear();
head = null;
}
/// <summary>LateUpdate, so the form's Animator has already posed the head this frame.</summary>
[HarmonyPatch(typeof(Player), "LateUpdate")]
public static class Player_LateUpdate_FormLightFollow_Patch
{
public static void Postfix(Player __instance)
{
Tick(__instance);
}
}
}
}