88 lines
2.6 KiB
C#
88 lines
2.6 KiB
C#
using System;
|
|
using YachtDice.Game;
|
|
|
|
namespace YachtDice.UI.Presentation
|
|
{
|
|
public sealed class DicePanelPresenter : IDisposable
|
|
{
|
|
private readonly DicePanelView _view;
|
|
private readonly GameLoopController _gameLoopController;
|
|
private readonly DiceManager _diceManager;
|
|
|
|
public event Action RollClicked;
|
|
public event Action<int> DiceToggled;
|
|
|
|
public DicePanelPresenter(DicePanelView view, GameLoopController gameLoopController, DiceManager diceManager)
|
|
{
|
|
_view = view;
|
|
_gameLoopController = gameLoopController;
|
|
_diceManager = diceManager;
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
_view.OnRollClicked += HandleRollClicked;
|
|
_view.OnDiceToggled += HandleDiceToggled;
|
|
_diceManager.OnDiceSettled += HandleDiceSettled;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_view.OnRollClicked -= HandleRollClicked;
|
|
_view.OnDiceToggled -= HandleDiceToggled;
|
|
_diceManager.OnDiceSettled -= HandleDiceSettled;
|
|
}
|
|
|
|
public void ResetForNewTurn()
|
|
{
|
|
_view.ResetForNewTurn();
|
|
_view.SetRollButtonState(true, 0, _gameLoopController.MaxRollsPerTurn);
|
|
}
|
|
|
|
public void PrepareForRoll()
|
|
{
|
|
_view.SetRollButtonState(false, _gameLoopController.CurrentRoll, _gameLoopController.MaxRollsPerTurn);
|
|
_view.SetDiceInteractable(false);
|
|
}
|
|
|
|
public void HandleRollComplete(int rollNumber)
|
|
{
|
|
var canRollAgain = _gameLoopController.CanRoll;
|
|
_view.SetRollButtonState(canRollAgain, rollNumber, _gameLoopController.MaxRollsPerTurn);
|
|
_view.SetDiceInteractable(true);
|
|
_view.SetAllDiceValues(_diceManager.GetCurrentValues());
|
|
}
|
|
|
|
public void HandleGameOver()
|
|
{
|
|
_view.SetRollButtonState(false, _gameLoopController.MaxRollsPerTurn, _gameLoopController.MaxRollsPerTurn);
|
|
_view.SetDiceInteractable(false);
|
|
}
|
|
|
|
public void ResetForNewGame()
|
|
{
|
|
_view.ResetForNewGame(_gameLoopController.MaxRollsPerTurn);
|
|
}
|
|
|
|
public void SetDiceLocked(int index, bool isLocked)
|
|
{
|
|
_view.SetDiceLocked(index, isLocked);
|
|
}
|
|
|
|
private void HandleRollClicked()
|
|
{
|
|
RollClicked?.Invoke();
|
|
}
|
|
|
|
private void HandleDiceToggled(int index)
|
|
{
|
|
DiceToggled?.Invoke(index);
|
|
}
|
|
|
|
private void HandleDiceSettled(int index, int value)
|
|
{
|
|
_view.SetDiceValue(index, value);
|
|
}
|
|
}
|
|
}
|