RacesOfValheim/JotunnModStub/Alfar/AlfarLevitation.cs
Ferdinando 3f8cf1ee27 feat(alfar): v1.6.0 - the Alfar, an eleventh race
Grey, tall and thin, with black eyes. Eitr is armour and fuel (shield,
diet, soft fall, levitation), Heimkall sends a lethal blow home at one
health every 20 minutes. Caster powers from Skrid to the archon
Sameining, with on-screen cooldowns. Automated base around the
Ljoskjarne: drones, three constructs, crystals, Ljosgard, Prismavard.
Compendium page in EN/IT/RU.

README: New in block, race section, progression, station line, changelog.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 17:38:28 +01:00

120 lines
5.2 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 && ZInput.GetButton("Crouch")) 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()
{
// ZInput's "Jump" covers the space bar and the pad's jump button alike, and respects the
// controls the player picked in the settings.
return ZInput.GetButton("Jump");
}
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;
}
}
}