Skip to content

Commit

Permalink
Change room state cell visibility from hash set to boolean array (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpewsey authored Sep 9, 2022
1 parent b4330fd commit 02e20e4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
7 changes: 3 additions & 4 deletions src/ManiaMap.Tests/Drawing/TestLayoutMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ public void TestSaveFilteredLLoopLayoutImages()
{
for (int j = 0; j < cells.Columns; j++)
{
if (random.NextDouble() > 0.3)
{
roomState.VisibleIndexes.Add(new Vector2DInt(i, j));
}
var index = new Vector2DInt(i, j);
var visibility = random.NextDouble() > 0.3;
Assert.IsTrue(roomState.SetCellVisibility(index, visibility));
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ManiaMap/Drawing/LayoutMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private void DrawMapTiles(IImageProcessingContext image, int z)
continue;

// If room state is defined and is not visible, go to next cell.
if (roomState != null && !roomState.VisibleIndexes.Contains(position))
if (roomState != null && !roomState.CellIsVisible(position))
continue;

// Calculate draw position
Expand Down
2 changes: 1 addition & 1 deletion src/ManiaMap/ManiaMap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<RepositoryUrl>https://github.com/mpewsey/ManiaMap</RepositoryUrl>
<PackageTags>procedural-generation;roguelike;metroidvania;videogames</PackageTags>
<PackageLicenseExpression>BSD-3-Clause</PackageLicenseExpression>
<VersionPrefix>2.0.3</VersionPrefix>
<VersionPrefix>2.1.0</VersionPrefix>
<Configurations>Debug;Release;Unity</Configurations>
<PackageProjectUrl>https://mpewsey.github.io/ManiaMap/</PackageProjectUrl>
</PropertyGroup>
Expand Down
51 changes: 49 additions & 2 deletions src/ManiaMap/RoomState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public class RoomState
public Uid Id { get; private set; }

/// <summary>
/// A set of visible room cell indexes.
/// An array of room cell visibilities.
/// </summary>
[DataMember(Order = 2)]
public HashSet<Vector2DInt> VisibleIndexes { get; private set; } = new HashSet<Vector2DInt>();
public Array2D<bool> VisibleCells { get; private set; }

/// <summary>
/// A set of acquired collectable location ID's.
Expand All @@ -40,6 +40,53 @@ public class RoomState
public RoomState(Room room)
{
Id = room.Id;
VisibleCells = new Array2D<bool>(room.Template.Cells.Rows, room.Template.Cells.Columns);
}

/// <summary>
/// Returns true if the specified index is within bounds and the cell is visible.
/// </summary>
/// <param name="index">The cell index.</param>
public bool CellIsVisible(Vector2DInt index)
{
return CellIsVisible(index.X, index.Y);
}

/// <summary>
/// Returns true if the specified index is within bounds and the cell is visible.
/// </summary>
/// <param name="row">The row index.</param>
/// <param name="column">The column index.</param>
public bool CellIsVisible(int row, int column)
{
return VisibleCells.GetOrDefault(row, column, false);
}

/// <summary>
/// Sets the visibility of a cell. Returns true if the index is within bounds.
/// </summary>
/// <param name="index">The cell index.</param>
/// <param name="value">The visibility.</param>
public bool SetCellVisibility(Vector2DInt index, bool value)
{
return SetCellVisibility(index.X, index.Y, value);
}

/// <summary>
/// Sets the visibility of a cell. Returns true if the index is within bounds.
/// </summary>
/// <param name="row">The row index.</param>
/// <param name="column">The column index.</param>
/// <param name="value">The visibility.</param>
public bool SetCellVisibility(int row, int column, bool value)
{
if (VisibleCells.IndexExists(row, column))
{
VisibleCells[row, column] = value;
return true;
}

return false;
}
}
}

0 comments on commit 02e20e4

Please sign in to comment.