Skip to content

Commit

Permalink
Add get rooms by layer method
Browse files Browse the repository at this point in the history
  • Loading branch information
mpewsey committed May 13, 2024
1 parent e63d2f9 commit 7b1ac84
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/ManiaMap.Tests/TestLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,31 @@ public void TestGetLayers()
Assert.IsNotNull(layout);

var expected = new int[] { -1, 0, 2 };
CollectionAssert.AreEqual(layout.GetLayers(), expected);
CollectionAssert.AreEqual(expected, layout.GetLayers());
}

[TestMethod]
public void TestGetRoomsByLayer()
{
var graph = Samples.GraphLibrary.StackedLoopGraph();

var templateGroups = new TemplateGroups();
templateGroups.Add("Default", Samples.TemplateLibrary.Miscellaneous.HyperSquareTemplate());

var generator = new LayoutGenerator();
var random = new RandomSeed(12345);
var layout = generator.Generate(1, graph, templateGroups, random);

Assert.IsNotNull(layout);

var roomsByLayer = layout.GetRoomsByLayer();
Assert.AreEqual(3, roomsByLayer.Count);
Assert.IsTrue(roomsByLayer.ContainsKey(-1));
Assert.IsTrue(roomsByLayer.ContainsKey(0));
Assert.IsTrue(roomsByLayer.ContainsKey(2));
Assert.AreEqual(1, roomsByLayer[-1].Count);
Assert.AreEqual(4, roomsByLayer[0].Count);
Assert.AreEqual(3, roomsByLayer[2].Count);
}
}
}
21 changes: 21 additions & 0 deletions src/ManiaMap/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,5 +504,26 @@ public int[] GetLayers()
Array.Sort(result);
return result;
}

/// <summary>
/// Returns a dictionary of rooms by layer (Z) coordinate.
/// </summary>
public Dictionary<int, List<Room>> GetRoomsByLayer()
{
var result = new Dictionary<int, List<Room>>();

foreach (var room in Rooms.Values)
{
if (!result.TryGetValue(room.Position.Z, out var rooms))
{
rooms = new List<Room>();
result.Add(room.Position.Z, rooms);
}

rooms.Add(room);
}

return result;
}
}
}

0 comments on commit 7b1ac84

Please sign in to comment.