341 lines
14 KiB
C#
341 lines
14 KiB
C#
using HarmonyLib;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
[HarmonyPatch(typeof(Player), "Awake")]
|
|
public static class Player_Awake_VisualSync_Patch
|
|
{
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
if (__instance.gameObject.GetComponent<RacesVisualController>() == null)
|
|
{
|
|
__instance.gameObject.AddComponent<RacesVisualController>();
|
|
}
|
|
}
|
|
}
|
|
|
|
[HarmonyPatch(typeof(Player), "OnSpawned")]
|
|
public static class Player_OnSpawned_Sync_Patch
|
|
{
|
|
public static void Postfix(Player __instance)
|
|
{
|
|
ZNetView nview = __instance.GetComponent<ZNetView>();
|
|
if (__instance == Player.m_localPlayer && nview != null && nview.IsValid())
|
|
{
|
|
if (__instance.m_customData.TryGetValue("Race", out string race))
|
|
{
|
|
nview.GetZDO().Set("RacesOfValheim_Race", race);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class RacesVisualController : MonoBehaviour
|
|
{
|
|
private Player m_player;
|
|
private string m_lastFenrirForm = "";
|
|
private string m_lastSkadiForm = "";
|
|
private GameObject m_currentFormInstance = null;
|
|
private GameObject m_fireAuraVFX = null;
|
|
|
|
private void Awake()
|
|
{
|
|
m_player = GetComponent<Player>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (m_player == null) return;
|
|
ZNetView nview = m_player.GetComponent<ZNetView>();
|
|
if (nview == null || !nview.IsValid()) return;
|
|
if (m_player == Player.m_localPlayer) return; // Local player handles their own visuals in Race scripts
|
|
|
|
if (m_player.IsDead())
|
|
{
|
|
RevertAllVisuals();
|
|
return;
|
|
}
|
|
|
|
string race = nview.GetZDO().GetString("RacesOfValheim_Race", "Human");
|
|
|
|
if (race == "Skeleton")
|
|
{
|
|
UpdateSkeletonVisuals();
|
|
}
|
|
else if (race == "Fenrir")
|
|
{
|
|
UpdateFenrirVisuals();
|
|
}
|
|
else if (race == "Skadi")
|
|
{
|
|
UpdateSkadiVisuals();
|
|
}
|
|
else if (race == "Aswid")
|
|
{
|
|
UpdateAswidVisuals();
|
|
}
|
|
else
|
|
{
|
|
RevertAllVisuals();
|
|
}
|
|
}
|
|
|
|
private void UpdateAswidVisuals()
|
|
{
|
|
// Empty for now, relying on VisEquipment patch
|
|
}
|
|
|
|
private void UpdateSkeletonVisuals()
|
|
{
|
|
ZNetView nview = m_player.GetComponent<ZNetView>();
|
|
int state = nview.GetZDO().GetInt("RacesOfValheim_SkeletonState", 0);
|
|
var rules = SkeletonProgression.GetRules(state);
|
|
|
|
// Hide hair and beard
|
|
VisEquipment vis = m_player.GetComponent<VisEquipment>();
|
|
if (vis != null)
|
|
{
|
|
vis.SetHairItem("");
|
|
vis.SetBeardItem("");
|
|
|
|
// Hide Helmet
|
|
var helmetObj = HarmonyLib.Traverse.Create(vis).Field<GameObject>("m_helmetItemInstance").Value;
|
|
if (helmetObj != null && helmetObj.transform.localScale != Vector3.zero)
|
|
{
|
|
helmetObj.transform.localScale = Vector3.zero;
|
|
}
|
|
|
|
// Scale Body Armor
|
|
string[] armorFields = { "m_chestItemInstances", "m_legItemInstances", "m_shoulderItemInstances" };
|
|
foreach (string field in armorFields)
|
|
{
|
|
var armorList = HarmonyLib.Traverse.Create(vis).Field<List<GameObject>>(field).Value;
|
|
if (armorList != null)
|
|
{
|
|
foreach (GameObject armor in armorList)
|
|
{
|
|
if (armor != null && Mathf.Abs(armor.transform.localScale.x - 1.05f) > 0.01f)
|
|
{
|
|
armor.transform.localScale = new Vector3(1.05f, 1.05f, 1.05f);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Hide Jaw for Charred Twitcher
|
|
if (vis.m_bodyModel != null && vis.m_bodyModel.bones != null)
|
|
{
|
|
foreach (Transform bone in vis.m_bodyModel.bones)
|
|
{
|
|
if (bone != null && bone.name.ToLower().Contains("jaw"))
|
|
{
|
|
if (state >= 8)
|
|
{
|
|
if (bone.localScale != Vector3.zero) bone.localScale = Vector3.zero;
|
|
}
|
|
else
|
|
{
|
|
if (bone.localScale == Vector3.zero) bone.localScale = Vector3.one;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Apply Scale
|
|
Vector3 targetScale = rules.ScaleMultiplier;
|
|
if (m_player.transform.localScale != targetScale)
|
|
{
|
|
m_player.transform.localScale = targetScale;
|
|
}
|
|
|
|
// Fire Aura VFX
|
|
EnsureFireAuraVFX(rules.FireAura);
|
|
}
|
|
|
|
private void UpdateFenrirVisuals()
|
|
{
|
|
ZNetView nview = m_player.GetComponent<ZNetView>();
|
|
string currentForm = nview.GetZDO().GetString("RacesOfValheim_FenrirForm", "");
|
|
|
|
if (currentForm != m_lastFenrirForm)
|
|
{
|
|
// Form changed
|
|
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
|
|
|
|
Transform playerVisual = m_player.transform.Find("Visual");
|
|
if (playerVisual != null)
|
|
{
|
|
if (string.IsNullOrEmpty(currentForm))
|
|
{
|
|
playerVisual.localScale = Vector3.one;
|
|
}
|
|
else
|
|
{
|
|
playerVisual.localScale = Vector3.one * 0.001f;
|
|
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(currentForm);
|
|
if (prefab != null)
|
|
{
|
|
Transform visual = prefab.transform.Find("Visual");
|
|
if (visual != null)
|
|
{
|
|
m_currentFormInstance = Instantiate(visual.gameObject, m_player.transform);
|
|
m_currentFormInstance.transform.localPosition = Vector3.zero;
|
|
m_currentFormInstance.transform.localRotation = Quaternion.identity;
|
|
Component animEvent = m_currentFormInstance.GetComponent("AnimEvent");
|
|
if (animEvent != null) Destroy(animEvent);
|
|
|
|
// Clean up colliders on remote visuals
|
|
foreach (var col in m_currentFormInstance.GetComponentsInChildren<Collider>())
|
|
{
|
|
Destroy(col);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_lastFenrirForm = currentForm;
|
|
}
|
|
}
|
|
|
|
private void UpdateSkadiVisuals()
|
|
{
|
|
ZNetView nview = m_player.GetComponent<ZNetView>();
|
|
string currentForm = nview.GetZDO().GetString("RacesOfValheim_SkadiForm", "");
|
|
|
|
if (currentForm != m_lastSkadiForm)
|
|
{
|
|
// Form changed
|
|
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
|
|
|
|
Transform playerVisual = m_player.transform.Find("Visual");
|
|
if (playerVisual != null)
|
|
{
|
|
if (string.IsNullOrEmpty(currentForm))
|
|
{
|
|
playerVisual.localScale = Vector3.one;
|
|
}
|
|
else
|
|
{
|
|
playerVisual.localScale = Vector3.one * 0.001f;
|
|
GameObject prefab = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(currentForm);
|
|
if (prefab != null)
|
|
{
|
|
Transform visual = prefab.transform.Find("Visual");
|
|
if (visual != null)
|
|
{
|
|
m_currentFormInstance = Instantiate(visual.gameObject, m_player.transform);
|
|
m_currentFormInstance.transform.localPosition = Vector3.zero;
|
|
m_currentFormInstance.transform.localRotation = Quaternion.identity;
|
|
Component animEvent = m_currentFormInstance.GetComponent("AnimEvent");
|
|
if (animEvent != null) Destroy(animEvent);
|
|
|
|
foreach (var col in m_currentFormInstance.GetComponentsInChildren<Collider>())
|
|
{
|
|
Destroy(col);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_lastSkadiForm = currentForm;
|
|
}
|
|
}
|
|
|
|
private void RevertAllVisuals()
|
|
{
|
|
if (m_player.transform.localScale != Vector3.one) m_player.transform.localScale = Vector3.one;
|
|
|
|
Transform playerVisual = m_player.transform.Find("Visual");
|
|
if (playerVisual != null && playerVisual.localScale != Vector3.one)
|
|
{
|
|
playerVisual.localScale = Vector3.one;
|
|
}
|
|
|
|
if (m_currentFormInstance != null) Destroy(m_currentFormInstance);
|
|
EnsureFireAuraVFX(false);
|
|
|
|
Animator anim = m_player.GetComponentInChildren<Animator>();
|
|
if (anim != null)
|
|
{
|
|
Transform headBone = anim.GetBoneTransform(HumanBodyBones.Head);
|
|
if (headBone != null)
|
|
{
|
|
Transform eyes = headBone.Find("AswidEyes");
|
|
if (eyes != null) Destroy(eyes.gameObject);
|
|
}
|
|
}
|
|
|
|
m_lastFenrirForm = "";
|
|
m_lastSkadiForm = "";
|
|
}
|
|
|
|
private void EnsureFireAuraVFX(bool enable)
|
|
{
|
|
if (enable)
|
|
{
|
|
if (m_fireAuraVFX == null)
|
|
{
|
|
GameObject sourceVFX = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>("vfx_smoke") ??
|
|
Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>("vfx_surtling_death");
|
|
|
|
m_fireAuraVFX = new GameObject("SkeletonFireAuraVFX");
|
|
m_fireAuraVFX.transform.SetParent(m_player.transform, false);
|
|
m_fireAuraVFX.transform.localPosition = new Vector3(0, 1f, 0);
|
|
|
|
if (sourceVFX != null)
|
|
{
|
|
ParticleSystem[] pss = sourceVFX.GetComponentsInChildren<ParticleSystem>();
|
|
if (pss.Length > 0)
|
|
{
|
|
ParticleSystem ps = m_fireAuraVFX.AddComponent<ParticleSystem>();
|
|
var main = ps.main;
|
|
main.loop = true;
|
|
main.duration = 1f;
|
|
main.startLifetime = 1.5f;
|
|
main.startSize = new ParticleSystem.MinMaxCurve(1.5f, 3f);
|
|
main.startSpeed = 0f;
|
|
main.simulationSpace = ParticleSystemSimulationSpace.World;
|
|
|
|
var renderer = m_fireAuraVFX.GetComponent<ParticleSystemRenderer>();
|
|
renderer.material = pss[0].GetComponent<ParticleSystemRenderer>().material;
|
|
|
|
var shape = ps.shape;
|
|
shape.shapeType = ParticleSystemShapeType.Sphere;
|
|
shape.radius = 4.5f;
|
|
|
|
var emission = ps.emission;
|
|
emission.rateOverTime = 40f;
|
|
|
|
var colorOverLifetime = ps.colorOverLifetime;
|
|
colorOverLifetime.enabled = true;
|
|
Gradient grad = new Gradient();
|
|
grad.SetKeys(
|
|
new GradientColorKey[] {
|
|
new GradientColorKey(new Color(1f, 0.8f, 0.2f), 0.0f),
|
|
new GradientColorKey(new Color(1f, 0.3f, 0.0f), 0.5f),
|
|
new GradientColorKey(new Color(0.2f, 0.0f, 0.0f), 1.0f)
|
|
},
|
|
new GradientAlphaKey[] {
|
|
new GradientAlphaKey(0.0f, 0.0f),
|
|
new GradientAlphaKey(0.6f, 0.2f),
|
|
new GradientAlphaKey(0.6f, 0.8f),
|
|
new GradientAlphaKey(0.0f, 1.0f)
|
|
}
|
|
);
|
|
colorOverLifetime.color = grad;
|
|
}
|
|
}
|
|
}
|
|
if (!m_fireAuraVFX.activeSelf) m_fireAuraVFX.SetActive(true);
|
|
}
|
|
else
|
|
{
|
|
if (m_fireAuraVFX != null && m_fireAuraVFX.activeSelf) m_fireAuraVFX.SetActive(false);
|
|
}
|
|
}
|
|
}
|
|
}
|