[Add] Playful interaction with cells
This commit is contained in:
@@ -55,26 +55,78 @@ namespace Minesweeper.Commands
|
||||
|
||||
public void Handle(OpenCellCommand command)
|
||||
{
|
||||
if (gameStateService.Current != GameState.Preparing)
|
||||
var state = gameStateService.Current;
|
||||
if (state != GameState.Preparing && state != GameState.Playing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!boardService.GenerateAfterFirstClick(command.X, command.Y))
|
||||
if (state == GameState.Preparing)
|
||||
{
|
||||
if (boardService.TryGetCell(command.X, command.Y, out var cell) && cell.IsFlagged)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!boardService.GenerateAfterFirstClick(command.X, command.Y))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var result = boardService.OpenCell(command.X, command.Y);
|
||||
if (result.Invalid || !result.Changed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gameStateService.SetState(GameState.Playing);
|
||||
if (result.HitMine)
|
||||
{
|
||||
gameStateService.SetState(GameState.Lost);
|
||||
}
|
||||
else if (result.Won)
|
||||
{
|
||||
gameStateService.SetState(GameState.Won);
|
||||
}
|
||||
else if (state == GameState.Preparing)
|
||||
{
|
||||
gameStateService.SetState(GameState.Playing);
|
||||
}
|
||||
|
||||
boardEcsSyncService.SyncBoard(boardService);
|
||||
boardEcsSyncService.SyncGameState(gameStateService.Current, true);
|
||||
boardEcsSyncService.SyncGameState(gameStateService.Current, boardService.IsGenerated);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class ToggleFlagCommandHandler : IGameCommandHandler<ToggleFlagCommand>
|
||||
{
|
||||
private readonly IBoardEcsSyncService boardEcsSyncService;
|
||||
private readonly IBoardService boardService;
|
||||
private readonly IGameStateService gameStateService;
|
||||
|
||||
public ToggleFlagCommandHandler(IBoardService boardService, IBoardEcsSyncService boardEcsSyncService, IGameStateService gameStateService)
|
||||
{
|
||||
this.boardService = boardService;
|
||||
this.boardEcsSyncService = boardEcsSyncService;
|
||||
this.gameStateService = gameStateService;
|
||||
}
|
||||
|
||||
public void Handle(ToggleFlagCommand command)
|
||||
{
|
||||
var state = gameStateService.Current;
|
||||
if (state != GameState.Preparing && state != GameState.Playing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var result = boardService.ToggleFlag(command.X, command.Y);
|
||||
if (result.Invalid || !result.Changed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
boardEcsSyncService.SyncBoard(boardService);
|
||||
boardEcsSyncService.SyncGameState(gameStateService.Current, boardService.IsGenerated);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user