From 00b80f1a0108d2c51009723e7d845b2ba402aece Mon Sep 17 00:00:00 2001 From: Syntriax Date: Thu, 25 Jan 2024 20:42:49 +0300 Subject: [PATCH] feat: Collider2DBehaviourBase --- Engine.Physics2D/Collider2DBehaviourBase.cs | 27 +++++++++++++++++++ Engine.Physics2D/Collider2DCircleBehaviour.cs | 16 ++--------- Engine.Physics2D/Collider2DShapeBehaviour.cs | 17 ++---------- 3 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 Engine.Physics2D/Collider2DBehaviourBase.cs diff --git a/Engine.Physics2D/Collider2DBehaviourBase.cs b/Engine.Physics2D/Collider2DBehaviourBase.cs new file mode 100644 index 0000000..c796770 --- /dev/null +++ b/Engine.Physics2D/Collider2DBehaviourBase.cs @@ -0,0 +1,27 @@ +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? OnCollisionPreResolve { get; set; } = null; + public Action? OnCollisionResolved { get; set; } = null; + + + protected IRigidBody2D? _rigidBody2D = null; + + + public IRigidBody2D? RigidBody2D => _rigidBody2D; + + ITransform IAssignableTransform.Transform => Transform; + Action? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; } + bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform); + + public abstract void Recalculate(); + + protected override void OnInitialize() => BehaviourController.TryGetBehaviour(out _rigidBody2D); +} diff --git a/Engine.Physics2D/Collider2DCircleBehaviour.cs b/Engine.Physics2D/Collider2DCircleBehaviour.cs index 8607e44..1995f49 100644 --- a/Engine.Physics2D/Collider2DCircleBehaviour.cs +++ b/Engine.Physics2D/Collider2DCircleBehaviour.cs @@ -1,26 +1,14 @@ -using System; - using Syntriax.Engine.Core; -using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Primitives; namespace Syntriax.Engine.Physics2D; -public class Collider2DCircleBehaviour : BehaviourOverride, ICircleCollider2D +public class Collider2DCircleBehaviour : Collider2DBehaviourBase, ICircleCollider2D { - public Action? OnCollisionPreResolve { get; set; } = null; - public Action? OnCollisionResolved { get; set; } = null; - - public Circle CircleWorld { get; protected set; } = new(Vector2D.Zero, 1f); public Circle CircleLocal { get; set; } = new(Vector2D.Zero, 1f); - public IRigidBody2D? RigidBody2D { get; set; } = null; - ITransform IAssignableTransform.Transform => Transform; - Action? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; } - bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform); - - public virtual void Recalculate() => CircleWorld = Transform.TransformCircle(CircleLocal).Displace(Transform.Position); + public override void Recalculate() => CircleWorld = Transform.TransformCircle(CircleLocal).Displace(Transform.Position); } diff --git a/Engine.Physics2D/Collider2DShapeBehaviour.cs b/Engine.Physics2D/Collider2DShapeBehaviour.cs index 13c0cff..d16950b 100644 --- a/Engine.Physics2D/Collider2DShapeBehaviour.cs +++ b/Engine.Physics2D/Collider2DShapeBehaviour.cs @@ -1,27 +1,14 @@ -using System; - -using Syntriax.Engine.Core; -using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D.Abstract; using Syntriax.Engine.Physics2D.Primitives; namespace Syntriax.Engine.Physics2D; -public class Collider2DShapeBehaviour : BehaviourOverride, IShapeCollider2D +public class Collider2DShapeBehaviour : Collider2DBehaviourBase, IShapeCollider2D { - public Action? OnCollisionPreResolve { get; set; } = null; - public Action? OnCollisionResolved { get; set; } = null; - - public Shape ShapeWorld => _shapeWorld; public Shape ShapeLocal { get; set; } = new([new(1f, 1f), new(-1f, 1f), new(-1f, -1f), new(1f, -1f)]); - public IRigidBody2D? RigidBody2D { get; set; } = null; protected Shape _shapeWorld = new([new(1f, 1f), new(-1f, 1f), new(-1f, -1f), new(1f, -1f)]); - ITransform IAssignableTransform.Transform => Transform; - Action? IAssignableTransform.OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; } - bool IAssignableTransform.Assign(ITransform transform) => GameObject.Assign(transform); - - public virtual void Recalculate() => Transform.TransformShape(ShapeLocal, ref _shapeWorld); + public override void Recalculate() => Transform.TransformShape(ShapeLocal, ref _shapeWorld); }