The body swap read the race off the ZDO and gave up when there was none, which is exactly the situation on the menu preview - FejdStartup instantiates a Player and calls PlayerProfile.LoadPlayerData on it, so the preview knows its own custom data and therefore its race, but it has no business owning a network view. It now falls back to m_customData the way every other race in the mod already does, so a Skeleton character looks like one before you press Start. The tier falls back the same way. In the menu there is no world loaded to have killed anything, so the preview is the base skeleton whatever the character has done elsewhere. feat(dvergar): age into a starred dverger's colours One star at Bonemass, two at Fader. Nothing is picked by eye. A starred creature in Valheim is not a second model: it is the same one recoloured by LevelEffects, whose LevelSetup list holds a hue/saturation/value offset per star. Those offsets are read off the Dverger prefab at runtime and applied to the race's own slate blue-grey, so the tiers land on the tints the game itself uses and keep following them if the prefab changes. The tier comes from the ZDO key DvergarRace already publishes, so a remote Dvergar is coloured at their own standing rather than the observer's - which matters as soon as anyone runs personal progression. Crossing a tier pushes a colour refresh through immediately, since the tint is only written from the UpdateColors postfix and Valheim runs that on its own schedule. Same treatment the health malus already gets a few lines above. The hood is deliberately untouched.
199 lines
8.8 KiB
C#
199 lines
8.8 KiB
C#
using Jotunn.Managers;
|
|
using UnityEngine;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The Skeleton's cosmetics that any client can grow for itself.
|
|
///
|
|
/// The head flame started life inside SkeletonRace, which is a single MonoBehaviour that only
|
|
/// ever looks at Player.m_localPlayer - so the fire existed on one client, on one player, and
|
|
/// everyone else saw a bare skull. Same shape of bug as the Draugh's aura, and the same fix:
|
|
/// the build takes a Player, and each client grows a copy for every skeleton it can see.
|
|
///
|
|
/// Nothing here is networked and nothing needs to be. The tier is already on the ZDO, so every
|
|
/// client can work out for itself whether a given skeleton should be alight.
|
|
/// </summary>
|
|
public static class SkeletonVisuals
|
|
{
|
|
public const string RaceId = "Skeleton";
|
|
|
|
/// <summary>
|
|
/// Reads the race off the ZDO, falling back to m_customData.
|
|
///
|
|
/// The fallback is what makes the race show up on the main-menu character preview.
|
|
/// FejdStartup.SetupCharacterPreview instantiates a Player and calls
|
|
/// PlayerProfile.LoadPlayerData on it, which restores m_customData - so the preview does
|
|
/// know it is a Skeleton. What it has no business having is a ZNetView, and reading the
|
|
/// race from the ZDO alone meant the body swap gave up before it ever looked.
|
|
/// </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 IsSkeletonPlayer(Player player) => GetPlayerRace(player) == RaceId;
|
|
|
|
/// <summary>
|
|
/// The tier a given player's body should be built for. The ZDO carries it so remote
|
|
/// players render at their own standing; without one - the menu preview - fall back to
|
|
/// whatever this client can work out, which in the menu is the base tier, since there is
|
|
/// no world loaded to have killed anything.
|
|
/// </summary>
|
|
public static int GetProgressionState(Player player)
|
|
{
|
|
ZNetView nview = player != null ? player.GetComponent<ZNetView>() : null;
|
|
if (nview != null && nview.IsValid())
|
|
{
|
|
return nview.GetZDO().GetInt("RacesOfValheim_SkeletonState", 0);
|
|
}
|
|
|
|
return BossProgressionManager.GetCurrentProgressionState();
|
|
}
|
|
|
|
// Fire sources in preference order. The torch is an item every world has; the ground
|
|
// torch and the fire pit are the building pieces to fall back on.
|
|
private static readonly string[] FlameSourcePrefabs =
|
|
{
|
|
"Torch", "piece_groundtorch_wood", "piece_groundtorch", "fire_pit"
|
|
};
|
|
|
|
private static GameObject cachedFlameSource;
|
|
private static bool flameSourceResolved;
|
|
|
|
/// <summary>
|
|
/// Finds a vanilla prefab that carries real fire particles. Resolved once per session,
|
|
/// failure included, so a miss cannot turn into a per-frame log.
|
|
/// </summary>
|
|
private static GameObject ResolveFlameSource()
|
|
{
|
|
if (flameSourceResolved) return cachedFlameSource;
|
|
flameSourceResolved = true;
|
|
|
|
foreach (string name in FlameSourcePrefabs)
|
|
{
|
|
GameObject prefab = PrefabManager.Cache.GetPrefab<GameObject>(name);
|
|
if (prefab == null) continue;
|
|
if (prefab.GetComponentsInChildren<ParticleSystem>(true).Length == 0) continue;
|
|
|
|
Jotunn.Logger.LogInfo($"[Skeleton] Head flame source resolved to '{name}'.");
|
|
cachedFlameSource = prefab;
|
|
return cachedFlameSource;
|
|
}
|
|
|
|
Jotunn.Logger.LogWarning(
|
|
"[Skeleton] No fire prefab resolved (" + string.Join(", ", FlameSourcePrefabs) +
|
|
"); the skull stays unlit.");
|
|
return null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// One player's burning skull. Cosmetic only - it deals nothing and blocks nothing.
|
|
///
|
|
/// Held per player rather than statically: a static would hand the second skeleton in view
|
|
/// the first one's fire. Call Apply every frame; it builds once and then only follows.
|
|
/// </summary>
|
|
public class HeadFlame
|
|
{
|
|
// Above the head bone's own origin, which sits at the base of the skull.
|
|
private const float Height = 0.18f;
|
|
|
|
// Torch flames are authored for a fist-sized fire on a stick, so the skull wears a big
|
|
// multiple of one.
|
|
private const float Scale = 1.8f;
|
|
|
|
private GameObject m_instance;
|
|
private Transform m_headBone;
|
|
|
|
public void Apply(Player player, bool enable)
|
|
{
|
|
if (!enable)
|
|
{
|
|
if (m_instance != null && m_instance.activeSelf) m_instance.SetActive(false);
|
|
return;
|
|
}
|
|
|
|
if (player == null) return;
|
|
|
|
if (m_instance == null && !Build(player)) return;
|
|
|
|
if (!m_instance.activeSelf) m_instance.SetActive(true);
|
|
|
|
// Follows the skull without inheriting its scale.
|
|
if (m_headBone != null)
|
|
{
|
|
m_instance.transform.position = m_headBone.position + Vector3.up * Height;
|
|
}
|
|
}
|
|
|
|
/// <summary>Returns false while the pieces it needs are not ready yet.</summary>
|
|
private bool Build(Player player)
|
|
{
|
|
Animator anim = player.GetComponentInChildren<Animator>();
|
|
m_headBone = anim != null ? anim.GetBoneTransform(HumanBodyBones.Head) : null;
|
|
if (m_headBone == null) return false; // rig not up yet; try again next frame
|
|
|
|
GameObject source = ResolveFlameSource();
|
|
if (source == null) return false; // already logged once by the resolver
|
|
|
|
// Deliberately not parented to the head bone: the player rig is authored at
|
|
// roughly 100x, and a particle system inheriting that would put a bonfire where a
|
|
// candle belongs. It hangs off the player and is moved onto the bone each frame.
|
|
m_instance = new GameObject("SkeletonHeadFlameVFX");
|
|
m_instance.transform.SetParent(player.transform, false);
|
|
m_instance.transform.localScale = Vector3.one * Scale;
|
|
|
|
// Real fire is cloned rather than built by hand. The first attempt at this made a
|
|
// particle system from scratch on the smoke material, and a hand-rolled gradient
|
|
// over a smoke sprite reads as a wisp, not a burning skull.
|
|
foreach (ParticleSystem ps in source.GetComponentsInChildren<ParticleSystem>(true))
|
|
{
|
|
GameObject copy = Object.Instantiate(ps.gameObject, m_instance.transform);
|
|
copy.name = ps.gameObject.name;
|
|
copy.transform.localPosition = Vector3.zero;
|
|
copy.transform.localRotation = Quaternion.identity;
|
|
copy.SetActive(true);
|
|
|
|
// So the holder's scale actually enlarges the fire rather than just spreading
|
|
// out where the particles spawn.
|
|
ParticleSystem.MainModule main = copy.GetComponent<ParticleSystem>().main;
|
|
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
|
|
}
|
|
|
|
// A torch's own light is tuned to light a corridor from waist height; on a head it
|
|
// blows out the whole face. Keep the glow, lose the floodlight.
|
|
foreach (Light light in m_instance.GetComponentsInChildren<Light>(true))
|
|
{
|
|
light.range *= 0.5f;
|
|
light.intensity *= 0.6f;
|
|
}
|
|
|
|
// Anything that made the source a torch rather than a fire.
|
|
foreach (MonoBehaviour comp in m_instance.GetComponentsInChildren<MonoBehaviour>(true))
|
|
{
|
|
Object.Destroy(comp);
|
|
}
|
|
foreach (Collider col in m_instance.GetComponentsInChildren<Collider>(true))
|
|
{
|
|
Object.Destroy(col);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|