2023-12-05 17:39:38 +03:00
|
|
|
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;
|
|
|
|
|
2023-12-05 17:55:38 +03:00
|
|
|
|
|
|
|
private Vector2 worldLowerBound = lowerBound;
|
|
|
|
private Vector2 worldUpperBound = upperBound;
|
|
|
|
|
|
|
|
|
2023-12-05 17:39:38 +03:00
|
|
|
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)
|
|
|
|
{
|
2023-12-05 17:55:38 +03:00
|
|
|
if (point.X >= worldLowerBound.X && point.X <= worldUpperBound.X &&
|
|
|
|
point.Y >= worldLowerBound.Y && point.Y <= worldUpperBound.Y)
|
2023-12-05 17:39:38 +03:00
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Recalculate()
|
|
|
|
{
|
2023-12-05 17:55:38 +03:00
|
|
|
worldLowerBound = LowerBound + Transform.Position;
|
|
|
|
worldUpperBound = UpperBound + Transform.Position;
|
2023-12-05 17:39:38 +03:00
|
|
|
}
|
|
|
|
}
|