Remove dependency on IWorldObject by introducing callbacks, and change terminology from "world object" to "(world) entity"
This commit is contained in:
@@ -6,32 +6,32 @@ namespace QuadtreeTests
|
||||
{
|
||||
public class Tests
|
||||
{
|
||||
private class TestWorldObject(Vector2 position) : IWorldObject
|
||||
private const float BoundingRadius = 1.0f;
|
||||
|
||||
private class TestEntity(Vector2 position)
|
||||
{
|
||||
public Vector2 Position { get; } = position;
|
||||
|
||||
public float BoundingRadius { get; } = 1.0f;
|
||||
|
||||
public Rectangle BoundingBox { get; } = new Rectangle(position - Vector2.One, position + Vector2.One);
|
||||
}
|
||||
|
||||
private List<Vector2> _positions;
|
||||
|
||||
private List<IWorldObject> _objects;
|
||||
private List<TestEntity> _entities;
|
||||
|
||||
private Quadtree _quadtree;
|
||||
private Quadtree<TestEntity> _quadtree;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_positions = [new Vector2(1.0f, 1.0f), new Vector2(3.0f, 3.0f), new Vector2(5.0f, 8.0f)];
|
||||
_objects = new();
|
||||
_quadtree = new(20, 4);
|
||||
_entities = new();
|
||||
_quadtree = new(20, 4, e => e.Position, e => e.BoundingBox);
|
||||
|
||||
for (int i = 0; i < _positions.Count; i++)
|
||||
{
|
||||
var obj = new TestWorldObject(_positions[i]);
|
||||
_objects.Add(obj);
|
||||
var obj = new TestEntity(_positions[i]);
|
||||
_entities.Add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ namespace QuadtreeTests
|
||||
{
|
||||
AddObjectsAndAssertCount();
|
||||
|
||||
for (int i = 0; i < _objects.Count; i++)
|
||||
for (int i = 0; i < _entities.Count; i++)
|
||||
{
|
||||
_quadtree.Remove(_objects[i]);
|
||||
Assert.That(_quadtree.Count, Is.EqualTo(_objects.Count - i - 1));
|
||||
_quadtree.Remove(_entities[i]);
|
||||
Assert.That(_quadtree.Count, Is.EqualTo(_entities.Count - i - 1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,16 +57,16 @@ namespace QuadtreeTests
|
||||
public void TestQuery()
|
||||
{
|
||||
AddObjectsAndAssertCount();
|
||||
List<IWorldObject> result = new();
|
||||
List<TestEntity> result = new();
|
||||
_quadtree.Query(new Rectangle(new Vector2(3.5f, 3.5f), new Vector2(10.0f, 10.0f)), result);
|
||||
Assert.That(result.Count, Is.EqualTo(2));
|
||||
}
|
||||
|
||||
private void AddObjectsAndAssertCount()
|
||||
{
|
||||
for (int i = 0; i < _objects.Count; i++)
|
||||
for (int i = 0; i < _entities.Count; i++)
|
||||
{
|
||||
_quadtree.Add(_objects[i]);
|
||||
_quadtree.Add(_entities[i], BoundingRadius);
|
||||
Assert.That(_quadtree.Count, Is.EqualTo(i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user