89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using YachtDice.Modifiers;
|
|
|
|
namespace YachtDice.Scoring
|
|
{
|
|
public class ScoringSystem : MonoBehaviour
|
|
{
|
|
public event Action<YachtCategory, int> OnCategoryScored;
|
|
public event Action<int> OnAllCategoriesScored;
|
|
public event Action<YachtCategory, ScoreResult> OnCategoryConfirmed;
|
|
|
|
private readonly Dictionary<YachtCategory, int> scorecard = new();
|
|
private readonly HashSet<YachtCategory> usedCategories = new();
|
|
private List<ModifierData> activeModifierData = new();
|
|
|
|
public bool IsCategoryUsed(YachtCategory category) => usedCategories.Contains(category);
|
|
|
|
public int GetCategoryScore(YachtCategory category)
|
|
{
|
|
return scorecard.TryGetValue(category, out int score) ? score : -1;
|
|
}
|
|
|
|
public int TotalScore
|
|
{
|
|
get
|
|
{
|
|
int total = 0;
|
|
foreach (var kvp in scorecard) total += kvp.Value;
|
|
return total;
|
|
}
|
|
}
|
|
|
|
public int CategoriesFilledCount => usedCategories.Count;
|
|
|
|
public int TotalCategoryCount => Enum.GetValues(typeof(YachtCategory)).Length;
|
|
|
|
public bool IsComplete => CategoriesFilledCount >= TotalCategoryCount;
|
|
|
|
public void SetActiveModifiers(List<ModifierData> modifiers)
|
|
{
|
|
activeModifierData = modifiers ?? new List<ModifierData>();
|
|
}
|
|
|
|
public IReadOnlyList<ModifierData> ActiveModifiers => activeModifierData;
|
|
|
|
public ScoreResult PreviewScore(int[] diceValues, YachtCategory category)
|
|
{
|
|
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
|
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
|
|
|
|
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
|
|
|
|
return result;
|
|
}
|
|
|
|
public ScoreResult ScoreCategory(int[] diceValues, YachtCategory category)
|
|
{
|
|
if (usedCategories.Contains(category))
|
|
throw new InvalidOperationException($"Category {category} has already been scored.");
|
|
|
|
int baseScore = CategoryScorer.Calculate(diceValues, category);
|
|
ScoreResult result = ScoreResult.Create(baseScore, diceValues, category);
|
|
|
|
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.SelectedCategory);
|
|
ModifierPipeline.Apply(activeModifierData, ref result, ModifierScope.AnyCategoryClosed);
|
|
|
|
int finalScore = result.FinalScore;
|
|
scorecard[category] = finalScore;
|
|
usedCategories.Add(category);
|
|
|
|
OnCategoryScored?.Invoke(category, finalScore);
|
|
OnCategoryConfirmed?.Invoke(category, result);
|
|
|
|
if (IsComplete)
|
|
OnAllCategoriesScored?.Invoke(TotalScore);
|
|
|
|
return result;
|
|
}
|
|
|
|
public void ResetScorecard()
|
|
{
|
|
scorecard.Clear();
|
|
usedCategories.Clear();
|
|
}
|
|
}
|
|
}
|