Trenta: a greydwarf shaman trophy is hung on the player head bone and sunk into the skull until only the horns and branches clear it - the Furling wears a goblin head the same way, aimed at the opposite result. A trophy is a plain static mesh, so unlike the Dvergar hood there is no rig to collapse: the player own head does all the hiding, which makes the three offsets in TrentaConfig the entire look. They are a first pass and want eyeballing in game. Draugh: the smoke and the cloud of flies only ever existed on the client of the player wearing them. DraughRace is a single MonoBehaviour that looks at Player.m_localPlayer and nothing else, so in multiplayer every other player saw an ordinary, unrotted Viking. The build moved to DraughVisuals, which takes a Player, and RacesVisualController now grows the same aura on every remote Draugh it can see. Nothing is networked and nothing needs to be - each client reads the race off the ZDO and makes its own particles. Both hang off VisEquipment.UpdateVisuals or the visual controller, the two hooks that already run for remote players, so neither had to invent a sync path.
208 lines
8.5 KiB
C#
208 lines
8.5 KiB
C#
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Trenta's looks: body scale, skin tint, and whatever cosmetic parts the race wears.
|
|
///
|
|
/// Everything here runs for local and remote players alike, which is why it takes a Player
|
|
/// rather than reading Player.m_localPlayer.
|
|
/// </summary>
|
|
public static class TrentaVisuals
|
|
{
|
|
/// <summary>
|
|
/// Reads the race off the ZDO so remote players render correctly, falling back to
|
|
/// m_customData for the main-menu preview which has no ZNetView.
|
|
/// </summary>
|
|
public static string GetPlayerRace(Player player)
|
|
{
|
|
if (player == null) return "Human";
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "");
|
|
if (!string.IsNullOrEmpty(race)) return race;
|
|
}
|
|
|
|
if (player.m_customData != null && player.m_customData.TryGetValue("Race", out string custom))
|
|
{
|
|
return custom;
|
|
}
|
|
|
|
return "Human";
|
|
}
|
|
|
|
public static bool IsTrentaPlayer(Player player)
|
|
{
|
|
return GetPlayerRace(player) == TrentaConfig.RaceId;
|
|
}
|
|
|
|
// TODO: the rest of the cosmetic work. Skin and hair/beard tint are already handled by
|
|
// Trenta_VisEquipment_UpdateColors_Patch in TrentaPatches.cs.
|
|
//
|
|
// Body scale goes on the player's "Visual" child, not the root transform - scaling the
|
|
// root drags the collider and the camera pivot with it. RaceScale owns the list of races
|
|
// that resize, so the Trenta has to be added there rather than fighting it per-frame.
|
|
|
|
// ─── Crown of branches ───────────────────────────────────────────────
|
|
|
|
private const string CrownName = "TrentaCrown";
|
|
|
|
// Resolved once per session, failure included, so a missing prefab cannot turn into a
|
|
// per-frame log and the prefab lookup does not run again for every player in view.
|
|
private static GameObject cachedCrownVisual;
|
|
private static bool crownResolved;
|
|
|
|
/// <summary>
|
|
/// Keeps the crown in step with who the player is. Safe to call every frame: it checks
|
|
/// whether the crown is already there and does nothing when it is.
|
|
/// </summary>
|
|
public static void ApplyCrown(Player player)
|
|
{
|
|
VisEquipment visEq = player != null ? player.GetComponent<VisEquipment>() : null;
|
|
if (visEq == null) return;
|
|
|
|
if (IsTrentaPlayer(player) && !player.IsDead())
|
|
{
|
|
EnsureCrown(visEq);
|
|
}
|
|
else
|
|
{
|
|
RemoveCrown(visEq);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hangs a greydwarf shaman trophy on the player's head, sunk far enough into the skull
|
|
/// that only the horns and branches clear it.
|
|
///
|
|
/// A trophy is a plain static mesh, so unlike the Dvergar's hood there is no rig to
|
|
/// collapse and no submesh to isolate - the whole thing is instantiated and then buried,
|
|
/// and the player's own head does the hiding. Which means the three numbers in
|
|
/// TrentaConfig are the entire look: get the depth wrong and a tiny greydwarf face floats
|
|
/// in front of the player's.
|
|
/// </summary>
|
|
private static void EnsureCrown(VisEquipment visEq)
|
|
{
|
|
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
|
|
|
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, TrentaConfig.PlayerHeadBoneName);
|
|
if (playerHead == null) return;
|
|
|
|
if (playerHead.Find(CrownName) != null) return; // already wearing it
|
|
|
|
GameObject source = ResolveCrownVisual();
|
|
if (source == null) return; // already logged once by the resolver
|
|
|
|
// Instantiated inactive so the trophy's own Awake never runs - it would register a
|
|
// pickup, a hover prompt and a rigidbody on the player's head.
|
|
bool wasActive = source.activeSelf;
|
|
source.SetActive(false);
|
|
GameObject crown = Object.Instantiate(source, playerHead, false);
|
|
source.SetActive(wasActive);
|
|
|
|
crown.name = CrownName;
|
|
StripComponents(crown);
|
|
crown.SetActive(true);
|
|
|
|
crown.transform.localPosition = TrentaConfig.CrownOffsetPosition;
|
|
crown.transform.localRotation = Quaternion.Euler(TrentaConfig.CrownOffsetRotation);
|
|
crown.transform.localScale = Vector3.one * TrentaConfig.CrownScale;
|
|
|
|
ApplyDitherShader(crown);
|
|
}
|
|
|
|
private static void RemoveCrown(VisEquipment visEq)
|
|
{
|
|
if (visEq.m_bodyModel == null || visEq.m_bodyModel.bones == null) return;
|
|
|
|
Transform playerHead = FindBoneByName(visEq.m_bodyModel.bones, TrentaConfig.PlayerHeadBoneName);
|
|
Transform crown = playerHead != null ? playerHead.Find(CrownName) : null;
|
|
if (crown == null) return;
|
|
|
|
// Destroy is deferred to the end of the frame, so rename first: without that, every
|
|
// call until it actually goes away finds it again and queues another destroy.
|
|
crown.name = CrownName + "_Removed";
|
|
Object.Destroy(crown.gameObject);
|
|
}
|
|
|
|
private static GameObject ResolveCrownVisual()
|
|
{
|
|
if (crownResolved) return cachedCrownVisual;
|
|
crownResolved = true;
|
|
|
|
ItemDrop trophy = PrefabManager.Cache.GetPrefab<ItemDrop>(TrentaConfig.CrownTrophyPrefabName);
|
|
if (trophy == null)
|
|
{
|
|
Jotunn.Logger.LogWarning(
|
|
$"[Trenta] Prefab '{TrentaConfig.CrownTrophyPrefabName}' not found; no crown.");
|
|
return null;
|
|
}
|
|
|
|
cachedCrownVisual = trophy.gameObject;
|
|
Jotunn.Logger.LogInfo($"[Trenta] Crown source resolved to '{TrentaConfig.CrownTrophyPrefabName}'.");
|
|
return cachedCrownVisual;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Strips everything that would make a dropped item behave like one: the ItemDrop itself,
|
|
/// hover prompts and physics, plus the LODGroup, which is not a MonoBehaviour and would
|
|
/// otherwise hide the mesh at the distances it was authored for as a floor pickup.
|
|
/// </summary>
|
|
private static void StripComponents(GameObject obj)
|
|
{
|
|
foreach (MonoBehaviour comp in obj.GetComponentsInChildren<MonoBehaviour>(true))
|
|
{
|
|
Object.DestroyImmediate(comp);
|
|
}
|
|
foreach (LODGroup lod in obj.GetComponentsInChildren<LODGroup>(true))
|
|
{
|
|
Object.DestroyImmediate(lod);
|
|
}
|
|
foreach (Rigidbody body in obj.GetComponentsInChildren<Rigidbody>(true))
|
|
{
|
|
Object.DestroyImmediate(body);
|
|
}
|
|
foreach (Collider col in obj.GetComponentsInChildren<Collider>(true))
|
|
{
|
|
Object.DestroyImmediate(col);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Without the creature shader the crown ignores the camera's dither fade and fills the
|
|
/// screen in first person; its own textures are kept.
|
|
/// </summary>
|
|
private static void ApplyDitherShader(GameObject obj)
|
|
{
|
|
Shader dither = Shader.Find("Custom/Creature");
|
|
if (dither == null) return;
|
|
|
|
foreach (Renderer renderer in obj.GetComponentsInChildren<Renderer>(true))
|
|
{
|
|
if (renderer.material != null) renderer.material.shader = dither;
|
|
}
|
|
}
|
|
|
|
private static Transform FindBoneByName(Transform[] bones, string targetBoneName)
|
|
{
|
|
if (bones == null || string.IsNullOrEmpty(targetBoneName)) return null;
|
|
|
|
foreach (Transform bone in bones)
|
|
{
|
|
if (bone != null && bone.name == targetBoneName) return bone;
|
|
}
|
|
|
|
string target = targetBoneName.ToLower();
|
|
foreach (Transform bone in bones)
|
|
{
|
|
if (bone == null) continue;
|
|
string name = bone.name.ToLower();
|
|
if (name == target || name.Contains(target) || target.Contains(name)) return bone;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|