Files
YachtDice/Assets/Scripts/UI/Presentation/DicePanelPresenter.cs
T
2026-03-18 09:13:48 +07:00

94 lines
2.8 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);
}
public void SetRollingEnabled(bool enabled)
{
_view.SetRollButtonInteractable(enabled);
_view.SetDiceInteractable(enabled && _gameLoopController.CurrentRoll > 0);
}
private void HandleRollClicked()
{
RollClicked?.Invoke();
}
private void HandleDiceToggled(int index)
{
DiceToggled?.Invoke(index);
}
private void HandleDiceSettled(int index, int value)
{
_view.SetDiceValue(index, value);
}
}
}