mirror of
https://github.com/AlexMacocian/MonoGame.Extended.git
synced 2026-07-15 15:09:29 +00:00
Remove old collision code.
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
public class CollisionActor : IActorTarget
|
||||
{
|
||||
private readonly IActorTarget _target;
|
||||
|
||||
public CollisionActor(IActorTarget target)
|
||||
{
|
||||
_target = target;
|
||||
}
|
||||
|
||||
public Vector2 Velocity
|
||||
{
|
||||
get { return _target.Velocity; }
|
||||
set { _target.Velocity = value; }
|
||||
}
|
||||
|
||||
public Vector2 Position
|
||||
{
|
||||
get { return _target.Position; }
|
||||
set { _target.Position = value; }
|
||||
}
|
||||
|
||||
public RectangleF BoundingBox => _target.BoundingBox;
|
||||
|
||||
public void OnCollision(CollisionInfo collisionInfo)
|
||||
{
|
||||
_target.OnCollision(collisionInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,101 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a collision grid. This is used to break the game world into
|
||||
/// chunks to detect collisions efficiently.
|
||||
/// </summary>
|
||||
public class CollisionGrid
|
||||
{
|
||||
private readonly CollisionGridCell[] _data;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new collision grid of specified size.
|
||||
/// </summary>
|
||||
/// <param name="data"></param>
|
||||
/// <param name="columns">Number of columns in the grid.</param>
|
||||
/// <param name="rows">Number of rows in the grid.</param>
|
||||
/// <param name="cellWidth">The width of each individual cell.</param>
|
||||
/// <param name="cellHeight">The height of each individual cell.</param>
|
||||
public CollisionGrid(int[] data, int columns, int rows, int cellWidth, int cellHeight)
|
||||
{
|
||||
_data = new CollisionGridCell[data.Length];
|
||||
|
||||
for (var y = 0; y < rows; y++)
|
||||
for (var x = 0; x < columns; x++)
|
||||
{
|
||||
var index = x + y*columns;
|
||||
_data[index] = new CollisionGridCell(this, x, y, data[index]);
|
||||
}
|
||||
|
||||
Columns = columns;
|
||||
Rows = rows;
|
||||
CellWidth = cellWidth;
|
||||
CellHeight = cellHeight;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of columns in this grid.
|
||||
/// </summary>
|
||||
public int Columns { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of rows in this grid.
|
||||
/// </summary>
|
||||
public int Rows { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of each cell.
|
||||
/// </summary>
|
||||
public int CellWidth { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of each cell.
|
||||
/// </summary>
|
||||
public int CellHeight { get; }
|
||||
|
||||
public CollisionGridCell GetCellAtIndex(int column, int row)
|
||||
{
|
||||
var index = column + row*Columns;
|
||||
|
||||
if ((index < 0) || (index >= _data.Length))
|
||||
return new CollisionGridCell(this, column, row, 0);
|
||||
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
public CollisionGridCell GetCellAtPosition(Vector3 position)
|
||||
{
|
||||
var column = (int) (position.X/CellWidth);
|
||||
var row = (int) (position.Y/CellHeight);
|
||||
|
||||
return GetCellAtIndex(column, row);
|
||||
}
|
||||
|
||||
public IEnumerable<CollisionGridCell> GetCellsOverlappingRectangle(RectangleF rectangle)
|
||||
{
|
||||
var sx = (int) (rectangle.Left/CellWidth);
|
||||
var sy = (int) (rectangle.Top/CellHeight);
|
||||
var ex = (int) (rectangle.Right/CellWidth + 1);
|
||||
var ey = (int) (rectangle.Bottom/CellHeight + 1);
|
||||
|
||||
for (var y = sy; y < ey; y++)
|
||||
for (var x = sx; x < ex; x++)
|
||||
yield return GetCellAtIndex(x, y);
|
||||
}
|
||||
|
||||
public IEnumerable<ICollidable> GetCollidables(RectangleF overlappingRectangle)
|
||||
{
|
||||
return GetCellsOverlappingRectangle(overlappingRectangle)
|
||||
.Where(cell => cell.Flag != CollisionGridCellFlag.Empty);
|
||||
}
|
||||
|
||||
public Rectangle GetCellRectangle(int column, int row)
|
||||
{
|
||||
return new Rectangle(column*CellWidth, row*CellHeight, CellWidth, CellHeight);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single cell in a collision grid.
|
||||
/// </summary>
|
||||
public class CollisionGridCell : ICollidable
|
||||
{
|
||||
private readonly CollisionGrid _parentGrid;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a collision grid cell at a location in the parent grid.
|
||||
/// </summary>
|
||||
/// <param name="parentGrid">The collision grid which this cell is a part of.</param>
|
||||
/// <param name="column">The column position of this cell.</param>
|
||||
/// <param name="row">The row position of this cell.</param>
|
||||
/// <param name="data"></param>
|
||||
public CollisionGridCell(CollisionGrid parentGrid, int column, int row, int data)
|
||||
{
|
||||
_parentGrid = parentGrid;
|
||||
Column = column;
|
||||
Row = row;
|
||||
Data = data;
|
||||
Flag = data == 0 ? CollisionGridCellFlag.Empty : CollisionGridCellFlag.Solid;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Column in the parent grid that this cell represents.
|
||||
/// </summary>
|
||||
public int Column { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Row in the parent grid that this cell represents.
|
||||
/// </summary>
|
||||
public int Row { get; }
|
||||
public int Data { get; private set; }
|
||||
public object Tag { get; set; }
|
||||
public CollisionGridCellFlag Flag { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bounding box of the cell.
|
||||
/// </summary>
|
||||
public RectangleF BoundingBox => _parentGrid.GetCellRectangle(Column, Row).ToRectangleF();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
public enum CollisionGridCellFlag
|
||||
{
|
||||
Empty,
|
||||
Solid,
|
||||
Interesting
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
/// <summary>
|
||||
/// This class holds data on a collision. It is passed as a parameter to
|
||||
/// OnCollision methods.
|
||||
/// </summary>
|
||||
public class CollisionInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the object being collided with.
|
||||
/// </summary>
|
||||
public ICollidable Other { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a vector representing the overlap between the two objects.
|
||||
/// </summary>
|
||||
public Vector2 PenetrationVector { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
public class CollisionWorld : IDisposable, IUpdate
|
||||
{
|
||||
private readonly List<CollisionActor> _actors;
|
||||
|
||||
private readonly Vector2 _gravity;
|
||||
private CollisionGrid _grid;
|
||||
|
||||
public CollisionWorld(Vector2 gravity)
|
||||
{
|
||||
_gravity = gravity;
|
||||
_actors = new List<CollisionActor>();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
var deltaTime = (float) gameTime.ElapsedGameTime.TotalSeconds;
|
||||
|
||||
foreach (var actor in _actors)
|
||||
{
|
||||
actor.Velocity += _gravity*deltaTime;
|
||||
actor.Position += actor.Velocity*deltaTime;
|
||||
|
||||
if (_grid != null)
|
||||
foreach (var collidable in _grid.GetCollidables(actor.BoundingBox))
|
||||
{
|
||||
var intersection = RectangleF.Intersection(collidable.BoundingBox, actor.BoundingBox);
|
||||
|
||||
if (intersection.IsEmpty)
|
||||
continue;
|
||||
|
||||
var info = GetCollisionInfo(actor, collidable, intersection);
|
||||
actor.OnCollision(info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CollisionActor CreateActor(IActorTarget target)
|
||||
{
|
||||
var actor = new CollisionActor(target);
|
||||
_actors.Add(actor);
|
||||
return actor;
|
||||
}
|
||||
|
||||
public CollisionGrid CreateGrid(int[] data, int columns, int rows, int cellWidth, int cellHeight)
|
||||
{
|
||||
if (_grid != null)
|
||||
throw new InvalidOperationException("Only one collision grid can be created per world");
|
||||
|
||||
_grid = new CollisionGrid(data, columns, rows, cellWidth, cellHeight);
|
||||
return _grid;
|
||||
}
|
||||
|
||||
private CollisionInfo GetCollisionInfo(ICollidable first, ICollidable second, RectangleF intersectingRectangle)
|
||||
{
|
||||
var info = new CollisionInfo
|
||||
{
|
||||
Other = second
|
||||
};
|
||||
|
||||
if (intersectingRectangle.Width < intersectingRectangle.Height)
|
||||
{
|
||||
var d = first.BoundingBox.Center.X < second.BoundingBox.Center.X
|
||||
? intersectingRectangle.Width
|
||||
: -intersectingRectangle.Width;
|
||||
info.PenetrationVector = new Vector2(d, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
var d = first.BoundingBox.Center.Y < second.BoundingBox.Center.Y
|
||||
? intersectingRectangle.Height
|
||||
: -intersectingRectangle.Height;
|
||||
info.PenetrationVector = new Vector2(0, d);
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//using System.Linq;
|
||||
//using MonoGame.Extended.Tiled;
|
||||
|
||||
//namespace MonoGame.Extended.Collisions
|
||||
//{
|
||||
// public static class CollisionWorldExtensions
|
||||
// {
|
||||
// public static CollisionGrid CreateGrid(this CollisionWorld world, TiledTileLayer tileLayer)
|
||||
// {
|
||||
// var data = tileLayer.Tiles
|
||||
// .Select(t => (int)t.GlobalIdentifier)
|
||||
// .ToArray();
|
||||
|
||||
// return world.CreateGrid(data, tileLayer.Width, tileLayer.Height, tileLayer.TileWidth, tileLayer.TileHeight);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -1,15 +0,0 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
|
||||
namespace MonoGame.Extended.Collisions
|
||||
{
|
||||
public interface ICollidable
|
||||
{
|
||||
RectangleF BoundingBox { get; }
|
||||
}
|
||||
|
||||
public interface IActorTarget : IMovable, ICollidable
|
||||
{
|
||||
Vector2 Velocity { get; set; }
|
||||
void OnCollision(CollisionInfo collisionInfo);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user