[Fix] ECS
This commit is contained in:
@@ -25,6 +25,7 @@ namespace Minesweeper.Presentation.Views
|
||||
private readonly Dictionary<int, CellView> cellsByCoordinate = new Dictionary<int, CellView>();
|
||||
private readonly Stack<CellView> pooledCells = new Stack<CellView>();
|
||||
private IReadOnlyList<BoardCellData> currentCells;
|
||||
private readonly List<BoardCellData> currentCellsCache = new List<BoardCellData>();
|
||||
private bool inputEnabled = true;
|
||||
private bool currentRevealUnflaggedMines;
|
||||
private bool resizeRefreshPending;
|
||||
@@ -82,7 +83,7 @@ namespace Minesweeper.Presentation.Views
|
||||
Clear();
|
||||
currentBoardWidth = width;
|
||||
currentBoardHeight = height;
|
||||
currentCells = cells;
|
||||
CacheCurrentCells(cells);
|
||||
currentRevealUnflaggedMines = revealUnflaggedMines;
|
||||
var layoutWasEnabled = gridLayoutGroup != null && gridLayoutGroup.enabled;
|
||||
SetGridLayoutEnabled(false);
|
||||
@@ -109,12 +110,23 @@ namespace Minesweeper.Presentation.Views
|
||||
|
||||
public void Refresh(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines)
|
||||
{
|
||||
currentCells = cells;
|
||||
CacheCurrentCells(cells);
|
||||
currentRevealUnflaggedMines = revealUnflaggedMines;
|
||||
|
||||
RefreshCells(cells, revealUnflaggedMines);
|
||||
}
|
||||
|
||||
public void RefreshCells(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines)
|
||||
{
|
||||
if (cells == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < cells.Count; i++)
|
||||
{
|
||||
var cell = cells[i];
|
||||
UpdateCachedCell(cell);
|
||||
if (cellsByCoordinate.TryGetValue(ToKey(cell.X, cell.Y), out var view))
|
||||
{
|
||||
view.Render(cell, uiConfig, currentPixelsPerUnitMultiplier, currentContentPadding, revealUnflaggedMines);
|
||||
@@ -250,12 +262,19 @@ namespace Minesweeper.Presentation.Views
|
||||
{
|
||||
view = pooledCells.Pop();
|
||||
view.transform.SetParent(gridLayoutGroup.transform, false);
|
||||
view.transform.SetAsLastSibling();
|
||||
view.Initialize(cell.X, cell.Y);
|
||||
view.gameObject.SetActive(true);
|
||||
return view;
|
||||
}
|
||||
|
||||
return cellViewFactory.CreateCell(cell, gridLayoutGroup.transform);
|
||||
view = cellViewFactory.CreateCell(cell, gridLayoutGroup.transform);
|
||||
if (view != null)
|
||||
{
|
||||
view.transform.SetAsLastSibling();
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
private void Clear()
|
||||
@@ -273,6 +292,31 @@ namespace Minesweeper.Presentation.Views
|
||||
}
|
||||
|
||||
cellsByCoordinate.Clear();
|
||||
currentCellsCache.Clear();
|
||||
currentCells = null;
|
||||
}
|
||||
|
||||
private void CacheCurrentCells(IReadOnlyList<BoardCellData> cells)
|
||||
{
|
||||
currentCellsCache.Clear();
|
||||
if (cells != null)
|
||||
{
|
||||
for (var i = 0; i < cells.Count; i++)
|
||||
{
|
||||
currentCellsCache.Add(cells[i]);
|
||||
}
|
||||
}
|
||||
|
||||
currentCells = currentCellsCache;
|
||||
}
|
||||
|
||||
private void UpdateCachedCell(BoardCellData cell)
|
||||
{
|
||||
var index = cell.Y * currentBoardWidth + cell.X;
|
||||
if (index >= 0 && index < currentCellsCache.Count)
|
||||
{
|
||||
currentCellsCache[index] = cell;
|
||||
}
|
||||
}
|
||||
|
||||
private void PoolCell(CellView cell)
|
||||
|
||||
Reference in New Issue
Block a user