[Fix] Code visual

This commit is contained in:
2026-02-28 19:25:26 +07:00
parent bee20fd1f8
commit e24b30743b
39 changed files with 2611 additions and 2687 deletions
-39
View File
@@ -1,39 +0,0 @@
using UnityEngine;
using YachtDice.Scoring;
namespace YachtDice.Game
{
public sealed class DebugGameInput : MonoBehaviour
{
[SerializeField] private GameManager gameManager;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
gameManager.Roll();
// 1-5: toggle lock on dice 0-4
if (Input.GetKeyDown(KeyCode.Alpha1)) gameManager.ToggleDiceLock(0);
if (Input.GetKeyDown(KeyCode.Alpha2)) gameManager.ToggleDiceLock(1);
if (Input.GetKeyDown(KeyCode.Alpha3)) gameManager.ToggleDiceLock(2);
if (Input.GetKeyDown(KeyCode.Alpha4)) gameManager.ToggleDiceLock(3);
if (Input.GetKeyDown(KeyCode.Alpha5)) gameManager.ToggleDiceLock(4);
// Score categories
if (Input.GetKeyDown(KeyCode.F1)) gameManager.ScoreInCategory(YachtCategory.Ones);
if (Input.GetKeyDown(KeyCode.F2)) gameManager.ScoreInCategory(YachtCategory.Twos);
if (Input.GetKeyDown(KeyCode.F3)) gameManager.ScoreInCategory(YachtCategory.Threes);
if (Input.GetKeyDown(KeyCode.F4)) gameManager.ScoreInCategory(YachtCategory.Fours);
if (Input.GetKeyDown(KeyCode.F5)) gameManager.ScoreInCategory(YachtCategory.Fives);
if (Input.GetKeyDown(KeyCode.F6)) gameManager.ScoreInCategory(YachtCategory.Sixes);
if (Input.GetKeyDown(KeyCode.F7)) gameManager.ScoreInCategory(YachtCategory.ThreeOfAKind);
if (Input.GetKeyDown(KeyCode.F8)) gameManager.ScoreInCategory(YachtCategory.FourOfAKind);
if (Input.GetKeyDown(KeyCode.F9)) gameManager.ScoreInCategory(YachtCategory.FullHouse);
if (Input.GetKeyDown(KeyCode.F10)) gameManager.ScoreInCategory(YachtCategory.SmallStraight);
if (Input.GetKeyDown(KeyCode.F11)) gameManager.ScoreInCategory(YachtCategory.LargeStraight);
if (Input.GetKeyDown(KeyCode.F12)) gameManager.ScoreInCategory(YachtCategory.Yacht);
if (Input.GetKeyDown(KeyCode.C)) gameManager.ScoreInCategory(YachtCategory.Chance);
}
}
}
@@ -1,2 +0,0 @@
fileFormatVersion: 2
guid: 982f0996b85edfe419b07ddb36d6e235
+82 -83
View File
@@ -5,104 +5,103 @@ using YachtDice.Dice;
namespace YachtDice.Game
{
public sealed class DiceManager : MonoBehaviour
{
[SerializeField] private List<DiceRoller> diceRollers = new();
public event Action OnAllDiceSettled;
public event Action<int, int> OnDieSettled;
public int DiceCount => diceRollers.Count;
private bool[] locked;
private int[] currentValues;
private int pendingCount;
private void Awake()
public class DiceManager : MonoBehaviour
{
int count = diceRollers.Count;
locked = new bool[count];
currentValues = new int[count];
}
[SerializeField] private List<DiceRoller> diceRollers = new();
public bool IsLocked(int index) => locked[index];
public event Action OnAllDiceSettled;
public event Action<int, int> OnDieSettled;
public void ToggleLock(int index)
{
locked[index] = !locked[index];
}
public int DiceCount => diceRollers.Count;
public void SetLocked(int index, bool isLocked)
{
locked[index] = isLocked;
}
private bool[] locked;
private int[] currentValues;
private int pendingCount;
public void UnlockAll()
{
for (int i = 0; i < locked.Length; i++) locked[i] = false;
}
public void RollUnlocked()
{
for (int i = 0; i < diceRollers.Count; i++)
if (diceRollers[i].IsRolling) return;
pendingCount = 0;
for (int i = 0; i < diceRollers.Count; i++)
private void Awake()
{
if (locked[i]) continue;
pendingCount++;
int capturedIndex = i;
void Handler(int value)
{
diceRollers[capturedIndex].OnRollFinished -= Handler;
currentValues[capturedIndex] = value;
OnDieSettled?.Invoke(capturedIndex, value);
pendingCount--;
if (pendingCount <= 0)
OnAllDiceSettled?.Invoke();
}
diceRollers[i].OnRollFinished += Handler;
diceRollers[i].Roll();
int count = diceRollers.Count;
locked = new bool[count];
currentValues = new int[count];
}
if (pendingCount == 0)
OnAllDiceSettled?.Invoke();
}
public bool IsLocked(int index) => locked[index];
public int[] GetCurrentValues()
{
int[] copy = new int[currentValues.Length];
Array.Copy(currentValues, copy, currentValues.Length);
return copy;
}
public void ToggleLock(int index)
{
locked[index] = !locked[index];
}
public int GetValue(int index) => currentValues[index];
public void SetLocked(int index, bool isLocked)
{
locked[index] = isLocked;
}
public bool IsAnyRolling
{
get
public void UnlockAll()
{
for (int i = 0; i < locked.Length; i++) locked[i] = false;
}
public void RollUnlocked()
{
for (int i = 0; i < diceRollers.Count; i++)
if (diceRollers[i].IsRolling) return true;
return false;
}
}
if (diceRollers[i].IsRolling) return;
public void ReadAllValues()
{
for (int i = 0; i < diceRollers.Count; i++)
pendingCount = 0;
for (int i = 0; i < diceRollers.Count; i++)
{
if (locked[i]) continue;
pendingCount++;
int capturedIndex = i;
void Handler(int value)
{
diceRollers[capturedIndex].OnRollFinished -= Handler;
currentValues[capturedIndex] = value;
OnDieSettled?.Invoke(capturedIndex, value);
pendingCount--;
if (pendingCount <= 0)
OnAllDiceSettled?.Invoke();
}
diceRollers[i].OnRollFinished += Handler;
diceRollers[i].Roll();
}
if (pendingCount == 0)
OnAllDiceSettled?.Invoke();
}
public int[] GetCurrentValues()
{
var diceComponent = diceRollers[i].GetComponent<Dice>();
if (diceComponent != null && diceComponent.TryGetTopValue(out int val))
currentValues[i] = val;
int[] copy = new int[currentValues.Length];
Array.Copy(currentValues, copy, currentValues.Length);
return copy;
}
public int GetValue(int index) => currentValues[index];
public bool IsAnyRolling
{
get
{
for (int i = 0; i < diceRollers.Count; i++)
if (diceRollers[i].IsRolling) return true;
return false;
}
}
public void ReadAllValues()
{
for (int i = 0; i < diceRollers.Count; i++)
{
var diceComponent = diceRollers[i].GetComponent<Dice.Dice>();
if (diceComponent != null && diceComponent.TryGetTopValue(out int val))
currentValues[i] = val;
}
}
}
}
}
+85 -86
View File
@@ -4,102 +4,101 @@ using YachtDice.Scoring;
namespace YachtDice.Game
{
public sealed class GameManager : MonoBehaviour
{
[Header("References")]
[SerializeField] private DiceManager diceManager;
[SerializeField] private ScoringSystem scoringSystem;
[Header("Settings")]
[SerializeField] private int maxRollsPerTurn = 3;
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<YachtCategory, int> OnScored;
public event Action<int> OnGameOver;
private void Start()
public class GameManager : MonoBehaviour
{
StartNewGame();
}
[Header("References")]
[SerializeField] private DiceManager diceManager;
[SerializeField] private ScoringSystem scoringSystem;
public void StartNewGame()
{
scoringSystem.ResetScorecard();
CurrentTurn = 0;
StartNewTurn();
}
[Header("Settings")]
[SerializeField] private int maxRollsPerTurn = 3;
private void StartNewTurn()
{
CurrentTurn++;
CurrentRoll = 0;
diceManager.UnlockAll();
OnTurnStarted?.Invoke(CurrentTurn);
Debug.Log($"=== Turn {CurrentTurn} ===");
}
public int CurrentRoll { get; private set; }
public int CurrentTurn { get; private set; }
public void Roll()
{
if (!CanRoll) return;
public bool CanRoll => CurrentRoll < maxRollsPerTurn && !diceManager.IsAnyRolling;
public bool CanScore => CurrentRoll > 0 && !diceManager.IsAnyRolling;
public bool IsGameOver => scoringSystem.IsComplete;
CurrentRoll++;
diceManager.OnAllDiceSettled += HandleAllDiceSettled;
diceManager.RollUnlocked();
}
public event Action<int> OnTurnStarted;
public event Action<int> OnRollComplete;
public event Action<YachtCategory, int> OnScored;
public event Action<int> OnGameOver;
private void HandleAllDiceSettled()
{
diceManager.OnAllDiceSettled -= HandleAllDiceSettled;
int[] 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);
bool isLocked = diceManager.IsLocked(index);
Debug.Log($"Dice {index + 1} (value={diceManager.GetValue(index)}): {(isLocked ? "LOCKED" : "UNLOCKED")}");
}
public void ScoreInCategory(YachtCategory category)
{
if (!CanScore) return;
if (scoringSystem.IsCategoryUsed(category)) return;
int[] values = diceManager.GetCurrentValues();
ScoreResult result = scoringSystem.ScoreCategory(values, category);
Debug.Log($"Scored {category}: 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)
private void Start()
{
int total = scoringSystem.TotalScore;
Debug.Log($"*** GAME OVER *** Total Score: {total}");
OnGameOver?.Invoke(total);
StartNewGame();
}
else
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;
int[] 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);
bool isLocked = diceManager.IsLocked(index);
Debug.Log($"Dice {index + 1} (value={diceManager.GetValue(index)}): {(isLocked ? "LOCKED" : "UNLOCKED")}");
}
public void ScoreInCategory(YachtCategory category)
{
if (!CanScore) return;
if (scoringSystem.IsCategoryUsed(category)) return;
int[] values = diceManager.GetCurrentValues();
ScoreResult result = scoringSystem.ScoreCategory(values, category);
Debug.Log($"Scored {category}: 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)
{
int total = scoringSystem.TotalScore;
Debug.Log($"*** GAME OVER *** Total Score: {total}");
OnGameOver?.Invoke(total);
}
else
{
StartNewTurn();
}
}
}
}
}