86 lines
3.2 KiB
C#
86 lines
3.2 KiB
C#
using HarmonyLib;
|
|
using UnityEngine;
|
|
using Jotunn.Managers;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
/// <summary>
|
|
/// The bat's flight, held on for as long as the worn model exists.
|
|
///
|
|
/// Debug fly has two halves, and this used to write only one. The field Player.m_debugFly is
|
|
/// what the local player's own physics reads; ZDOVars.s_debugFly is what every OTHER client
|
|
/// reads, because Character.IsDebugFlying consults the ZDO whenever it is not the owner. With
|
|
/// only the field written, a bat flew on its own screen and fell, walked and swam on everyone
|
|
/// else's - the fault WarVultureMount.SetFlying already names in its own summary. Both halves
|
|
/// go through SetFlying below now, so there is one place that can ever get it wrong again.
|
|
///
|
|
/// Writing the ZDO every frame is not network spam: ZDO.Set(int, bool) bottoms out in
|
|
/// ZDOExtraData.Set, which returns false when the value is unchanged, and only a true return
|
|
/// bumps the data revision. Read out of the assembly rather than assumed, because "every frame"
|
|
/// deserved the check.
|
|
/// </summary>
|
|
public class BatFormBehavior : MonoBehaviour
|
|
{
|
|
private static readonly System.Reflection.FieldInfo DebugFlyField =
|
|
AccessTools.Field(typeof(Player), "m_debugFly");
|
|
|
|
private Animator anim;
|
|
private AswidProgression.FormRules formRules;
|
|
|
|
public void Initialize(AswidProgression.FormRules rules)
|
|
{
|
|
this.formRules = rules;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
anim = GetComponentInChildren<Animator>();
|
|
SetFlying(true);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
|
|
|
|
// Ensure flight remains on continuously
|
|
SetFlying(true);
|
|
|
|
// Sync Animator
|
|
if (anim != null)
|
|
{
|
|
Vector3 vel = Player.m_localPlayer.GetVelocity();
|
|
vel.y = 0f;
|
|
float speed = vel.magnitude;
|
|
|
|
// Bats don't necessarily have a complex animator like players,
|
|
// but if they do have a speed or flying parameter, we set it here.
|
|
anim.SetFloat("forward_speed", speed);
|
|
anim.SetBool("flying", true);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
SetFlying(false);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Both halves of debug fly, always together. The field write survives a missing or invalid
|
|
/// ZNetView on purpose: without it a bat whose networking went away mid-flight would keep
|
|
/// flying on foot forever, which is the worse of the two failures.
|
|
/// </summary>
|
|
private static void SetFlying(bool flying)
|
|
{
|
|
Player player = Player.m_localPlayer;
|
|
if (player == null) return;
|
|
|
|
if (DebugFlyField != null) DebugFlyField.SetValue(player, flying);
|
|
|
|
ZNetView nview = player.GetComponent<ZNetView>();
|
|
if (nview != null && nview.IsValid() && nview.IsOwner())
|
|
{
|
|
nview.GetZDO().Set(ZDOVars.s_debugFly, flying);
|
|
}
|
|
}
|
|
}
|
|
}
|