[Refactor] Replace hardcoded categories with data-driven SO system and abstract dice
- Add abstract dice system (IDie interface, DieDefinitionSO, StandardDieSO, DieInstance) to support future custom dice types while keeping backward compat via int[] DiceValues - Replace YachtCategory enum and CategoryScorer switch with CategoryDefinitionSO hierarchy: SumOfValueCategorySO, NOfAKindCategorySO, FullHouseCategorySO, StraightCategorySO, SumAllCategorySO - Add CategoryCatalogSO for ordered category collections and DiceCheckUtility for shared logic - Refactor ScoringSystem, Views, GameManager, GameController to use SO references - Update CategoryCondition modifier to use SO reference instead of enum - Update all editor tests to use SO-based categories and DieInstance Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using VContainer;
|
||||
using YachtDice.Categories;
|
||||
using YachtDice.Dice;
|
||||
using YachtDice.Events;
|
||||
using YachtDice.Modifiers.Core;
|
||||
using YachtDice.Modifiers.Runtime;
|
||||
@@ -11,26 +13,30 @@ namespace YachtDice.Scoring
|
||||
{
|
||||
public class ScoringSystem : MonoBehaviour
|
||||
{
|
||||
public event Action<YachtCategory, int> OnCategoryScored;
|
||||
public event Action<CategoryDefinitionSO, int> OnCategoryScored;
|
||||
public event Action<int> OnAllCategoriesScored;
|
||||
public event Action<YachtCategory, ScoreResult> OnCategoryConfirmed;
|
||||
public event Action<CategoryDefinitionSO, ScoreResult> OnCategoryConfirmed;
|
||||
|
||||
private readonly Dictionary<YachtCategory, int> scorecard = new();
|
||||
private readonly HashSet<YachtCategory> usedCategories = new();
|
||||
private readonly Dictionary<CategoryDefinitionSO, int> scorecard = new();
|
||||
private readonly HashSet<CategoryDefinitionSO> usedCategories = new();
|
||||
|
||||
private GameEventBus eventBus;
|
||||
private ModifierRegistry modifierRegistry;
|
||||
private CategoryCatalogSO catalog;
|
||||
|
||||
[Inject]
|
||||
public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry)
|
||||
public void Construct(GameEventBus eventBus, ModifierRegistry modifierRegistry, CategoryCatalogSO catalog)
|
||||
{
|
||||
this.eventBus = eventBus;
|
||||
this.modifierRegistry = modifierRegistry;
|
||||
this.catalog = catalog;
|
||||
}
|
||||
|
||||
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category);
|
||||
public CategoryCatalogSO Catalog => catalog;
|
||||
|
||||
public int GetCategoryScore(YachtCategory category)
|
||||
public bool IsCategoryUsed(CategoryDefinitionSO category) => usedCategories.Contains(category);
|
||||
|
||||
public int GetCategoryScore(CategoryDefinitionSO category)
|
||||
{
|
||||
return scorecard.TryGetValue(category, out int score) ? score : -1;
|
||||
}
|
||||
@@ -47,20 +53,20 @@ namespace YachtDice.Scoring
|
||||
|
||||
public int CategoriesFilledCount => usedCategories.Count;
|
||||
|
||||
public int TotalCategoryCount => Enum.GetValues(typeof(YachtCategory)).Length;
|
||||
public int TotalCategoryCount => catalog != null ? catalog.Count : 0;
|
||||
|
||||
public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount;
|
||||
|
||||
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category,
|
||||
public ScoreResult PreviewScore(IReadOnlyList<IDie> dice, CategoryDefinitionSO category,
|
||||
int currentRoll = 0, int currentTurn = 0, int playerCurrency = 0)
|
||||
{
|
||||
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
||||
int baseScore = category.Calculate(dice);
|
||||
|
||||
if (eventBus == null || modifierRegistry == null)
|
||||
return ScoreResult.Create(baseScore, diceValues, category);
|
||||
return ScoreResult.Create(baseScore, dice, category);
|
||||
|
||||
var context = ModifierContext.CreateForScoring(
|
||||
baseScore, diceValues, category,
|
||||
baseScore, dice, category,
|
||||
currentRoll, currentTurn, playerCurrency,
|
||||
modifierRegistry.Active);
|
||||
|
||||
@@ -69,19 +75,19 @@ namespace YachtDice.Scoring
|
||||
return context.ToScoreResult();
|
||||
}
|
||||
|
||||
public async UniTask<ScoreResult> ScoreCategoryAsync(int[] diceValues, YachtCategory category,
|
||||
public async UniTask<ScoreResult> ScoreCategoryAsync(IReadOnlyList<IDie> dice, CategoryDefinitionSO category,
|
||||
int currentRoll, int currentTurn, int playerCurrency)
|
||||
{
|
||||
if (usedCategories.Contains(category))
|
||||
throw new InvalidOperationException($"Category {category} has already been scored.");
|
||||
throw new InvalidOperationException($"Category {category.DisplayName} has already been scored.");
|
||||
|
||||
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
||||
int baseScore = category.Calculate(dice);
|
||||
|
||||
ModifierContext context;
|
||||
if (eventBus != null && modifierRegistry != null)
|
||||
{
|
||||
context = ModifierContext.CreateForScoring(
|
||||
baseScore, diceValues, category,
|
||||
baseScore, dice, category,
|
||||
currentRoll, currentTurn, playerCurrency,
|
||||
modifierRegistry.Active);
|
||||
|
||||
@@ -92,7 +98,8 @@ namespace YachtDice.Scoring
|
||||
context = new ModifierContext
|
||||
{
|
||||
BaseScore = baseScore,
|
||||
DiceValues = diceValues,
|
||||
Dice = dice,
|
||||
DiceValues = DiceCheckUtility.ExtractValues(dice),
|
||||
Category = category,
|
||||
};
|
||||
}
|
||||
@@ -111,18 +118,18 @@ namespace YachtDice.Scoring
|
||||
return result;
|
||||
}
|
||||
|
||||
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
|
||||
public ScoreResult ScoreCategory(IReadOnlyList<IDie> dice, CategoryDefinitionSO category)
|
||||
{
|
||||
if (usedCategories.Contains(category))
|
||||
throw new InvalidOperationException($"Category {category} has already been scored.");
|
||||
throw new InvalidOperationException($"Category {category.DisplayName} has already been scored.");
|
||||
|
||||
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
||||
int baseScore = category.Calculate(dice);
|
||||
|
||||
ModifierContext context = null;
|
||||
if (eventBus != null && modifierRegistry != null)
|
||||
{
|
||||
context = ModifierContext.CreateForScoring(
|
||||
baseScore, diceValues, category,
|
||||
baseScore, dice, category,
|
||||
0, 0, 0,
|
||||
modifierRegistry.Active);
|
||||
|
||||
@@ -133,7 +140,7 @@ namespace YachtDice.Scoring
|
||||
if (context != null)
|
||||
result = context.ToScoreResult();
|
||||
else
|
||||
result = ScoreResult.Create(baseScore, diceValues, category);
|
||||
result = ScoreResult.Create(baseScore, dice, category);
|
||||
|
||||
int finalScore = result.FinalScore;
|
||||
scorecard[category] = finalScore;
|
||||
|
||||
Reference in New Issue
Block a user