Engine-Pong/Game/Physics2D/Collider2DAABBBehaviour.cs

56 lines
1.6 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(Vector2 lowerBound, Vector2 upperBound) : BehaviourOverride, ICollider2D
{
public Vector2 LowerBound { get; } = lowerBound;
public Vector2 UpperBound { get; } = upperBound;
private Vector2 worldLowerBound = lowerBound;
private Vector2 worldUpperBound = upperBound;
private IRigidBody2D? _rigidBody2D = null;
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 bool CheckCollision(Vector2 point, ICollider2D otherCollider)
{
if (point.X >= worldLowerBound.X && point.X <= worldUpperBound.X &&
point.Y >= worldLowerBound.Y && point.Y <= worldUpperBound.Y)
return true;
return false;
}
public void Recalculate()
{
worldLowerBound = LowerBound + Transform.Position;
worldUpperBound = UpperBound + Transform.Position;
}
}