_dynamic list

This commit is contained in:
2025-06-21 21:00:22 +03:00
parent 429b0c4468
commit 54ac069797

View File

@@ -8,6 +8,7 @@ using FlaxEngine;
using FlaxEngine.Assertions; using FlaxEngine.Assertions;
using FlaxEngine.Networking; using FlaxEngine.Networking;
using Console = Game.Console; using Console = Game.Console;
using System.Buffers;
namespace Game; namespace Game;
@@ -83,7 +84,7 @@ public struct PlayerMovementState
public struct TraceInfo public struct TraceInfo
{ {
public RayCastHit[] hitInfos; public List<RayCastHit> hitInfos;
public bool startSolid; public bool startSolid;
// closest hit // closest hit
@@ -141,6 +142,8 @@ public class PlayerMovement : Script
public uint PlayerId => playerActor ? playerActor.PlayerId : 0; public uint PlayerId => playerActor ? playerActor.PlayerId : 0;
public LayersMask maskTest;
[ReadOnly] [ReadOnly]
public float CurrentVelocity public float CurrentVelocity
{ {
@@ -757,15 +760,17 @@ public class PlayerMovement : Script
//viewAnglesLastFrame = angles; //viewAnglesLastFrame = angles;
} }
private static bool SweepPlayerCollider(PlayerActor actor, Float3 start, Vector3 end, out RayCastHit[] hits) private static List<RayCastHit> _cachedHits = new();
private static bool SweepPlayerCollider(PlayerActor actor, Float3 start, Vector3 end, out List<RayCastHit> hits)
{ {
Vector3 delta = end - start; Vector3 delta = end - start;
float distance = delta.Length; float distance = delta.Length;
Vector3 direction = delta.Normalized; Vector3 direction = delta.Normalized;
//_cachedHits.Clear();
if (distance < 0.00000001f) if (distance < 0.00000001f)
{ {
hits = new RayCastHit[0];//Array.Empty<RayCastHit>(); hits = _cachedHits;//Array.Empty<RayCastHit>();
return false; return false;
} }
@@ -773,7 +778,7 @@ public class PlayerMovement : Script
CapsuleCollider capsuleCollider = actor.capsuleCollider; CapsuleCollider capsuleCollider = actor.capsuleCollider;
BoxCollider boxCollider = actor.boxCollider; BoxCollider boxCollider = actor.boxCollider;
MeshCollider meshCollider = actor.meshCollider; MeshCollider meshCollider = actor.meshCollider;
if (capsuleCollider && capsuleCollider.IsActive) /*if (capsuleCollider && capsuleCollider.IsActive)
collided = Physics.CapsuleCastAll(start, collided = Physics.CapsuleCastAll(start,
capsuleCollider.Radius, capsuleCollider.Height, capsuleCollider.Radius, capsuleCollider.Height,
direction, out hits, capsuleCollider.Orientation, distance, direction, out hits, capsuleCollider.Orientation, distance,
@@ -785,16 +790,16 @@ public class PlayerMovement : Script
direction, out hits, meshCollider.Orientation, distance, direction, out hits, meshCollider.Orientation, distance,
uint.MaxValue, uint.MaxValue,
false); false);
else if (boxCollider && boxCollider.IsActive) else*/ if (boxCollider && boxCollider.IsActive)
collided = Physics.BoxCastAll(start, collided = Physics.BoxCastAll(start,
boxCollider.OrientedBox.Extents, boxCollider.OrientedBox.Extents,
direction, out hits, boxCollider.Orientation, distance, direction, ref _cachedHits, boxCollider.Orientation, distance,
uint.MaxValue, uint.MaxValue,
false); false);
else else
throw new Exception("Player does not have a collider"); throw new Exception("Player does not have a collider");
hits = _cachedHits;
return collided; return collided;
} }
@@ -857,6 +862,7 @@ public class PlayerMovement : Script
return collided; return collided;
} }
private static List<RayCastHit> _cachedHitInfos = new List<RayCastHit>();
/// <summary> /// <summary>
/// Sweeps the player rigidbody in world and returns geometry which was hit during the trace. /// Sweeps the player rigidbody in world and returns geometry which was hit during the trace.
/// </summary> /// </summary>
@@ -867,6 +873,7 @@ public class PlayerMovement : Script
private static TraceInfo TracePlayer(PlayerActor actor, Vector3 start, Vector3 end) private static TraceInfo TracePlayer(PlayerActor actor, Vector3 start, Vector3 end)
{ {
TraceInfo traceInfo = new TraceInfo(); TraceInfo traceInfo = new TraceInfo();
traceInfo.hitInfos = _cachedHitInfos;
Vector3 delta = end - start; Vector3 delta = end - start;
float maxDistance = delta.Length; float maxDistance = delta.Length;
@@ -921,7 +928,7 @@ public class PlayerMovement : Script
if (hitInfo.Distance < closest.Distance) if (hitInfo.Distance < closest.Distance)
closest = hitInfo; closest = hitInfo;
traceInfo.hitInfos = hitInfosFiltered.ToArray(); traceInfo.hitInfos = hitInfosFiltered;
traceInfo.fraction = closest.Distance / maxDistance; traceInfo.fraction = closest.Distance / maxDistance;
traceInfo.hitNormal = closest.Normal; traceInfo.hitNormal = closest.Normal;
@@ -943,7 +950,7 @@ public class PlayerMovement : Script
if (!collided) if (!collided)
{ {
traceInfo.hitInfos = new RayCastHit[0]; traceInfo.hitInfos = new List<RayCastHit>();
traceInfo.fraction = 1f; traceInfo.fraction = 1f;
traceInfo.endPosition = end; traceInfo.endPosition = end;
} }