Files
YachtDice/Assets/Scripts/UI/ScoreCardView.cs
T
horooko 0f9b162061 [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>
2026-03-01 11:46:50 +07:00

106 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using YachtDice.Categories;
namespace YachtDice.UI
{
public class ScoreCardView : MonoBehaviour
{
[Header("Category Rows (порядок соответствует каталогу)")]
[SerializeField] private List<CategoryRowView> categoryRows = new();
[Header("Summary")]
[SerializeField] private TMP_Text upperSumText;
[SerializeField] private TMP_Text upperBonusText;
[SerializeField] private TMP_Text totalScoreText;
public event Action<CategoryDefinitionSO> OnCategorySelected;
private CategoryCatalogSO catalog;
private Dictionary<CategoryDefinitionSO, int> categoryToRowIndex;
/// <summary>
/// Инициализирует скоркарту из каталога категорий.
/// Вызывается из GameController после DI.
/// </summary>
public void Initialize(CategoryCatalogSO categoryCatalog)
{
catalog = categoryCatalog;
categoryToRowIndex = new Dictionary<CategoryDefinitionSO, int>();
var all = catalog.All;
int count = Mathf.Min(categoryRows.Count, all.Count);
for (int i = 0; i < count; i++)
{
categoryRows[i].Initialize(all[i]);
categoryRows[i].OnCategorySelected += HandleCategorySelected;
categoryToRowIndex[all[i]] = i;
}
UpdateTotalDisplay(0, 0, false);
}
public void UpdatePreviews(Dictionary<CategoryDefinitionSO, int> previews)
{
foreach (var kvp in previews)
{
if (categoryToRowIndex.TryGetValue(kvp.Key, out int rowIndex))
{
categoryRows[rowIndex].ShowPreview(kvp.Value);
categoryRows[rowIndex].SetInteractable(true);
}
}
}
public void ClearAllPreviews()
{
for (int i = 0; i < categoryRows.Count; i++)
{
categoryRows[i].HidePreview();
categoryRows[i].SetInteractable(false);
}
}
public void SetCategoryScored(CategoryDefinitionSO category, int score)
{
if (categoryToRowIndex.TryGetValue(category, out int index))
categoryRows[index].SetRecordedScore(score);
}
public void SetAllInteractable(bool interactable)
{
for (int i = 0; i < categoryRows.Count; i++)
categoryRows[i].SetInteractable(interactable);
}
public void UpdateTotalDisplay(int totalScore, int upperSum, bool hasUpperBonus)
{
totalScoreText.text = totalScore.ToString();
upperSumText.text = $"{upperSum} / 63";
upperBonusText.text = hasUpperBonus ? "+35" : "---";
}
public void ResetAll()
{
for (int i = 0; i < categoryRows.Count; i++)
categoryRows[i].ResetRow();
UpdateTotalDisplay(0, 0, false);
}
private void HandleCategorySelected(CategoryDefinitionSO category)
{
OnCategorySelected?.Invoke(category);
}
private void OnDestroy()
{
for (int i = 0; i < categoryRows.Count; i++)
categoryRows[i].OnCategorySelected -= HandleCategorySelected;
}
}
}