bugfix 07/10/26 + evil laugh
This commit is contained in:
parent
eaa87f892a
commit
0159f1e8c4
11 changed files with 417 additions and 8 deletions
294
JotunnModStub/Skeleton/SkeletonEvilLaugh.cs
Normal file
294
JotunnModStub/Skeleton/SkeletonEvilLaugh.cs
Normal file
|
|
@ -0,0 +1,294 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Jotunn.Entities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RacesOfValheim
|
||||
{
|
||||
/// <summary>
|
||||
/// The Skeleton's villain laugh: /evillaugh. Head thrown back, jaw clacking, shoulders
|
||||
/// shaking, both fists punched up into the air.
|
||||
///
|
||||
/// It is not an animation clip and it is not the vanilla 'laugh' emote. It is a pose written
|
||||
/// over the rig in LateUpdate, the way SkeletonJawFix does for the mandible, so it needs no
|
||||
/// asset bundle and no Animator parameter.
|
||||
///
|
||||
/// The start time goes on the player's ZDO, so every client that can see the Skeleton plays
|
||||
/// the same laugh - the command only has to run on the laughing player's own machine.
|
||||
///
|
||||
/// Two kinds of bone, two rules (see SkeletonJawFix for why it matters):
|
||||
/// - Bones the Animator rewrites every frame (head, chest, arms) get the pose multiplied on
|
||||
/// top of whatever the animation left, so it can never accumulate.
|
||||
/// - Bones nothing animates (the jaw, the fingers) are written ABSOLUTELY from the rest
|
||||
/// pose captured once, so the pose is the same however many frames it has been applied.
|
||||
///
|
||||
/// Every direction is worked out in the player's own frame (right / up / forward) rather than
|
||||
/// in a bone's local axes, which the rig does not make guessable. The angles below are first
|
||||
/// guesses to be tuned in game.
|
||||
/// </summary>
|
||||
public class SkeletonEvilLaugh : MonoBehaviour
|
||||
{
|
||||
public const string ZdoKey = "RoV_skelEvilLaughAt";
|
||||
|
||||
private const float DurationSeconds = 4f;
|
||||
private const float BlendSeconds = 0.3f;
|
||||
|
||||
// The laugh itself: how fast the shoulders shake and the jaw clacks, per second.
|
||||
private const float ShakeHz = 8f;
|
||||
|
||||
private const float HeadBackDegrees = 28f;
|
||||
private const float NeckBackDegrees = 8f;
|
||||
private const float ChestBackDegrees = 10f;
|
||||
private const float ChestShakeDegrees = 3.5f;
|
||||
private const float HeadNodDegrees = 4f;
|
||||
private const float JawOpenDegrees = 24f;
|
||||
|
||||
// Fingers curl about the bone's own Z, the usual axis of a humanoid finger. If the fists
|
||||
// open instead of closing in game, flip the sign.
|
||||
private const float FingerCurlDegrees = 70f;
|
||||
|
||||
// Where the fists go, in the player's frame: outward, up, a little forward.
|
||||
private static readonly Vector3 UpperArmAim = new Vector3(0.5f, 0.8f, 0.25f);
|
||||
private static readonly Vector3 ForearmAim = new Vector3(0.15f, 1f, 0.1f);
|
||||
|
||||
private struct Side
|
||||
{
|
||||
public Transform upper, lower, hand;
|
||||
public float sign;
|
||||
public List<Transform> fingers;
|
||||
public List<Quaternion> fingerRest;
|
||||
}
|
||||
|
||||
private Player m_player;
|
||||
private VisEquipment m_vis;
|
||||
private ZNetView m_nview;
|
||||
|
||||
private bool m_resolved;
|
||||
private Transform m_head, m_neck, m_chest, m_jaw;
|
||||
private Quaternion m_jawRest;
|
||||
private Side m_left, m_right;
|
||||
|
||||
private float m_weight;
|
||||
private bool m_wroteHands;
|
||||
|
||||
// ── Starting it ──────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>Starts the laugh for the local player; everyone else picks it up from the ZDO.</summary>
|
||||
public static bool Begin(Player player)
|
||||
{
|
||||
if (player == null || ZNet.instance == null) return false;
|
||||
if (!SkeletonVisuals.IsSkeletonPlayer(player)) return false;
|
||||
|
||||
ZNetView nview = player.GetComponent<ZNetView>();
|
||||
if (nview == null || !nview.IsValid()) return false;
|
||||
|
||||
nview.GetZDO().Set(ZdoKey, ZNet.instance.GetTime().Ticks);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
m_player = GetComponent<Player>();
|
||||
m_vis = GetComponent<VisEquipment>();
|
||||
m_nview = GetComponent<ZNetView>();
|
||||
}
|
||||
|
||||
// ── The pose ─────────────────────────────────────────────────────────
|
||||
|
||||
private void LateUpdate()
|
||||
{
|
||||
if (m_player == null || m_nview == null || !m_nview.IsValid() || ZNet.instance == null) return;
|
||||
|
||||
float elapsed = ElapsedSinceStart();
|
||||
bool laughing = elapsed >= 0f && elapsed < DurationSeconds
|
||||
&& SkeletonVisuals.IsSkeletonPlayer(m_player)
|
||||
&& !m_player.IsDead();
|
||||
|
||||
// Fade in over the first moments, out over the last, so the pose never pops.
|
||||
float target = 0f;
|
||||
if (laughing) target = Mathf.Clamp01(Mathf.Min(elapsed, DurationSeconds - elapsed) / BlendSeconds);
|
||||
|
||||
m_weight = Mathf.MoveTowards(m_weight, target, Time.deltaTime / BlendSeconds);
|
||||
|
||||
if (m_weight <= 0f)
|
||||
{
|
||||
if (m_wroteHands) RestoreHands();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_resolved) Resolve();
|
||||
|
||||
float t = Time.time;
|
||||
float wave = Mathf.Sin(t * ShakeHz * Mathf.PI * 2f);
|
||||
float open = 0.5f + 0.5f * Mathf.Sin(t * ShakeHz * Mathf.PI * 2f + 0.6f);
|
||||
|
||||
Vector3 right = m_player.transform.right;
|
||||
|
||||
// Back and up, the whole spine in on it: chest first, then neck, then head.
|
||||
Pitch(m_chest, -(ChestBackDegrees + ChestShakeDegrees * wave) * m_weight, right);
|
||||
Pitch(m_neck, -NeckBackDegrees * m_weight, right);
|
||||
Pitch(m_head, -(HeadBackDegrees + HeadNodDegrees * wave) * m_weight, right);
|
||||
|
||||
if (m_jaw != null && m_jaw.parent != null)
|
||||
{
|
||||
m_jaw.localRotation = m_jawRest;
|
||||
m_jaw.rotation = Quaternion.AngleAxis(JawOpenDegrees * open * m_weight, right) * m_jaw.rotation;
|
||||
}
|
||||
|
||||
Fists(m_left, wave);
|
||||
Fists(m_right, wave);
|
||||
m_wroteHands = true;
|
||||
}
|
||||
|
||||
/// <summary>Seconds since the laugh began, by the same clock on every client; -1 if there is none.</summary>
|
||||
private float ElapsedSinceStart()
|
||||
{
|
||||
long ticks = m_nview.GetZDO().GetLong(ZdoKey, 0L);
|
||||
if (ticks == 0L) return -1f;
|
||||
|
||||
double age = (ZNet.instance.GetTime() - new DateTime(ticks)).TotalSeconds;
|
||||
return age < 0 ? -1f : (float)age;
|
||||
}
|
||||
|
||||
private static void Pitch(Transform bone, float degrees, Vector3 axis)
|
||||
{
|
||||
if (bone == null) return;
|
||||
bone.rotation = Quaternion.AngleAxis(degrees, axis) * bone.rotation;
|
||||
}
|
||||
|
||||
private void Fists(Side side, float wave)
|
||||
{
|
||||
if (side.upper == null || side.lower == null || side.hand == null) return;
|
||||
|
||||
Vector3 right = m_player.transform.right;
|
||||
Vector3 up = Vector3.up;
|
||||
Vector3 fwd = m_player.transform.forward;
|
||||
|
||||
// Each fist punches up and down a little with the laugh.
|
||||
Vector3 pump = up * (0.12f * wave);
|
||||
|
||||
Aim(side.upper, side.lower, Frame(UpperArmAim, side.sign, right, up, fwd) + pump);
|
||||
Aim(side.lower, side.hand, Frame(ForearmAim, -side.sign, right, up, fwd) + pump);
|
||||
|
||||
if (side.fingers == null) return;
|
||||
|
||||
for (int i = 0; i < side.fingers.Count; i++)
|
||||
{
|
||||
Transform finger = side.fingers[i];
|
||||
if (finger == null) continue;
|
||||
finger.localRotation = side.fingerRest[i]
|
||||
* Quaternion.Euler(0f, 0f, FingerCurlDegrees * m_weight);
|
||||
}
|
||||
}
|
||||
|
||||
private static Vector3 Frame(Vector3 aim, float sideSign, Vector3 right, Vector3 up, Vector3 fwd)
|
||||
=> (right * (aim.x * sideSign) + up * aim.y + fwd * aim.z).normalized;
|
||||
|
||||
/// <summary>
|
||||
/// Turns a bone so the line from it to its child points along 'desired', by as much as the
|
||||
/// weight allows. The child follows the parent, so the forearm is aimed after the upper arm.
|
||||
/// </summary>
|
||||
private void Aim(Transform bone, Transform child, Vector3 desired)
|
||||
{
|
||||
Vector3 current = child.position - bone.position;
|
||||
if (current.sqrMagnitude < 1e-8f) return;
|
||||
|
||||
Quaternion full = Quaternion.FromToRotation(current.normalized, desired.normalized);
|
||||
bone.rotation = Quaternion.Slerp(Quaternion.identity, full, m_weight) * bone.rotation;
|
||||
}
|
||||
|
||||
private void RestoreHands()
|
||||
{
|
||||
foreach (Side side in new[] { m_left, m_right })
|
||||
{
|
||||
if (side.fingers == null) continue;
|
||||
for (int i = 0; i < side.fingers.Count; i++)
|
||||
if (side.fingers[i] != null) side.fingers[i].localRotation = side.fingerRest[i];
|
||||
}
|
||||
|
||||
if (m_jaw != null) m_jaw.localRotation = m_jawRest;
|
||||
m_wroteHands = false;
|
||||
}
|
||||
|
||||
// ── Finding the bones ────────────────────────────────────────────────
|
||||
|
||||
private void Resolve()
|
||||
{
|
||||
Animator anim = m_player.GetComponentInChildren<Animator>();
|
||||
|
||||
m_head = Find(anim, HumanBodyBones.Head, "head");
|
||||
m_neck = Find(anim, HumanBodyBones.Neck, "neck");
|
||||
m_chest = Find(anim, HumanBodyBones.UpperChest, "spine2") ?? Find(anim, HumanBodyBones.Chest, "spine1");
|
||||
|
||||
m_jaw = FindByName("jaw");
|
||||
if (m_jaw != null) m_jawRest = m_jaw.localRotation;
|
||||
|
||||
m_left = BuildSide(anim, true);
|
||||
m_right = BuildSide(anim, false);
|
||||
|
||||
m_resolved = true;
|
||||
}
|
||||
|
||||
private Side BuildSide(Animator anim, bool left)
|
||||
{
|
||||
string prefix = left ? "left" : "right";
|
||||
Side side = new Side
|
||||
{
|
||||
sign = left ? -1f : 1f,
|
||||
upper = Find(anim, left ? HumanBodyBones.LeftUpperArm : HumanBodyBones.RightUpperArm, prefix + "arm"),
|
||||
lower = Find(anim, left ? HumanBodyBones.LeftLowerArm : HumanBodyBones.RightLowerArm, prefix + "forearm"),
|
||||
hand = Find(anim, left ? HumanBodyBones.LeftHand : HumanBodyBones.RightHand, prefix + "hand"),
|
||||
fingers = new List<Transform>(),
|
||||
fingerRest = new List<Quaternion>()
|
||||
};
|
||||
|
||||
// Every bone below the hand is a finger or a thumb; a hand with none just opens and
|
||||
// closes nothing, and the arms still go up.
|
||||
if (side.hand != null)
|
||||
{
|
||||
foreach (Transform child in side.hand.GetComponentsInChildren<Transform>())
|
||||
{
|
||||
if (child == side.hand) continue;
|
||||
side.fingers.Add(child);
|
||||
side.fingerRest.Add(child.localRotation);
|
||||
}
|
||||
}
|
||||
|
||||
return side;
|
||||
}
|
||||
|
||||
private Transform Find(Animator anim, HumanBodyBones bone, string fallbackName)
|
||||
{
|
||||
Transform t = anim != null ? anim.GetBoneTransform(bone) : null;
|
||||
return t != null ? t : FindByName(fallbackName);
|
||||
}
|
||||
|
||||
private Transform FindByName(string part)
|
||||
{
|
||||
if (m_vis == null || m_vis.m_bodyModel == null || m_vis.m_bodyModel.bones == null) return null;
|
||||
|
||||
foreach (Transform bone in m_vis.m_bodyModel.bones)
|
||||
if (bone != null && bone.name.ToLower().Contains(part)) return bone;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class EvilLaughCommand : ConsoleCommand
|
||||
{
|
||||
public override string Name => "evillaugh";
|
||||
public override string Help => "The Skeleton's villain laugh";
|
||||
|
||||
public override void Run(string[] args)
|
||||
{
|
||||
if (Player.m_localPlayer == null)
|
||||
{
|
||||
Console.instance.Print("You must be in-game to laugh.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!SkeletonEvilLaugh.Begin(Player.m_localPlayer))
|
||||
Console.instance.Print("Only a Skeleton laughs like that.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -359,6 +359,12 @@ namespace RacesOfValheim
|
|||
p.gameObject.AddComponent<SkeletonJawFix>();
|
||||
}
|
||||
|
||||
// The /evillaugh pose; same reasoning as the jaw, so remote players laugh too.
|
||||
if (p.GetComponent<SkeletonEvilLaugh>() == null)
|
||||
{
|
||||
p.gameObject.AddComponent<SkeletonEvilLaugh>();
|
||||
}
|
||||
|
||||
// The lights in the eye sockets, cloned out of this tier's own creature so
|
||||
// that each one arrives its own colour. Outside the build block above on
|
||||
// purpose: they hang off the player's rig bones rather than off the body
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue