RacesOfValheim/JotunnModStub/Fenrir/FenrirBehaviors.cs
kanym 6755c263f3 feat(fenrir): make ability numbers and keybinds BepInEx-configurable
Adds FenrirConfig.cs, following the established per-race pattern: one
Configuration Manager section per topic (Keybinds, Passive Combat, Form
Buffs, and one per transformation form - Fenring, Cultist, Wolf, Ulv),
covering every attack's damage/radius/push-force number across all four
independent form behaviors, plus the four transform/howl keybinds.
Defaults match the previous hardcoded constants exactly.

Only the four transform/howl keys are rebindable - unlike Skadi's dragon
forms, which share one unified attack-button scheme, each of Fenrir's
four forms is an independent MonoBehaviour with its own hardcoded mouse
reads, so making all eleven attack buttons rebindable was left out as a
much larger, separately-scoped change.

The Form Buffs section opens with a non-editable note (the CustomDrawer
trick from ConfigurationManagerAttributes) explaining that those specific
values are baked into status effects once at mod startup and need a
relog to pick up a change - everything else reads live.

Config sections stay bound alphabetically by race (Aswid, Draugh,
Dvergar, Fenrir, Skadi) in RacesOfValheim.Awake().
2026-08-06 14:12:07 +02:00

1033 lines
42 KiB
C#

using System.Collections;
using BepInEx;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using UnityEngine;
using UnityEngine.UI; // Added for UI
namespace RacesOfValheim
{
public class FenrirFormBehavior : MonoBehaviour
{
private Animator playerAnim;
private Animator fenrirAnim;
private bool isAttacking = false;
void Start()
{
playerAnim = Player.m_localPlayer.transform.Find("Visual").GetComponent<Animator>();
fenrirAnim = GetComponent<Animator>();
if (fenrirAnim == null) fenrirAnim = GetComponentInChildren<Animator>();
}
void Update()
{
if (playerAnim == null || fenrirAnim == null) return;
// Se stiamo attaccando, l'Update smette di inviare costantemente
// "onGround", inWater, forward_speed ecc. alle variabili di base?
// Meglio continuare a sincronizzare i movimenti base.
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
fenrirAnim.SetBool("onGround", isGrounded);
fenrirAnim.SetBool("inWater", playerAnim.GetBool("inWater"));
if (!isGrounded)
{
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
}
else
{
fenrirAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed"));
fenrirAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed"));
}
}
if (isGrounded && RaceInput.Allowed)
{
if (FenrirConfig.HowlKey.Value.IsDown())
{
StartCoroutine(HowlRoutine());
}
else if (Input.GetMouseButtonDown(1)) // Tasto destro (Heavy Claw)
{
StartCoroutine(ClawRoutine("attack_claw1", FenrirConfig.FenringHeavyClawDamage.Value, 1.2f, 0.5f));
}
else if (Input.GetMouseButtonDown(0)) // Tasto sinistro
{
StartCoroutine(ClawRoutine("attack_claw0", FenrirConfig.FenringLightClawDamage.Value, 1.0f, 0.4f));
}
else if (Input.GetMouseButtonDown(2)) // Tasto centrale
{
StartCoroutine(JumpAttackRoutine());
}
}
}
}
private void PlaySFX(params string[] prefabNames)
{
foreach (string name in prefabNames)
{
GameObject sfx = PrefabManager.Cache.GetPrefab<GameObject>(name);
if (sfx != null)
{
Instantiate(sfx, Player.m_localPlayer.transform.position, Quaternion.identity);
return;
}
}
Jotunn.Logger.LogWarning($"[FenrirForm] Nessun suono trovato tra: {string.Join(", ", prefabNames)}");
}
private IEnumerator HowlRoutine()
{
isAttacking = true;
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
fenrirAnim.SetTrigger("attack_taunt");
PlaySFX("sfx_wolf_howl", "sfx_wolf_alert", "sfx_fenring_idle");
yield return new WaitForSeconds(2.0f);
isAttacking = false;
}
private IEnumerator ClawRoutine(string triggerName, float baseDamage, float lockDuration, float dmgDelay)
{
isAttacking = true;
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
fenrirAnim.SetTrigger(triggerName);
PlaySFX("sfx_fenring_attack", "sfx_fenring_attack_claw", "sfx_wolf_attack");
yield return new WaitForSeconds(dmgDelay);
if (ZNetScene.instance != null)
{
GameObject attackSfx = ZNetScene.instance.GetPrefab("sfx_fenring_attack");
if (attackSfx != null)
{
Instantiate(attackSfx, Player.m_localPlayer.transform.position, Quaternion.identity);
}
}
ExecuteClawDamage(baseDamage);
float remainingTimeToIdle = lockDuration - dmgDelay;
if (remainingTimeToIdle > 0)
{
yield return new WaitForSeconds(remainingTimeToIdle);
}
fenrirAnim.ResetTrigger(triggerName);
isAttacking = false;
}
private IEnumerator JumpAttackRoutine()
{
isAttacking = true;
fenrirAnim.SetFloat("forward_speed", 0f);
fenrirAnim.SetFloat("sideway_speed", 0f);
fenrirAnim.SetTrigger("attack_jump");
// Aspettiamo che l'animazione si carichi (0.75s)
yield return new WaitForSeconds(0.75f);
if (ZNetScene.instance != null)
{
GameObject attackSfx = ZNetScene.instance.GetPrefab("sfx_fenring_attack");
if (attackSfx != null)
{
Instantiate(attackSfx, Player.m_localPlayer.transform.position, Quaternion.identity);
}
}
// Invece di una singola spinta di velocità (che viene subito annullata da Valheim),
// muoviamo esplicitamente il personaggio fotogramma per fotogramma per 0.2 secondi
float dashDuration = 0.2f;
float elapsedDash = 0f;
while (elapsedDash < dashDuration)
{
if (Player.m_localPlayer != null)
{
Rigidbody rb = Player.m_localPlayer.GetComponent<Rigidbody>();
if (rb != null)
{
Vector3 dashVelocity = Player.m_localPlayer.transform.forward * FenrirConfig.FenringJumpDashSpeed.Value;
// Spinta verso l'alto solo nella prima metà dello scatto
if (elapsedDash < dashDuration / 2f)
{
dashVelocity += Vector3.up * FenrirConfig.FenringJumpDashUpwardSpeed.Value;
}
rb.MovePosition(rb.position + dashVelocity * Time.fixedDeltaTime);
}
}
elapsedDash += Time.fixedDeltaTime;
yield return new WaitForFixedUpdate();
}
// Infliggiamo il danno al termine dello scatto
ExecuteClawDamage(FenrirConfig.FenringJumpAttackDamage.Value);
// Tempo per riprendersi dall'atterraggio prima di potersi muovere di nuovo
yield return new WaitForSeconds(0.55f);
fenrirAnim.ResetTrigger("attack_jump");
isAttacking = false;
}
private void ExecuteClawDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 1f;
float attackRadius = FenrirConfig.FenringAttackRadius.Value;
float unarmedSkillLevel = Player.m_localPlayer.GetSkillLevel(Skills.SkillType.Unarmed);
float totalDamage = baseDamage + unarmedSkillLevel;
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
{
if (hit.gameObject == Player.m_localPlayer.gameObject) continue;
if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == Player.m_localPlayer.gameObject) continue;
IDestructible target = hit.GetComponentInParent<IDestructible>();
if (target != null)
{
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
{
Vector3 eyePos = playerPos + Vector3.up * 1.5f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
hitData.m_pushForce = FenrirConfig.FenringPushForce.Value;
hitData.m_point = targetCenter;
hitData.m_dir = Player.m_localPlayer.transform.forward;
hitData.m_skill = Skills.SkillType.Unarmed;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
target.Damage(hitData);
}
}
}
}
}
}
// --- CLASSE CUSTOM PER L'ATTACCO "FIRE" DEL CULTIST ---
public class CultistFormBehavior : MonoBehaviour
{
private Animator playerAnim;
private Animator cultistAnim;
private bool isAttacking = false;
private bool isShootingFire = false;
private float fireTimer = 0f;
void Start()
{
playerAnim = Player.m_localPlayer.transform.Find("Visual").GetComponent<Animator>();
cultistAnim = GetComponent<Animator>();
if (cultistAnim == null) cultistAnim = GetComponentInChildren<Animator>();
}
void Update()
{
if (playerAnim == null || cultistAnim == null) return;
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
cultistAnim.SetBool("onGround", isGrounded);
cultistAnim.SetBool("inWater", playerAnim.GetBool("inWater"));
if (!isGrounded)
{
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
}
else
{
// Se tiene premuto Ctrl (Crouch), rallenta o ferma il movimento per preparare l'attacco
if (Player.m_localPlayer.IsCrouching())
{
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
}
else
{
cultistAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed"));
cultistAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed"));
}
}
if (isGrounded)
{
// Gated per-read rather than around the block, so opening a GUI mid-stream
// still falls through to StopFire() instead of leaving the flames running.
bool canAct = RaceInput.Allowed;
// Fire attack on Right Click
if (canAct && Input.GetMouseButton(1))
{
if (Player.m_localPlayer.GetEitr() > 0)
{
if (!isShootingFire)
{
StartFire();
}
else
{
UpdateFire();
}
}
else if (isShootingFire)
{
StopFire();
}
}
else if (isShootingFire)
{
StopFire();
}
if (canAct && FenrirConfig.HowlKey.Value.IsDown())
{
StartCoroutine(HowlRoutine());
}
// Attacks on Left Click
if (canAct && Input.GetMouseButtonDown(0) && !isShootingFire)
{
StartCoroutine(ClawRoutine()); // Single claw
}
// Double claw on Middle Click
else if (canAct && Input.GetMouseButtonDown(2) && !isShootingFire)
{
StartCoroutine(DoubleClawRoutine());
}
}
else if (isShootingFire)
{
StopFire();
}
}
}
private void PlaySFX(params string[] prefabNames)
{
foreach (string name in prefabNames)
{
GameObject sfx = PrefabManager.Cache.GetPrefab<GameObject>(name);
if (sfx != null)
{
Instantiate(sfx, Player.m_localPlayer.transform.position, Quaternion.identity);
return;
}
}
Jotunn.Logger.LogWarning($"[CultistForm] Nessun suono trovato tra: {string.Join(", ", prefabNames)}");
}
private IEnumerator HowlRoutine()
{
isAttacking = true;
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
// Cultist ha una vera e propria animazione di "taunt"
cultistAnim.SetTrigger("attack_taunt");
PlaySFX("sfx_wolf_howl", "sfx_wolf_alert", "sfx_fenring_idle");
// Durata approssimativa dell'animazione di taunt
yield return new WaitForSeconds(2.0f);
isAttacking = false;
}
private GameObject fireEffectInstance;
private void StartFire()
{
if (Player.m_localPlayer == null || isShootingFire) return;
isShootingFire = true;
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
cultistAnim.SetBool("attack_fire", true);
PlaySFX("sfx_cultist_attack_fire_start");
cultistAnim.SetTrigger("attack_flames");
if (ZNetScene.instance != null && fireEffectInstance == null)
{
// Un effetto che dura nel tempo, trovato dai log!
GameObject fireVfx = ZNetScene.instance.GetPrefab("fx_fenring_flames");
if (fireVfx != null)
{
fireEffectInstance = Instantiate(fireVfx, Player.m_localPlayer.transform.position + Vector3.up * 1.5f + Player.m_localPlayer.transform.forward, Player.m_localPlayer.transform.rotation, Player.m_localPlayer.transform);
// Aumentiamo la durata delle particelle del 20%
ParticleSystem[] pss = fireEffectInstance.GetComponentsInChildren<ParticleSystem>();
foreach (ParticleSystem ps in pss)
{
var main = ps.main;
main.startLifetimeMultiplier *= 1.2f;
}
}
}
}
private void UpdateFire()
{
Player.m_localPlayer.UseEitr(FenrirConfig.CultistFireEitrDrainPerSecond.Value * Time.deltaTime);
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
fireTimer += Time.deltaTime;
if (fireTimer >= 0.2f) // Damage tick every 0.2s
{
fireTimer = 0f;
ExecuteFireDamage();
}
}
private void StopFire()
{
isShootingFire = false;
cultistAnim.ResetTrigger("attack_flames");
if (fireEffectInstance != null)
{
Destroy(fireEffectInstance);
fireEffectInstance = null;
}
}
private void ExecuteFireDamage()
{
if (Player.m_localPlayer == null) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 2.5f + Vector3.up * 1f;
float attackRadius = FenrirConfig.CultistFireAttackRadius.Value;
float magicSkillLevel = Player.m_localPlayer.GetSkillLevel(Skills.SkillType.ElementalMagic);
float baseFireDamage = FenrirConfig.CultistFireDamagePerTick.Value;
float totalDamage = baseFireDamage + (magicSkillLevel * FenrirConfig.CultistFireMagicSkillScaling.Value);
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
{
if (hit.gameObject == Player.m_localPlayer.gameObject) continue;
if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == Player.m_localPlayer.gameObject) continue;
IDestructible target = hit.GetComponentInParent<IDestructible>();
if (target != null)
{
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 45f)
{
HitData hitData = new HitData();
hitData.m_damage.m_fire = totalDamage;
hitData.m_point = targetCenter;
hitData.m_dir = Player.m_localPlayer.transform.forward;
hitData.m_skill = Skills.SkillType.ElementalMagic;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.SetAttacker(Player.m_localPlayer);
target.Damage(hitData);
if (ZNetScene.instance != null)
{
GameObject hitFx = ZNetScene.instance.GetPrefab("vfx_FireballHit");
if (hitFx != null) Instantiate(hitFx, targetCenter, Quaternion.identity);
}
}
}
}
}
private IEnumerator ClawRoutine()
{
isAttacking = true;
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
// Per il Cultista usiamo esplicitamente il trigger dell'artigliata
cultistAnim.SetTrigger("attack_claw");
yield return new WaitForSeconds(0.4f);
PlaySFX("sfx_cultist_attack", "sfx_fenring_attack", "sfx_wolf_attack");
ExecuteClawDamage(FenrirConfig.CultistClawDamage.Value);
float remainingTimeToIdle = 0.5f - 0.4f;
if (remainingTimeToIdle > 0)
{
yield return new WaitForSeconds(remainingTimeToIdle);
}
cultistAnim.ResetTrigger("attack_claw");
isAttacking = false;
}
private IEnumerator DoubleClawRoutine()
{
isAttacking = true;
cultistAnim.SetFloat("forward_speed", 0f);
cultistAnim.SetFloat("sideway_speed", 0f);
cultistAnim.SetTrigger("attack_claw_double");
// L'animazione double claw è più lunga, facciamo il danno un po' dopo
yield return new WaitForSeconds(0.6f);
PlaySFX("sfx_cultist_attack_double", "sfx_cultist_attack", "sfx_fenring_attack");
ExecuteClawDamage(FenrirConfig.CultistDoubleClawDamage.Value);
// Attendiamo che l'animazione finisca (locking del giocatore)
// L'animazione doppia del cultista dura circa 1.5 - 2 secondi
yield return new WaitForSeconds(1.2f);
cultistAnim.ResetTrigger("attack_claw_double");
isAttacking = false;
}
private void ExecuteClawDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 1f;
float attackRadius = FenrirConfig.CultistAttackRadius.Value;
float unarmedSkillLevel = Player.m_localPlayer.GetSkillLevel(Skills.SkillType.Unarmed);
float totalDamage = baseDamage + unarmedSkillLevel;
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
{
if (hit.gameObject == Player.m_localPlayer.gameObject) continue;
if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == Player.m_localPlayer.gameObject) continue;
IDestructible target = hit.GetComponentInParent<IDestructible>();
if (target != null)
{
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
{
Vector3 eyePos = playerPos + Vector3.up * 1.5f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
hitData.m_pushForce = FenrirConfig.CultistPushForce.Value;
hitData.m_point = targetCenter;
hitData.m_dir = Player.m_localPlayer.transform.forward;
hitData.m_skill = Skills.SkillType.Unarmed;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
target.Damage(hitData);
}
}
}
}
}
}
public class WolfFormBehavior : MonoBehaviour
{
private Animator playerAnim;
private Animator wolfAnim;
private bool isAttacking = false;
void Start()
{
playerAnim = Player.m_localPlayer.transform.Find("Visual").GetComponent<Animator>();
wolfAnim = GetComponent<Animator>();
if (wolfAnim == null) wolfAnim = GetComponentInChildren<Animator>();
}
void Update()
{
if (playerAnim == null || wolfAnim == null) return;
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
try { wolfAnim.SetBool("onGround", isGrounded); } catch {}
try { wolfAnim.SetBool("inWater", playerAnim.GetBool("inWater")); } catch {}
if (!isGrounded)
{
try { wolfAnim.SetFloat("forward_speed", 0f); } catch {}
try { wolfAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
try { wolfAnim.SetFloat("forward_speed", 0f); } catch {}
try { wolfAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
try { wolfAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed")); } catch {}
try { wolfAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); } catch {}
}
}
if (isGrounded && RaceInput.Allowed)
{
if (Input.GetMouseButtonDown(0)) // Left Click
{
StartCoroutine(WolfAttackRoutine("attack1", FenrirConfig.WolfAttack1Damage.Value, 0.6f, 0.2f));
}
else if (Input.GetMouseButtonDown(1)) // Right Click
{
StartCoroutine(WolfAttackRoutine("attack2", FenrirConfig.WolfAttack2Damage.Value, 0.8f, 0.3f));
}
else if (Input.GetMouseButtonDown(2)) // Middle Click
{
StartCoroutine(WolfAttackRoutine("attack3", FenrirConfig.WolfAttack3Damage.Value, 1.0f, 0.4f));
}
}
}
}
private void PlaySFX(params string[] prefabNames)
{
foreach (string name in prefabNames)
{
GameObject sfx = PrefabManager.Cache.GetPrefab<GameObject>(name);
if (sfx != null)
{
Instantiate(sfx, Player.m_localPlayer.transform.position, Quaternion.identity);
return;
}
}
}
private IEnumerator WolfAttackRoutine(string triggerName, float baseDamage, float lockDuration, float dmgDelay)
{
isAttacking = true;
try { wolfAnim.SetFloat("forward_speed", 0f); } catch {}
try { wolfAnim.SetFloat("sideway_speed", 0f); } catch {}
wolfAnim.SetTrigger(triggerName);
yield return new WaitForSeconds(dmgDelay);
if (ZNetScene.instance != null)
{
GameObject attackSfx = ZNetScene.instance.GetPrefab("sfx_wolf_attack");
if (attackSfx != null)
{
Instantiate(attackSfx, Player.m_localPlayer.transform.position, Quaternion.identity);
}
}
ExecuteBiteDamage(baseDamage);
float remainingTimeToIdle = lockDuration - dmgDelay;
if (remainingTimeToIdle > 0)
{
yield return new WaitForSeconds(remainingTimeToIdle);
}
wolfAnim.ResetTrigger(triggerName);
isAttacking = false;
}
private void ExecuteBiteDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
// Il lupo è basso, abbassiamo l'altezza dell'attacco
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 0.5f;
float attackRadius = FenrirConfig.WolfAttackRadius.Value;
float unarmedSkillLevel = Player.m_localPlayer.GetSkillLevel(Skills.SkillType.Unarmed);
float totalDamage = baseDamage + unarmedSkillLevel;
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
{
if (hit.gameObject == Player.m_localPlayer.gameObject) continue;
if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == Player.m_localPlayer.gameObject) continue;
IDestructible target = hit.GetComponentInParent<IDestructible>();
if (target != null)
{
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
{
Vector3 eyePos = playerPos + Vector3.up * 1.0f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
hitData.m_pushForce = FenrirConfig.WolfPushForce.Value;
hitData.m_point = targetCenter;
hitData.m_dir = Player.m_localPlayer.transform.forward;
hitData.m_skill = Skills.SkillType.Unarmed;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
target.Damage(hitData);
}
}
}
}
}
}
// --- CLASSE CUSTOM PER L'ULV ---
public class UlvFormBehavior : MonoBehaviour
{
private Animator playerAnim;
private Animator ulvAnim;
private bool isAttacking = false;
void Start()
{
playerAnim = Player.m_localPlayer.transform.Find("Visual").GetComponent<Animator>();
ulvAnim = GetComponent<Animator>();
if (ulvAnim == null) ulvAnim = GetComponentInChildren<Animator>();
}
void Update()
{
if (Player.m_localPlayer == null || playerAnim == null || ulvAnim == null) return;
if (!isAttacking)
{
bool isGrounded = playerAnim.GetBool("onGround");
try { ulvAnim.SetBool("onGround", isGrounded); } catch {}
try { ulvAnim.SetBool("inWater", playerAnim.GetBool("inWater")); } catch {}
if (!isGrounded)
{
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
if (Player.m_localPlayer.IsCrouching())
{
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
}
else
{
try { ulvAnim.SetFloat("forward_speed", playerAnim.GetFloat("forward_speed")); } catch {}
try { ulvAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); } catch {}
}
}
if (isGrounded && RaceInput.Allowed)
{
if (Input.GetMouseButtonDown(0)) // Left Click
{
StartCoroutine(UlvAttackRoutine("attack_bite", FenrirConfig.UlvBiteDamage.Value, 0.6f, 0.2f));
}
else if (Input.GetMouseButtonDown(1)) // Right Click
{
// Right click uses bite but as a heavier attack since slash is moved to jump
StartCoroutine(UlvAttackRoutine("attack_bite", FenrirConfig.UlvHeavyBiteDamage.Value, 0.8f, 0.3f));
}
else if (Input.GetMouseButtonDown(2)) // Middle Click
{
StartCoroutine(UlvJumpAttackRoutine());
}
}
}
}
private void PlaySFX(params string[] prefabNames)
{
foreach (string name in prefabNames)
{
GameObject sfx = PrefabManager.Cache.GetPrefab<GameObject>(name);
if (sfx != null)
{
Instantiate(sfx, Player.m_localPlayer.transform.position, Quaternion.identity);
return;
}
}
}
private IEnumerator UlvAttackRoutine(string triggerName, float baseDamage, float lockDuration, float dmgDelay)
{
isAttacking = true;
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
try { ulvAnim.SetTrigger(triggerName); } catch {}
// Scegli il suono appropriato per l'attacco
if (triggerName == "attack_bite")
{
PlaySFX("sfx_ulv_attack", "sfx_wolf_attack", "sfx_fenring_attack");
}
else
{
PlaySFX("sfx_ulv_attack", "sfx_wolf_attack", "sfx_fenring_attack");
}
yield return new WaitForSeconds(dmgDelay);
if (ZNetScene.instance != null)
{
GameObject attackSfx = ZNetScene.instance.GetPrefab("sfx_wood_hit");
if (attackSfx != null)
{
Instantiate(attackSfx, Player.m_localPlayer.transform.position, Quaternion.identity);
}
}
ExecuteBiteDamage(baseDamage);
float remainingTimeToIdle = lockDuration - dmgDelay;
if (remainingTimeToIdle > 0)
{
yield return new WaitForSeconds(remainingTimeToIdle);
}
try { ulvAnim.ResetTrigger(triggerName); } catch {}
isAttacking = false;
}
private void ExecuteBiteDamage(float baseDamage)
{
if (Player.m_localPlayer == null) return;
Vector3 playerPos = Player.m_localPlayer.transform.position;
Vector3 attackPoint = playerPos + Player.m_localPlayer.transform.forward * 1.5f + Vector3.up * 0.5f;
float attackRadius = FenrirConfig.UlvAttackRadius.Value;
float unarmedSkillLevel = Player.m_localPlayer.GetSkillLevel(Skills.SkillType.Unarmed);
float totalDamage = baseDamage + unarmedSkillLevel;
int layerMask = LayerMask.GetMask("Default", "static_solid", "Default_small", "piece", "piece_nonsolid", "terrain", "character", "character_net", "character_ghost", "hitbox", "character_noenv", "vehicle");
Collider[] hits = Physics.OverlapSphere(attackPoint, attackRadius, layerMask);
System.Collections.Generic.HashSet<GameObject> hitObjects = new System.Collections.Generic.HashSet<GameObject>();
foreach (Collider hit in hits)
{
if (hit.gameObject == Player.m_localPlayer.gameObject) continue;
if (hit.attachedRigidbody != null && hit.attachedRigidbody.gameObject == Player.m_localPlayer.gameObject) continue;
IDestructible target = hit.GetComponentInParent<IDestructible>();
if (target != null)
{
Character characterTarget = target as Character;
if (characterTarget != null && characterTarget == Player.m_localPlayer) continue;
GameObject targetObj = (target as MonoBehaviour)?.gameObject;
if (targetObj != null)
{
if (hitObjects.Contains(targetObj)) continue;
hitObjects.Add(targetObj);
}
Vector3 targetCenter = hit.bounds.center;
Vector3 dirToTarget = (targetCenter - playerPos).normalized;
float angle = Vector3.Angle(Player.m_localPlayer.transform.forward, dirToTarget);
if (angle < 70f)
{
Vector3 eyePos = playerPos + Vector3.up * 1.0f;
if (!Physics.Linecast(eyePos, targetCenter, out RaycastHit rayHit, LayerMask.GetMask("Default", "static_solid", "terrain", "piece")))
{
HitData hitData = new HitData();
hitData.m_damage.m_slash = totalDamage;
hitData.m_pushForce = FenrirConfig.UlvPushForce.Value;
hitData.m_point = targetCenter;
hitData.m_dir = Player.m_localPlayer.transform.forward;
hitData.m_skill = Skills.SkillType.Unarmed;
hitData.m_dodgeable = true;
hitData.m_blockable = true;
hitData.m_hitType = HitData.HitType.PlayerHit;
hitData.m_toolTier = 0;
hitData.SetAttacker(Player.m_localPlayer);
target.Damage(hitData);
}
}
}
}
}
private IEnumerator UlvJumpAttackRoutine()
{
isAttacking = true;
try { ulvAnim.SetFloat("forward_speed", 0f); } catch {}
try { ulvAnim.SetFloat("sideway_speed", 0f); } catch {}
try { ulvAnim.SetTrigger("attack_slash"); } catch {}
PlaySFX("sfx_ulv_attack", "sfx_wolf_attack");
yield return new WaitForSeconds(0.3f); // Wait 0.3s before leaping
// Leap forward (+50% range)
float leapDuration = 0.25f;
float leapSpeed = FenrirConfig.UlvLeapSpeed.Value;
float elapsed = 0f;
while (elapsed < leapDuration)
{
if (Player.m_localPlayer != null)
{
Vector3 moveDir = Player.m_localPlayer.transform.forward;
Player.m_localPlayer.transform.position += moveDir * leapSpeed * Time.deltaTime;
}
elapsed += Time.deltaTime;
yield return null;
}
if (ZNetScene.instance != null)
{
GameObject attackSfx = ZNetScene.instance.GetPrefab("sfx_wood_hit");
if (attackSfx != null)
{
Instantiate(attackSfx, Player.m_localPlayer.transform.position, Quaternion.identity);
}
}
ExecuteBiteDamage(FenrirConfig.UlvJumpAttackDamage.Value);
yield return new WaitForSeconds(0.4f);
try { ulvAnim.ResetTrigger("attack_slash"); } catch {}
isAttacking = false;
}
}
}