RacesOfValheim/JotunnModStub/Trenta/TrentaPortal.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

189 lines
7.9 KiB
C#

using System.Collections;
using HarmonyLib;
using Jotunn.Managers;
using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// One end of a Homecoming Portal pair. Entirely custom - not a re-skinned vanilla
/// TeleportWorld - which is exactly why it has none of the real portal's restrictions
/// (raw ore, matching tags, needing to be built at all): it is a local, unregistered prop the
/// ability spawns and despawns itself, with its own trigger and its own teleport call.
/// </summary>
public class TrentaPortalLink : MonoBehaviour
{
public TrentaPortalLink Other;
// Sidesteps a same-frame ping-pong: without it, a player who teleports in and is still
// standing inside the destination's own trigger collider would be bounced straight back
// out the way they came, forever.
private float reentryBlockedUntil = -1f;
private void OnTriggerEnter(Collider other)
{
if (Other == null || Time.time < reentryBlockedUntil) return;
Player player = other.GetComponentInParent<Player>();
if (player == null) return;
Vector3 destination = Other.transform.position + Other.transform.forward * 1.5f;
player.TeleportTo(destination, Other.transform.rotation, true);
Other.reentryBlockedUntil = Time.time + 1.5f;
}
}
public static class TrentaPortal
{
private static Material cachedVfxMaterial;
private static bool vfxMaterialResolved;
/// <summary>
/// Spawns the linked pair - one at the cast point, one at the player's respawn point -
/// and schedules both to close together after TrentaConfig.PortalLifetime.
/// </summary>
public static void Open(Player player, Vector3 castPoint, Vector3 homePoint)
{
GameObject castPortal = SpawnPortal(castPoint, player.transform.rotation);
GameObject homePortal = SpawnPortal(homePoint, Quaternion.identity);
TrentaPortalLink castLink = castPortal.GetComponent<TrentaPortalLink>();
TrentaPortalLink homeLink = homePortal.GetComponent<TrentaPortalLink>();
castLink.Other = homeLink;
homeLink.Other = castLink;
if (TrentaRace.Instance != null)
{
TrentaRace.Instance.StartCoroutine(CloseAfter(castPortal, homePortal, TrentaConfig.PortalLifetime));
}
else
{
Object.Destroy(castPortal, TrentaConfig.PortalLifetime);
Object.Destroy(homePortal, TrentaConfig.PortalLifetime);
}
}
private static GameObject SpawnPortal(Vector3 position, Quaternion rotation)
{
GameObject portal = new GameObject("TrentaHomecomingPortal");
portal.transform.position = position;
portal.transform.rotation = rotation;
CapsuleCollider trigger = portal.AddComponent<CapsuleCollider>();
trigger.isTrigger = true;
trigger.height = 3f;
trigger.radius = 1.2f;
trigger.center = new Vector3(0, 1.5f, 0);
portal.AddComponent<TrentaPortalLink>();
BuildSwirlVfx(portal.transform);
return portal;
}
/// <summary>
/// The swirl only - deliberately not the vanilla wooden frame, since the player asked for
/// just the VFX component. Built the same proven way as Purple Rain's rain burst: borrow a
/// material off the real portal (read straight off the prefab asset, so this never
/// instantiates - and never risks running - any of the real portal's own gameplay
/// components), and drive shape/emission/colour here instead of cloning its hierarchy
/// wholesale.
/// </summary>
private static void BuildSwirlVfx(Transform parent)
{
if (!vfxMaterialResolved)
{
vfxMaterialResolved = true;
GameObject source = PrefabManager.Cache.GetPrefab<GameObject>(TrentaConfig.PortalVfxSourcePrefab);
if (source != null)
{
ParticleSystemRenderer sourceRenderer = source.GetComponentInChildren<ParticleSystemRenderer>(true);
if (sourceRenderer != null) cachedVfxMaterial = sourceRenderer.material;
}
if (cachedVfxMaterial == null)
{
Jotunn.Logger.LogWarning($"[Trenta] Homecoming Portal: no particle material found on '{TrentaConfig.PortalVfxSourcePrefab}'; falling back to the default particle material.");
}
}
GameObject vfx = new GameObject("Swirl");
vfx.transform.SetParent(parent, false);
vfx.transform.localPosition = new Vector3(0, 1.2f, 0);
ParticleSystem ps = vfx.AddComponent<ParticleSystem>();
var main = ps.main;
main.loop = true;
main.startLifetime = 1.5f;
main.startSpeed = 0f;
main.startSize = new ParticleSystem.MinMaxCurve(0.15f, 0.3f);
main.maxParticles = 200;
main.simulationSpace = ParticleSystemSimulationSpace.Local;
var emission = ps.emission;
emission.rateOverTime = 80f;
var shape = ps.shape;
shape.shapeType = ParticleSystemShapeType.Circle;
shape.radius = 1f;
shape.radiusThickness = 0.1f; // hollow ring, not a filled disc
var rotationOverLifetime = ps.rotationOverLifetime;
rotationOverLifetime.enabled = true;
rotationOverLifetime.z = new ParticleSystem.MinMaxCurve(90f); // the "swirl" - constant spin
ParticleSystemRenderer renderer = ps.GetComponent<ParticleSystemRenderer>();
if (cachedVfxMaterial != null) renderer.material = cachedVfxMaterial;
var colorOverLifetime = ps.colorOverLifetime;
colorOverLifetime.enabled = true;
Gradient grad = new Gradient();
grad.SetKeys(
new GradientColorKey[] { new GradientColorKey(TrentaConfig.PortalVfxColor, 0f), new GradientColorKey(TrentaConfig.PortalVfxColor, 1f) },
new GradientAlphaKey[] { new GradientAlphaKey(0f, 0f), new GradientAlphaKey(0.9f, 0.3f), new GradientAlphaKey(0.9f, 0.7f), new GradientAlphaKey(0f, 1f) }
);
colorOverLifetime.color = grad;
ps.Play();
}
/// <summary>
/// Closes both ends together: the trigger and swirl come down first (so nothing can walk
/// through mid-collapse), then a one-second shrink stands in for "the right closing
/// animation" - vanilla portals never actually open or close, they are simply present or
/// absent, so there is no real animation to borrow here the way Purple Rain could borrow
/// staff_summon.
/// </summary>
private static IEnumerator CloseAfter(GameObject castPortal, GameObject homePortal, float seconds)
{
yield return new WaitForSeconds(seconds);
yield return CollapsePortal(castPortal);
yield return CollapsePortal(homePortal);
}
private static IEnumerator CollapsePortal(GameObject portal)
{
if (portal == null) yield break;
TrentaPortalLink link = portal.GetComponent<TrentaPortalLink>();
if (link != null) Object.Destroy(link);
CapsuleCollider trigger = portal.GetComponent<CapsuleCollider>();
if (trigger != null) Object.Destroy(trigger);
float t = 0f;
Vector3 startScale = portal.transform.localScale;
while (t < 1f)
{
if (portal == null) yield break;
t += Time.deltaTime;
portal.transform.localScale = Vector3.Lerp(startScale, Vector3.zero, t);
yield return null;
}
if (portal != null) Object.Destroy(portal);
}
}
}