[Add] Dice & Refactor private names

This commit is contained in:
2026-03-02 11:22:01 +07:00
parent 4890fa946e
commit f65976796d
36 changed files with 883 additions and 489 deletions
+6 -6
View File
@@ -8,14 +8,14 @@ namespace YachtDice.Shop
{
[SerializeField] private List<ScriptableObject> items = new();
private List<IShopItem> cachedItems;
private List<IShopItem> _cachedItems;
public IReadOnlyList<IShopItem> All
{
get
{
if (cachedItems == null) RebuildCache();
return cachedItems;
if (_cachedItems == null) RebuildCache();
return _cachedItems;
}
}
@@ -32,14 +32,14 @@ namespace YachtDice.Shop
private void RebuildCache()
{
cachedItems = new List<IShopItem>();
_cachedItems = new List<IShopItem>();
for (int i = 0; i < items.Count; i++)
{
if (items[i] is IShopItem shopItem)
cachedItems.Add(shopItem);
_cachedItems.Add(shopItem);
}
}
private void OnValidate() => cachedItems = null;
private void OnValidate() => _cachedItems = null;
}
}
+18 -18
View File
@@ -8,28 +8,28 @@ namespace YachtDice.Shop
{
[SerializeField] private ShopView shopView;
private ShopCatalog catalog;
private CurrencyBank currencyBank;
private ShopModel model;
private ShopCatalog _catalog;
private CurrencyBank _currencyBank;
private ShopModel _model;
public ShopCatalog Catalog => catalog;
public ShopCatalog Catalog => _catalog;
[Inject]
public void Construct(ShopCatalog catalog, CurrencyBank currencyBank, ShopModel model)
{
this.catalog = catalog;
this.currencyBank = currencyBank;
this.model = model;
this._catalog = catalog;
this._currencyBank = currencyBank;
this._model = model;
}
private void Start()
{
shopView.OnBuyClicked += HandleBuyClicked;
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
model.OnItemPurchased += HandleItemPurchased;
_currencyBank.OnBalanceChanged += HandleCurrencyChanged;
_model.OnItemPurchased += HandleItemPurchased;
shopView.Populate(catalog.All, model);
shopView.UpdateCurrencyDisplay(currencyBank.Balance);
shopView.Populate(_catalog.All, _model);
shopView.UpdateCurrencyDisplay(_currencyBank.Balance);
}
private void OnDestroy()
@@ -37,11 +37,11 @@ namespace YachtDice.Shop
if (shopView != null)
shopView.OnBuyClicked -= HandleBuyClicked;
if (currencyBank != null)
currencyBank.OnBalanceChanged -= HandleCurrencyChanged;
if (_currencyBank != null)
_currencyBank.OnBalanceChanged -= HandleCurrencyChanged;
if (model != null)
model.OnItemPurchased -= HandleItemPurchased;
if (_model != null)
_model.OnItemPurchased -= HandleItemPurchased;
}
public void ToggleVisibility()
@@ -56,18 +56,18 @@ namespace YachtDice.Shop
private void HandleBuyClicked(IShopItem item)
{
model.TryPurchase(item);
_model.TryPurchase(item);
}
private void HandleCurrencyChanged(int newBalance)
{
shopView.UpdateCurrencyDisplay(newBalance);
shopView.RefreshStates(catalog.All, model);
shopView.RefreshStates(_catalog.All, _model);
}
private void HandleItemPurchased(IShopItem item)
{
shopView.RefreshStates(catalog.All, model);
shopView.RefreshStates(_catalog.All, _model);
}
}
}
+9 -9
View File
@@ -25,7 +25,7 @@ namespace YachtDice.Shop
[SerializeField] private Color rareColor = new(0.4f, 0.6f, 1f);
[SerializeField] private Color epicColor = new(0.8f, 0.4f, 1f);
private IShopItem data;
private IShopItem _data;
public event Action<IShopItem> OnBuyClicked;
public event Action<IShopItem, RectTransform> OnHoverEnter;
@@ -34,21 +34,21 @@ namespace YachtDice.Shop
private void Awake()
{
if (buyButton != null)
buyButton.onClick.AddListener(() => OnBuyClicked?.Invoke(data));
buyButton.onClick.AddListener(() => OnBuyClicked?.Invoke(_data));
}
public void Setup(IShopItem item, ShopItemState state)
{
data = item;
_data = item;
if (nameText != null) nameText.text = data.DisplayName;
if (descriptionText != null) descriptionText.text = data.Description;
if (priceText != null) priceText.text = data.ShopPrice.ToString();
if (iconImage != null && data.Icon != null) iconImage.sprite = data.Icon;
if (nameText != null) nameText.text = _data.DisplayName;
if (descriptionText != null) descriptionText.text = _data.Description;
if (priceText != null) priceText.text = _data.ShopPrice.ToString();
if (iconImage != null && _data.Icon != null) iconImage.sprite = _data.Icon;
if (rarityText != null)
{
if (data is ModifierDefinition mod)
if (_data is ModifierDefinition mod)
{
rarityText.gameObject.SetActive(true);
rarityText.text = mod.Rarity.ToString();
@@ -90,7 +90,7 @@ namespace YachtDice.Shop
public void OnPointerEnter(PointerEventData eventData)
{
OnHoverEnter?.Invoke(data, transform as RectTransform);
OnHoverEnter?.Invoke(_data, transform as RectTransform);
}
public void OnPointerExit(PointerEventData eventData)
+19 -19
View File
@@ -10,26 +10,26 @@ namespace YachtDice.Shop
{
public class ShopModel
{
private readonly CurrencyBank currencyBank;
private readonly InventoryModel inventoryModel;
private readonly DiceCollection diceCollection;
private readonly HashSet<string> purchasedPermanentIds = new();
private readonly CurrencyBank _currencyBank;
private readonly InventoryModel _inventoryModel;
private readonly DiceCollection _diceCollection;
private readonly HashSet<string> _purchasedPermanentIds = new();
public event Action<IShopItem> OnItemPurchased;
public ShopModel(CurrencyBank currencyBank, InventoryModel inventoryModel, DiceCollection diceCollection)
{
this.currencyBank = currencyBank;
this.inventoryModel = inventoryModel;
this.diceCollection = diceCollection;
this._currencyBank = currencyBank;
this._inventoryModel = inventoryModel;
this._diceCollection = diceCollection;
}
public bool CanPurchase(IShopItem item)
{
if (item == null) return false;
if (!currencyBank.CanAfford(item.ShopPrice)) return false;
if (!_currencyBank.CanAfford(item.ShopPrice)) return false;
if (!item.IsRepurchasable && purchasedPermanentIds.Contains(item.Id))
if (!item.IsRepurchasable && _purchasedPermanentIds.Contains(item.Id))
return false;
return true;
@@ -39,18 +39,18 @@ namespace YachtDice.Shop
{
if (!CanPurchase(item)) return false;
if (!currencyBank.Spend(item.ShopPrice)) return false;
if (!_currencyBank.Spend(item.ShopPrice)) return false;
if (!item.IsRepurchasable)
purchasedPermanentIds.Add(item.Id);
_purchasedPermanentIds.Add(item.Id);
switch (item)
{
case ModifierDefinition modifier:
inventoryModel.AddModifier(modifier);
_inventoryModel.AddModifier(modifier);
break;
case DiceDefinition dice:
diceCollection.Add(dice);
_diceCollection.Add(dice);
break;
}
@@ -58,16 +58,16 @@ namespace YachtDice.Shop
return true;
}
public bool IsPermanentOwned(string itemId) => purchasedPermanentIds.Contains(itemId);
public bool IsPermanentOwned(string itemId) => _purchasedPermanentIds.Contains(itemId);
public ShopItemState GetItemState(IShopItem item)
{
if (item == null) return ShopItemState.TooExpensive;
if (!item.IsRepurchasable && purchasedPermanentIds.Contains(item.Id))
if (!item.IsRepurchasable && _purchasedPermanentIds.Contains(item.Id))
return ShopItemState.Owned;
if (!currencyBank.CanAfford(item.ShopPrice))
if (!_currencyBank.CanAfford(item.ShopPrice))
return ShopItemState.TooExpensive;
return item.IsRepurchasable
@@ -77,12 +77,12 @@ namespace YachtDice.Shop
public void LoadPurchasedPermanentIds(IEnumerable<string> ids)
{
purchasedPermanentIds.Clear();
_purchasedPermanentIds.Clear();
if (ids != null)
foreach (var id in ids) purchasedPermanentIds.Add(id);
foreach (var id in ids) _purchasedPermanentIds.Add(id);
}
public HashSet<string> GetPurchasedPermanentIds() => new(purchasedPermanentIds);
public HashSet<string> GetPurchasedPermanentIds() => new(_purchasedPermanentIds);
}
public enum ShopItemState
+10 -10
View File
@@ -14,7 +14,7 @@ namespace YachtDice.Shop
[SerializeField] private Button closeButton;
[SerializeField] private ShopTooltipView tooltipView;
private readonly List<ShopItemView> spawnedItems = new();
private readonly List<ShopItemView> _spawnedItems = new();
public event Action<IShopItem> OnBuyClicked;
@@ -49,16 +49,16 @@ namespace YachtDice.Shop
item.OnBuyClicked += HandleBuy;
item.OnHoverEnter += HandleHoverEnter;
item.OnHoverExit += HandleHoverExit;
spawnedItems.Add(item);
_spawnedItems.Add(item);
}
}
public void RefreshStates(IReadOnlyList<IShopItem> catalog, ShopModel model)
{
for (int i = 0; i < spawnedItems.Count && i < catalog.Count; i++)
for (int i = 0; i < _spawnedItems.Count && i < catalog.Count; i++)
{
var state = model.GetItemState(catalog[i]);
spawnedItems[i].SetState(state);
_spawnedItems[i].SetState(state);
}
}
@@ -70,14 +70,14 @@ namespace YachtDice.Shop
private void ClearItems()
{
for (int i = 0; i < spawnedItems.Count; i++)
for (int i = 0; i < _spawnedItems.Count; i++)
{
spawnedItems[i].OnBuyClicked -= HandleBuy;
spawnedItems[i].OnHoverEnter -= HandleHoverEnter;
spawnedItems[i].OnHoverExit -= HandleHoverExit;
Destroy(spawnedItems[i].gameObject);
_spawnedItems[i].OnBuyClicked -= HandleBuy;
_spawnedItems[i].OnHoverEnter -= HandleHoverEnter;
_spawnedItems[i].OnHoverExit -= HandleHoverExit;
Destroy(_spawnedItems[i].gameObject);
}
spawnedItems.Clear();
_spawnedItems.Clear();
}
private void HandleBuy(IShopItem item) => OnBuyClicked?.Invoke(item);