69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Syntriax.Engine.Core;
|
|
using Syntriax.Engine.Core.Abstract;
|
|
using Syntriax.Engine.Physics2D.Abstract;
|
|
|
|
namespace Syntriax.Engine.Physics2D;
|
|
|
|
public class Collider2DAABBBehaviour : BehaviourOverride, ICollider2D
|
|
{
|
|
public AABB AABBLocal { get; set; } = null!;
|
|
public AABB AABBWorld { get; private set; } = null!;
|
|
|
|
private IRigidBody2D? _rigidBody2D = null;
|
|
private List<Vector2> _vertices = new List<Vector2>(4);
|
|
|
|
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);
|
|
|
|
public IList<Vector2> Vertices => _vertices;
|
|
|
|
public bool CheckCollision(Vector2 point)
|
|
{
|
|
return AABBWorld.Inside(point);
|
|
}
|
|
|
|
public void Recalculate()
|
|
{
|
|
AABBWorld = new AABB(
|
|
AABBLocal.LowerBoundary.Scale(Transform.Scale) + Transform.Position,
|
|
AABBLocal.UpperBoundary.Scale(Transform.Scale) + Transform.Position
|
|
);
|
|
|
|
Vertices.Clear();
|
|
Vertices.Add(AABBWorld.LowerBoundary);
|
|
Vertices.Add(new Vector2(AABBWorld.LowerBoundary.X, AABBWorld.UpperBoundary.Y));
|
|
Vertices.Add(AABBWorld.UpperBoundary);
|
|
Vertices.Add(new Vector2(AABBWorld.UpperBoundary.X, AABBWorld.LowerBoundary.Y));
|
|
}
|
|
public Collider2DAABBBehaviour(Vector2 lowerBoundary, Vector2 upperBoundary)
|
|
{
|
|
AABBLocal = new AABB(lowerBoundary, upperBoundary);
|
|
AABBWorld = new AABB(lowerBoundary, upperBoundary);
|
|
}
|
|
|
|
public Collider2DAABBBehaviour()
|
|
{
|
|
AABBLocal = new(Vector2.Zero, Vector2.Zero);
|
|
AABBWorld = new(Vector2.Zero, Vector2.Zero);
|
|
}
|
|
}
|