Engine-Pong/Game/Physics2D/Collider2DAABBBehaviour.cs

68 lines
2.2 KiB
C#
Raw Normal View History

2023-12-05 17:39:38 +03:00
using System;
using System.Collections.Generic;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Physics2D.Abstract;
2023-12-07 10:55:49 +03:00
using Syntriax.Engine.Physics2D.Primitives;
2023-12-05 17:39:38 +03:00
namespace Syntriax.Engine.Physics2D;
2023-12-06 13:25:16 +03:00
public class Collider2DAABBBehaviour : BehaviourOverride, ICollider2D
2023-12-05 17:39:38 +03:00
{
2023-12-06 13:25:16 +03:00
public AABB AABBLocal { get; set; } = null!;
public AABB AABBWorld { get; private set; } = null!;
2023-12-05 17:55:38 +03:00
2023-12-05 17:39:38 +03:00
private IRigidBody2D? _rigidBody2D = null;
2024-01-23 12:16:58 +03:00
private List<Vector2D> _vertices = new List<Vector2D>(4);
2023-12-05 17:39:38 +03:00
public IRigidBody2D? RigidBody2D
{
get
{
if (_rigidBody2D is null)
BehaviourController.TryGetBehaviour(out _rigidBody2D);
return _rigidBody2D;
}
}
public Action<ICollider2D, ICollider2D>? OnCollisionPreResolve { get; set; } = null;
public Action<IAssignableTransform>? OnTransformAssigned { get => GameObject.OnTransformAssigned; set => GameObject.OnTransformAssigned = value; }
ITransform IAssignableTransform.Transform => Transform;
public bool Assign(ITransform transform) => GameObject.Assign(transform);
2024-01-23 12:16:58 +03:00
public IList<Vector2D> Vertices => _vertices;
2023-12-05 17:39:38 +03:00
2024-01-23 12:16:58 +03:00
public bool CheckCollision(Vector2D point)
2023-12-06 13:25:16 +03:00
{
2023-12-07 10:55:49 +03:00
return AABBWorld.Overlaps(point);
2023-12-05 17:39:38 +03:00
}
public void Recalculate()
{
2023-12-06 13:25:16 +03:00
AABBWorld = new AABB(
2023-12-06 13:50:39 +03:00
AABBLocal.LowerBoundary.Scale(Transform.Scale) + Transform.Position,
AABBLocal.UpperBoundary.Scale(Transform.Scale) + Transform.Position
2023-12-06 13:25:16 +03:00
);
2023-12-06 15:56:46 +03:00
Vertices.Clear();
Vertices.Add(AABBWorld.LowerBoundary);
2024-01-23 12:16:58 +03:00
Vertices.Add(new Vector2D(AABBWorld.LowerBoundary.X, AABBWorld.UpperBoundary.Y));
2023-12-06 15:56:46 +03:00
Vertices.Add(AABBWorld.UpperBoundary);
2024-01-23 12:16:58 +03:00
Vertices.Add(new Vector2D(AABBWorld.UpperBoundary.X, AABBWorld.LowerBoundary.Y));
2023-12-06 13:25:16 +03:00
}
2024-01-23 12:16:58 +03:00
public Collider2DAABBBehaviour(Vector2D lowerBoundary, Vector2D upperBoundary)
2023-12-06 13:25:16 +03:00
{
AABBLocal = new AABB(lowerBoundary, upperBoundary);
AABBWorld = new AABB(lowerBoundary, upperBoundary);
}
public Collider2DAABBBehaviour()
{
2024-01-23 12:16:58 +03:00
AABBLocal = new(Vector2D.Zero, Vector2D.Zero);
AABBWorld = new(Vector2D.Zero, Vector2D.Zero);
2023-12-05 17:39:38 +03:00
}
}