108 lines
3.4 KiB
C#
108 lines
3.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
using YachtDice.Categories;
|
|
using YachtDice.Scoring;
|
|
|
|
namespace YachtDice.Game
|
|
{
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[SerializeField] private int maxRollsPerTurn = 3;
|
|
|
|
private DiceManager _diceManager;
|
|
private ScoringSystem _scoringSystem;
|
|
|
|
public int CurrentRoll { get; private set; }
|
|
public int CurrentTurn { get; private set; }
|
|
|
|
public bool CanRoll => CurrentRoll < maxRollsPerTurn && !_diceManager.IsAnyRolling;
|
|
public bool CanScore => CurrentRoll > 0 && !_diceManager.IsAnyRolling;
|
|
public bool IsGameOver => _scoringSystem.IsComplete;
|
|
|
|
public event Action<int> OnTurnStarted;
|
|
public event Action<int> OnRollComplete;
|
|
public event Action<CategoryDefinition, int> OnScored;
|
|
public event Action<int> OnGameOver;
|
|
|
|
[Inject]
|
|
public void Construct(DiceManager diceManager, ScoringSystem scoringSystem)
|
|
{
|
|
this._diceManager = diceManager;
|
|
this._scoringSystem = scoringSystem;
|
|
}
|
|
|
|
public void StartNewGame()
|
|
{
|
|
_scoringSystem.ResetScorecard();
|
|
CurrentTurn = 0;
|
|
StartNewTurn();
|
|
}
|
|
|
|
private void StartNewTurn()
|
|
{
|
|
CurrentTurn++;
|
|
CurrentRoll = 0;
|
|
_diceManager.UnlockAll();
|
|
OnTurnStarted?.Invoke(CurrentTurn);
|
|
Debug.Log($"=== Turn {CurrentTurn} ===");
|
|
}
|
|
|
|
public void Roll()
|
|
{
|
|
if (!CanRoll) return;
|
|
|
|
CurrentRoll++;
|
|
_diceManager.OnAllDiceSettled += HandleAllDiceSettled;
|
|
_diceManager.RollUnlocked();
|
|
}
|
|
|
|
private void HandleAllDiceSettled()
|
|
{
|
|
_diceManager.OnAllDiceSettled -= HandleAllDiceSettled;
|
|
|
|
var values = _diceManager.GetCurrentValues();
|
|
Debug.Log($"Roll {CurrentRoll}/{maxRollsPerTurn} | Dice: [{string.Join(", ", values)}]");
|
|
|
|
OnRollComplete?.Invoke(CurrentRoll);
|
|
}
|
|
|
|
public void ToggleDiceLock(int index)
|
|
{
|
|
if (_diceManager.IsAnyRolling) return;
|
|
if (CurrentRoll == 0) return;
|
|
_diceManager.ToggleLock(index);
|
|
|
|
var isLocked = _diceManager.IsLocked(index);
|
|
Debug.Log($"Dice {index + 1} (value={_diceManager.GetValue(index)}): {(isLocked ? "LOCKED" : "UNLOCKED")}");
|
|
}
|
|
|
|
public void ScoreInCategory(CategoryDefinition category)
|
|
{
|
|
if (!CanScore) return;
|
|
if (_scoringSystem.IsCategoryUsed(category)) return;
|
|
|
|
var dice = _diceManager.GetDice();
|
|
ScoreResult result = _scoringSystem.ScoreCategory(dice, category);
|
|
|
|
Debug.Log($"Scored {category.DisplayName}: base={result.baseScore}, " +
|
|
$"bonus=+{result.flatBonus}, mult=x{result.multiplier:F1}, " +
|
|
$"FINAL={result.FinalScore} | Total={_scoringSystem.TotalScore}");
|
|
|
|
OnScored?.Invoke(category, result.FinalScore);
|
|
|
|
if (_scoringSystem.IsComplete)
|
|
{
|
|
var total = _scoringSystem.TotalScore;
|
|
Debug.Log($"*** GAME OVER *** Total Score: {total}");
|
|
OnGameOver?.Invoke(total);
|
|
}
|
|
else
|
|
{
|
|
StartNewTurn();
|
|
}
|
|
}
|
|
}
|
|
}
|