RacesOfValheim/JotunnModStub/Trenta/TrentaHornetsNest.cs
Ferdinando d0ed92c553 feat(trenta): add the Trenta race
A vegan, tree-born race unlocking new perks per boss defeated: skin/hair
tint, blocked non-vegan food, awakened seed foods after Eikthyr, a mob
hostility list, Photosynthesis, elemental resistances, Purple Rain after
the Elder, swim/poison/wet immunity after Bonemass, Last Stand and Thorns
after Moder, a Homecoming Portal after Yagluth, Hornet's Nest after the
Queen, and Muspell (a half-size Elder transformation with Stomp/Spike
attacks) after Fader.

Also fixes RaceScale never listing Trenta as self-sizing, which let a
shared Player.Update postfix fight Muspell's own scale changes every
frame.
2026-08-04 20:58:01 +02:00

185 lines
7.7 KiB
C#

using HarmonyLib;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// The swarm itself. Not a Character - it has no health, no hitbox anything else can target,
/// so "indistruttibile" falls out of what it simply isn't rather than needing invulnerability
/// code of its own. Idles in a slow orbit around the Trenta; the instant an enemy comes within
/// range it breaks off, hovers over that enemy instead, and sting-ticks it on a timer until it
/// dies or leaves range, then returns to orbiting.
/// </summary>
public class TrentaHornetSwarm : MonoBehaviour
{
private Player owner;
private Character target;
private float tickTimer;
private float orbitAngle;
private AudioSource buzz;
public static GameObject Spawn(Player player)
{
GameObject go = new GameObject("TrentaHornetsNest");
go.transform.position = player.transform.position + Vector3.up;
TrentaHornetSwarm swarm = go.AddComponent<TrentaHornetSwarm>();
swarm.owner = player;
BuildFliesVfx(go.transform);
swarm.buzz = BuildBuzzAudio(go.transform);
return go;
}
private void Update()
{
if (owner == null || owner.IsDead())
{
Destroy(gameObject);
return;
}
if (target != null && (target.IsDead() || Vector3.Distance(target.transform.position, owner.transform.position) > TrentaConfig.HornetsNestEngageRadius * 1.5f))
{
target = null;
}
if (target == null)
{
target = FindTarget();
}
if (target != null)
{
Vector3 hoverPoint = target.transform.position + Vector3.up * TrentaConfig.HornetsNestAttackHeight;
transform.position = Vector3.MoveTowards(transform.position, hoverPoint, TrentaConfig.HornetsNestFollowSpeed * Time.deltaTime);
if (!buzz.isPlaying) buzz.Play();
tickTimer += Time.deltaTime;
if (tickTimer >= TrentaConfig.HornetsNestTickInterval)
{
tickTimer = 0f;
Sting(target);
}
}
else
{
if (buzz.isPlaying) buzz.Stop();
tickTimer = 0f;
// A slow, lazy orbit around the Trenta rather than sitting dead-still on top of
// them - reads as "trying to stay on him" without looking glued in place.
orbitAngle += Time.deltaTime * 60f;
Vector3 orbitOffset = new Vector3(Mathf.Cos(orbitAngle * Mathf.Deg2Rad), 0.3f, Mathf.Sin(orbitAngle * Mathf.Deg2Rad)) * TrentaConfig.HornetsNestOrbitRadius;
Vector3 restPoint = owner.transform.position + Vector3.up + orbitOffset;
transform.position = Vector3.MoveTowards(transform.position, restPoint, TrentaConfig.HornetsNestFollowSpeed * Time.deltaTime);
}
}
private Character FindTarget()
{
Character nearest = null;
float nearestDist = float.MaxValue;
foreach (Collider collider in Physics.OverlapSphere(owner.transform.position, TrentaConfig.HornetsNestEngageRadius))
{
if (collider == null) continue;
Character character = collider.GetComponentInParent<Character>();
if (character == null || character == owner || character.IsDead()) continue;
if (!BaseAI.IsEnemy(owner, character)) continue;
float dist = Vector3.Distance(character.transform.position, owner.transform.position);
if (dist < nearestDist)
{
nearestDist = dist;
nearest = character;
}
}
return nearest;
}
/// <summary>
/// Two hits, matching "come se il giocatore fosse costantemente avvelenato dalle api, più
/// lo stesso danno ma perforante da sopra" literally: one Poison hit at the wild Bee
/// Nest's own numbers, and one Pierce hit of the same size, its direction pointed straight
/// down since the swarm is hovering above the target when it lands.
/// </summary>
private void Sting(Character victim)
{
HitData poison = new HitData();
poison.m_damage.m_poison = TrentaConfig.HornetsNestDamagePerTick;
poison.SetAttacker(owner);
poison.m_point = victim.GetCenterPoint();
poison.m_dir = Vector3.zero;
victim.Damage(poison);
HitData pierce = new HitData();
pierce.m_damage.m_pierce = TrentaConfig.HornetsNestDamagePerTick;
pierce.SetAttacker(owner);
pierce.m_point = victim.GetCenterPoint();
pierce.m_dir = Vector3.down;
victim.Damage(pierce);
}
/// <summary>
/// Clones only the "flies" particle system off the Draugr prefab, discarding smoke and
/// drip - the exact filtering rule DraughRace.EnsureVFX already uses for its own aura, off
/// the same prefab, just keeping the opposite subset.
/// </summary>
private static void BuildFliesVfx(Transform parent)
{
GameObject draugrPrefab = PrefabManager.Cache.GetPrefab<GameObject>(TrentaConfig.HornetsNestFliesSourcePrefab);
if (draugrPrefab == null)
{
Jotunn.Logger.LogWarning($"[Trenta] Hornet's Nest: could not find VFX source prefab '{TrentaConfig.HornetsNestFliesSourcePrefab}'.");
return;
}
foreach (ParticleSystem ps in draugrPrefab.GetComponentsInChildren<ParticleSystem>(true))
{
if (!ps.main.loop) continue;
if (!ps.gameObject.name.ToLower().Contains("flies")) continue;
GameObject copy = Object.Instantiate(ps.gameObject, parent);
copy.name = ps.gameObject.name;
copy.SetActive(true);
copy.transform.localPosition = Vector3.zero;
copy.transform.localRotation = ps.transform.localRotation;
}
}
/// <summary>
/// Borrows only the AudioClip off the Beehive's ambient bee-swarm effect - read straight
/// off the prefab asset, never instantiated, so none of that effect's own visuals or
/// scripts ever come along with it.
/// </summary>
private static AudioSource BuildBuzzAudio(Transform parent)
{
AudioSource source = parent.gameObject.AddComponent<AudioSource>();
source.loop = true;
source.spatialBlend = 1f; // 3D - it should sound like it is coming from the swarm
source.volume = 0.6f;
GameObject beehivePrefab = PrefabManager.Cache.GetPrefab<GameObject>(TrentaConfig.HornetsNestBeehivePrefab);
Beehive beehive = beehivePrefab != null ? beehivePrefab.GetComponent<Beehive>() : null;
GameObject beeEffect = beehive != null ? Traverse.Create(beehive).Field<GameObject>("m_beeEffect").Value : null;
AudioSource sourceClipHolder = beeEffect != null ? beeEffect.GetComponentInChildren<AudioSource>(true) : null;
if (sourceClipHolder != null && sourceClipHolder.clip != null)
{
source.clip = sourceClipHolder.clip;
}
else
{
Jotunn.Logger.LogWarning("[Trenta] Hornet's Nest: could not find a buzzing AudioClip on the Beehive's bee effect.");
}
return source;
}
}
}