feat: ICollider.OnTriggered

This commit is contained in:
Syntriax 2024-01-27 20:31:51 +03:00
parent 15788f2eca
commit affd2bb8c4
3 changed files with 18 additions and 0 deletions

View File

@ -9,6 +9,8 @@ public interface ICollider2D : IBehaviour, IAssignableTransform
Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { get; set; } Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { get; set; }
Action<ICollider2D, CollisionDetectionInformation>? OnCollisionResolved { get; set; } Action<ICollider2D, CollisionDetectionInformation>? OnCollisionResolved { get; set; }
Action<ICollider2D, ICollider2D>? OnTriggered { get; set; }
IRigidBody2D? RigidBody2D { get; } IRigidBody2D? RigidBody2D { get; }
bool IsTrigger { get; set; } bool IsTrigger { get; set; }

View File

@ -10,6 +10,7 @@ public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D
{ {
public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { get; set; } = null; public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { get; set; } = null;
public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionResolved { get; set; } = null; public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionResolved { get; set; } = null;
public Action<ICollider2D, ICollider2D>? OnTriggered { get; set; } = null;
protected bool NeedsRecalculation { get; private set; } = true; protected bool NeedsRecalculation { get; private set; } = true;

View File

@ -61,8 +61,23 @@ public class PhysicsEngine2D : IPhysicsEngine2D
if (colliderX.RigidBody2D == colliderY.RigidBody2D) if (colliderX.RigidBody2D == colliderY.RigidBody2D)
continue; continue;
bool bothCollidersAreTriggers = colliderX.IsTrigger && colliderX.IsTrigger == colliderY.IsTrigger;
if (bothCollidersAreTriggers)
continue;
if (collisionDetector.TryDetect(colliderX, colliderY, out CollisionDetectionInformation information)) 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); colliderX.OnCollisionDetected?.Invoke(colliderX, information);
colliderY.OnCollisionDetected?.Invoke(colliderY, information); colliderY.OnCollisionDetected?.Invoke(colliderY, information);