using System; using System.Collections.Generic; using Godot; using RustToDust.Core; namespace RustToDust.Client; /// /// Manages physically detached armor plates and sheared hardpoint gear as tumbling, /// terrain-bouncing debris pieces submitted directly into /// (zero per-piece Node3D/MeshInstance3D/StandardMaterial3D allocations). /// public partial class DebrisSystem : Node3D { private const float Gravity = -20f; private const float DefaultLifetime = 8.0f; private const float FadeDuration = 2.0f; private const int MaxActiveDebris = 256; private sealed class DebrisPiece { public required ArrayMesh Mesh; public Vector3 Position; public Basis Rotation; public Vector3 Scale; public Vector3 Velocity; public Vector3 AngularVelocity; public float RoleIndex; public float Scorch; public float HeatGlow; public float Age; public float Lifetime; public bool Settled; } private readonly List _pieces = new(); private uint _rngState = 0x9E3779B9u; public int ActiveCount => _pieces.Count; public bool TryGetPiecePosition(int index, out Vector3 position) { if (index >= 0 && index < _pieces.Count) { position = _pieces[index].Position; return true; } position = Vector3.Zero; return false; } public void ClearAll() { _pieces.Clear(); } /// /// Registers a detached rigid part or sheared gear mesh at its world transform /// and launches it with an outward blast impulse and tumble. /// public void SpawnDetachedPart( ArrayMesh mesh, Transform3D worldXform, Vector3 blastOriginWorld, Vector3 inheritedVelocity, float roleIndex, float impulseScale = 1.0f, bool applyScorch = true) { if (mesh is null || mesh.GetSurfaceCount() == 0) { return; } while (_pieces.Count >= MaxActiveDebris) { _pieces.RemoveAt(0); } Vector3 scale = worldXform.Basis.Scale; if (!scale.IsFinite() || scale.LengthSquared() < 1e-6f) { scale = Vector3.One; } Basis rot = worldXform.Basis.Orthonormalized(); Vector3 outward = worldXform.Origin - blastOriginWorld; outward.Y = 0f; if (outward.LengthSquared() < 1e-4f) { float ang = NextFloat() * MathF.Tau; outward = new Vector3(MathF.Cos(ang), 0f, MathF.Sin(ang)); } else { outward = outward.Normalized(); } float horizSpeed = (4.2f + NextFloat() * 4.8f) * impulseScale; float upSpeed = (4.0f + NextFloat() * 4.5f) * impulseScale; Vector3 vel = inheritedVelocity * 0.45f + outward * horizSpeed + Vector3.Up * upSpeed; Vector3 angVel = new( (NextFloat() * 2f - 1f) * 7.5f, (NextFloat() * 2f - 1f) * 7.5f, (NextFloat() * 2f - 1f) * 7.5f); _pieces.Add(new DebrisPiece { Mesh = mesh, Position = worldXform.Origin, Rotation = rot, Scale = scale, Velocity = vel, AngularVelocity = angVel, RoleIndex = roleIndex, Scorch = applyScorch ? 0.85f : 0.15f, HeatGlow = applyScorch ? 0.45f : 0f, Age = 0f, Lifetime = DefaultLifetime, Settled = false }); } public void Step(float dt, TerrainField terrain) { if (dt <= 0f || !float.IsFinite(dt)) { return; } float stepDt = Math.Clamp(dt, 0f, 0.05f); for (int i = _pieces.Count - 1; i >= 0; i--) { DebrisPiece p = _pieces[i]; p.Age += stepDt; if (p.Age >= p.Lifetime) { _pieces.RemoveAt(i); continue; } if (p.HeatGlow > 0f) { p.HeatGlow = MathF.Max(0f, p.HeatGlow - stepDt * 0.22f); } if (!p.Settled) { p.Velocity += new Vector3(0f, Gravity * stepDt, 0f); Vector3 pos = p.Position + p.Velocity * stepDt; Basis rot = p.Rotation; float spinLen = p.AngularVelocity.Length(); if (spinLen > 1e-4f) { rot = (new Basis(p.AngularVelocity / spinLen, spinLen * stepDt) * rot).Orthonormalized(); } float floorY = terrain.SampleHeight(pos.X, pos.Z) + 0.10f; if (pos.Y <= floorY) { pos.Y = floorY; if (p.Velocity.Y < -1.4f) { p.Velocity = new Vector3( p.Velocity.X * 0.62f, -p.Velocity.Y * 0.32f, p.Velocity.Z * 0.62f); p.AngularVelocity *= 0.55f; } else { p.Velocity = Vector3.Zero; p.AngularVelocity = Vector3.Zero; p.Settled = true; } } p.Position = pos; p.Rotation = rot; } } } public void SubmitToPool(MultiMeshPartPool pool) { for (int i = 0; i < _pieces.Count; i++) { DebrisPiece p = _pieces[i]; float remaining = p.Lifetime - p.Age; float alpha = remaining < FadeDuration ? Math.Clamp(remaining / FadeDuration, 0.02f, 1f) : 1f; float sinkY = remaining < FadeDuration ? (1f - alpha) * 0.65f : 0f; Vector3 scale = p.Scale * (remaining < FadeDuration ? (0.35f + 0.65f * alpha) : 1f); Transform3D xf = new(p.Rotation * Basis.FromScale(scale), p.Position - new Vector3(0f, sinkY, 0f)); Color custom = new(p.Scorch, p.HeatGlow, p.RoleIndex, alpha); pool.Submit(p.Mesh, in xf, in custom, wantsShadow: false); } } private float NextFloat() { uint x = _rngState; x ^= x << 13; x ^= x >> 17; x ^= x << 5; _rngState = x; return (x & 0x00FFFFFFu) / 16777216f; } }