Skip to content

Commit

Permalink
Merge pull request #4 from mpewsey/remove-manager
Browse files Browse the repository at this point in the history
Replaced ManiaMapManger singleton with LayoutPack class instances
  • Loading branch information
mpewsey authored Jun 8, 2024
2 parents 8560f2d + d172418 commit 9e0c396
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public static bool DoorExists(this IDoorNode door)
public static DoorConnection FindDoorConnection(this IDoorNode door)
{
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 door.RoomNode.DoorConnections)
foreach (var connection in doorConnections)
{
if (connection.ContainsDoor(roomId, position, direction))
return connection;
Expand Down
15 changes: 2 additions & 13 deletions addons/mpewsey.maniamap/scripts/runtime/IRoomNode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MPewsey.ManiaMap;
using System.Collections.Generic;

namespace MPewsey.ManiaMapGodot
{
Expand Down Expand Up @@ -29,14 +28,9 @@ public interface IRoomNode
public Godot.Collections.Array<Godot.Collections.Array<bool>> ActiveCells { get; set; }

/// <summary>
/// The current layout.
/// The current layout pack.
/// </summary>
public Layout Layout { get; }

/// <summary>
/// The current layout state.
/// </summary>
public LayoutState LayoutState { get; }
public LayoutPack LayoutPack { get; }

/// <summary>
/// This room's layout.
Expand All @@ -48,11 +42,6 @@ public interface IRoomNode
/// </summary>
public RoomState RoomState { get; }

/// <summary>
/// A list of door connections for the room.
/// </summary>
public IReadOnlyList<DoorConnection> DoorConnections { get; }

/// <summary>
/// True if the room has been initialized.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,79 +8,60 @@ namespace MPewsey.ManiaMapGodot
/// <summary>
/// Holds the current `Layout` and `LayoutState`.
/// </summary>
public partial class ManiaMapManager : GodotObject
public class LayoutPack
{
/// <summary>
/// The current manager.
/// The layout.
/// </summary>
public static ManiaMapManager Current { get; private set; }
public Layout Layout { get; }

/// <summary>
/// The current layout.
/// The layout state.
/// </summary>
public Layout Layout { get; private set; }
public LayoutState LayoutState { get; }

/// <summary>
/// The current layout state.
/// The settings.
/// </summary>
public LayoutState LayoutState { get; private set; }

/// <summary>
/// The current settings.
/// </summary>
public ManiaMapSettings Settings { get; private set; }
public ManiaMapSettings Settings { get; }

/// <summary>
/// A dictionary of door connections by room ID.
/// </summary>
private Dictionary<Uid, List<DoorConnection>> RoomConnections { get; set; } = new Dictionary<Uid, List<DoorConnection>>();
private Dictionary<Uid, List<DoorConnection>> RoomConnections { get; } = new Dictionary<Uid, List<DoorConnection>>();

/// <summary>
/// Initializes a new manager and sets it as the current manager.
/// Initializes a new layout pack.
/// </summary>
/// <param name="layout">The layout.</param>
/// <param name="state">The layout state.</param>
/// <param name="settingsPath">The path to the ManiaMapSettings resource.</param>
public static ManiaMapManager Initialize(Layout layout, LayoutState state, string settingsPath)
public LayoutPack(Layout layout, LayoutState state, string settingsPath)
: this(layout, state, ResourceLoader.Load<ManiaMapSettings>(settingsPath))
{
var settings = ResourceLoader.Load<ManiaMapSettings>(settingsPath);
return Initialize(layout, state, settings);

}

/// <summary>
/// Initializes a new manager and sets it as the current manager.
/// Initializes a new layout pack.
/// </summary>
/// <param name="layout">The layout.</param>
/// <param name="state">The layout state.</param>
/// <param name="settings">The settings resource. If null, a new default instance of the settings will be used.</param>
/// <exception cref="ArgumentNullException">Thrown if the layout or layout state is null.</exception>
/// <exception cref="ArgumentException">Thrown if the layout and layout state ID's do not match.</exception>
public static ManiaMapManager Initialize(Layout layout, LayoutState state, ManiaMapSettings settings = null)
public LayoutPack(Layout layout, LayoutState state, ManiaMapSettings settings = null)
{
ArgumentNullException.ThrowIfNull(layout, nameof(layout));
ArgumentNullException.ThrowIfNull(state, nameof(state));

if (layout.Id != state.Id)
throw new ArgumentException($"Layout and layout state ID's do not match: (Layout ID = {layout.Id}, Layout State ID = {state.Id})");

var manager = new ManiaMapManager()
{
Layout = layout,
LayoutState = state,
Settings = settings ?? new ManiaMapSettings(),
RoomConnections = layout.GetRoomConnections(),
};

manager.SetAsCurrent();
return manager;
}

/// <summary>
/// Assigns the manager to the current manager static property.
/// </summary>
private void SetAsCurrent()
{
Current = this;
Layout = layout;
LayoutState = state;
Settings = settings;
RoomConnections = layout.GetRoomConnections();
}

/// <summary>
Expand Down
47 changes: 15 additions & 32 deletions addons/mpewsey.maniamap/scripts/runtime/RoomNode2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using MPewsey.ManiaMap;
using MPewsey.ManiaMapGodot.Exceptions;
using System;
using System.Collections.Generic;

namespace MPewsey.ManiaMapGodot
{
Expand Down Expand Up @@ -59,20 +58,14 @@ public partial class RoomNode2D : Node2D, IRoomNode
[Export] public Godot.Collections.Array<Godot.Collections.Array<bool>> ActiveCells { get; set; } = new Godot.Collections.Array<Godot.Collections.Array<bool>>();

/// <inheritdoc/>
public Layout Layout { get; private set; }

/// <inheritdoc/>
public LayoutState LayoutState { get; private set; }
public LayoutPack LayoutPack { get; private set; }

/// <inheritdoc/>
public Room RoomLayout { get; private set; }

/// <inheritdoc/>
public RoomState RoomState { get; private set; }

/// <inheritdoc/>
public IReadOnlyList<DoorConnection> DoorConnections { get; private set; } = Array.Empty<DoorConnection>();

/// <inheritdoc/>
public bool IsInitialized { get; private set; }

Expand Down Expand Up @@ -182,39 +175,33 @@ private static bool MouseIsInsideMainScreen()
/// The layout and layout state are assigned to the room based on the current ManiaMapManager.
/// </summary>
/// <param name="id">The room ID.</param>
/// <param name="layoutPack">The layout pack.</param>
/// <param name="scene">The room scene.</param>
/// <param name="parent">The node to which the room will be added as a child.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public static RoomNode2D CreateInstance(Uid id, PackedScene scene, Node parent, bool assignLayoutPosition = false)
public static RoomNode2D CreateInstance(Uid id, LayoutPack layoutPack, PackedScene scene, Node parent, bool assignLayoutPosition = false)
{
var manager = ManiaMapManager.Current;
var layout = manager.Layout;
var layoutState = manager.LayoutState;
var roomLayout = layout.Rooms[id];
var roomState = layoutState.RoomStates[id];
var doorConnections = manager.GetDoorConnections(id);
var collisionMask = manager.Settings.CellCollisionMask;
return CreateInstance(scene, parent, layout, layoutState, roomLayout, roomState, doorConnections, collisionMask, assignLayoutPosition);
var roomLayout = layoutPack.Layout.Rooms[id];
var roomState = layoutPack.LayoutState.RoomStates[id];
var collisionMask = layoutPack.Settings.CellCollisionMask;
return CreateInstance(scene, parent, layoutPack, roomLayout, roomState, collisionMask, assignLayoutPosition);
}

/// <summary>
/// Creates and initializes an instance of a room.
/// </summary>
/// <param name="scene">The room scene.</param>
/// <param name="parent">The node to which the room will be added as a child.</param>
/// <param name="layout">The full layout.</param>
/// <param name="layoutState">The full layout state.</param>
/// <param name="layoutPack">The layout pack..</param>
/// <param name="roomLayout">The room's layout.</param>
/// <param name="roomState">The room's state.</param>
/// <param name="doorConnections">The room's door connections.</param>
/// <param name="cellCollisionMask">The cell collision mask.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public static RoomNode2D CreateInstance(PackedScene scene, Node parent, Layout layout, LayoutState layoutState,
Room roomLayout, RoomState roomState, IReadOnlyList<DoorConnection> doorConnections,
uint cellCollisionMask, bool assignLayoutPosition)
public static RoomNode2D CreateInstance(PackedScene scene, Node parent, LayoutPack layoutPack,
Room roomLayout, RoomState roomState, uint cellCollisionMask, bool assignLayoutPosition)
{
var room = scene.Instantiate<RoomNode2D>();
room.Initialize(layout, layoutState, roomLayout, roomState, doorConnections, cellCollisionMask, assignLayoutPosition);
room.Initialize(layoutPack, roomLayout, roomState, cellCollisionMask, assignLayoutPosition);
parent.AddChild(room);
return room;
}
Expand All @@ -226,24 +213,20 @@ public static RoomNode2D CreateInstance(PackedScene scene, Node parent, Layout l
/// If the room has already been initialized, no action is taken, and this method returns false.
/// Othwerwise, it returns true.
/// </summary>
/// <param name="layout">The full layout.</param>
/// <param name="layoutState">The full layout state.</param>
/// <param name="layoutPack">The layout pack.</param>
/// <param name="roomLayout">The room's layout.</param>
/// <param name="roomState">The room's state.</param>
/// <param name="doorConnections">The room's door connections.</param>
/// <param name="cellCollisionMask">The cell collision mask.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public bool Initialize(Layout layout, LayoutState layoutState, Room roomLayout, RoomState roomState,
IReadOnlyList<DoorConnection> doorConnections, uint cellCollisionMask, bool assignLayoutPosition)
public bool Initialize(LayoutPack layoutPack, Room roomLayout, RoomState roomState,
uint cellCollisionMask, bool assignLayoutPosition)
{
if (IsInitialized)
return false;

Layout = layout;
LayoutState = layoutState;
LayoutPack = layoutPack;
RoomLayout = roomLayout;
RoomState = roomState;
DoorConnections = doorConnections;

if (assignLayoutPosition)
MoveToLayoutPosition();
Expand Down
39 changes: 13 additions & 26 deletions addons/mpewsey.maniamap/scripts/runtime/RoomNode3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using MPewsey.ManiaMap;
using MPewsey.ManiaMapGodot.Exceptions;
using System;
using System.Collections.Generic;

namespace MPewsey.ManiaMapGodot
{
Expand Down Expand Up @@ -59,20 +58,14 @@ public partial class RoomNode3D : Node3D, IRoomNode
[Export] public Godot.Collections.Array<Godot.Collections.Array<bool>> ActiveCells { get; set; } = new Godot.Collections.Array<Godot.Collections.Array<bool>>();

/// <inheritdoc/>
public Layout Layout { get; private set; }

/// <inheritdoc/>
public LayoutState LayoutState { get; private set; }
public LayoutPack LayoutPack { get; private set; }

/// <inheritdoc/>
public Room RoomLayout { get; private set; }

/// <inheritdoc/>
public RoomState RoomState { get; private set; }

/// <inheritdoc/>
public IReadOnlyList<DoorConnection> DoorConnections { get; private set; } = Array.Empty<DoorConnection>();

/// <inheritdoc/>
public bool IsInitialized { get; private set; }

Expand Down Expand Up @@ -200,19 +193,16 @@ private static bool MouseIsInsideMainScreen()
/// The layout and layout state are assigned to the room based on the current ManiaMapManager.
/// </summary>
/// <param name="id">The room ID.</param>
/// <param name="layoutPack">The layout pack.</param>
/// <param name="scene">The room scene.</param>
/// <param name="parent">The node to which the room will be added as a child.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public static RoomNode3D CreateInstance(Uid id, PackedScene scene, Node parent, bool assignLayoutPosition = false)
public static RoomNode3D CreateInstance(Uid id, LayoutPack layoutPack, PackedScene scene, Node parent, bool assignLayoutPosition = false)
{
var manager = ManiaMapManager.Current;
var layout = manager.Layout;
var layoutState = manager.LayoutState;
var roomLayout = layout.Rooms[id];
var roomState = layoutState.RoomStates[id];
var doorConnections = manager.GetDoorConnections(id);
var collisionMask = manager.Settings.CellCollisionMask;
return CreateInstance(scene, parent, layout, layoutState, roomLayout, roomState, doorConnections, collisionMask, assignLayoutPosition);
var roomLayout = layoutPack.Layout.Rooms[id];
var roomState = layoutPack.LayoutState.RoomStates[id];
var collisionMask = layoutPack.Settings.CellCollisionMask;
return CreateInstance(scene, parent, layoutPack, roomLayout, roomState, collisionMask, assignLayoutPosition);
}

/// <summary>
Expand All @@ -227,12 +217,11 @@ public static RoomNode3D CreateInstance(Uid id, PackedScene scene, Node parent,
/// <param name="doorConnections">The room's door connections.</param>
/// <param name="cellCollisionMask">The cell collision mask.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public static RoomNode3D CreateInstance(PackedScene scene, Node parent, Layout layout, LayoutState layoutState,
Room roomLayout, RoomState roomState, IReadOnlyList<DoorConnection> doorConnections,
uint cellCollisionMask, bool assignLayoutPosition)
public static RoomNode3D CreateInstance(PackedScene scene, Node parent, LayoutPack layoutPack,
Room roomLayout, RoomState roomState, uint cellCollisionMask, bool assignLayoutPosition)
{
var room = scene.Instantiate<RoomNode3D>();
room.Initialize(layout, layoutState, roomLayout, roomState, doorConnections, cellCollisionMask, assignLayoutPosition);
room.Initialize(layoutPack, roomLayout, roomState, cellCollisionMask, assignLayoutPosition);
parent.AddChild(room);
return room;
}
Expand All @@ -251,17 +240,15 @@ public static RoomNode3D CreateInstance(PackedScene scene, Node parent, Layout l
/// <param name="doorConnections">The room's door connections.</param>
/// <param name="cellCollisionMask">The cell collision mask.</param>
/// <param name="assignLayoutPosition">If true, the position of the room will be set to that of the room layout. Otherwise, it will be initialized at its original position.</param>
public bool Initialize(Layout layout, LayoutState layoutState, Room roomLayout, RoomState roomState,
IReadOnlyList<DoorConnection> doorConnections, uint cellCollisionMask, bool assignLayoutPosition)
public bool Initialize(LayoutPack layoutPack, Room roomLayout, RoomState roomState,
uint cellCollisionMask, bool assignLayoutPosition)
{
if (IsInitialized)
return false;

Layout = layout;
LayoutState = layoutState;
LayoutPack = layoutPack;
RoomLayout = roomLayout;
RoomState = roomState;
DoorConnections = doorConnections;

if (assignLayoutPosition)
MoveToLayoutPosition();
Expand Down
Loading

0 comments on commit 9e0c396

Please sign in to comment.