Add Inventory system with Inventory, InventorySlot, and StatModifierItem classes; update character attributes to include level and experience
This commit is contained in:
@@ -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
|
Reference in New Issue
Block a user