[Fix] ECS

This commit is contained in:
2026-06-07 01:12:10 +07:00
parent 285c11597a
commit 5a58c9031a
16 changed files with 225 additions and 36 deletions
+47 -3
View File
@@ -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)
@@ -17,6 +17,7 @@ namespace Minesweeper.Presentation.Views
void Hide();
void Rebuild(IReadOnlyList<BoardCellData> cells, int width, int height, ICellViewFactory cellViewFactory, bool revealUnflaggedMines);
void Refresh(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines);
void RefreshCells(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines);
void SetInputEnabled(bool enabled);
}
}
@@ -16,6 +16,7 @@ namespace Minesweeper.Presentation.Views
public void Hide() { }
public void Rebuild(IReadOnlyList<BoardCellData> cells, int width, int height, ICellViewFactory cellViewFactory, bool revealUnflaggedMines) { }
public void Refresh(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines) { }
public void RefreshCells(IReadOnlyList<BoardCellData> cells, bool revealUnflaggedMines) { }
public void SetInputEnabled(bool enabled) { }
}
}