761 lines
30 KiB
C#
761 lines
30 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
/// <summary>
|
|
/// Owns ONLY the strictly capped <see cref="OmniLight3D"/>[2] dynamic combat flash pool
|
|
/// plus idempotent <see cref="ISimEvent"/> deduplication and dispatch into
|
|
/// <see cref="VolumetricEmitterPool"/> (smoke/fire/shockwaves) and <see cref="GpuSparksSystem"/>
|
|
/// (<see cref="GpuParticles3D"/> sparks/embers/dust/muzzle bursts).
|
|
/// Zero CPU particle buffers or billboard MultiMeshes.
|
|
/// </summary>
|
|
public partial class ParticlePool : Node3D
|
|
{
|
|
public const int MaxFlashLights = 2;
|
|
|
|
private struct FlashLightSlot
|
|
{
|
|
public OmniLight3D Light;
|
|
public bool Active;
|
|
public int Priority;
|
|
public float Age;
|
|
public float Duration;
|
|
public float StartEnergy;
|
|
public float Range;
|
|
public Color Color;
|
|
}
|
|
|
|
private readonly FlashLightSlot[] _flashLights = new FlashLightSlot[MaxFlashLights];
|
|
|
|
// Bounded rolling dedupe buffers (zero polling cursor on MatchState)
|
|
private readonly int[] _recentShotIds = new int[64];
|
|
private int _recentShotCursor;
|
|
private readonly long[] _recentHitIds = new long[64];
|
|
private int _recentHitCursor;
|
|
|
|
private VolumetricEmitterPool? _volumetrics;
|
|
private GpuSparksSystem? _gpuSparks;
|
|
private int _warmupFramesRemaining;
|
|
private bool _initialized;
|
|
|
|
public int FlashLightPoolSize => MaxFlashLights;
|
|
|
|
public int ActiveFlashLightCount
|
|
{
|
|
get
|
|
{
|
|
int count = 0;
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
if (_flashLights[i].Active) count++;
|
|
}
|
|
return count;
|
|
}
|
|
}
|
|
|
|
public int PeakFlashLightCount { get; private set; }
|
|
|
|
public void BindTargets(VolumetricEmitterPool volumetrics, GpuSparksSystem gpuSparks)
|
|
{
|
|
_volumetrics = volumetrics;
|
|
_gpuSparks = gpuSparks;
|
|
}
|
|
|
|
public override void _Ready()
|
|
{
|
|
EnsureInitialized();
|
|
}
|
|
|
|
public void EnsureInitialized()
|
|
{
|
|
if (_initialized)
|
|
{
|
|
return;
|
|
}
|
|
_initialized = true;
|
|
|
|
for (int i = 0; i < MaxFlashLights; i++)
|
|
{
|
|
var light = new OmniLight3D
|
|
{
|
|
Name = $"CombatFlashLight_{i}",
|
|
ShadowEnabled = false,
|
|
OmniAttenuation = 1.6f,
|
|
OmniRange = 14f,
|
|
LightEnergy = 0f,
|
|
LightVolumetricFogEnergy = 0f,
|
|
Visible = false
|
|
};
|
|
AddChild(light);
|
|
_flashLights[i] = new FlashLightSlot { Light = light };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Pre-warms the pooled <see cref="OmniLight3D"/> nodes on startup for 2 rendered frames
|
|
/// with near-zero energy so Forward+ compiles lit shader variants before combat begins.
|
|
/// </summary>
|
|
public void PrewarmFlashLights(Vector3 nearPos, Vector3 forwardHoriz)
|
|
{
|
|
EnsureInitialized();
|
|
_warmupFramesRemaining = 2;
|
|
Vector3 fwd = forwardHoriz.LengthSquared() > 1e-4f ? forwardHoriz.Normalized() : Vector3.Forward;
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
ref FlashLightSlot slot = ref _flashLights[i];
|
|
slot.Active = false;
|
|
slot.Priority = 0;
|
|
slot.Age = 0f;
|
|
slot.Duration = 0f;
|
|
slot.StartEnergy = 0f;
|
|
slot.Range = 22f;
|
|
slot.Light.Position = i == 0 ? nearPos + new Vector3(0f, 2f, 0f) : nearPos + fwd * 72f + new Vector3(0f, 3f, 0f);
|
|
slot.Light.LightColor = Colors.White;
|
|
slot.Light.OmniRange = 22f;
|
|
slot.Light.OmniAttenuation = 1.6f;
|
|
slot.Light.LightEnergy = 0.001f;
|
|
slot.Light.Visible = true;
|
|
}
|
|
}
|
|
|
|
public void ClearAll()
|
|
{
|
|
EnsureInitialized();
|
|
Array.Clear(_recentShotIds, 0, _recentShotIds.Length);
|
|
Array.Clear(_recentHitIds, 0, _recentHitIds.Length);
|
|
_recentShotCursor = 0;
|
|
_recentHitCursor = 0;
|
|
_warmupFramesRemaining = 0;
|
|
PeakFlashLightCount = 0;
|
|
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
_flashLights[i].Active = false;
|
|
_flashLights[i].Age = 0f;
|
|
_flashLights[i].Duration = 0f;
|
|
_flashLights[i].Light.Visible = false;
|
|
_flashLights[i].Light.LightEnergy = 0f;
|
|
}
|
|
}
|
|
|
|
public void ConsumeEvents(
|
|
IClientArenaSession session,
|
|
IReadOnlyList<ISimEvent> events,
|
|
Vector3 camPos,
|
|
bool hasCam,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
EnsureInitialized();
|
|
var state = session.ViewState;
|
|
|
|
for (int i = 0; i < events.Count; i++)
|
|
{
|
|
switch (events[i])
|
|
{
|
|
case MatchResetEvent:
|
|
ClearAll();
|
|
break;
|
|
|
|
case ShotFiredEvent shot:
|
|
HandleShotFired(shot, camPos, hasCam, sampleGroundTint);
|
|
break;
|
|
|
|
case HitImpactEvent hit:
|
|
HandleHitImpact(hit, camPos, hasCam, sampleGroundTint);
|
|
break;
|
|
|
|
case ContenderEliminatedEvent elim:
|
|
if ((elim.VictimId == session.LocalContenderId || session.IsContenderVisible(elim.VictimId)) &&
|
|
elim.VictimId >= 0 && elim.VictimId < state.Contenders.Count)
|
|
{
|
|
var v = state.Contenders[elim.VictimId];
|
|
EmitChassisExplosion(new Vector3(v.X, v.Y + 0.6f, v.Z), v.Grid.Spec.MassKg, camPos, hasCam, sampleGroundTint);
|
|
}
|
|
break;
|
|
|
|
case SpectatorDestroyedEvent sdet:
|
|
{
|
|
if (!session.IsSpectatorVisible(sdet.SpectatorId))
|
|
{
|
|
break;
|
|
}
|
|
var specs = state.Spectators.Spectators;
|
|
for (int s = 0; s < specs.Count; s++)
|
|
{
|
|
if (specs[s].Id == sdet.SpectatorId)
|
|
{
|
|
var sp = specs[s];
|
|
EmitChassisExplosion(new Vector3(sp.X, sp.Y + 0.5f, sp.Z), sp.Grid.Spec.MassKg, camPos, hasCam, sampleGroundTint);
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
|
|
case DropPodEvent dpe:
|
|
if ((DropPodEventKind)dpe.Kind == DropPodEventKind.PodTouchdownBlowPetals)
|
|
{
|
|
bool isLocal = dpe.ContenderId == session.LocalContenderId;
|
|
bool isContender = dpe.PodKind == (byte)OrbitalPodKind.Contender;
|
|
Vector3 pos = new(dpe.X, dpe.Y, dpe.Z);
|
|
Vector3 nrm = new(dpe.NormalX, dpe.NormalY, dpe.NormalZ);
|
|
if (nrm.LengthSquared() < 1e-4f) nrm = Vector3.Up;
|
|
nrm = nrm.Normalized();
|
|
EmitPodTouchdownBurst(pos, nrm, sampleGroundTint(pos.X, pos.Z), isContender, isLocal, camPos, hasCam);
|
|
}
|
|
else if ((DropPodEventKind)dpe.Kind == DropPodEventKind.PodRetroBurnStart)
|
|
{
|
|
bool isLocal = dpe.ContenderId == session.LocalContenderId;
|
|
Vector3 pos = new(dpe.X, dpe.Y, dpe.Z);
|
|
if (isLocal || !hasCam || pos.DistanceSquaredTo(camPos) <= 220f * 220f)
|
|
{
|
|
SpawnFlashLight(pos - new Vector3(0f, 0.8f, 0f), new Color(1.0f, 0.62f, 0.22f), 4.5f, 15f, 0.14f, priority: isLocal ? 9 : 4);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void EmitFootstepDust(in FootstepPulse pulse, Color groundTint, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
Vector3 footPos = new(pulse.WorldPos.X, pulse.WorldPos.Y + 0.14f, pulse.WorldPos.Z);
|
|
if (hasCam && footPos.DistanceSquaredTo(camPos) > 115f * 115f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float massScale = Math.Clamp(pulse.MassKg / 2600f, 0.72f, 1.55f);
|
|
float loadScale = Math.Clamp(pulse.LoadG, 0.5f, 1.8f);
|
|
int count = pulse.MassKg >= 3600f ? 42 : 26;
|
|
_gpuSparks?.EmitDustMotes(footPos, Vector3.Up, count, groundTint with { A = 0.76f }, 3.4f * massScale * MathF.Sqrt(loadScale), 0.048f * massScale);
|
|
_volumetrics?.EmitFootstepOrTirePuff(footPos, groundTint, massScale);
|
|
}
|
|
|
|
public void EmitTireDust(Vector3 worldPos, Vector3 vehicleVel, Color groundTint, float intensity, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && worldPos.DistanceSquaredTo(camPos) > 95f * 95f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float scale = Math.Clamp(intensity, 0.5f, 1.5f);
|
|
_gpuSparks?.EmitDustMotes(worldPos, Vector3.Up, 18, groundTint with { A = 0.70f }, 2.6f * scale, 0.045f * scale);
|
|
_volumetrics?.EmitFootstepOrTirePuff(worldPos, groundTint, scale * 0.9f);
|
|
}
|
|
|
|
public void EmitThermalVent(Vector3 worldPos, float thermalRatio, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && worldPos.DistanceSquaredTo(camPos) > 105f * 105f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_volumetrics?.EmitThermalVentPlume(worldPos, thermalRatio);
|
|
if (thermalRatio >= 0.94f)
|
|
{
|
|
_gpuSparks?.EmitSparkBurst(worldPos, Vector3.Up, 20, new Color(4.6f, 2.4f, 0.55f, 0.96f), 6.5f, 0.026f);
|
|
}
|
|
}
|
|
|
|
public void EmitDamageSmokeAndFire(Vector3 worldPos, float coreHpRatio, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && worldPos.DistanceSquaredTo(camPos) > 125f * 125f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_volumetrics?.EmitDamageSmokePlume(worldPos, coreHpRatio);
|
|
if (coreHpRatio < 0.24f)
|
|
{
|
|
_gpuSparks?.EmitEmberBurst(worldPos, 16, new Color(4.2f, 1.8f, 0.35f, 0.94f), 2.4f, 0.030f);
|
|
}
|
|
}
|
|
|
|
public void EmitOrdnanceBurst(Vector3 pos, Color accentCol, int sparkCount, float radiusScale, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 220f * 220f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Color moltenHdr = new(4.8f, 2.6f, 0.65f, 0.98f);
|
|
Color subtleTint = new(
|
|
Mathf.Lerp(moltenHdr.R, accentCol.R * 3.8f, 0.25f),
|
|
Mathf.Lerp(moltenHdr.G, accentCol.G * 3.8f, 0.25f),
|
|
Mathf.Lerp(moltenHdr.B, accentCol.B * 3.8f, 0.25f),
|
|
0.98f);
|
|
_gpuSparks?.EmitSparkBurst(pos, Vector3.Up, Math.Clamp(sparkCount * 16, 48, 160), subtleTint, 15f * radiusScale, 0.028f);
|
|
_gpuSparks?.EmitEmberBurst(pos, Math.Clamp(sparkCount * 7, 22, 80), moltenHdr * 0.85f, 5.0f * radiusScale, 0.032f);
|
|
}
|
|
|
|
public void EmitThermalPoolEmber(Vector3 pos, float radius, Color accentCol, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 150f * 150f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_gpuSparks?.EmitThermalPoolEmber(pos, radius, accentCol);
|
|
}
|
|
|
|
public void EmitTreeImpactBurst(Vector3 canopyPos, float treeHeightM, bool isChar, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && canopyPos.DistanceSquaredTo(camPos) > 260f * 260f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (isChar)
|
|
{
|
|
_gpuSparks?.EmitEmberBurst(canopyPos, 24, new Color(4.0f, 1.4f, 0.25f, 0.92f), 3.6f, 0.036f);
|
|
_volumetrics?.EmitDamageSmokePlume(canopyPos, 0.35f);
|
|
}
|
|
else
|
|
{
|
|
_gpuSparks?.EmitDustMotes(canopyPos, Vector3.Up, 44, new Color(0.28f, 0.44f, 0.16f, 0.88f), 6.5f, 0.075f);
|
|
_gpuSparks?.EmitDustMotes(
|
|
canopyPos - Vector3.Up * (treeHeightM * 0.25f),
|
|
Vector3.Up,
|
|
20,
|
|
new Color(0.54f, 0.40f, 0.24f, 0.85f),
|
|
5.0f,
|
|
0.055f);
|
|
_volumetrics?.EmitFootstepOrTirePuff(canopyPos, new Color(0.32f, 0.38f, 0.22f, 1f), 1.35f);
|
|
}
|
|
}
|
|
|
|
public void EmitPodReentryStreak(Vector3 podPos, Vector3 podVel, float heatGlow, bool isContender, Vector3 camPos, bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
if (hasCam && podPos.DistanceSquaredTo(camPos) > 650f * 650f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_volumetrics?.EmitPodReentryPlume(podPos, podVel, heatGlow, isContender);
|
|
_gpuSparks?.EmitEmberBurst(podPos, isContender ? 20 : 12, new Color(4.4f, 2.2f, 0.55f, 0.95f), 2.8f, 0.030f);
|
|
}
|
|
|
|
public void EmitPodRetroBurn(
|
|
Vector3 podBasePos,
|
|
float agl,
|
|
Color groundTint,
|
|
bool isContender,
|
|
bool isLocal,
|
|
Vector3 camPos,
|
|
bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
float distSq = hasCam ? podBasePos.DistanceSquaredTo(camPos) : 0f;
|
|
if (hasCam && distSq > 480f * 480f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_volumetrics?.EmitPodRetroPlume(podBasePos, agl, groundTint, isContender);
|
|
_gpuSparks?.EmitPodRetroSparksAndDust(podBasePos, agl, groundTint, isContender);
|
|
|
|
if (isLocal || !hasCam || distSq <= 95f * 95f)
|
|
{
|
|
SpawnFlashLight(podBasePos - new Vector3(0f, 0.6f, 0f), new Color(1.0f, 0.60f, 0.18f), 3.8f, 14f, 0.09f, priority: isLocal ? 8 : 3);
|
|
}
|
|
}
|
|
|
|
public void EmitPodTouchdownBurst(
|
|
Vector3 pos,
|
|
Vector3 normal,
|
|
Color groundTint,
|
|
bool isContender,
|
|
bool isLocal,
|
|
Vector3 camPos,
|
|
bool hasCam)
|
|
{
|
|
EnsureInitialized();
|
|
float distSq = hasCam ? pos.DistanceSquaredTo(camPos) : 0f;
|
|
if (!isLocal && hasCam && distSq > 360f * 360f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_volumetrics?.EmitPodTouchdownPlume(pos, normal, groundTint, isContender);
|
|
_gpuSparks?.EmitPodTouchdownSparksAndDust(pos, normal, groundTint, isContender);
|
|
|
|
if (isLocal || !hasCam || distSq <= 140f * 140f)
|
|
{
|
|
SpawnFlashLight(pos + normal * 1.2f, new Color(1.0f, 0.58f, 0.16f), 5.8f, 18f, 0.18f, priority: isLocal ? 10 : 6);
|
|
}
|
|
}
|
|
|
|
public void TriggerFlashLightForTest(Vector3 pos, Color color, float energy, float range, float duration, int priority)
|
|
{
|
|
EnsureInitialized();
|
|
SpawnFlashLight(pos, color, energy, range, duration, priority);
|
|
}
|
|
|
|
public void AdvanceAndCommit(float dt)
|
|
{
|
|
EnsureInitialized();
|
|
float stepDt = float.IsFinite(dt) && dt > 0f ? Math.Clamp(dt, 0f, 0.05f) : 0f;
|
|
|
|
int activeLights = 0;
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
ref FlashLightSlot slot = ref _flashLights[i];
|
|
if (!slot.Active)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (stepDt > 0f)
|
|
{
|
|
slot.Age += stepDt;
|
|
if (slot.Age >= slot.Duration)
|
|
{
|
|
slot.Active = false;
|
|
slot.Light.Visible = false;
|
|
slot.Light.LightEnergy = 0f;
|
|
continue;
|
|
}
|
|
}
|
|
|
|
float t = Math.Clamp(slot.Age / MathF.Max(0.01f, slot.Duration), 0f, 1f);
|
|
float decay = MathF.Pow(1f - t, 1.8f);
|
|
slot.Light.LightColor = slot.Color;
|
|
slot.Light.OmniRange = slot.Range;
|
|
slot.Light.LightEnergy = slot.StartEnergy * decay;
|
|
slot.Light.Visible = slot.Light.LightEnergy > 0.01f;
|
|
if (slot.Light.Visible)
|
|
{
|
|
activeLights++;
|
|
}
|
|
}
|
|
if (_warmupFramesRemaining > 0)
|
|
{
|
|
_warmupFramesRemaining--;
|
|
if (_warmupFramesRemaining == 0)
|
|
{
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
if (!_flashLights[i].Active)
|
|
{
|
|
_flashLights[i].Light.Visible = false;
|
|
_flashLights[i].Light.LightEnergy = 0f;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (activeLights > PeakFlashLightCount)
|
|
{
|
|
PeakFlashLightCount = activeLights;
|
|
}
|
|
}
|
|
|
|
private void HandleShotFired(
|
|
ShotFiredEvent shot,
|
|
Vector3 camPos,
|
|
bool hasCam,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
if (shot.TracerId != 0)
|
|
{
|
|
for (int k = 0; k < _recentShotIds.Length; k++)
|
|
{
|
|
if (_recentShotIds[k] == shot.TracerId)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
_recentShotIds[_recentShotCursor] = shot.TracerId;
|
|
_recentShotCursor = (_recentShotCursor + 1) % _recentShotIds.Length;
|
|
}
|
|
|
|
Vector3 start = new(shot.StartX, shot.StartY, shot.StartZ);
|
|
Vector3 end = new(shot.EndX, shot.EndY, shot.EndZ);
|
|
if (hasCam && start.DistanceSquaredTo(camPos) > 140f * 140f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var wClass = (WeaponVisualClass)shot.WeaponClass;
|
|
bool isRecoilless = string.Equals(shot.WeaponId, "recoilless", StringComparison.Ordinal);
|
|
bool isLance = string.Equals(shot.WeaponId, "gilded_lance", StringComparison.Ordinal);
|
|
bool isPd = string.Equals(shot.WeaponId, "pd_flak", StringComparison.Ordinal);
|
|
Vector3 aimDir = (end - start).LengthSquared() > 1e-4f ? (end - start).Normalized() : Vector3.Forward;
|
|
|
|
switch (wClass)
|
|
{
|
|
case WeaponVisualClass.KineticLight:
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(4.2f, 2.8f, 1.0f, 0.96f), isPd ? 0.42f : 0.30f, 0.07f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, 18, new Color(4.5f, 2.8f, 0.8f, 0.96f), 11f, 0.024f);
|
|
break;
|
|
|
|
case WeaponVisualClass.Autocannon:
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(4.6f, 2.6f, 0.75f, 0.98f), 0.56f, 0.08f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, 32, new Color(4.8f, 2.6f, 0.65f, 0.96f), 13f, 0.026f);
|
|
_volumetrics?.EmitMuzzleBreechPuff(start, end, sampleGroundTint(start.X, start.Z), wClass, isRecoilless: false);
|
|
if (!hasCam || start.DistanceSquaredTo(camPos) <= 45f * 45f)
|
|
{
|
|
SpawnFlashLight(start, new Color(1.0f, 0.68f, 0.24f), 2.8f, 10.5f, 0.10f, priority: 3);
|
|
}
|
|
break;
|
|
|
|
case WeaponVisualClass.Explosive:
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(5.0f, 2.6f, 0.70f, 0.98f), isRecoilless ? 0.78f : 0.48f, 0.09f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, isRecoilless ? 64 : 32, new Color(5.0f, 2.5f, 0.60f, 0.98f), 15f, 0.028f);
|
|
_volumetrics?.EmitMuzzleBreechPuff(start, end, sampleGroundTint(start.X, start.Z), wClass, isRecoilless);
|
|
if (!hasCam || start.DistanceSquaredTo(camPos) <= 65f * 65f)
|
|
{
|
|
SpawnFlashLight(start, new Color(1.0f, 0.58f, 0.18f), isRecoilless ? 4.2f : 2.6f, isRecoilless ? 14f : 10f, 0.13f, priority: 5);
|
|
}
|
|
break;
|
|
|
|
case WeaponVisualClass.ThermalBeam:
|
|
{
|
|
Color beamCol = isLance ? new Color(4.8f, 3.2f, 0.95f, 0.96f) : new Color(2.8f, 3.4f, 3.8f, 0.96f);
|
|
_gpuSparks?.EmitMuzzleStarburst(start, beamCol, isLance ? 0.68f : 0.48f, 0.09f);
|
|
if (!hasCam || start.DistanceSquaredTo(camPos) <= 45f * 45f)
|
|
{
|
|
Color lCol = isLance ? new Color(1.0f, 0.78f, 0.28f) : new Color(0.32f, 0.90f, 1.0f);
|
|
SpawnFlashLight(start, lCol, isLance ? 3.5f : 2.6f, 11f, 0.12f, priority: 4);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.GaussRail:
|
|
{
|
|
Color gaussHdr = new(3.6f, 2.8f, 1.8f, 0.98f);
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(1.4f, 3.5f, 4.8f, 0.98f), 0.72f, 0.09f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, 56, gaussHdr, 19f, 0.026f);
|
|
if (!hasCam || start.DistanceSquaredTo(camPos) <= 65f * 65f)
|
|
{
|
|
SpawnFlashLight(start, new Color(0.22f, 0.78f, 1.0f), 4.2f, 14f, 0.11f, priority: 5);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.PlasmaPpc:
|
|
{
|
|
Color plasmaHdr = new(3.4f, 2.8f, 1.6f, 0.96f);
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(1.0f, 3.4f, 4.2f, 0.96f), 0.65f, 0.09f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, 38, plasmaHdr, 12f, 0.028f);
|
|
if (!hasCam || start.DistanceSquaredTo(camPos) <= 55f * 55f)
|
|
{
|
|
SpawnFlashLight(start, new Color(0.16f, 0.88f, 0.95f), 3.4f, 12f, 0.12f, priority: 4);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.RocketSalvo:
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(4.8f, 2.6f, 0.75f, 0.96f), 0.62f, 0.095f);
|
|
_gpuSparks?.EmitSparkBurst(start, aimDir, 42, new Color(4.8f, 2.4f, 0.60f, 0.96f), 13f, 0.028f);
|
|
_volumetrics?.EmitMuzzleBreechPuff(start, end, sampleGroundTint(start.X, start.Z), wClass, isRecoilless: false);
|
|
break;
|
|
|
|
case WeaponVisualClass.SwarmSmart:
|
|
_gpuSparks?.EmitMuzzleStarburst(start, new Color(2.8f, 3.8f, 1.2f, 0.96f), 0.48f, 0.085f);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void HandleHitImpact(
|
|
HitImpactEvent hit,
|
|
Vector3 camPos,
|
|
bool hasCam,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
if (hit.EventId != 0)
|
|
{
|
|
for (int k = 0; k < _recentHitIds.Length; k++)
|
|
{
|
|
if (_recentHitIds[k] == hit.EventId)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
_recentHitIds[_recentHitCursor] = hit.EventId;
|
|
_recentHitCursor = (_recentHitCursor + 1) % _recentHitIds.Length;
|
|
}
|
|
|
|
Vector3 pos = new(hit.HitX, hit.HitY, hit.HitZ);
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 180f * 180f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector3 normal = new(hit.NormalX, hit.NormalY, hit.NormalZ);
|
|
if (normal.LengthSquared() < 1e-4f) normal = Vector3.Up;
|
|
normal = normal.Normalized();
|
|
|
|
var wClass = (WeaponVisualClass)hit.WeaponClass;
|
|
bool isTerrain = hit.TargetKind == (byte)HitTargetKind.Terrain;
|
|
Color groundTint = isTerrain ? sampleGroundTint(pos.X, pos.Z) : new Color(0.46f, 0.42f, 0.38f, 1f);
|
|
|
|
if (hit.IsAirburst || (wClass == WeaponVisualClass.KineticLight && string.Equals(hit.WeaponId, "pd_flak", StringComparison.Ordinal)))
|
|
{
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless: false, isAirburst: true);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 76, new Color(5.0f, 2.7f, 0.70f, 0.98f), 14f, 0.026f);
|
|
_gpuSparks?.EmitEmberBurst(pos, 28, new Color(4.0f, 1.8f, 0.35f, 0.95f), 4.5f, 0.030f);
|
|
return;
|
|
}
|
|
|
|
switch (wClass)
|
|
{
|
|
case WeaponVisualClass.KineticLight:
|
|
if (isTerrain)
|
|
{
|
|
_gpuSparks?.EmitDustMotes(pos, normal, 36, groundTint with { A = 0.76f }, 4.5f, 0.045f);
|
|
}
|
|
else
|
|
{
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 62, new Color(4.8f, 3.0f, 0.95f, 0.96f), 11f, 0.024f);
|
|
}
|
|
break;
|
|
|
|
case WeaponVisualClass.Autocannon:
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 76, new Color(4.8f, 2.6f, 0.70f, 0.98f), 13.5f, 0.026f);
|
|
_gpuSparks?.EmitDustMotes(pos, normal, 42, groundTint with { A = 0.78f }, 5.5f, 0.048f);
|
|
_volumetrics?.EmitFootstepOrTirePuff(pos, groundTint, 1.0f);
|
|
break;
|
|
|
|
case WeaponVisualClass.Explosive:
|
|
{
|
|
bool isRecoilless = string.Equals(hit.WeaponId, "recoilless", StringComparison.Ordinal);
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless, isAirburst: false);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, isRecoilless ? 140 : 88, new Color(5.2f, 2.6f, 0.65f, 0.98f), 17f, 0.028f);
|
|
_gpuSparks?.EmitEmberBurst(pos, isRecoilless ? 54 : 32, new Color(4.2f, 1.7f, 0.35f, 0.95f), 5.5f, 0.032f);
|
|
_gpuSparks?.EmitDustMotes(pos, normal, isRecoilless ? 68 : 44, groundTint with { A = 0.80f }, 7.5f, 0.052f);
|
|
if (!hasCam || pos.DistanceSquaredTo(camPos) <= 75f * 75f)
|
|
{
|
|
SpawnFlashLight(pos + normal * 0.45f, new Color(1.0f, 0.55f, 0.16f), isRecoilless ? 4.8f : 3.2f, isRecoilless ? 14.5f : 11f, 0.14f, priority: 7);
|
|
}
|
|
break;
|
|
}
|
|
|
|
case WeaponVisualClass.ThermalBeam:
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless: false, isAirburst: false);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 68, new Color(4.6f, 2.8f, 1.4f, 0.98f), 13f, 0.026f);
|
|
break;
|
|
|
|
case WeaponVisualClass.GaussRail:
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 96, new Color(4.2f, 3.0f, 1.8f, 0.98f), 20f, 0.026f);
|
|
_gpuSparks?.EmitDustMotes(pos, normal, 48, groundTint with { A = 0.78f }, 6.5f, 0.048f);
|
|
_volumetrics?.EmitFootstepOrTirePuff(pos, groundTint, 1.2f);
|
|
if (!hasCam || pos.DistanceSquaredTo(camPos) <= 65f * 65f)
|
|
{
|
|
SpawnFlashLight(pos + normal * 0.35f, new Color(0.28f, 0.82f, 1.0f), 4.0f, 13f, 0.12f, priority: 6);
|
|
}
|
|
break;
|
|
|
|
case WeaponVisualClass.PlasmaPpc:
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless: false, isAirburst: false);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 84, new Color(4.0f, 2.8f, 1.6f, 0.98f), 15f, 0.028f);
|
|
_gpuSparks?.EmitEmberBurst(pos, 40, new Color(3.6f, 2.0f, 0.85f, 0.92f), 4.5f, 0.032f);
|
|
break;
|
|
|
|
case WeaponVisualClass.RocketSalvo:
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless: false, isAirburst: false);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 112, new Color(5.0f, 2.7f, 0.65f, 0.98f), 16f, 0.028f);
|
|
_gpuSparks?.EmitEmberBurst(pos, 38, new Color(4.0f, 1.7f, 0.35f, 0.95f), 5.0f, 0.032f);
|
|
_gpuSparks?.EmitDustMotes(pos, normal, 56, groundTint with { A = 0.80f }, 7.0f, 0.052f);
|
|
if (!hasCam || pos.DistanceSquaredTo(camPos) <= 75f * 75f)
|
|
{
|
|
SpawnFlashLight(pos + normal * 0.45f, new Color(1.0f, 0.56f, 0.16f), 4.5f, 14f, 0.14f, priority: 7);
|
|
}
|
|
break;
|
|
|
|
case WeaponVisualClass.SwarmSmart:
|
|
_volumetrics?.EmitExplosiveHit(pos, normal, groundTint, wClass, isRecoilless: false, isAirburst: false);
|
|
_gpuSparks?.EmitSparkBurst(pos, normal, 68, new Color(4.2f, 2.8f, 0.85f, 0.98f), 13f, 0.026f);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void EmitChassisExplosion(
|
|
Vector3 pos,
|
|
float massKg,
|
|
Vector3 camPos,
|
|
bool hasCam,
|
|
Func<float, float, Color> sampleGroundTint)
|
|
{
|
|
if (hasCam && pos.DistanceSquaredTo(camPos) > 280f * 280f)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Color gTint = sampleGroundTint(pos.X, pos.Z);
|
|
_volumetrics?.EmitChassisExplosion(pos, massKg, gTint);
|
|
_gpuSparks?.EmitSparkBurst(pos, Vector3.Up, 260, new Color(5.6f, 2.9f, 0.70f, 0.98f), 22f, 0.028f);
|
|
_gpuSparks?.EmitEmberBurst(pos, 140, new Color(4.4f, 1.8f, 0.35f, 0.95f), 7.5f, 0.034f);
|
|
_gpuSparks?.EmitDustMotes(pos, Vector3.Up, 64, gTint with { A = 0.82f }, 9.0f, 0.056f);
|
|
|
|
if (!hasCam || pos.DistanceSquaredTo(camPos) <= 95f * 95f)
|
|
{
|
|
SpawnFlashLight(pos + new Vector3(0f, 1.2f, 0f), new Color(1.0f, 0.56f, 0.14f), 6.2f, 16f, 0.18f, priority: 10);
|
|
}
|
|
}
|
|
|
|
private void SpawnFlashLight(Vector3 pos, Color color, float energy, float range, float duration, int priority)
|
|
{
|
|
int targetIdx = -1;
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
if (!_flashLights[i].Active)
|
|
{
|
|
targetIdx = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (targetIdx < 0)
|
|
{
|
|
int lowestPri = int.MaxValue;
|
|
float oldestAge = -1f;
|
|
for (int i = 0; i < _flashLights.Length; i++)
|
|
{
|
|
if (_flashLights[i].Priority < lowestPri ||
|
|
(_flashLights[i].Priority == lowestPri && _flashLights[i].Age > oldestAge))
|
|
{
|
|
lowestPri = _flashLights[i].Priority;
|
|
oldestAge = _flashLights[i].Age;
|
|
targetIdx = i;
|
|
}
|
|
}
|
|
if (targetIdx < 0 || lowestPri > priority)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
float clampedRange = Math.Clamp(range, 6.5f, 22f);
|
|
ref FlashLightSlot slot = ref _flashLights[targetIdx];
|
|
slot.Active = true;
|
|
slot.Priority = priority;
|
|
slot.Age = 0f;
|
|
slot.Duration = MathF.Max(0.04f, duration);
|
|
slot.StartEnergy = energy;
|
|
slot.Range = clampedRange;
|
|
slot.Color = color;
|
|
slot.Light.Position = pos;
|
|
slot.Light.LightColor = color;
|
|
slot.Light.OmniRange = clampedRange;
|
|
slot.Light.LightEnergy = energy;
|
|
slot.Light.Visible = true;
|
|
|
|
int active = ActiveFlashLightCount;
|
|
if (active > PeakFlashLightCount)
|
|
{
|
|
PeakFlashLightCount = active;
|
|
}
|
|
}
|
|
}
|