2024-01-25 20:42:49 +03:00
|
|
|
using System;
|
|
|
|
|
|
|
|
using Syntriax.Engine.Core;
|
|
|
|
using Syntriax.Engine.Core.Abstract;
|
|
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
|
|
|
|
|
|
|
namespace Syntriax.Engine.Physics2D;
|
|
|
|
|
|
|
|
public abstract class Collider2DBehaviourBase : BehaviourOverride, ICollider2D
|
|
|
|
{
|
2024-01-27 20:17:46 +03:00
|
|
|
public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { get; set; } = null;
|
|
|
|
public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionResolved { get; set; } = null;
|
2024-01-27 20:31:51 +03:00
|
|
|
public Action<ICollider2D, ICollider2D>? OnTriggered { get; set; } = null;
|
2024-01-25 20:42:49 +03:00
|
|
|
|
|
|
|
|
2024-01-25 22:00: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;
|
|
|
|
Action<IAssignableTransform>? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => 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);
|
|
|
|
|
2024-01-25 22:00:49 +03:00
|
|
|
public void Recalculate()
|
|
|
|
{
|
|
|
|
if (!NeedsRecalculation)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CalculateCollider();
|
|
|
|
NeedsRecalculation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void CalculateCollider();
|
2024-01-25 20:42:49 +03:00
|
|
|
|
2024-01-25 21:52:56 +03:00
|
|
|
protected override void OnInitialize()
|
|
|
|
{
|
|
|
|
BehaviourController.TryGetBehaviour(out _rigidBody2D);
|
|
|
|
|
|
|
|
BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController;
|
|
|
|
BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController;
|
2024-01-25 22:00:49 +03:00
|
|
|
|
2024-01-26 18:49:26 +03:00
|
|
|
Transform.OnPositionChanged += SetNeedsRecalculation;
|
|
|
|
Transform.OnRotationChanged += SetNeedsRecalculation;
|
|
|
|
Transform.OnScaleChanged += SetNeedsRecalculation;
|
2024-01-25 21:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBehaviourAddedToController(IBehaviourController _, IBehaviour behaviour)
|
|
|
|
{
|
2024-01-26 18:49:26 +03:00
|
|
|
if (behaviour is IRigidBody2D rigidBody)
|
|
|
|
_rigidBody2D = rigidBody;
|
2024-01-25 21:52:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnBehaviourRemovedFromController(IBehaviourController _, IBehaviour behaviour)
|
|
|
|
{
|
|
|
|
if (behaviour is IRigidBody2D _)
|
|
|
|
_rigidBody2D = null;
|
|
|
|
}
|
|
|
|
|
2024-01-26 18:49:26 +03:00
|
|
|
private void SetNeedsRecalculation(ITransform transform) => NeedsRecalculation = true;
|
2024-01-25 22:00:49 +03:00
|
|
|
|
2024-01-25 21:52:56 +03:00
|
|
|
protected override void OnFinalize()
|
|
|
|
{
|
|
|
|
BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController;
|
2024-01-25 22:00:49 +03:00
|
|
|
BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController;
|
2024-01-26 18:49:26 +03:00
|
|
|
Transform.OnScaleChanged -= SetNeedsRecalculation;
|
2024-01-25 22:00:49 +03:00
|
|
|
|
2024-01-26 18:49:26 +03:00
|
|
|
Transform.OnPositionChanged -= SetNeedsRecalculation;
|
|
|
|
Transform.OnRotationChanged -= SetNeedsRecalculation;
|
2024-01-25 21:52:56 +03:00
|
|
|
}
|
2024-01-25 20:42:49 +03:00
|
|
|
}
|