[Fix] Code visual
This commit is contained in:
@@ -4,22 +4,21 @@ using YachtDice.Modifiers;
|
||||
|
||||
namespace YachtDice.Shop
|
||||
{
|
||||
|
||||
[CreateAssetMenu(fileName = "ShopCatalog", menuName = "YachtDice/Shop Catalog")]
|
||||
public sealed class ShopCatalog : ScriptableObject
|
||||
{
|
||||
[SerializeField] private List<ModifierData> availableModifiers = new();
|
||||
|
||||
public IReadOnlyList<ModifierData> AvailableModifiers => availableModifiers;
|
||||
|
||||
public ModifierData FindById(string id)
|
||||
[CreateAssetMenu(fileName = "ShopCatalog", menuName = "YachtDice/Shop Catalog")]
|
||||
public sealed class ShopCatalog : ScriptableObject
|
||||
{
|
||||
for (int i = 0; i < availableModifiers.Count; i++)
|
||||
[SerializeField] private List<ModifierData> availableModifiers = new();
|
||||
|
||||
public IReadOnlyList<ModifierData> AvailableModifiers => availableModifiers;
|
||||
|
||||
public ModifierData FindById(string id)
|
||||
{
|
||||
if (availableModifiers[i] != null && availableModifiers[i].Id == id)
|
||||
return availableModifiers[i];
|
||||
for (int i = 0; i < availableModifiers.Count; i++)
|
||||
{
|
||||
if (availableModifiers[i] != null && availableModifiers[i].Id == id)
|
||||
return availableModifiers[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,58 +4,57 @@ using YachtDice.Modifiers;
|
||||
|
||||
namespace YachtDice.Shop
|
||||
{
|
||||
|
||||
public sealed class ShopController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private ShopCatalog catalog;
|
||||
[SerializeField] private ShopView shopView;
|
||||
[SerializeField] private CurrencyBank currencyBank;
|
||||
|
||||
private ShopModel model;
|
||||
|
||||
public ShopCatalog Catalog => catalog;
|
||||
|
||||
public void Initialize(ShopModel shopModel)
|
||||
public class ShopController : MonoBehaviour
|
||||
{
|
||||
model = shopModel;
|
||||
[SerializeField] private ShopCatalog catalog;
|
||||
[SerializeField] private ShopView shopView;
|
||||
[SerializeField] private CurrencyBank currencyBank;
|
||||
|
||||
shopView.OnBuyClicked += HandleBuyClicked;
|
||||
private ShopModel model;
|
||||
|
||||
if (currencyBank != null)
|
||||
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
||||
public ShopCatalog Catalog => catalog;
|
||||
|
||||
model.OnItemPurchased += HandleItemPurchased;
|
||||
public void Initialize(ShopModel shopModel)
|
||||
{
|
||||
model = shopModel;
|
||||
|
||||
shopView.Populate(catalog.AvailableModifiers, model);
|
||||
shopView.UpdateCurrencyDisplay(currencyBank != null ? currencyBank.Balance : 0);
|
||||
}
|
||||
shopView.OnBuyClicked += HandleBuyClicked;
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (shopView != null)
|
||||
shopView.OnBuyClicked -= HandleBuyClicked;
|
||||
if (currencyBank != null)
|
||||
currencyBank.OnBalanceChanged += HandleCurrencyChanged;
|
||||
|
||||
if (currencyBank != null)
|
||||
currencyBank.OnBalanceChanged -= HandleCurrencyChanged;
|
||||
model.OnItemPurchased += HandleItemPurchased;
|
||||
|
||||
if (model != null)
|
||||
model.OnItemPurchased -= HandleItemPurchased;
|
||||
}
|
||||
shopView.Populate(catalog.AvailableModifiers, model);
|
||||
shopView.UpdateCurrencyDisplay(currencyBank != null ? currencyBank.Balance : 0);
|
||||
}
|
||||
|
||||
private void HandleBuyClicked(ModifierData data)
|
||||
{
|
||||
model.TryPurchase(data);
|
||||
}
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (shopView != null)
|
||||
shopView.OnBuyClicked -= HandleBuyClicked;
|
||||
|
||||
private void HandleCurrencyChanged(int newBalance)
|
||||
{
|
||||
shopView.UpdateCurrencyDisplay(newBalance);
|
||||
shopView.RefreshStates(catalog.AvailableModifiers, model);
|
||||
}
|
||||
if (currencyBank != null)
|
||||
currencyBank.OnBalanceChanged -= HandleCurrencyChanged;
|
||||
|
||||
private void HandleItemPurchased(ModifierData data)
|
||||
{
|
||||
shopView.RefreshStates(catalog.AvailableModifiers, model);
|
||||
if (model != null)
|
||||
model.OnItemPurchased -= HandleItemPurchased;
|
||||
}
|
||||
|
||||
private void HandleBuyClicked(ModifierData data)
|
||||
{
|
||||
model.TryPurchase(data);
|
||||
}
|
||||
|
||||
private void HandleCurrencyChanged(int newBalance)
|
||||
{
|
||||
shopView.UpdateCurrencyDisplay(newBalance);
|
||||
shopView.RefreshStates(catalog.AvailableModifiers, model);
|
||||
}
|
||||
|
||||
private void HandleItemPurchased(ModifierData data)
|
||||
{
|
||||
shopView.RefreshStates(catalog.AvailableModifiers, model);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,87 +6,86 @@ using YachtDice.Modifiers;
|
||||
|
||||
namespace YachtDice.Shop
|
||||
{
|
||||
|
||||
public sealed class ShopItemView : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Image iconImage;
|
||||
[SerializeField] private TMP_Text nameText;
|
||||
[SerializeField] private TMP_Text descriptionText;
|
||||
[SerializeField] private TMP_Text priceText;
|
||||
[SerializeField] private TMP_Text rarityText;
|
||||
[SerializeField] private Button buyButton;
|
||||
[SerializeField] private TMP_Text buyButtonText;
|
||||
[SerializeField] private Image background;
|
||||
|
||||
[Header("Rarity Colors")]
|
||||
[SerializeField] private Color commonColor = Color.white;
|
||||
[SerializeField] private Color uncommonColor = new(0.4f, 0.8f, 0.4f);
|
||||
[SerializeField] private Color rareColor = new(0.4f, 0.6f, 1f);
|
||||
[SerializeField] private Color epicColor = new(0.8f, 0.4f, 1f);
|
||||
|
||||
private ModifierData data;
|
||||
|
||||
public event Action<ModifierData> OnBuyClicked;
|
||||
|
||||
private void Awake()
|
||||
public class ShopItemView : MonoBehaviour
|
||||
{
|
||||
if (buyButton != null)
|
||||
buyButton.onClick.AddListener(() => OnBuyClicked?.Invoke(data));
|
||||
}
|
||||
[SerializeField] private Image iconImage;
|
||||
[SerializeField] private TMP_Text nameText;
|
||||
[SerializeField] private TMP_Text descriptionText;
|
||||
[SerializeField] private TMP_Text priceText;
|
||||
[SerializeField] private TMP_Text rarityText;
|
||||
[SerializeField] private Button buyButton;
|
||||
[SerializeField] private TMP_Text buyButtonText;
|
||||
[SerializeField] private Image background;
|
||||
|
||||
public void Setup(ModifierData modifierData, ShopItemState state)
|
||||
{
|
||||
data = modifierData;
|
||||
[Header("Rarity Colors")]
|
||||
[SerializeField] private Color commonColor = Color.white;
|
||||
[SerializeField] private Color uncommonColor = new(0.4f, 0.8f, 0.4f);
|
||||
[SerializeField] private Color rareColor = new(0.4f, 0.6f, 1f);
|
||||
[SerializeField] private Color epicColor = new(0.8f, 0.4f, 1f);
|
||||
|
||||
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;
|
||||
private ModifierData data;
|
||||
|
||||
if (rarityText != null)
|
||||
public event Action<ModifierData> OnBuyClicked;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
rarityText.text = data.Rarity.ToString();
|
||||
rarityText.color = GetRarityColor(data.Rarity);
|
||||
if (buyButton != null)
|
||||
buyButton.onClick.AddListener(() => OnBuyClicked?.Invoke(data));
|
||||
}
|
||||
|
||||
SetState(state);
|
||||
}
|
||||
|
||||
public void SetState(ShopItemState state)
|
||||
{
|
||||
if (buyButton == null) return;
|
||||
|
||||
switch (state)
|
||||
public void Setup(ModifierData modifierData, ShopItemState state)
|
||||
{
|
||||
case ShopItemState.Available:
|
||||
buyButton.interactable = true;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.RepurchaseAvailable:
|
||||
buyButton.interactable = true;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.TooExpensive:
|
||||
buyButton.interactable = false;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.Owned:
|
||||
buyButton.interactable = false;
|
||||
if (buyButtonText != null) buyButtonText.text = "Owned";
|
||||
break;
|
||||
data = modifierData;
|
||||
|
||||
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)
|
||||
{
|
||||
rarityText.text = data.Rarity.ToString();
|
||||
rarityText.color = GetRarityColor(data.Rarity);
|
||||
}
|
||||
|
||||
SetState(state);
|
||||
}
|
||||
|
||||
public void SetState(ShopItemState state)
|
||||
{
|
||||
if (buyButton == null) return;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case ShopItemState.Available:
|
||||
buyButton.interactable = true;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.RepurchaseAvailable:
|
||||
buyButton.interactable = true;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.TooExpensive:
|
||||
buyButton.interactable = false;
|
||||
if (buyButtonText != null) buyButtonText.text = "Buy";
|
||||
break;
|
||||
case ShopItemState.Owned:
|
||||
buyButton.interactable = false;
|
||||
if (buyButtonText != null) buyButtonText.text = "Owned";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private Color GetRarityColor(ModifierRarity rarity)
|
||||
{
|
||||
return rarity switch
|
||||
{
|
||||
ModifierRarity.Common => commonColor,
|
||||
ModifierRarity.Uncommon => uncommonColor,
|
||||
ModifierRarity.Rare => rareColor,
|
||||
ModifierRarity.Epic => epicColor,
|
||||
_ => commonColor
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private Color GetRarityColor(ModifierRarity rarity)
|
||||
{
|
||||
return rarity switch
|
||||
{
|
||||
ModifierRarity.Common => commonColor,
|
||||
ModifierRarity.Uncommon => uncommonColor,
|
||||
ModifierRarity.Rare => rareColor,
|
||||
ModifierRarity.Epic => epicColor,
|
||||
_ => commonColor
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,80 +6,79 @@ using YachtDice.Modifiers;
|
||||
|
||||
namespace YachtDice.Shop
|
||||
{
|
||||
|
||||
public sealed class ShopModel
|
||||
{
|
||||
private readonly CurrencyBank currencyBank;
|
||||
private readonly InventoryModel inventoryModel;
|
||||
private readonly HashSet<string> purchasedPermanentIds = new();
|
||||
|
||||
public event Action<ModifierData> OnItemPurchased;
|
||||
|
||||
public ShopModel(CurrencyBank currencyBank, InventoryModel inventoryModel)
|
||||
public class ShopModel
|
||||
{
|
||||
this.currencyBank = currencyBank;
|
||||
this.inventoryModel = inventoryModel;
|
||||
private readonly CurrencyBank currencyBank;
|
||||
private readonly InventoryModel inventoryModel;
|
||||
private readonly HashSet<string> purchasedPermanentIds = new();
|
||||
|
||||
public event Action<ModifierData> OnItemPurchased;
|
||||
|
||||
public ShopModel(CurrencyBank currencyBank, InventoryModel inventoryModel)
|
||||
{
|
||||
this.currencyBank = currencyBank;
|
||||
this.inventoryModel = inventoryModel;
|
||||
}
|
||||
|
||||
public bool CanPurchase(ModifierData modifier)
|
||||
{
|
||||
if (modifier == null) return false;
|
||||
if (!currencyBank.CanAfford(modifier.ShopPrice)) return false;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent &&
|
||||
purchasedPermanentIds.Contains(modifier.Id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryPurchase(ModifierData modifier)
|
||||
{
|
||||
if (!CanPurchase(modifier)) return false;
|
||||
|
||||
if (!currencyBank.Spend(modifier.ShopPrice)) return false;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent)
|
||||
purchasedPermanentIds.Add(modifier.Id);
|
||||
|
||||
inventoryModel.AddModifier(modifier);
|
||||
OnItemPurchased?.Invoke(modifier);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsPermanentOwned(string modifierId) => purchasedPermanentIds.Contains(modifierId);
|
||||
|
||||
public ShopItemState GetItemState(ModifierData modifier)
|
||||
{
|
||||
if (modifier == null) return ShopItemState.TooExpensive;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent &&
|
||||
purchasedPermanentIds.Contains(modifier.Id))
|
||||
return ShopItemState.Owned;
|
||||
|
||||
if (!currencyBank.CanAfford(modifier.ShopPrice))
|
||||
return ShopItemState.TooExpensive;
|
||||
|
||||
return modifier.Durability == ModifierDurability.LimitedUses
|
||||
? ShopItemState.RepurchaseAvailable
|
||||
: ShopItemState.Available;
|
||||
}
|
||||
|
||||
public void LoadPurchasedPermanentIds(IEnumerable<string> ids)
|
||||
{
|
||||
purchasedPermanentIds.Clear();
|
||||
if (ids != null)
|
||||
foreach (var id in ids) purchasedPermanentIds.Add(id);
|
||||
}
|
||||
|
||||
public HashSet<string> GetPurchasedPermanentIds() => new(purchasedPermanentIds);
|
||||
}
|
||||
|
||||
public bool CanPurchase(ModifierData modifier)
|
||||
public enum ShopItemState
|
||||
{
|
||||
if (modifier == null) return false;
|
||||
if (!currencyBank.CanAfford(modifier.ShopPrice)) return false;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent &&
|
||||
purchasedPermanentIds.Contains(modifier.Id))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
Available,
|
||||
TooExpensive,
|
||||
Owned,
|
||||
RepurchaseAvailable
|
||||
}
|
||||
|
||||
public bool TryPurchase(ModifierData modifier)
|
||||
{
|
||||
if (!CanPurchase(modifier)) return false;
|
||||
|
||||
if (!currencyBank.Spend(modifier.ShopPrice)) return false;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent)
|
||||
purchasedPermanentIds.Add(modifier.Id);
|
||||
|
||||
inventoryModel.AddModifier(modifier);
|
||||
OnItemPurchased?.Invoke(modifier);
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsPermanentOwned(string modifierId) => purchasedPermanentIds.Contains(modifierId);
|
||||
|
||||
public ShopItemState GetItemState(ModifierData modifier)
|
||||
{
|
||||
if (modifier == null) return ShopItemState.TooExpensive;
|
||||
|
||||
if (modifier.Durability == ModifierDurability.Permanent &&
|
||||
purchasedPermanentIds.Contains(modifier.Id))
|
||||
return ShopItemState.Owned;
|
||||
|
||||
if (!currencyBank.CanAfford(modifier.ShopPrice))
|
||||
return ShopItemState.TooExpensive;
|
||||
|
||||
return modifier.Durability == ModifierDurability.LimitedUses
|
||||
? ShopItemState.RepurchaseAvailable
|
||||
: ShopItemState.Available;
|
||||
}
|
||||
|
||||
public void LoadPurchasedPermanentIds(IEnumerable<string> ids)
|
||||
{
|
||||
purchasedPermanentIds.Clear();
|
||||
if (ids != null)
|
||||
foreach (var id in ids) purchasedPermanentIds.Add(id);
|
||||
}
|
||||
|
||||
public HashSet<string> GetPurchasedPermanentIds() => new(purchasedPermanentIds);
|
||||
}
|
||||
|
||||
public enum ShopItemState
|
||||
{
|
||||
Available,
|
||||
TooExpensive,
|
||||
Owned,
|
||||
RepurchaseAvailable
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,76 +7,75 @@ using YachtDice.Modifiers;
|
||||
|
||||
namespace YachtDice.Shop
|
||||
{
|
||||
|
||||
public sealed class ShopView : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform itemContainer;
|
||||
[SerializeField] private ShopItemView itemPrefab;
|
||||
[SerializeField] private TMP_Text currencyText;
|
||||
[SerializeField] private Button closeButton;
|
||||
|
||||
private readonly List<ShopItemView> spawnedItems = new();
|
||||
|
||||
public event Action<ModifierData> OnBuyClicked;
|
||||
|
||||
private void Awake()
|
||||
public class ShopView : MonoBehaviour
|
||||
{
|
||||
if (closeButton != null)
|
||||
closeButton.onClick.AddListener(Hide);
|
||||
}
|
||||
[SerializeField] private Transform itemContainer;
|
||||
[SerializeField] private ShopItemView itemPrefab;
|
||||
[SerializeField] private TMP_Text currencyText;
|
||||
[SerializeField] private Button closeButton;
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (closeButton != null)
|
||||
closeButton.onClick.RemoveListener(Hide);
|
||||
}
|
||||
private readonly List<ShopItemView> spawnedItems = new();
|
||||
|
||||
public void Show() => gameObject.SetActive(true);
|
||||
public void Hide() => gameObject.SetActive(false);
|
||||
public bool IsVisible => gameObject.activeSelf;
|
||||
public event Action<ModifierData> OnBuyClicked;
|
||||
|
||||
public void Populate(IReadOnlyList<ModifierData> catalog, ShopModel model)
|
||||
{
|
||||
ClearItems();
|
||||
|
||||
for (int i = 0; i < catalog.Count; i++)
|
||||
private void Awake()
|
||||
{
|
||||
var data = catalog[i];
|
||||
if (data == null) continue;
|
||||
|
||||
var item = Instantiate(itemPrefab, itemContainer);
|
||||
var state = model.GetItemState(data);
|
||||
item.Setup(data, state);
|
||||
item.OnBuyClicked += HandleBuy;
|
||||
spawnedItems.Add(item);
|
||||
if (closeButton != null)
|
||||
closeButton.onClick.AddListener(Hide);
|
||||
}
|
||||
}
|
||||
|
||||
public void RefreshStates(IReadOnlyList<ModifierData> catalog, ShopModel model)
|
||||
{
|
||||
for (int i = 0; i < spawnedItems.Count && i < catalog.Count; i++)
|
||||
private void OnDestroy()
|
||||
{
|
||||
var state = model.GetItemState(catalog[i]);
|
||||
spawnedItems[i].SetState(state);
|
||||
if (closeButton != null)
|
||||
closeButton.onClick.RemoveListener(Hide);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateCurrencyDisplay(int currency)
|
||||
{
|
||||
if (currencyText != null)
|
||||
currencyText.text = currency.ToString();
|
||||
}
|
||||
public void Show() => gameObject.SetActive(true);
|
||||
public void Hide() => gameObject.SetActive(false);
|
||||
public bool IsVisible => gameObject.activeSelf;
|
||||
|
||||
private void ClearItems()
|
||||
{
|
||||
for (int i = 0; i < spawnedItems.Count; i++)
|
||||
public void Populate(IReadOnlyList<ModifierData> catalog, ShopModel model)
|
||||
{
|
||||
spawnedItems[i].OnBuyClicked -= HandleBuy;
|
||||
Destroy(spawnedItems[i].gameObject);
|
||||
}
|
||||
spawnedItems.Clear();
|
||||
}
|
||||
ClearItems();
|
||||
|
||||
private void HandleBuy(ModifierData data) => OnBuyClicked?.Invoke(data);
|
||||
}
|
||||
for (int i = 0; i < catalog.Count; i++)
|
||||
{
|
||||
var data = catalog[i];
|
||||
if (data == null) continue;
|
||||
|
||||
var item = Instantiate(itemPrefab, itemContainer);
|
||||
var state = model.GetItemState(data);
|
||||
item.Setup(data, state);
|
||||
item.OnBuyClicked += HandleBuy;
|
||||
spawnedItems.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public void RefreshStates(IReadOnlyList<ModifierData> catalog, ShopModel model)
|
||||
{
|
||||
for (int i = 0; i < spawnedItems.Count && i < catalog.Count; i++)
|
||||
{
|
||||
var state = model.GetItemState(catalog[i]);
|
||||
spawnedItems[i].SetState(state);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateCurrencyDisplay(int currency)
|
||||
{
|
||||
if (currencyText != null)
|
||||
currencyText.text = currency.ToString();
|
||||
}
|
||||
|
||||
private void ClearItems()
|
||||
{
|
||||
for (int i = 0; i < spawnedItems.Count; i++)
|
||||
{
|
||||
spawnedItems[i].OnBuyClicked -= HandleBuy;
|
||||
Destroy(spawnedItems[i].gameObject);
|
||||
}
|
||||
spawnedItems.Clear();
|
||||
}
|
||||
|
||||
private void HandleBuy(ModifierData data) => OnBuyClicked?.Invoke(data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user