RacesOfValheim/JotunnModStub/Alfar/AlfarLevitation.cs
kanym b9ec452e72 fix: Draugh eats Bjorn and Volture meat, Alfar levitation on gamepad
Draugh: add BjornMeat -> CookedBjornMeat to the raw-to-cooked table and
correct VoltureMeat's cooked name (RoastedVoltureMeat does not exist,
CookedVoltureMeat does), so neither raw meat got an EdibleUncooked_ item.
Names checked against the SoftRef manifests.

Alfar: ZInput "Jump" is the keyboard binding only. Read "JoyJump" and
"JoyCrouch" too, as PlayerController does, for levitation and the
Archon's climb and descent.

Reported by lexsonn.
2026-10-27 19:25:03 +01:00

126 lines
5.5 KiB
C#

using HarmonyLib;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// LEVITATION (Alfar_Progettazione.txt sec. 4.6): in the air, hold the jump key and the Alfr rises
/// slowly, burning eitr slowly. Let go and the soft fall takes over.
///
/// There is no hover in vanilla. This pushes the rigidbody's vertical speed up toward a small
/// target while the key is held, and nothing else: no horizontal speed is added, so it is a way
/// over a ledge or out of a pit and not flight (the pool of an early Alfr lasts about half a
/// minute of it).
///
/// It runs on the owner's FixedUpdate, where the rigidbody is read, and the position syncs like
/// any other movement. The eitr is spent in small batches, not every physics step: each spend arms
/// the regeneration delay and goes through an RPC call.
/// </summary>
public static class AlfarLevitation
{
// A short moment in the air before it starts, so a normal jump (a tap) is just a jump.
private const float AirDelay = 0.2f;
private const float SpendInterval = 0.25f;
private static float airTime;
private static float owed;
private static float nextSpend;
public static bool Rising { get; private set; }
[HarmonyPatch(typeof(Player), "FixedUpdate")]
public static class Alfar_Levitation_Patch
{
public static void Postfix(Player __instance)
{
if (__instance == null || __instance != Player.m_localPlayer) return;
if (!AlfarVisuals.IsAlfarPlayer(__instance) || __instance.IsDead()) { Reset(); return; }
// THE ARCHON FLIES for as long as the form lasts: it hovers where it is, climbs while
// jump is held and sinks while crouch is held. The eitr is already gone, so nothing is
// spent. Horizontal movement stays the normal controls.
if (AlfarArchon.Active)
{
Rigidbody flyer = Traverse.Create(__instance).Field("m_body").GetValue<Rigidbody>();
if (flyer != null)
{
float speed = AlfarPowerConfig.LevitationRise.Value * 2f;
float target = 0f;
if (Time.time - AlfarArchon.StartedAt < 0.6f) target = 4f; // lift off
else if (RaceInput.Allowed && JumpHeld()) target = speed;
else if (RaceInput.Allowed && CrouchHeld()) target = -speed;
Vector3 velocity = flyer.linearVelocity;
velocity.y = Mathf.Lerp(velocity.y, target, 0.25f);
flyer.linearVelocity = velocity;
}
Traverse.Create(__instance).Field("m_maxAirAltitude").SetValue(__instance.transform.position.y);
Rising = true;
return;
}
bool wants = RaceInput.Allowed && JumpHeld();
bool airborne = !__instance.IsOnGround() && !__instance.IsSwimming() && !__instance.IsAttached() &&
!__instance.IsFlying();
airTime = airborne ? airTime + Time.fixedDeltaTime : 0f;
float cost = AlfarPowerConfig.LevitationCostPerSecond.Value;
bool canPay = AlfarArchon.Active || __instance.GetEitr() > 0f;
if (!wants || !airborne || airTime < AirDelay || !canPay)
{
Rising = false;
FlushSpend(__instance);
return;
}
Rising = true;
Rigidbody body = Traverse.Create(__instance).Field("m_body").GetValue<Rigidbody>();
if (body != null)
{
Vector3 velocity = body.linearVelocity;
velocity.y = Mathf.Lerp(velocity.y, AlfarPowerConfig.LevitationRise.Value, 0.25f);
body.linearVelocity = velocity;
}
// Rising resets the fall: the point of the soft fall is that the way down starts at
// the top, and a long climb must not count as a long drop at eitr zero.
Traverse.Create(__instance).Field("m_maxAirAltitude").SetValue(__instance.transform.position.y);
// In the archon form the eitr is already gone and the pool pays for nothing here.
if (!AlfarArchon.Active) owed += cost * Time.fixedDeltaTime;
if (Time.time >= nextSpend) FlushSpend(__instance);
}
}
private static bool JumpHeld()
{
// "Jump" is the keyboard binding only; the pad's button is a separate "JoyJump", read
// the way PlayerController reads it, kept off while a radial menu has the pad.
return ZInput.GetButton("Jump") || (ZInput.GetButton("JoyJump") && !Hud.InRadial());
}
private static bool CrouchHeld()
{
// Same split as jump: PlayerController ORs "Crouch" with "JoyCrouch".
return ZInput.GetButton("Crouch") || (ZInput.GetButton("JoyCrouch") && !Hud.InRadial());
}
private static void FlushSpend(Player player)
{
nextSpend = Time.time + SpendInterval;
if (owed <= 0f) return;
AlfarEitr.Spend(player, owed);
owed = 0f;
}
private static void Reset()
{
airTime = 0f;
owed = 0f;
Rising = false;
}
}
}