Syntriax.Engine/Engine.Physics2D/PhysicsEngine2DCacher.cs

151 lines
5.0 KiB
C#
Raw Normal View History

using System;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
public class PhysicsEngine2DCacher : IPhysicsEngine2D, IAssignableGameManager
{
public Action<IAssignable>? OnUnassigned { get; set; } = null;
public Action<IAssignableGameManager>? OnGameManagerAssigned { get; set; } = null;
private int _iterationCount = 1;
protected readonly ICollisionDetector2D collisionDetector = null!;
protected readonly ICollisionResolver2D collisionResolver = null!;
protected BehaviourCacher<IRigidBody2D> rigidBodyCacher = new();
protected BehaviourCacher<ICollider2D> colliderCacher = new();
public int IterationCount { get => _iterationCount; set => _iterationCount = value < 1 ? 1 : value; }
public IGameManager GameManager { get; private set; } = null!;
public void Step(float deltaTime)
{
float intervalDeltaTime = deltaTime / IterationCount;
for (int iterationIndex = 0; iterationIndex < IterationCount; iterationIndex++)
{
// Can Parallel
foreach (var rigidBody in rigidBodyCacher)
StepRigidBody(rigidBody, intervalDeltaTime);
// Can Parallel
foreach (var collider in colliderCacher)
collider.Recalculate();
// Can Parallel
for (int x = 0; x < colliderCacher.Behaviours.Count; x++)
{
ICollider2D? colliderX = colliderCacher.Behaviours[x];
if (!colliderX.IsActive)
return;
for (int y = x + 1; y < colliderCacher.Behaviours.Count; y++)
{
ICollider2D? colliderY = colliderCacher.Behaviours[y];
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;
colliderCacher.Assign(gameManager);
rigidBodyCacher.Assign(gameManager);
GameManager = gameManager;
OnGameManagerAssigned?.Invoke(this);
return true;
}
public bool Unassign()
{
if (GameManager is null)
return false;
colliderCacher.Unassign();
rigidBodyCacher.Unassign();
GameManager = null!;
OnUnassigned?.Invoke(this);
return true;
}
public PhysicsEngine2DCacher()
{
collisionDetector = new CollisionDetector2D();
collisionResolver = new CollisionResolver2D();
}
public PhysicsEngine2DCacher(IGameManager gameManager)
{
Assign(gameManager);
collisionDetector = new CollisionDetector2D();
collisionResolver = new CollisionResolver2D();
}
public PhysicsEngine2DCacher(IGameManager gameManager, ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
{
Assign(gameManager);
this.collisionDetector = collisionDetector;
this.collisionResolver = collisionResolver;
}
public PhysicsEngine2DCacher(ICollisionDetector2D collisionDetector, ICollisionResolver2D collisionResolver)
{
this.collisionDetector = collisionDetector;
this.collisionResolver = collisionResolver;
}
}