354 lines
15 KiB
C#
354 lines
15 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using Jotunn.Managers;
|
|
|
|
namespace RacesOfValheim
|
|
{
|
|
public class DrakeFormBehavior : MonoBehaviour
|
|
{
|
|
private Animator anim;
|
|
private bool isFlying = false;
|
|
private bool isLanding = false;
|
|
private float lastAttackTime = 0f;
|
|
|
|
private SkadiProgression.FormRules formRules;
|
|
|
|
public void Initialize(SkadiProgression.FormRules rules)
|
|
{
|
|
this.formRules = rules;
|
|
}
|
|
|
|
private const float ATTACK_COST = 20f;
|
|
private const float ATTACK_COOLDOWN = 3f;
|
|
|
|
private void Start()
|
|
{
|
|
anim = GetComponentInChildren<Animator>();
|
|
if (Player.m_localPlayer != null)
|
|
{
|
|
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return;
|
|
|
|
// Handle Flight Toggle (RMB) - Disabled for Fader
|
|
if (Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon)
|
|
{
|
|
if (!isFlying && !isLanding)
|
|
{
|
|
isFlying = true;
|
|
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(true);
|
|
}
|
|
else if (isFlying && !isLanding)
|
|
{
|
|
isLanding = true;
|
|
isFlying = false;
|
|
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false);
|
|
}
|
|
else if (isLanding)
|
|
{
|
|
// Cancel landing, resume flight
|
|
isLanding = false;
|
|
isFlying = true;
|
|
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").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 (formRules != null && formRules.CanAttackInForm)
|
|
{
|
|
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 (Player.m_localPlayer != null)
|
|
{
|
|
HarmonyLib.Traverse.Create(Player.m_localPlayer).Field("m_debugFly").SetValue(false);
|
|
}
|
|
}
|
|
}
|
|
}
|