Skip to content

Commit

Permalink
Merge pull request #5 from mpewsey/refactor
Browse files Browse the repository at this point in the history
Move common methods to LayoutPack
  • Loading branch information
mpewsey authored Jun 9, 2024
2 parents 9e0c396 + fc16b98 commit 312f9ba
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 84 deletions.
13 changes: 4 additions & 9 deletions addons/mpewsey.maniamap/scripts/runtime/IDoorNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,13 @@ public static bool DoorExists(this IDoorNode door)
/// </summary>
public static DoorConnection FindDoorConnection(this IDoorNode door)
{
if (!door.RoomNode.IsInitialized)
return null;

var roomId = door.RoomNode.RoomLayout.Id;
var doorConnections = door.RoomNode.LayoutPack.GetDoorConnections(roomId);
var position = new Vector2DInt(door.Row, door.Column);
var direction = door.DoorDirection;

foreach (var connection in doorConnections)
{
if (connection.ContainsDoor(roomId, position, direction))
return connection;
}

return null;
return door.RoomNode.LayoutPack.FindDoorConnection(roomId, position, direction);
}

/// <summary>
Expand Down
81 changes: 81 additions & 0 deletions addons/mpewsey.maniamap/scripts/runtime/LayoutPack.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Godot;
using MPewsey.Common.Mathematics;
using MPewsey.ManiaMap;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -30,6 +31,16 @@ public class LayoutPack
/// </summary>
private Dictionary<Uid, List<DoorConnection>> RoomConnections { get; } = new Dictionary<Uid, List<DoorConnection>>();

/// <summary>
/// A dictionary of rooms by layer coordinate.
/// </summary>
private Dictionary<int, List<Room>> RoomsByLayer { get; } = new Dictionary<int, List<Room>>();

/// <summary>
/// A dictionary of door positions by room ID.
/// </summary>
private Dictionary<Uid, List<DoorPosition>> RoomDoors { get; } = new Dictionary<Uid, List<DoorPosition>>();

/// <summary>
/// Initializes a new layout pack.
/// </summary>
Expand Down Expand Up @@ -62,6 +73,8 @@ public LayoutPack(Layout layout, LayoutState state, ManiaMapSettings settings =
LayoutState = state;
Settings = settings;
RoomConnections = layout.GetRoomConnections();
RoomsByLayer = layout.GetRoomsByLayer();
RoomDoors = layout.GetRoomDoors();
}

/// <summary>
Expand All @@ -75,5 +88,73 @@ public IReadOnlyList<DoorConnection> GetDoorConnections(Uid id)
return connections;
return Array.Empty<DoorConnection>();
}

/// <summary>
/// Returns a list of rooms corresponding to the specified layer (z) coordinate.
/// If the layer does not exist, returns an empty list.
/// </summary>
/// <param name="z">The layer coordinate.</param>
public IReadOnlyList<Room> GetRoomsInLayer(int z)
{
if (RoomsByLayer.TryGetValue(z, out var rooms))
return rooms;
return Array.Empty<Room>();
}

/// <summary>
/// Returns a list of door positions for the specified room ID.
/// If the room ID does not exist, returns an empty list.
/// </summary>
/// <param name="id">The room ID.</param>
public IReadOnlyList<DoorPosition> GetRoomDoors(Uid id)
{
if (RoomDoors.TryGetValue(id, out var doors))
return doors;
return Array.Empty<DoorPosition>();
}

/// <summary>
/// Returns true if the door exists for the specified cell index and direction.
/// </summary>
/// <param name="id">The room ID.</param>
/// <param name="position">The cell index in the room.</param>
/// <param name="direction">The door direction.</param>
public bool DoorExists(Uid id, Vector2DInt position, DoorDirection direction)
{
foreach (var door in GetRoomDoors(id))
{
if (door.Matches(position, direction))
return true;
}

return false;
}

/// <summary>
/// Returns an enumerable of unique layer (z) coordinates for the layout.
/// The coordinates are not in any particular order.
/// </summary>
public IEnumerable<int> GetLayerCoordinates()
{
return RoomsByLayer.Keys;
}

/// <summary>
/// Returns the door connection with the given room ID, position, and direction if it exists.
/// Returns null if the connection does not exist.
/// </summary>
/// <param name="id">The room ID.</param>
/// <param name="position">The cell position index.</param>
/// <param name="direction">The door direction.</param>
public DoorConnection FindDoorConnection(Uid id, Vector2DInt position, DoorDirection direction)
{
foreach (var connection in GetDoorConnections(id))
{
if (connection.ContainsDoor(id, position, direction))
return connection;
}

return null;
}
}
}
18 changes: 7 additions & 11 deletions addons/mpewsey.maniamap/scripts/runtime/drawing/LayoutTileMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@ public partial class LayoutTileMap : LayoutTileMapBase
/// </summary>
public int LayerCoordinate { get; private set; }

/// <inheritdoc/>
protected override void Initialize(Layout layout, LayoutState layoutState)
{
base.Initialize(layout, layoutState);
TileMap ??= CreateTileMap();
}

/// <summary>
/// Draws the map for the specified layer (z) coordinate.
/// </summary>
/// <param name="layoutPack">The layout pack.</param>
/// <param name="z">The layer coordinate. If null, the minimum layer coordinate in the layout will be used.</param>
public void DrawMap(LayoutPack layoutPack, int? z = null)
{
DrawMap(layoutPack.Layout, layoutPack.LayoutState, z);
LayoutPack = layoutPack;
LayerCoordinate = z ?? layoutPack.GetLayerCoordinates().Order().First();
TileMap ??= CreateTileMap();
SetTiles(TileMap, LayerCoordinate);
}

/// <summary>
Expand All @@ -45,9 +41,9 @@ public void DrawMap(LayoutPack layoutPack, int? z = null)
/// <param name="z">The layer coordinate. If null, the minimum layer coordinate in the layout will be used.</param>
public void DrawMap(Layout layout, LayoutState layoutState = null, int? z = null)
{
Initialize(layout, layoutState);
LayerCoordinate = z ?? RoomsByLayer.Keys.Order().First();
SetTiles(TileMap, LayerCoordinate);
layoutState ??= CreateFullyVisibleLayoutState(layout);
var layoutPack = new LayoutPack(layout, layoutState, new ManiaMapSettings());
DrawMap(layoutPack, z);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Godot;
using MPewsey.Common.Mathematics;
using MPewsey.ManiaMap;
using System.Collections.Generic;
using System;

namespace MPewsey.ManiaMapGodot.Drawing
{
Expand Down Expand Up @@ -77,24 +77,9 @@ public abstract partial class LayoutTileMapBase : Node2D
[Export] public DoorDrawMode DoorDrawMode { get; set; } = DoorDrawMode.AllDoors;

/// <summary>
/// The currently drawn layout.
/// The currently drawn layout pack.
/// </summary>
public Layout Layout { get; protected set; }

/// <summary>
/// The currently drawn layout state.
/// </summary>
public LayoutState LayoutState { get; protected set; }

/// <summary>
/// A dictionary of rooms by layer coordinate.
/// </summary>
protected Dictionary<int, List<Room>> RoomsByLayer { get; set; } = new Dictionary<int, List<Room>>();

/// <summary>
/// A dictionary of door positions by room ID.
/// </summary>
protected Dictionary<Uid, List<DoorPosition>> RoomDoors { get; set; } = new Dictionary<Uid, List<DoorPosition>>();
public LayoutPack LayoutPack { get; protected set; }

public override void _Ready()
{
Expand All @@ -103,16 +88,19 @@ public override void _Ready()
}

/// <summary>
/// Performs basic set up for the tile map before drawing.
/// Returns a new layout state for the given layout with all cells visible.
/// </summary>
/// <param name="layout">The layout.</param>
/// <param name="layoutState">The layout state.</param>
protected virtual void Initialize(Layout layout, LayoutState layoutState)
protected static LayoutState CreateFullyVisibleLayoutState(Layout layout)
{
Layout = layout;
LayoutState = layoutState;
RoomDoors = layout.GetRoomDoors();
RoomsByLayer = layout.GetRoomsByLayer();
var layoutState = new LayoutState(layout);

foreach (var roomState in layoutState.RoomStates.Values)
{
Array.Fill(roomState.VisibleCells.Array, ~0);
}

return layoutState;
}

/// <summary>
Expand All @@ -124,10 +112,10 @@ protected void SetTiles(TileMap tileMap, int z)
{
tileMap.Clear();

foreach (var room in RoomsByLayer[z])
foreach (var room in LayoutPack.GetRoomsInLayer(z))
{
var cells = room.Template.Cells;
var roomState = LayoutState?.RoomStates[room.Id];
var roomState = LayoutPack.LayoutState?.RoomStates[room.Id];

for (int i = 0; i < cells.Rows; i++)
{
Expand Down Expand Up @@ -235,7 +223,7 @@ protected Vector2I GetFeatureCoordinate(Cell cell)
/// <param name="direction">The door direction.</param>
protected Vector2I GetTileCoordinate(Room room, Cell cell, Cell neighbor, Vector2DInt position, DoorDirection direction)
{
if (Door.ShowDoor(DoorDrawMode, direction) && cell.GetDoor(direction) != null && DoorExists(room, position, direction))
if (Door.ShowDoor(DoorDrawMode, direction) && cell.GetDoor(direction) != null && LayoutPack.DoorExists(room.Id, position, direction))
return MapTileSet.GetTileCoordinate(MapTileType.GetDoorTileType(direction));

if (neighbor == null)
Expand All @@ -244,26 +232,6 @@ protected Vector2I GetTileCoordinate(Room room, Cell cell, Cell neighbor, Vector
return new Vector2I(-1, -1);
}

/// <summary>
/// Returns true if the door exists for the specified cell index and direction.
/// </summary>
/// <param name="room">The room.</param>
/// <param name="position">The cell index in the room.</param>
/// <param name="direction">The door direction.</param>
protected bool DoorExists(Room room, Vector2DInt position, DoorDirection direction)
{
if (RoomDoors.TryGetValue(room.Id, out var doors))
{
foreach (var door in doors)
{
if (door.Matches(position, direction))
return true;
}
}

return false;
}

/// <summary>
/// Creates a new tile map in the tile map container and returns it.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,21 @@ public partial class LayoutTileMapBook : LayoutTileMapBase
/// </summary>
public IReadOnlyList<int> GetPageLayerCoordinates() => PageLayerCoordinates;

/// <inheritdoc/>
protected override void Initialize(Layout layout, LayoutState layoutState)
{
base.Initialize(layout, layoutState);
PageLayerCoordinates = RoomsByLayer.Keys.ToList();
PageLayerCoordinates.Sort();
SizePages();
}

/// <summary>
/// Draws all layout layers onto tile maps.
/// </summary>
/// <param name="layoutPack">The layout pack.</param>
public void DrawPages(LayoutPack layoutPack)
{
DrawPages(layoutPack.Layout, layoutPack.LayoutState);
LayoutPack = layoutPack;
PageLayerCoordinates = layoutPack.GetLayerCoordinates().ToList();
PageLayerCoordinates.Sort();
SizePages();

for (int i = 0; i < Pages.Count; i++)
{
SetTiles(Pages[i], PageLayerCoordinates[i]);
}
}

/// <summary>
Expand All @@ -56,12 +55,9 @@ public void DrawPages(LayoutPack layoutPack)
/// <param name="layoutState">The layout state. If null, the full map will be drawn. Otherwise, the layout state cell visibilities will be applied.</param>
public void DrawPages(Layout layout, LayoutState layoutState = null)
{
Initialize(layout, layoutState);

for (int i = 0; i < Pages.Count; i++)
{
SetTiles(Pages[i], PageLayerCoordinates[i]);
}
layoutState ??= CreateFullyVisibleLayoutState(layout);
var layoutPack = new LayoutPack(layout, layoutState, new ManiaMapSettings());
DrawPages(layoutPack);
}

/// <summary>
Expand Down

0 comments on commit 312f9ba

Please sign in to comment.