Add Shop system with ShopManager, ShopUI, and ShopSlotUI; implement item purchasing and shop UI functionality
This commit is contained in:
@@ -28,6 +28,7 @@ namespace Data
|
||||
[OdinSerialize] public float armor = 0f;
|
||||
[OdinSerialize] public int level = 1;
|
||||
[OdinSerialize] public int experience = 0;
|
||||
[OdinSerialize] public int baseExperienceToLevelUp = 100;
|
||||
|
||||
[OdinSerialize, PropertyTooltip("This is damage multiplier")]
|
||||
public float damage = 1f;
|
||||
@@ -54,6 +55,7 @@ namespace Data
|
||||
|
||||
public event Action<int> OnExperienceChanged;
|
||||
public event Action<int> OnLevelChanged;
|
||||
public event Action OnLevelUp;
|
||||
|
||||
public float Health
|
||||
{
|
||||
@@ -174,7 +176,15 @@ namespace Data
|
||||
experience = value;
|
||||
OnExperienceChanged?.Invoke(experience);
|
||||
|
||||
//TODO: Implement level up logic
|
||||
if (experience >= ExperienceToNextLevel())
|
||||
{
|
||||
Level++;
|
||||
experience -= ExperienceToNextLevel();
|
||||
}
|
||||
else if (experience < 0)
|
||||
{
|
||||
experience = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,6 +196,7 @@ namespace Data
|
||||
if (level == value) return;
|
||||
level = value;
|
||||
OnLevelChanged?.Invoke(level);
|
||||
OnLevelUp?.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,5 +338,10 @@ namespace Data
|
||||
Level = 1;
|
||||
Experience = 0;
|
||||
}
|
||||
|
||||
private int ExperienceToNextLevel()
|
||||
{
|
||||
return (int)(baseExperienceToLevelUp * Math.Pow(Level, 2));
|
||||
}
|
||||
}
|
||||
}
|
@@ -12,8 +12,31 @@ namespace Inventory
|
||||
public string itemName;
|
||||
[TextArea] public string description;
|
||||
public Sprite icon;
|
||||
public int price;
|
||||
|
||||
public List<IStatModifier> cures = new();
|
||||
public List<IStatModifier> curses = new();
|
||||
|
||||
[Button("Build Description")]
|
||||
private void BuildDescription()
|
||||
{
|
||||
var descriptionBuilder = new System.Text.StringBuilder();
|
||||
foreach (var modifier in cures)
|
||||
{
|
||||
if (descriptionBuilder.Length > 0) descriptionBuilder.Append(", ");
|
||||
|
||||
var desc = $"Cure: {modifier.Description}";
|
||||
descriptionBuilder.Append(desc);
|
||||
}
|
||||
|
||||
foreach (var modifier in curses)
|
||||
{
|
||||
if (descriptionBuilder.Length > 0) descriptionBuilder.Append(", ");
|
||||
var desc = $"Curse: {modifier.Description}";
|
||||
descriptionBuilder.Append(desc);
|
||||
}
|
||||
|
||||
description = descriptionBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
@@ -10,5 +10,6 @@ namespace Inventory
|
||||
[OdinSerialize, TextArea] public string description;
|
||||
[OdinSerialize] public GameObject prefab;
|
||||
[OdinSerialize] public Sprite icon;
|
||||
[OdinSerialize] public int price;
|
||||
}
|
||||
}
|
@@ -11,7 +11,7 @@ namespace Modifiers
|
||||
|
||||
public Stat stat;
|
||||
public float percent;
|
||||
public string Description => $"{stat} +{percent * 100}%";
|
||||
public string Description => GetDescription();
|
||||
|
||||
public void Apply(CharacterAttributes attributes)
|
||||
{
|
||||
@@ -55,5 +55,11 @@ namespace Modifiers
|
||||
_ => throw new System.ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
private string GetDescription()
|
||||
{
|
||||
var sign = percent >= 0 ? "+" : "";
|
||||
return $"{stat} {sign}{percent * 100}%";
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Shop.meta
Normal file
3
Assets/Scripts/Shop.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b873cfd693194508ae2ef2947a6c42fd
|
||||
timeCreated: 1752272703
|
96
Assets/Scripts/Shop/ShopManager.cs
Normal file
96
Assets/Scripts/Shop/ShopManager.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Inventory;
|
||||
using Sirenix.Serialization;
|
||||
using Systems;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Shop
|
||||
{
|
||||
public class ShopManager : MonoBehaviour
|
||||
{
|
||||
private List<StatModifierItem> currentItemChoices = new();
|
||||
private List<WeaponItem> currentWeaponChoices = new();
|
||||
|
||||
[SerializeField] private ShopUI shopUI;
|
||||
[SerializeField] private InventoryManager inventoryManager;
|
||||
|
||||
[SerializeField] private int itemsPerShop = 4;
|
||||
[OdinSerialize, SerializeField] private List<StatModifierItem> possibleItems = new();
|
||||
[OdinSerialize, SerializeField] private List<WeaponItem> possibleWeapons = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
// GameManager.Instance.OnRoundEnd += OpenShop;
|
||||
GameManager.Instance.OnStoreOpen += OpenShop;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
// GameManager.Instance.OnRoundEnd -= OpenShop;
|
||||
GameManager.Instance.OnStoreOpen -= OpenShop;
|
||||
}
|
||||
|
||||
public void CloseShop()
|
||||
{
|
||||
shopUI.Hide();
|
||||
Time.timeScale = 1f;
|
||||
}
|
||||
|
||||
public void BuyItem(StatModifierItem item, int price)
|
||||
{
|
||||
if (GameManager.Instance.Coins < price) return;
|
||||
|
||||
GameManager.Instance.SpendCoins(price);
|
||||
inventoryManager.EquipItem(item);
|
||||
shopUI.MarkAsPurchased(item);
|
||||
}
|
||||
|
||||
public void BuyWeapon(WeaponItem weapon, int price)
|
||||
{
|
||||
if (GameManager.Instance.Coins < price) return;
|
||||
|
||||
GameManager.Instance.SpendCoins(price);
|
||||
inventoryManager.EquipWeapon(weapon);
|
||||
shopUI.MarkAsPurchased(weapon);
|
||||
}
|
||||
|
||||
public void RerollShop()
|
||||
{
|
||||
currentItemChoices = DrawRandomItems(possibleItems, itemsPerShop);
|
||||
currentWeaponChoices = DrawRandomItems(possibleWeapons, itemsPerShop);
|
||||
|
||||
shopUI.Show(currentItemChoices, currentWeaponChoices, this);
|
||||
}
|
||||
|
||||
private void OpenShop()
|
||||
{
|
||||
OpenShop(GameManager.Instance.CurrentRound);
|
||||
}
|
||||
|
||||
private void OpenShop(int round)
|
||||
{
|
||||
currentItemChoices = DrawRandomItems(possibleItems, itemsPerShop);
|
||||
currentWeaponChoices = DrawRandomItems(possibleWeapons, itemsPerShop);
|
||||
|
||||
shopUI.Show(currentItemChoices, currentWeaponChoices, this);
|
||||
Time.timeScale = 0f;
|
||||
}
|
||||
|
||||
private List<T> DrawRandomItems<T>(List<T> pool, int count)
|
||||
{
|
||||
var result = new List<T>();
|
||||
var poolCopy = new List<T>(pool);
|
||||
|
||||
for (var i = 0; i < count && poolCopy.Count > 0; i++)
|
||||
{
|
||||
var idx = Random.Range(0, poolCopy.Count);
|
||||
result.Add(poolCopy[idx]);
|
||||
poolCopy.RemoveAt(idx);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Shop/ShopManager.cs.meta
Normal file
3
Assets/Scripts/Shop/ShopManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f37c728615643a1abc988bbfd34986c
|
||||
timeCreated: 1752272671
|
56
Assets/Scripts/Shop/ShopSlotUI.cs
Normal file
56
Assets/Scripts/Shop/ShopSlotUI.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using Inventory;
|
||||
using Systems;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Shop
|
||||
{
|
||||
public class ShopSlotUI : MonoBehaviour
|
||||
{
|
||||
private ScriptableObject item;
|
||||
private ShopManager shopManager;
|
||||
private int price;
|
||||
|
||||
[SerializeField] private Image icon;
|
||||
[SerializeField] private TextMeshProUGUI nameText;
|
||||
[SerializeField] private TextMeshProUGUI descriptionText;
|
||||
[SerializeField] private TextMeshProUGUI priceText;
|
||||
[SerializeField] private Button purchaseButton;
|
||||
|
||||
public void Setup(StatModifierItem item, ShopManager manager)
|
||||
{
|
||||
this.item = item;
|
||||
shopManager = manager;
|
||||
price = item.price;
|
||||
|
||||
icon.sprite = item.icon;
|
||||
nameText.text = item.name;
|
||||
descriptionText.text = item.description;
|
||||
priceText.text = $"Price: {price}";
|
||||
|
||||
purchaseButton.interactable = GameManager.Instance.Coins >= price;
|
||||
purchaseButton.onClick.AddListener(() => shopManager.BuyItem(item, price));
|
||||
}
|
||||
|
||||
public void Setup(WeaponItem weapon, ShopManager manager)
|
||||
{
|
||||
item = weapon;
|
||||
shopManager = manager;
|
||||
price = weapon.price;
|
||||
icon.sprite = weapon.icon;
|
||||
nameText.text = weapon.weaponName;
|
||||
descriptionText.text = weapon.description;
|
||||
priceText.text = $"Price: {price}";
|
||||
purchaseButton.interactable = GameManager.Instance.Coins >= price;
|
||||
purchaseButton.onClick.AddListener(() => shopManager.BuyWeapon(weapon, price));
|
||||
}
|
||||
|
||||
public bool MatchesItem(ScriptableObject item) => this.item == item;
|
||||
|
||||
public void MarkAsPurchased()
|
||||
{
|
||||
purchaseButton.interactable = false;
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Shop/ShopSlotUI.cs.meta
Normal file
3
Assets/Scripts/Shop/ShopSlotUI.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d76cc9c15ed441b930dbe52e5e9af5f
|
||||
timeCreated: 1752273703
|
83
Assets/Scripts/Shop/ShopUI.cs
Normal file
83
Assets/Scripts/Shop/ShopUI.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Inventory;
|
||||
using Systems;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Shop
|
||||
{
|
||||
public class ShopUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject shopPanel;
|
||||
[SerializeField] private Transform itemSlotParent;
|
||||
[SerializeField] private Transform weaponSlotParent;
|
||||
[SerializeField] private ShopSlotUI slotPrefab;
|
||||
[SerializeField] private TextMeshProUGUI roundsText;
|
||||
|
||||
private List<ShopSlotUI> currentSlots = new();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
GameManager.Instance.OnRoundEnd += UpdateRoundText;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
GameManager.Instance.OnRoundEnd -= UpdateRoundText;
|
||||
}
|
||||
|
||||
public void Show(List<StatModifierItem> items, List<WeaponItem> weapons, ShopManager shopManager)
|
||||
{
|
||||
GameManager.Instance.StoreIsClosed = false;
|
||||
UpdateRoundText(GameManager.Instance.CurrentRound);
|
||||
|
||||
shopPanel.SetActive(true);
|
||||
ClearSlots();
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
var slot = Instantiate(slotPrefab, itemSlotParent);
|
||||
slot.Setup(item, shopManager);
|
||||
currentSlots.Add(slot);
|
||||
}
|
||||
|
||||
foreach (var weapon in weapons)
|
||||
{
|
||||
var slot = Instantiate(slotPrefab, weaponSlotParent);
|
||||
slot.Setup(weapon, shopManager);
|
||||
currentSlots.Add(slot);
|
||||
}
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
GameManager.Instance.StoreIsClosed = true;
|
||||
shopPanel.SetActive(false);
|
||||
ClearSlots();
|
||||
}
|
||||
|
||||
public void MarkAsPurchased(ScriptableObject item)
|
||||
{
|
||||
foreach (var slot in currentSlots)
|
||||
{
|
||||
if (slot.MatchesItem(item)) slot.MarkAsPurchased();
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearSlots()
|
||||
{
|
||||
foreach (var slot in currentSlots) Destroy(slot.gameObject);
|
||||
|
||||
currentSlots.Clear();
|
||||
}
|
||||
|
||||
private void UpdateRoundText(int round)
|
||||
{
|
||||
var nextRound = Mathf.Min(round + 1, GameManager.Instance.MaxRounds);
|
||||
roundsText.text = $"Round: {nextRound}/{GameManager.Instance.MaxRounds}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
3
Assets/Scripts/Shop/ShopUI.cs.meta
Normal file
3
Assets/Scripts/Shop/ShopUI.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: abe11e705b4f47feb25a4e4845d1e6e9
|
||||
timeCreated: 1752272723
|
@@ -1,15 +1,21 @@
|
||||
using Interfaces;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
public class EnemyDeathBehavior : MonoBehaviour, IDeathBehavior
|
||||
{
|
||||
[OdinSerialize, SerializeField] private int expReward = 5;
|
||||
[OdinSerialize, SerializeField] private int coinReward = 1;
|
||||
|
||||
public void Die()
|
||||
{
|
||||
GameManager.Instance.Player.attributes.ModifyExperience(expReward);
|
||||
GameManager.Instance.AddCoins(coinReward);
|
||||
Destroy(gameObject);
|
||||
|
||||
// later let's add particle effects, sound effects, etc.
|
||||
// and give player experience points
|
||||
}
|
||||
}
|
||||
}
|
88
Assets/Scripts/Systems/GameManager.cs
Normal file
88
Assets/Scripts/Systems/GameManager.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Data;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
private float timer;
|
||||
|
||||
public static GameManager Instance { get; private set; }
|
||||
|
||||
[OdinSerialize, SerializeField] private int currentRound = 1;
|
||||
[OdinSerialize, SerializeField] private int coins = 0;
|
||||
[OdinSerialize, SerializeField] private float roundTime = 60f;
|
||||
[OdinSerialize, SerializeField] private int maxRounds = 20;
|
||||
|
||||
[OdinSerialize, SerializeField] private Character player;
|
||||
|
||||
public Character Player => player;
|
||||
public int Coins => coins;
|
||||
public int CurrentRound => currentRound;
|
||||
public float RoundTime => roundTime;
|
||||
public int MaxRounds => maxRounds;
|
||||
public bool StoreIsClosed { get; set; } = true;
|
||||
|
||||
public event Action<int> OnRoundStart;
|
||||
public event Action<int> OnRoundEnd;
|
||||
public event Action OnStoreOpen;
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
StartCoroutine(RoundLoop());
|
||||
}
|
||||
|
||||
private IEnumerator RoundLoop()
|
||||
{
|
||||
OnStoreOpen?.Invoke();
|
||||
yield return new WaitUntil(() => StoreIsClosed);
|
||||
|
||||
for (currentRound = 1; currentRound <= maxRounds; currentRound++)
|
||||
{
|
||||
OnRoundStart?.Invoke(currentRound);
|
||||
timer = roundTime;
|
||||
|
||||
while (timer > 0)
|
||||
{
|
||||
timer -= Time.deltaTime;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
OnRoundEnd?.Invoke(currentRound);
|
||||
StoreIsClosed = false;
|
||||
OnStoreOpen?.Invoke();
|
||||
yield return new WaitUntil(() => StoreIsClosed);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddCoins(int amount)
|
||||
{
|
||||
coins += amount;
|
||||
}
|
||||
|
||||
public void SpendCoins(int amount)
|
||||
{
|
||||
if (coins >= amount)
|
||||
{
|
||||
coins -= amount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Systems/GameManager.cs.meta
Normal file
3
Assets/Scripts/Systems/GameManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 41ea29e7d7d04eb5be628e56520e9bbd
|
||||
timeCreated: 1752271597
|
Reference in New Issue
Block a user