80 lines
2.8 KiB
C#
80 lines
2.8 KiB
C#
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
|
|
{
|
|
public Action<ICollider2D, CollisionDetectionInformation>? OnCollisionDetected { 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 IRigidBody2D? _rigidBody2D = null;
|
|
|
|
public IRigidBody2D? RigidBody2D => _rigidBody2D;
|
|
public bool IsTrigger { get; set; } = false;
|
|
|
|
ITransform IAssignableTransform.Transform => Transform;
|
|
Action<IAssignableTransform>? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
|
|
|
|
bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform);
|
|
|
|
public void Recalculate()
|
|
{
|
|
if (!NeedsRecalculation)
|
|
return;
|
|
|
|
CalculateCollider();
|
|
NeedsRecalculation = false;
|
|
}
|
|
|
|
public abstract void CalculateCollider();
|
|
|
|
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;
|
|
}
|
|
}
|