Syntriax.Engine/Engine.Physics2D/Collider2DBehaviourBase.cs

82 lines
3.1 KiB
C#
Raw Normal View History

2024-01-25 20:42:49 +03:00
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
namespace Syntriax.Engine.Physics2D;
public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D
2024-01-25 20:42:49 +03:00
{
2024-07-15 01:13:39 +03:00
public event ICollider2D.OnCollisionDetectedDelegate? OnCollisionDetected = null;
public event ICollider2D.OnCollisionResolvedDelegate? OnCollisionResolved = null;
public event ICollider2D.OnTriggeredDelegate? OnTriggered = null;
2024-01-25 20:42:49 +03:00
protected bool NeedsRecalculation { get; private set; } = true;
2024-01-25 20:42:49 +03:00
protected IRigidBody2D? _rigidBody2D = null;
public IRigidBody2D? RigidBody2D => _rigidBody2D;
2024-01-27 20:19:00 +03:00
public bool IsTrigger { get; set; } = false;
2024-01-25 20:42:49 +03:00
ITransform IAssignableTransform.Transform => Transform;
2024-07-15 01:13:39 +03:00
public event IAssignableTransform.OnTransformAssignedDelegate? OnTransformAssigned { add => GameObject.OnTransformAssigned += value; remove => GameObject.OnTransformAssigned -= value; }
2024-01-27 20:19:00 +03:00
2024-01-25 20:42:49 +03:00
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
public void Recalculate()
{
if (!NeedsRecalculation)
return;
CalculateCollider();
NeedsRecalculation = false;
}
public abstract void CalculateCollider();
2024-01-25 20:42:49 +03:00
protected override void OnInitialize()
{
BehaviourController.TryGetBehaviourInParent(out _rigidBody2D);
BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController;
BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController;
Transform.OnPositionChanged += SetNeedsRecalculation;
Transform.OnRotationChanged += SetNeedsRecalculation;
Transform.OnScaleChanged += SetNeedsRecalculation;
Transform.OnParentChanged += UpdateRigidBody2D;
}
private void UpdateRigidBody2D(ITransform _1, ITransform? _2)
{
BehaviourController.TryGetBehaviourInParent(out _rigidBody2D);
}
private void OnBehaviourAddedToController(IBehaviourController _, IBehaviour behaviour)
{
if (behaviour is IRigidBody2D rigidBody)
_rigidBody2D = rigidBody;
}
private void OnBehaviourRemovedFromController(IBehaviourController _, IBehaviour behaviour)
{
if (behaviour is IRigidBody2D _)
_rigidBody2D = null;
}
private void SetNeedsRecalculation(ITransform transform) => NeedsRecalculation = true;
protected override void OnFinalize()
{
BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController;
BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController;
Transform.OnScaleChanged -= SetNeedsRecalculation;
Transform.OnPositionChanged -= SetNeedsRecalculation;
Transform.OnRotationChanged -= SetNeedsRecalculation;
}
2024-07-15 01:13:39 +03:00
public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation);
public void Resolve(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionResolved?.Invoke(this, collisionDetectionInformation);
public void Trigger(ICollider2D initiator) => OnTriggered?.Invoke(this, initiator);
2024-01-25 20:42:49 +03:00
}