[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
@@ -70,7 +70,7 @@ namespace Minesweeper.Presentation.Presenters
{
topPanelPresenter.SetCellPressActive(false);
commandDispatcher.Dispatch(new OpenCellCommand(x, y));
RefreshBoard();
RefreshChangedCellsOrBoard();
topPanelPresenter.RefreshCounters();
UpdateBoardInput();
}
@@ -78,7 +78,7 @@ namespace Minesweeper.Presentation.Presenters
private void OnCellFlagRequested(int x, int y)
{
commandDispatcher.Dispatch(new ToggleFlagCommand(x, y));
RefreshBoard();
RefreshChangedCellsOrBoard();
topPanelPresenter.RefreshCounters();
UpdateBoardInput();
}
@@ -156,6 +156,17 @@ namespace Minesweeper.Presentation.Presenters
boardView.Refresh(readModel.GetCells(), IsFinalState());
}
private void RefreshChangedCellsOrBoard()
{
if (IsFinalState())
{
RefreshBoard();
return;
}
boardView.RefreshCells(readModel.GetChangedCells(), false);
}
private bool IsFinalState()
{
var state = gameStateService.Current;
@@ -20,7 +20,7 @@ namespace Minesweeper.Presentation.ReadModels
public int Width => boardService.Width > 0 ? boardService.Width : settingsService.SizeX;
public int Height => boardService.Height > 0 ? boardService.Height : settingsService.SizeY;
public int MinesCount => boardService.MinesCount > 0 ? boardService.MinesCount : settingsService.MinesCount;
public int FlaggedCellsCount => CountFlaggedCells();
public int FlaggedCellsCount => boardService.FlaggedCellsCount;
public int RemainingMinesCount => MinesCount - FlaggedCellsCount;
public bool TryGetCell(int x, int y, out BoardCellData cell)
@@ -33,19 +33,10 @@ namespace Minesweeper.Presentation.ReadModels
return boardService.GetCells();
}
private int CountFlaggedCells()
public IReadOnlyList<BoardCellData> GetChangedCells()
{
var cells = boardService.GetCells();
var count = 0;
for (var i = 0; i < cells.Count; i++)
{
if (cells[i].IsFlagged)
{
count++;
}
}
return count;
return boardService.LastChangedCells;
}
}
}
@@ -14,5 +14,6 @@ namespace Minesweeper.Presentation.ReadModels
bool TryGetCell(int x, int y, out BoardCellData cell);
IReadOnlyList<BoardCellData> GetCells();
IReadOnlyList<BoardCellData> GetChangedCells();
}
}
+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) { }
}
}