Add Inventory system with Inventory, InventorySlot, and StatModifierItem classes; update character attributes to include level and experience
This commit is contained in:
3
Assets/Scripts/Inventory.meta
Normal file
3
Assets/Scripts/Inventory.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 845fc46be00c4d5896b8794c59b19bfa
|
||||
timeCreated: 1752268724
|
66
Assets/Scripts/Inventory/Inventory.cs
Normal file
66
Assets/Scripts/Inventory/Inventory.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System.Collections.Generic;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Inventory
|
||||
{
|
||||
public class Inventory : MonoBehaviour
|
||||
{
|
||||
[OdinSerialize, SerializeField] private List<InventorySlot> weaponSlots = new();
|
||||
[OdinSerialize, SerializeField] private List<InventorySlot> itemSlots = new();
|
||||
|
||||
public IReadOnlyList<InventorySlot> WeaponSlots => weaponSlots.AsReadOnly();
|
||||
public IReadOnlyList<InventorySlot> ItemSlots => itemSlots.AsReadOnly();
|
||||
|
||||
public void AddWeapon(WeaponItem weaponItem)
|
||||
{
|
||||
foreach (var slot in weaponSlots)
|
||||
{
|
||||
if (slot.item == weaponItem) return;
|
||||
}
|
||||
|
||||
weaponSlots.Add(new InventorySlot { item = weaponItem, quantity = 1 });
|
||||
}
|
||||
|
||||
public void AddItem(ScriptableObject item)
|
||||
{
|
||||
foreach (var slot in itemSlots)
|
||||
{
|
||||
if (slot.item != item) continue;
|
||||
|
||||
slot.quantity++;
|
||||
return;
|
||||
}
|
||||
|
||||
itemSlots.Add(new InventorySlot { item = item, quantity = 1 });
|
||||
}
|
||||
|
||||
public void RemoveWeapon(WeaponItem weaponItem)
|
||||
{
|
||||
for (var i = 0; i < weaponSlots.Count; i++)
|
||||
{
|
||||
if (weaponSlots[i].item != weaponItem) continue;
|
||||
|
||||
if (--weaponSlots[i].quantity <= 0)
|
||||
{
|
||||
weaponSlots.RemoveAt(i);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveItem(ScriptableObject item)
|
||||
{
|
||||
for (var i = 0; i < itemSlots.Count; i++)
|
||||
{
|
||||
if (itemSlots[i].item != item) continue;
|
||||
|
||||
if (--itemSlots[i].quantity <= 0)
|
||||
{
|
||||
itemSlots.RemoveAt(i);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Inventory/Inventory.cs.meta
Normal file
3
Assets/Scripts/Inventory/Inventory.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9810b6967ffd4c3589464d687e982ee6
|
||||
timeCreated: 1752268911
|
13
Assets/Scripts/Inventory/InventorySlot.cs
Normal file
13
Assets/Scripts/Inventory/InventorySlot.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Inventory
|
||||
{
|
||||
[Serializable]
|
||||
public class InventorySlot
|
||||
{
|
||||
[OdinSerialize] public ScriptableObject item;
|
||||
[OdinSerialize] public int quantity;
|
||||
}
|
||||
}
|
3
Assets/Scripts/Inventory/InventorySlot.cs.meta
Normal file
3
Assets/Scripts/Inventory/InventorySlot.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88a6fde0304145ba9f9cdd1b2782e363
|
||||
timeCreated: 1752268731
|
19
Assets/Scripts/Inventory/StatModifierItem.cs
Normal file
19
Assets/Scripts/Inventory/StatModifierItem.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Generic;
|
||||
using Interfaces;
|
||||
using Sirenix.OdinInspector;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Inventory
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Game/Item/StatModifierItem")]
|
||||
public class StatModifierItem : SerializedScriptableObject
|
||||
{
|
||||
public string itemName;
|
||||
[TextArea] public string description;
|
||||
public Sprite icon;
|
||||
|
||||
public List<IStatModifier> cures = new();
|
||||
public List<IStatModifier> curses = new();
|
||||
}
|
||||
}
|
3
Assets/Scripts/Inventory/StatModifierItem.cs.meta
Normal file
3
Assets/Scripts/Inventory/StatModifierItem.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c437a5565f39495281bf1dd14dd6ba40
|
||||
timeCreated: 1752268788
|
14
Assets/Scripts/Inventory/WeaponItem.cs
Normal file
14
Assets/Scripts/Inventory/WeaponItem.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Inventory
|
||||
{
|
||||
[CreateAssetMenu(menuName = "Game/Item/WeaponItem")]
|
||||
public class WeaponItem : ScriptableObject
|
||||
{
|
||||
[OdinSerialize] public string weaponName;
|
||||
[OdinSerialize, TextArea] public string description;
|
||||
[OdinSerialize] public GameObject prefab;
|
||||
[OdinSerialize] public Sprite icon;
|
||||
}
|
||||
}
|
3
Assets/Scripts/Inventory/WeaponItem.cs.meta
Normal file
3
Assets/Scripts/Inventory/WeaponItem.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: efa44a330e8343c08e3af638d466c118
|
||||
timeCreated: 1752268856
|
@@ -1,25 +1,27 @@
|
||||
using System;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
|
||||
namespace Modifiers
|
||||
{
|
||||
[Serializable]
|
||||
public class PercentStatModifier : IStatModifier
|
||||
{
|
||||
private float lastAppliedAmount;
|
||||
|
||||
public Stat Stat { get; set; }
|
||||
public float Percent { get; set; }
|
||||
public string Description => $"{Stat} +{Percent * 100}%";
|
||||
public Stat stat;
|
||||
public float percent;
|
||||
public string Description => $"{stat} +{percent * 100}%";
|
||||
|
||||
public void Apply(CharacterAttributes attributes)
|
||||
{
|
||||
var baseValue = GetBaseValue<float>(attributes);
|
||||
lastAppliedAmount = baseValue * Percent;
|
||||
lastAppliedAmount = baseValue * percent;
|
||||
|
||||
var flatModifier = new FlatStatModifier
|
||||
{
|
||||
value = lastAppliedAmount,
|
||||
stat = Stat
|
||||
stat = stat
|
||||
};
|
||||
|
||||
flatModifier.Apply(attributes);
|
||||
@@ -30,7 +32,7 @@ namespace Modifiers
|
||||
var flatModifier = new FlatStatModifier
|
||||
{
|
||||
value = -lastAppliedAmount,
|
||||
stat = Stat
|
||||
stat = stat
|
||||
};
|
||||
|
||||
flatModifier.Apply(attributes);
|
||||
@@ -38,7 +40,7 @@ namespace Modifiers
|
||||
|
||||
private T GetBaseValue<T>(CharacterAttributes attributes)
|
||||
{
|
||||
return Stat switch
|
||||
return stat switch
|
||||
{
|
||||
Stat.Health => (T)(object)attributes.Health,
|
||||
Stat.MaxHealth => (T)(object)attributes.MaxHealth,
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using Interfaces;
|
||||
using KBCore.Refs;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -10,7 +11,7 @@ namespace Systems
|
||||
{
|
||||
[OdinSerialize] private List<IStatModifier> activeModifiers = new();
|
||||
|
||||
[SerializeField] private Character character;
|
||||
[SerializeField, Self] private Character character;
|
||||
|
||||
public void EquipItem(IStatModifier modifier)
|
||||
{
|
||||
|
@@ -1,4 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Data;
|
||||
using KBCore.Refs;
|
||||
using Sirenix.Serialization;
|
||||
using UnityEngine;
|
||||
using Weapons;
|
||||
@@ -8,5 +10,57 @@ namespace Systems
|
||||
public class CharacterWeaponsManager : MonoBehaviour
|
||||
{
|
||||
[OdinSerialize] private List<Weapon> equippedWeapons = new();
|
||||
|
||||
[SerializeField] private IReadOnlyList<Weapon> EquippedWeapons => equippedWeapons.AsReadOnly();
|
||||
[SerializeField, Self] private Character character;
|
||||
|
||||
public Weapon EquipWeapon(GameObject weaponPrefab)
|
||||
{
|
||||
var weaponObject = Instantiate(weaponPrefab, transform);
|
||||
if (weaponObject.TryGetComponent(out Weapon weapon))
|
||||
{
|
||||
weapon.character = character;
|
||||
equippedWeapons.Add(weapon);
|
||||
weapon.enabled = true;
|
||||
return weapon;
|
||||
}
|
||||
|
||||
Destroy(weaponObject);
|
||||
return null;
|
||||
}
|
||||
|
||||
public void UnequipWeapon(Weapon weapon, bool destroy = true)
|
||||
{
|
||||
if (!equippedWeapons.Remove(weapon)) return;
|
||||
weapon.enabled = false;
|
||||
|
||||
if (!destroy) return;
|
||||
Destroy(weapon.gameObject);
|
||||
}
|
||||
|
||||
public void DisableAllWeapons()
|
||||
{
|
||||
foreach (var weapon in equippedWeapons)
|
||||
{
|
||||
weapon.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void EnableAllWeapons()
|
||||
{
|
||||
foreach (var weapon in equippedWeapons)
|
||||
{
|
||||
weapon.enabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearWeapons()
|
||||
{
|
||||
foreach (var weapon in equippedWeapons)
|
||||
{
|
||||
Destroy(weapon.gameObject);
|
||||
}
|
||||
equippedWeapons.Clear();
|
||||
}
|
||||
}
|
||||
}
|
86
Assets/Scripts/Systems/InventoryManager.cs
Normal file
86
Assets/Scripts/Systems/InventoryManager.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Inventory;
|
||||
using KBCore.Refs;
|
||||
using UnityEngine;
|
||||
using Weapons;
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField, Self] private Inventory.Inventory inventory;
|
||||
[SerializeField, Self] private CharacterModifierManager characterModifierManager;
|
||||
[SerializeField, Self] private CharacterWeaponsManager characterWeaponsManager;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (inventory == null)
|
||||
{
|
||||
Debug.LogError("Inventory is not assigned in InventoryManager.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (characterModifierManager == null)
|
||||
{
|
||||
Debug.LogError("CharacterModifierManager is not assigned in InventoryManager.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (characterWeaponsManager == null)
|
||||
{
|
||||
Debug.LogError("CharacterWeaponsManager is not assigned in InventoryManager.");
|
||||
return;
|
||||
}
|
||||
|
||||
EquipAllItems();
|
||||
EquipAllWeapons();
|
||||
}
|
||||
|
||||
public void EquipItem(StatModifierItem item)
|
||||
{
|
||||
inventory.AddItem(item);
|
||||
foreach (var cure in item.cures) characterModifierManager.EquipItem(cure);
|
||||
foreach (var curse in item.curses) characterModifierManager.EquipItem(curse);
|
||||
}
|
||||
|
||||
public void UnequipItem(StatModifierItem item)
|
||||
{
|
||||
if (inventory.ItemSlots.All(slot => slot.item != item)) return;
|
||||
inventory.RemoveItem(item);
|
||||
|
||||
foreach (var cure in item.cures) characterModifierManager.UnequipItem(cure);
|
||||
foreach (var curse in item.curses) characterModifierManager.UnequipItem(curse);
|
||||
}
|
||||
|
||||
public void EquipWeapon(WeaponItem weaponItem)
|
||||
{
|
||||
inventory.AddWeapon(weaponItem);
|
||||
characterWeaponsManager.EquipWeapon(weaponItem.prefab);
|
||||
}
|
||||
|
||||
public void UnequipWeapon(WeaponItem weaponItem)
|
||||
{
|
||||
if (inventory.WeaponSlots.All(slot => slot.item != weaponItem)) return;
|
||||
inventory.RemoveWeapon(weaponItem);
|
||||
weaponItem.prefab.TryGetComponent(out Weapon weapon);
|
||||
characterWeaponsManager.UnequipWeapon(weapon);
|
||||
}
|
||||
|
||||
private void EquipAllWeapons()
|
||||
{
|
||||
foreach (var slot in inventory.WeaponSlots)
|
||||
{
|
||||
EquipWeapon(slot.item as WeaponItem);
|
||||
}
|
||||
}
|
||||
|
||||
private void EquipAllItems()
|
||||
{
|
||||
foreach (var slot in inventory.ItemSlots)
|
||||
{
|
||||
EquipItem(slot.item as StatModifierItem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Assets/Scripts/Systems/InventoryManager.cs.meta
Normal file
3
Assets/Scripts/Systems/InventoryManager.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 57782f9b469d49ab9f4ad6c6e1ddf844
|
||||
timeCreated: 1752269055
|
@@ -11,8 +11,8 @@ namespace Weapons
|
||||
{
|
||||
private float timer;
|
||||
|
||||
[SerializeField] protected Character character;
|
||||
[OdinSerialize, InlineProperty] public WeaponStats weaponStats = new();
|
||||
public Character character;
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
Reference in New Issue
Block a user