RacesOfValheim/JotunnModStub/Sverm/SvermHiveVfx.cs
2026-08-11 16:30:20 +02:00

150 lines
6.8 KiB
C#

using UnityEngine;
namespace RacesOfValheim
{
/// <summary>
/// What the colony looks like from across the field: flies over anything the hive owns, and a
/// green reek that only rises when something is actually going on inside.
///
/// THE FLIES ARE THE DRAUGH'S, borrowed the same way DraughVisuals borrows them - the looping
/// particle systems off the Draugr prefab, the ones with "flies" in the name. That file already
/// proved the technique in game, and the filter it uses is the important part: LOOPING is what
/// separates a creature's ambient rot from its one-shot hit and death effects.
///
/// THE GAS IS TOLD WHAT COLOUR TO BE. Its shape is borrowed off whichever poisonous thing this
/// build has, but the colour is set here rather than inherited, so "green smoke" is green
/// whether it came from a blob, a gjall or a draugr's rot.
///
/// NOTHING HERE IS NETWORKED and none of it needs to be: every client can read the nest's ZDO
/// and grow its own particles, exactly as every client grows its own Draugh aura. Which is also
/// why all of this runs ABOVE the owner check - a nest digesting is a thing everybody sees.
/// </summary>
public static class SvermHiveVfx
{
private const string FliesName = "SvermFliesVFX";
private const string GasName = "SvermGasVFX";
private const string FliesSource = "Draugr";
/// <summary>Anything with a looping smoke on it, best first. Only its shape is kept.</summary>
private static readonly string[] GasSources = { "Blob", "BlobElite", "Gjall", "Draugr" };
/// <summary>The reek of a stomach at work. Alpha low, or it reads as a wall rather than a haze.</summary>
private static readonly Color GasColor = new Color(0.35f, 0.85f, 0.25f, 0.5f);
// ─── The flies ───────────────────────────────────────────────────────
public static void EnsureFlies(Transform host, float scale)
{
if (host == null || host.Find(FliesName) != null) return;
GameObject source = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(FliesSource);
if (source == null) return;
GameObject swarm = new GameObject(FliesName);
swarm.transform.SetParent(host, false);
swarm.transform.localPosition = new Vector3(0f, scale, 0f);
swarm.transform.localScale = new Vector3(scale, scale, scale);
int taken = 0;
foreach (ParticleSystem ps in source.GetComponentsInChildren<ParticleSystem>(true))
{
if (!ps.main.loop) continue;
if (!ps.gameObject.name.ToLowerInvariant().Contains("flies")) continue;
GameObject copy = Object.Instantiate(ps.gameObject, swarm.transform);
copy.name = ps.gameObject.name;
copy.SetActive(true);
copy.transform.localPosition = Vector3.zero;
// Hierarchy scaling, or the holder's scale is ignored and every swarm is the size
// the Draugr wears it at, whatever it is hanging on.
ParticleSystem.MainModule main = copy.GetComponent<ParticleSystem>().main;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
taken++;
}
if (taken == 0)
{
Jotunn.Logger.LogWarning($"[Sverm] No looping 'flies' particles on '{FliesSource}'; " +
"the colony gets no swarm.");
}
}
// ─── The gas ─────────────────────────────────────────────────────────
/// <summary>
/// Turns the reek on or off, building it the first time it is asked for. Safe to call every
/// couple of seconds: after the first pass this is a Find and a SetActive.
/// </summary>
public static void SetGas(Transform host, bool on, float scale)
{
if (host == null) return;
Transform existing = host.Find(GasName);
if (existing == null)
{
if (!on) return; // nothing to switch off, and no reason to build it yet
existing = BuildGas(host, scale);
if (existing == null) return;
}
if (existing.gameObject.activeSelf != on) existing.gameObject.SetActive(on);
}
private static Transform BuildGas(Transform host, float scale)
{
GameObject cloud = new GameObject(GasName);
cloud.transform.SetParent(host, false);
cloud.transform.localPosition = new Vector3(0f, scale * 0.5f, 0f);
cloud.transform.localScale = new Vector3(scale, scale, scale);
int taken = 0;
foreach (string candidate in GasSources)
{
GameObject source = Jotunn.Managers.PrefabManager.Cache.GetPrefab<GameObject>(candidate);
if (source == null) continue;
foreach (ParticleSystem ps in source.GetComponentsInChildren<ParticleSystem>(true))
{
if (!ps.main.loop) continue;
string name = ps.gameObject.name.ToLowerInvariant();
// Flies are the other half of this file, and dripping water reads as rain.
if (name.Contains("flies")) continue;
if (name.Contains("drip") || name.Contains("drop") || name.Contains("rain") ||
name.Contains("splash")) continue;
GameObject copy = Object.Instantiate(ps.gameObject, cloud.transform);
copy.name = ps.gameObject.name;
copy.SetActive(true);
copy.transform.localPosition = Vector3.zero;
ParticleSystem.MainModule main = copy.GetComponent<ParticleSystem>().main;
main.scalingMode = ParticleSystemScalingMode.Hierarchy;
main.startColor = new ParticleSystem.MinMaxGradient(GasColor);
taken++;
}
if (taken > 0)
{
Jotunn.Logger.LogInfo($"[Sverm] Hive gas built from '{candidate}' ({taken} systems).");
break;
}
}
if (taken == 0)
{
Jotunn.Logger.LogWarning("[Sverm] Nothing to build the hive's gas from (" +
string.Join(", ", GasSources) + "); it will digest quietly.");
Object.Destroy(cloud);
return null;
}
return cloud.transform;
}
}
}