[Add] UI, menu, pause and timer
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Minesweeper.Commands;
|
||||
using Minesweeper.Core;
|
||||
using Minesweeper.Presentation.ReadModels;
|
||||
using Minesweeper.Presentation.Views;
|
||||
|
||||
@@ -7,24 +8,39 @@ namespace Minesweeper.Presentation.Presenters
|
||||
public sealed class GamePresenter : IPresenter
|
||||
{
|
||||
private readonly IGameCommandDispatcher commandDispatcher;
|
||||
private readonly IGamePauseService pauseService;
|
||||
private readonly IGameReadModel readModel;
|
||||
private readonly IGameStateService gameStateService;
|
||||
private readonly IGameTimerService timerService;
|
||||
private readonly IGameView view;
|
||||
private bool boardBuilt;
|
||||
|
||||
public GamePresenter(IGameCommandDispatcher commandDispatcher, IGameReadModel readModel, IGameView view = null)
|
||||
public GamePresenter(IGameCommandDispatcher commandDispatcher, IGamePauseService pauseService, IGameReadModel readModel, IGameStateService gameStateService, IGameTimerService timerService, IGameView view = null)
|
||||
{
|
||||
this.commandDispatcher = commandDispatcher;
|
||||
this.pauseService = pauseService;
|
||||
this.readModel = readModel;
|
||||
this.gameStateService = gameStateService;
|
||||
this.timerService = timerService;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_ = readModel.State;
|
||||
|
||||
if (view != null)
|
||||
{
|
||||
view.RestartRequested += OnRestartRequested;
|
||||
view.GoToMenuRequested += OnGoToMenuRequested;
|
||||
view.PauseRequested += OnPauseRequested;
|
||||
view.ResumeRequested += OnResumeRequested;
|
||||
view.CellOpenRequested += OnCellOpenRequested;
|
||||
view.CellFlagRequested += OnCellFlagRequested;
|
||||
gameStateService.StateChanged += OnStateChanged;
|
||||
pauseService.PauseChanged += OnPauseChanged;
|
||||
timerService.TimeChanged += OnTimeChanged;
|
||||
OnStateChanged(gameStateService.Current);
|
||||
OnPauseChanged(pauseService.IsPaused);
|
||||
OnTimeChanged(timerService.ElapsedSeconds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,17 +50,112 @@ namespace Minesweeper.Presentation.Presenters
|
||||
{
|
||||
view.RestartRequested -= OnRestartRequested;
|
||||
view.GoToMenuRequested -= OnGoToMenuRequested;
|
||||
view.PauseRequested -= OnPauseRequested;
|
||||
view.ResumeRequested -= OnResumeRequested;
|
||||
view.CellOpenRequested -= OnCellOpenRequested;
|
||||
view.CellFlagRequested -= OnCellFlagRequested;
|
||||
gameStateService.StateChanged -= OnStateChanged;
|
||||
pauseService.PauseChanged -= OnPauseChanged;
|
||||
timerService.TimeChanged -= OnTimeChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRestartRequested()
|
||||
{
|
||||
commandDispatcher.Dispatch(new RestartCommand());
|
||||
RebuildBoard();
|
||||
}
|
||||
|
||||
private void OnGoToMenuRequested()
|
||||
{
|
||||
commandDispatcher.Dispatch(new GoToMenuCommand());
|
||||
}
|
||||
|
||||
private void OnPauseRequested()
|
||||
{
|
||||
commandDispatcher.Dispatch(new PauseCommand());
|
||||
}
|
||||
|
||||
private void OnResumeRequested()
|
||||
{
|
||||
commandDispatcher.Dispatch(new ResumeCommand());
|
||||
}
|
||||
|
||||
private void OnCellOpenRequested(int x, int y)
|
||||
{
|
||||
commandDispatcher.Dispatch(new OpenCellCommand(x, y));
|
||||
RefreshBoard();
|
||||
UpdateBoardInput();
|
||||
}
|
||||
|
||||
private void OnCellFlagRequested(int x, int y)
|
||||
{
|
||||
commandDispatcher.Dispatch(new ToggleFlagCommand(x, y));
|
||||
RefreshBoard();
|
||||
UpdateBoardInput();
|
||||
}
|
||||
|
||||
private void OnStateChanged(GameState state)
|
||||
{
|
||||
if (state == GameState.FieldSelection)
|
||||
{
|
||||
boardBuilt = false;
|
||||
view.HideGame();
|
||||
view.HidePause();
|
||||
return;
|
||||
}
|
||||
|
||||
view.ShowGame();
|
||||
|
||||
if (!boardBuilt || state == GameState.Preparing)
|
||||
{
|
||||
RebuildBoard();
|
||||
}
|
||||
else
|
||||
{
|
||||
RefreshBoard();
|
||||
}
|
||||
|
||||
UpdateBoardInput();
|
||||
}
|
||||
|
||||
private void OnPauseChanged(bool isPaused)
|
||||
{
|
||||
if (isPaused)
|
||||
{
|
||||
view.ShowPause();
|
||||
}
|
||||
else
|
||||
{
|
||||
view.HidePause();
|
||||
}
|
||||
|
||||
UpdateBoardInput();
|
||||
}
|
||||
|
||||
private void OnTimeChanged(float seconds)
|
||||
{
|
||||
view.SetTimer(seconds);
|
||||
}
|
||||
|
||||
private void RebuildBoard()
|
||||
{
|
||||
var cells = readModel.GetCells();
|
||||
view.SetMineCount(readModel.MinesCount);
|
||||
view.RebuildBoard(cells, readModel.Width, readModel.Height);
|
||||
boardBuilt = true;
|
||||
UpdateBoardInput();
|
||||
}
|
||||
|
||||
private void RefreshBoard()
|
||||
{
|
||||
view.RefreshBoard(readModel.GetCells());
|
||||
}
|
||||
|
||||
private void UpdateBoardInput()
|
||||
{
|
||||
var state = gameStateService.Current;
|
||||
view.SetBoardInputEnabled(!pauseService.IsPaused && (state == GameState.Preparing || state == GameState.Playing));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Minesweeper.Commands;
|
||||
using Minesweeper.Core;
|
||||
using Minesweeper.Presentation.Views;
|
||||
|
||||
namespace Minesweeper.Presentation.Presenters
|
||||
@@ -6,11 +7,13 @@ namespace Minesweeper.Presentation.Presenters
|
||||
public sealed class MainMenuPresenter : IPresenter
|
||||
{
|
||||
private readonly IGameCommandDispatcher commandDispatcher;
|
||||
private readonly IGameStateService gameStateService;
|
||||
private readonly IMainMenuView view;
|
||||
|
||||
public MainMenuPresenter(IGameCommandDispatcher commandDispatcher, IMainMenuView view = null)
|
||||
public MainMenuPresenter(IGameCommandDispatcher commandDispatcher, IGameStateService gameStateService, IMainMenuView view = null)
|
||||
{
|
||||
this.commandDispatcher = commandDispatcher;
|
||||
this.gameStateService = gameStateService;
|
||||
this.view = view;
|
||||
}
|
||||
|
||||
@@ -19,6 +22,8 @@ namespace Minesweeper.Presentation.Presenters
|
||||
if (view != null)
|
||||
{
|
||||
view.StartClicked += OnStartClicked;
|
||||
gameStateService.StateChanged += OnStateChanged;
|
||||
OnStateChanged(gameStateService.Current);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +32,7 @@ namespace Minesweeper.Presentation.Presenters
|
||||
if (view != null)
|
||||
{
|
||||
view.StartClicked -= OnStartClicked;
|
||||
gameStateService.StateChanged -= OnStateChanged;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,5 +40,17 @@ namespace Minesweeper.Presentation.Presenters
|
||||
{
|
||||
commandDispatcher.Dispatch(new StartGameCommand());
|
||||
}
|
||||
|
||||
private void OnStateChanged(GameState state)
|
||||
{
|
||||
if (state == GameState.FieldSelection)
|
||||
{
|
||||
view.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
view.Hide();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user