Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 59 additions & 4 deletions Tests/TwoDimension/LevelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TileSystem.Interfaces.TwoDimension;
using TileSystem.Implementation.TwoDimension;
using TileSystem.Interfaces.Creation;
using System.Collections.Generic;

namespace Tests.TwoDimension
{
Expand Down Expand Up @@ -112,15 +113,69 @@ public void AreaRemove()
[Test]
public void PositionGet()
{
// TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15)
Assert.Fail();
Level level = new Level();
var mockArea = new Mock<IArea>();

mockArea.Object.SetPosition(level, new Position2D(5, 5));

//Add Area to Level
level.Add(mockArea.Object);

IPosition2D position2D = new Position2D(5, 5);

//Grab the Area by position
IArea area = level.Get(position2D);

//Test that area isn't null
Assert.IsNotNull(area);

//Test that wrong position throws error
IPosition2D wrongPosition2D = new Position2D(3, 2);
Assert.That(() => level.Get(wrongPosition2D), Throws.ArgumentNullException);
}

[Test]
public void PositionGetNeighbours()
{
// TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15)
Assert.Fail();
Level level = new Level();
var mockArea1 = new Mock<IArea>();
var mockArea2 = new Mock<IArea>();
var mockArea3 = new Mock<IArea>();
var mockArea4 = new Mock<IArea>();
var mockArea5 = new Mock<IArea>();
var mockArea6 = new Mock<IArea>();

//3 Areas that will be each other neighbours
mockArea1.Object.SetPosition(level, new Position2D(2, 2));
mockArea2.Object.SetPosition(level, new Position2D(1, 2));
mockArea3.Object.SetPosition(level, new Position2D(2, 3));

//2 Areas that will be each other neighbours
mockArea4.Object.SetPosition(level, new Position2D(8, 8));
mockArea5.Object.SetPosition(level, new Position2D(8, 9));

//Single Area with no neighbours
mockArea3.Object.SetPosition(level, new Position2D(0, 0));

//Add Areas to the level
level.Add(mockArea1.Object);
level.Add(mockArea2.Object);
level.Add(mockArea3.Object);
level.Add(mockArea4.Object);
level.Add(mockArea5.Object);
level.Add(mockArea6.Object);

//Test that mockArea2 has 2 neighbours
List<IArea> neighbours1 = level.GetNeighbours(mockArea2.Object);
Assert.IsTrue(neighbours1.Count == 2);

//Test that mockArea4 has 1 neighbour
List<IArea> neighbours2 = level.GetNeighbours(mockArea4.Object);
Assert.IsTrue(neighbours2.Count == 1);

//Test that mockArea6 has no neighbours
List<IArea> neighbours3 = level.GetNeighbours(mockArea6.Object);
Assert.IsTrue(neighbours3.Count == 0);
}
}
}
58 changes: 54 additions & 4 deletions TileSystem/Implementation/TwoDimension/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using TileSystem.Interfaces.Base;
using TileSystem.Interfaces.Creation;
using TileSystem.Interfaces.Management;
using TileSystem.Interfaces.TwoDimension;

namespace TileSystem.Implementation.TwoDimension
{
Expand Down Expand Up @@ -232,8 +233,30 @@ public bool Remove(IArea area)
/// <returns>Reference to the area</returns>
public IArea Get(IPosition position)
{
// TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15)
throw new NotImplementedException();
IArea currentArea = null;

//Assuming it's 2D for comparison sake
IPosition2D position2D = position as IPosition2D;

//Looking through all areas in current level
foreach (Area area in areas)
{
IPosition2D areaPosition2D = area.Position as IPosition2D;

if (areaPosition2D.X == position2D.X && areaPosition2D.Y == position2D.Y)
{
currentArea = area;
//We can stop searching since there won't be any areas with duplicate positions
break;
}
}

if (currentArea == null)
{
throw new ArgumentNullException("area", "No Area found at position");
}

return currentArea;
}

/// <summary>
Expand All @@ -243,8 +266,35 @@ public IArea Get(IPosition position)
/// <returns>List of IArea which are next to the supplied area</returns>
public List<IArea> GetNeighbours(IArea area)
{
// TODO: Issue 15 (https://github.com/Wizcorp/TileSystem/issues/15)
throw new NotImplementedException();
//The method will return maximum of 4 neighbours in the 4 cardinal directions, doesn't take into account diagonal neighbours
List<IArea> neighbours = new List<IArea>();

//Assuming it's 2D for ease of comparison
IPosition2D areaPosition2D = area.Position as IPosition2D;

foreach (Area a in areas)
{
IPosition2D potentialNeighbourPosition2D = a.Position as IPosition2D;

//Assuming negative positions in areas are possible
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
//If the position is 0, 0 offset from current area, ignore it, since it's the current area
if (x == 0 && y == 0)
continue;

if (potentialNeighbourPosition2D.X + x == areaPosition2D.X && potentialNeighbourPosition2D.Y + y == areaPosition2D.Y)
{
//If potential Neighbour Position + offset equals the current area position, add it to our list, since it's a neighbour
neighbours.Add(a);
}
}
}
}

return neighbours;
}

#endregion
Expand Down