mirror of
https://github.com/TheRavenwoodArts/LunaLightXMG.git
synced 2026-07-30 23:35:02 -07:00
b9b339d51c
* Created a GetCollisionSide method. * Bug Fixed and Tested Co-authored-by: jme9 <jme9@pdx.edu>
80 lines
2.7 KiB
C#
80 lines
2.7 KiB
C#
using Microsoft.Xna.Framework;
|
|
|
|
namespace LunaLightXMG
|
|
{
|
|
public class CollisionManager
|
|
{
|
|
// Method to check if a position is colliding with any platform
|
|
public static bool IsColliding(Vector2 newPosition, BoundingBox boundingBox, Platform[] platforms)
|
|
{
|
|
Rectangle newBounds = new Rectangle((int)newPosition.X, (int)newPosition.Y, boundingBox.Bounds.Width, boundingBox.Bounds.Height);
|
|
foreach (var platform in platforms)
|
|
{
|
|
if (newBounds.Intersects(platform.BoundingBox.Bounds))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Method to check if a position is colliding with any platform and return the colliding platform
|
|
public static Platform GetCollidingPlatform(Vector2 newPosition, BoundingBox boundingBox, Platform[] platforms)
|
|
{
|
|
Rectangle newBounds = new Rectangle((int)newPosition.X, (int)newPosition.Y, boundingBox.Bounds.Width, boundingBox.Bounds.Height);
|
|
foreach (var platform in platforms)
|
|
{
|
|
if (newBounds.Intersects(platform.BoundingBox.Bounds))
|
|
{
|
|
return platform;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Method to check if a position is colliding with any wall
|
|
public static bool WallCheck(Vector2 position, BoundingBox boundingBox, Platform[] platform)
|
|
{
|
|
if (IsColliding(position + new Vector2(1, 0), boundingBox, platform) || IsColliding(position + new Vector2(-1, 0), boundingBox, platform))
|
|
{
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Method to get the collision side
|
|
public enum CollisionSide
|
|
{
|
|
None,
|
|
Top,
|
|
Bottom,
|
|
Left,
|
|
Right
|
|
}
|
|
|
|
public static CollisionSide GetCollisionSide(Vector2 position, BoundingBox boundingBox, Platform[] platform)
|
|
{
|
|
if (IsColliding(position + new Vector2(-1, 0), boundingBox, platform))
|
|
{
|
|
return CollisionSide.Left;
|
|
}
|
|
else if (IsColliding(position + new Vector2(1, 0), boundingBox, platform))
|
|
{
|
|
return CollisionSide.Right;
|
|
}
|
|
else if (IsColliding(position + new Vector2(0, -1), boundingBox, platform))
|
|
{
|
|
return CollisionSide.Top;
|
|
}
|
|
else if (IsColliding(position + new Vector2(0, 1), boundingBox, platform))
|
|
{
|
|
return CollisionSide.Bottom;
|
|
}
|
|
else
|
|
{
|
|
return CollisionSide.None;
|
|
}
|
|
}
|
|
}
|
|
}
|