2024-01-30 19:31:01 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Physics2D;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public class PhysicsEngine2DCollector : IPhysicsEngine2D, IAssignableGameManager
|
2024-01-30 19:31:01 +03:00
|
|
|
{
|
|
|
|
public Action<IAssignable>? OnUnassigned { get; set; } = null;
|
|
|
|
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
|
|
|
|
|
|
|
|
|
2024-02-01 12:34:06 +03:00
|
|
|
private int _iterationPerStep = 1;
|
2024-01-30 19:31:01 +03:00
|
|
|
|
2024-01-30 19:35:36 +03:00
|
|
|
protected readonly ICollisionDetector2D collisionDetector = null!;
|
|
|
|
protected readonly ICollisionResolver2D collisionResolver = null!;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
protected BehaviourCollector<IRigidBody2D> rigidBodyCollector = new();
|
|
|
|
protected BehaviourCollector<ICollider2D> colliderCollector = new();
|
2024-01-30 19:31:01 +03:00
|
|
|
|
|
|
|
|
2024-02-01 12:34:06 +03:00
|
|
|
public int IterationPerStep { get => _iterationPerStep; set => _iterationPerStep = value < 1 ? 1 : value; }
|
2024-01-30 19:31:01 +03:00
|
|
|
public IGameManager GameManager { get; private set; } = null!;
|
|
|
|
|
|
|
|
public void Step(float deltaTime)
|
|
|
|
{
|
2024-02-01 12:34:06 +03:00
|
|
|
float intervalDeltaTime = deltaTime / IterationPerStep;
|
2024-01-30 19:31:01 +03:00
|
|
|
|
2024-02-01 12:34:06 +03:00
|
|
|
for (int iterationIndex = 0; iterationIndex < IterationPerStep; iterationIndex++)
|
2024-01-30 19:31:01 +03:00
|
|
|
{
|
|
|
|
// Can Parallel
|
2024-02-09 09:43:15 +03:00
|
|
|
foreach (var rigidBody in rigidBodyCollector)
|
2024-01-30 19:31:01 +03:00
|
|
|
StepRigidBody(rigidBody, intervalDeltaTime);
|
|
|
|
|
|
|
|
// Can Parallel
|
2024-02-09 09:43:15 +03:00
|
|
|
foreach (var collider in colliderCollector)
|
2024-01-30 19:31:01 +03:00
|
|
|
collider.Recalculate();
|
|
|
|
|
|
|
|
// Can Parallel
|
2024-02-09 09:43:15 +03:00
|
|
|
for (int x = 0; x < colliderCollector.Behaviours.Count; x++)
|
2024-01-30 19:31:01 +03:00
|
|
|
{
|
2024-02-09 09:43:15 +03:00
|
|
|
ICollider2D? colliderX = colliderCollector.Behaviours[x];
|
2024-01-30 19:31:01 +03:00
|
|
|
if (!colliderX.IsActive)
|
|
|
|
return;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
for (int y = x + 1; y < colliderCollector.Behaviours.Count; y++)
|
2024-01-30 19:31:01 +03:00
|
|
|
{
|
2024-02-09 09:43:15 +03:00
|
|
|
ICollider2D? colliderY = colliderCollector.Behaviours[y];
|
2024-01-30 19:31:01 +03:00
|
|
|
|
|
|
|
if (!colliderY.IsActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (colliderX.RigidBody2D == colliderY.RigidBody2D)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
|
|
|
|
if (bothCollidersAreTriggers)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool bothCollidersAreStatic = colliderX.RigidBody2D?.IsStatic ?? true && colliderX.RigidBody2D?.IsStatic == colliderY.RigidBody2D?.IsStatic;
|
|
|
|
if (bothCollidersAreStatic)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information))
|
|
|
|
{
|
|
|
|
if (colliderX.IsTrigger)
|
|
|
|
{
|
|
|
|
colliderX.OnTriggered?.Invoke(colliderX, colliderY);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (colliderY.IsTrigger)
|
|
|
|
{
|
|
|
|
colliderY.OnTriggered?.Invoke(colliderY, colliderY);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
colliderX.OnCollisionDetected?.Invoke(colliderX, information);
|
|
|
|
colliderY.OnCollisionDetected?.Invoke(colliderY, information);
|
|
|
|
|
|
|
|
collisionResolver?.Resolve(information);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void StepRigidBody(IRigidBody2D rigidBody, float intervalDeltaTime)
|
|
|
|
{
|
|
|
|
if (rigidBody.IsStatic || !rigidBody.IsActive)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rigidBody.Transform.Position += rigidBody.Velocity * intervalDeltaTime;
|
|
|
|
rigidBody.Transform.Rotation += rigidBody.AngularVelocity * intervalDeltaTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Assign(IGameManager gameManager)
|
|
|
|
{
|
|
|
|
if (GameManager is not null)
|
|
|
|
return false;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
colliderCollector.Assign(gameManager);
|
|
|
|
rigidBodyCollector.Assign(gameManager);
|
2024-01-30 19:31:01 +03:00
|
|
|
|
|
|
|
GameManager = gameManager;
|
|
|
|
OnGameManagerAssigned?.Invoke(this);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Unassign()
|
|
|
|
{
|
|
|
|
if (GameManager is null)
|
|
|
|
return false;
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
colliderCollector.Unassign();
|
|
|
|
rigidBodyCollector.Unassign();
|
2024-01-30 19:31:01 +03:00
|
|
|
|
|
|
|
GameManager = null!;
|
|
|
|
OnUnassigned?.Invoke(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public PhysicsEngine2DCollector()
|
2024-01-30 19:35:36 +03:00
|
|
|
{
|
|
|
|
collisionDetector = new CollisionDetector2D();
|
|
|
|
collisionResolver = new CollisionResolver2D();
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public PhysicsEngine2DCollector(IGameManager gameManager)
|
2024-01-30 19:35:36 +03:00
|
|
|
{
|
|
|
|
Assign(gameManager);
|
|
|
|
collisionDetector = new CollisionDetector2D();
|
|
|
|
collisionResolver = new CollisionResolver2D();
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public PhysicsEngine2DCollector(IGameManager gameManager, ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
2024-01-30 19:35:36 +03:00
|
|
|
{
|
|
|
|
Assign(gameManager);
|
|
|
|
this.collisionDetector = collisionDetector;
|
|
|
|
this.collisionResolver = collisionResolver;
|
|
|
|
}
|
|
|
|
|
2024-02-09 09:43:15 +03:00
|
|
|
public PhysicsEngine2DCollector(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
|
2024-01-30 19:35:36 +03:00
|
|
|
{
|
|
|
|
this.collisionDetector = collisionDetector;
|
|
|
|
this.collisionResolver = collisionResolver;
|
|
|
|
}
|
2024-01-30 19:31:01 +03:00
|
|
|
}
|