RacesOfValheim/JotunnModStub/Skadi/DrakeFormBehavior.cs
kanym bca47beda6 Ignore race ability input while a GUI has focus
UnityEngine.Input reads the device directly, so abilities bound to keys
or mouse buttons kept firing with the map, the inventory, a chest, the
chat or a menu open - clicking an item in a container also swung the
transformed attack.

Route that input through Player.TakeInput, which is what Valheim runs
its own controls through and already reports false for those states
(InventoryGui covers the backpack and chests alike). Aswid's transform
keys were the only place already doing this; the shared helper replaces
that one-off and covers Skadi's cold spell, form keys, flight toggle,
form attacks and flight boost, Fenrir's form keys and the attacks of all
four form behaviors, Trol's rock throw and true form, and Aswid's devour
toggle.

Cultist fire is a held button, so it is gated per read rather than
around the block: opening a GUI mid-stream now falls through to
StopFire() instead of leaving the flames running.
2026-08-01 13:25:02 +02:00

389 lines
17 KiB
C#

using UnityEngine;
using System.Collections;
using Jotunn.Managers;
namespace RacesOfValheim
{
public class DrakeFormBehavior : MonoBehaviour
{
// Single active instance, read by SkadiDirectedFlightPatch to feed a
// look-direction-based m_moveDir into Character.UpdateFlying every frame.
public static DrakeFormBehavior Active { get; private set; }
private Animator anim;
private bool isFlying = false;
private bool isLanding = false;
private float lastAttackTime = 0f;
public bool IsFlying => isFlying;
// Time.time of the last powered/grounded moment. SkadiDirectedFlightPatch holds the form
// level for a short window past this before letting it start sinking.
public float LastPoweredTime;
private SkadiProgression.FormRules formRules;
public void Initialize(SkadiProgression.FormRules rules)
{
this.formRules = rules;
}
// formRules is a snapshot taken when the form was entered, so a progression change while
// still transformed (defeating a boss, or the increaseprogression debug command) wouldn't
// grant the attack until the player reverted and transformed again. Re-read the rules for
// the form we're currently in so the unlock applies as soon as the tier is reached.
private bool CanAttackNow()
{
if (formRules == null) return false;
int state = BossProgressionManager.GetCurrentProgressionState();
SkadiProgression.FormRules live = null;
switch (formRules.Form)
{
case SkadiProgression.FormType.Drake: live = SkadiProgression.GetGKeyRules(state); break;
case SkadiProgression.FormType.Moder: live = SkadiProgression.GetHKeyRules(state); break;
case SkadiProgression.FormType.SkeletonDragon: live = SkadiProgression.GetJKeyRules(state); break;
}
return live != null && live.Form == formRules.Form && live.CanAttackInForm;
}
private const float ATTACK_COST = 20f;
private const float ATTACK_COOLDOWN = 3f;
private void Start()
{
Active = this;
anim = GetComponentInChildren<Animator>();
if (Player.m_localPlayer != null)
{
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false);
}
}
private void Update()
{
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
// Handle Flight Toggle (RMB) - Disabled for Fader
if (RaceInput.Allowed && Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon)
{
if (!isFlying && !isLanding)
{
isFlying = true;
LastPoweredTime = Time.time;
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(true);
}
else if (isFlying && !isLanding)
{
isLanding = true;
isFlying = false;
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false);
}
else if (isLanding)
{
// Cancel landing, resume flight
isLanding = false;
isFlying = true;
LastPoweredTime = Time.time;
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(true);
}
}
if (isLanding)
{
// Prevent fall damage by keeping maxAirAltitude equal to current height
var altitudeField = HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_maxAirAltitude");
if (altitudeField.FieldExists())
{
altitudeField.SetValue(Player.m_localPlayer.transform.position.y);
}
if (Player.m_localPlayer.IsOnGround())
{
isLanding = false;
}
}
// Sync Animator
if (anim != null)
{
bool grounded = !isFlying && !isLanding && Player.m_localPlayer.IsOnGround();
// Use standard Valheim monster animator parameters
anim.SetBool("onGround", grounded);
anim.SetBool("flying", isFlying || isLanding);
Vector3 vel = Player.m_localPlayer.GetVelocity();
vel.y = 0f;
float speed = vel.magnitude;
anim.SetFloat("forward_speed", speed);
}
// Handle Attack
if (CanAttackNow() && RaceInput.Allowed)
{
if (Input.GetMouseButtonDown(0))
{
if (Time.time - lastAttackTime >= ATTACK_COOLDOWN)
{
StartCoroutine(PerformAttackCoroutine(0));
}
}
else if (Input.GetMouseButtonDown(2)) // MMB
{
if (Time.time - lastAttackTime >= ATTACK_COOLDOWN)
{
StartCoroutine(PerformAttackCoroutine(2));
}
}
else if (Input.GetMouseButtonDown(1) && formRules.Form == SkadiProgression.FormType.SkeletonDragon) // RMB for Fader Spin
{
if (Time.time - lastAttackTime >= ATTACK_COOLDOWN)
{
StartCoroutine(PerformAttackCoroutine(1));
}
}
}
}
private IEnumerator PerformAttackCoroutine(int button)
{
lastAttackTime = Time.time;
if (formRules.Form == SkadiProgression.FormType.Drake)
{
if (button == 2) yield break; // Drake only uses LMB
Player.m_localPlayer.UseEitr(ATTACK_COST);
if (anim != null) anim.SetTrigger("attack");
// Delay to match animation spit timing
yield return new WaitForSeconds(0.3f);
GameObject projPrefab = PrefabManager.Cache.GetPrefab<GameObject>("hatchling_cold_projectile");
if (projPrefab == null) yield break;
ItemDrop staffDrop = PrefabManager.Cache.GetPrefab<GameObject>("StaffIceShards")?.GetComponent<ItemDrop>();
if (staffDrop == null) yield break;
float vel = staffDrop.m_itemData.m_shared.m_attack.m_projectileVel;
for (int i = 0; i < 3; i++)
{
Vector3 spawnPos = Player.m_localPlayer.GetEyePoint() + Player.m_localPlayer.GetLookDir() * 1.5f;
Vector3 targetDir = Player.m_localPlayer.GetLookDir();
Vector3 right = Vector3.Cross(Vector3.up, targetDir);
targetDir += right * UnityEngine.Random.Range(-0.1f, 0.1f) + Vector3.up * UnityEngine.Random.Range(-0.1f, 0.1f);
targetDir.Normalize();
GameObject projInstance = Instantiate(projPrefab, spawnPos, Quaternion.LookRotation(targetDir));
Projectile proj = projInstance.GetComponent<Projectile>();
if (proj != null)
{
HitData hitData = new HitData();
hitData.m_damage = staffDrop.m_itemData.m_shared.m_damages;
hitData.SetAttacker(Player.m_localPlayer);
proj.Setup(Player.m_localPlayer, targetDir * vel, -1f, hitData, staffDrop.m_itemData, null);
}
yield return new WaitForSeconds(0.2f);
}
}
else if (formRules.Form == SkadiProgression.FormType.Moder)
{
string attackTrigger = "attack_bite";
string projName = null;
int projCount = 1;
float projDelay = 0.5f;
bool isMelee = true;
if (isFlying || isLanding)
{
// Air attack (Ice Barrage)
attackTrigger = "attack_iceball";
projName = "dragon_ice_projectile";
projCount = 16;
projDelay = 0.8f;
isMelee = false;
}
else
{
if (button == 2)
{
// Ground MMB (Breath)
attackTrigger = "attack_breath";
projName = "hatchling_cold_projectile"; // Use cold explosion projectiles for a breath stream
projCount = 20;
projDelay = 0.5f; // Wait for the head to lower before breathing
isMelee = false;
}
else
{
// Ground LMB (Bite/Claw)
attackTrigger = UnityEngine.Random.value > 0.5f ? "attack_bite" : (UnityEngine.Random.value > 0.5f ? "attack_claw_left" : "attack_claw_right");
isMelee = true;
projDelay = 0.5f;
}
}
Player.m_localPlayer.UseEitr(ATTACK_COST);
if (anim != null) anim.SetTrigger(attackTrigger);
yield return new WaitForSeconds(projDelay);
if (!isMelee && projName != null)
{
GameObject projPrefab = PrefabManager.Cache.GetPrefab<GameObject>(projName);
if (projPrefab == null) yield break;
ItemDrop staffDrop = PrefabManager.Cache.GetPrefab<GameObject>("StaffIceShards")?.GetComponent<ItemDrop>();
if (staffDrop == null) yield break;
float vel = staffDrop.m_itemData.m_shared.m_attack.m_projectileVel;
for (int i = 0; i < projCount; i++)
{
Vector3 spawnPos = Player.m_localPlayer.GetEyePoint() + Player.m_localPlayer.GetLookDir() * 3.0f;
Vector3 targetDir = Player.m_localPlayer.GetLookDir();
if (projCount > 1)
{
Vector3 right = Vector3.Cross(Vector3.up, targetDir);
targetDir += right * UnityEngine.Random.Range(-0.3f, 0.3f) + Vector3.up * UnityEngine.Random.Range(-0.3f, 0.3f);
targetDir.Normalize();
}
GameObject projInstance = Instantiate(projPrefab, spawnPos, Quaternion.LookRotation(targetDir));
Projectile proj = projInstance.GetComponent<Projectile>();
if (proj != null)
{
HitData hitData = new HitData();
hitData.m_damage = staffDrop.m_itemData.m_shared.m_damages;
hitData.SetAttacker(Player.m_localPlayer);
proj.Setup(Player.m_localPlayer, targetDir * vel, -1f, hitData, staffDrop.m_itemData, null);
}
if (projCount > 1) yield return new WaitForSeconds(0.05f);
}
}
}
else if (formRules.Form == SkadiProgression.FormType.SkeletonDragon)
{
string attackTrigger = "attack_bite";
string projName = null;
int projCount = 1;
float projDelay = 0.5f;
bool isMelee = true;
if (isFlying || isLanding)
{
// Air attack (Meteors replacement -> Ice Barrage)
attackTrigger = "attack_roar"; // taunt animation has issues in air
projName = "dragon_ice_projectile"; // spawn_fader_meteors is buggy without Fader boss script
projCount = 16;
projDelay = 1.0f;
isMelee = false;
}
else
{
if (button == 2)
{
// Ground MMB (Frost Breath stream)
attackTrigger = "attack_flamebreath";
projName = "hatchling_cold_projectile";
projCount = 60; // Tripled!
projDelay = 0.5f;
isMelee = false;
}
else if (button == 1)
{
// Ground RMB (Spin)
attackTrigger = "attack_Spin";
isMelee = true;
projDelay = 0.5f;
}
else
{
// Ground LMB (Melee mix - bite/claws)
float rand = UnityEngine.Random.value;
if (rand < 0.33f) attackTrigger = "attack_bite";
else if (rand < 0.66f) attackTrigger = "attack_ClawL";
else attackTrigger = "attack_ClawR";
isMelee = true;
projDelay = 0.5f;
}
}
Player.m_localPlayer.UseEitr(ATTACK_COST);
if (anim != null) anim.SetTrigger(attackTrigger);
yield return new WaitForSeconds(projDelay);
if (!isMelee && projName != null)
{
GameObject projPrefab = PrefabManager.Cache.GetPrefab<GameObject>(projName);
if (projPrefab == null) yield break;
ItemDrop staffDrop = PrefabManager.Cache.GetPrefab<GameObject>("StaffIceShards")?.GetComponent<ItemDrop>();
if (staffDrop == null) yield break;
float vel = staffDrop.m_itemData.m_shared.m_attack.m_projectileVel;
for (int i = 0; i < projCount; i++)
{
Vector3 spawnPos = Player.m_localPlayer.GetEyePoint() + Player.m_localPlayer.GetLookDir() * 4.0f;
Vector3 targetDir = Player.m_localPlayer.GetLookDir();
if (projCount > 1)
{
Vector3 right = Vector3.Cross(Vector3.up, targetDir);
targetDir += right * UnityEngine.Random.Range(-0.2f, 0.2f) + Vector3.up * UnityEngine.Random.Range(-0.2f, 0.2f);
targetDir.Normalize();
}
GameObject projInstance = Instantiate(projPrefab, spawnPos, Quaternion.LookRotation(targetDir));
Projectile proj = projInstance.GetComponent<Projectile>();
HitData hitData = new HitData();
hitData.m_damage = staffDrop.m_itemData.m_shared.m_damages;
// Convert Fire to Frost
hitData.m_damage.m_frost += hitData.m_damage.m_fire;
hitData.m_damage.m_fire = 0f;
hitData.SetAttacker(Player.m_localPlayer);
if (proj != null)
{
proj.Setup(Player.m_localPlayer, targetDir * vel, -1f, hitData, staffDrop.m_itemData, null);
}
// Turn fire VFX to blue/frost
foreach (ParticleSystem ps in projInstance.GetComponentsInChildren<ParticleSystem>())
{
var main = ps.main;
main.startColor = new Color(0.4f, 0.8f, 1f, 1f); // Ice blue
}
foreach (Light l in projInstance.GetComponentsInChildren<Light>())
{
l.color = new Color(0.4f, 0.8f, 1f, 1f);
}
if (projCount > 1) yield return new WaitForSeconds(0.05f);
}
}
}
}
private void OnDestroy()
{
if (Active == this) Active = null;
if (Player.m_localPlayer != null)
{
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_flying").SetValue(false);
}
}
}
}