test mesh
This commit is contained in:
@@ -318,10 +318,11 @@ public class InventoryManager : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
remaining -= slot.amount;
|
||||
int cleared = slot.amount; // сохранить ДО Clear()
|
||||
remaining -= cleared;
|
||||
slot.Clear();
|
||||
OnItemRemoved?.Invoke(itemId, slot.amount);
|
||||
InventoryEvents.Instance?.InvokeItemRemoved(itemId, slot.amount);
|
||||
OnItemRemoved?.Invoke(itemId, cleared);
|
||||
InventoryEvents.Instance?.InvokeItemRemoved(itemId, cleared);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -342,10 +343,11 @@ public class InventoryManager : MonoBehaviour
|
||||
}
|
||||
else
|
||||
{
|
||||
remaining -= slot.amount;
|
||||
int cleared = slot.amount; // сохранить ДО Clear()
|
||||
remaining -= cleared;
|
||||
slot.Clear();
|
||||
OnItemRemoved?.Invoke(itemId, slot.amount);
|
||||
InventoryEvents.Instance?.InvokeItemRemoved(itemId, slot.amount);
|
||||
OnItemRemoved?.Invoke(itemId, cleared);
|
||||
InventoryEvents.Instance?.InvokeItemRemoved(itemId, cleared);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -447,7 +449,7 @@ public class InventoryManager : MonoBehaviour
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Поменять предметы между двумя слотами
|
||||
/// Поменять предметы между двумя слотами (по индексам, только основной инвентарь)
|
||||
/// </summary>
|
||||
public void SwapSlots(int fromIndex, int toIndex)
|
||||
{
|
||||
@@ -455,14 +457,19 @@ public class InventoryManager : MonoBehaviour
|
||||
toIndex < 0 || toIndex >= slots.Length)
|
||||
return;
|
||||
|
||||
var fromSlot = slots[fromIndex];
|
||||
var toSlot = slots[toIndex];
|
||||
SwapSlots(slots[fromIndex], slots[toIndex]);
|
||||
}
|
||||
|
||||
// Сохраняем данные
|
||||
var tempItem = fromSlot.item;
|
||||
/// <summary>
|
||||
/// Поменять предметы между любыми двумя слотами (инвентарь ↔ хотбар и т.д.)
|
||||
/// </summary>
|
||||
public void SwapSlots(Slot fromSlot, Slot toSlot)
|
||||
{
|
||||
if (fromSlot == null || toSlot == null || fromSlot == toSlot) return;
|
||||
|
||||
var tempItem = fromSlot.item;
|
||||
var tempAmount = fromSlot.amount;
|
||||
|
||||
// Меняем
|
||||
if (toSlot.IsEmpty)
|
||||
{
|
||||
fromSlot.Clear();
|
||||
@@ -541,6 +548,19 @@ public class InventoryManager : MonoBehaviour
|
||||
};
|
||||
}
|
||||
|
||||
if (hotbarSlots != null)
|
||||
{
|
||||
inventoryData.hotbarSlots = new SlotData[hotbarSlots.Length];
|
||||
for (int i = 0; i < hotbarSlots.Length; i++)
|
||||
{
|
||||
inventoryData.hotbarSlots[i] = new SlotData
|
||||
{
|
||||
itemId = hotbarSlots[i].item?.id,
|
||||
amount = hotbarSlots[i].amount
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return JsonUtility.ToJson(inventoryData, true);
|
||||
}
|
||||
|
||||
@@ -600,12 +620,20 @@ public class InventoryManager : MonoBehaviour
|
||||
{
|
||||
var slotData = inventoryData.slots[i];
|
||||
if (!string.IsNullOrEmpty(slotData.itemId) && _itemDict.TryGetValue(slotData.itemId, out ItemData item))
|
||||
{
|
||||
slots[i].SetItem(item, slotData.amount);
|
||||
}
|
||||
else
|
||||
{
|
||||
slots[i].Clear();
|
||||
}
|
||||
|
||||
if (inventoryData.hotbarSlots != null && hotbarSlots != null)
|
||||
{
|
||||
for (int i = 0; i < Mathf.Min(inventoryData.hotbarSlots.Length, hotbarSlots.Length); i++)
|
||||
{
|
||||
var slotData = inventoryData.hotbarSlots[i];
|
||||
if (!string.IsNullOrEmpty(slotData.itemId) && _itemDict.TryGetValue(slotData.itemId, out ItemData item))
|
||||
hotbarSlots[i].SetItem(item, slotData.amount);
|
||||
else
|
||||
hotbarSlots[i].Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -614,6 +642,7 @@ public class InventoryManager : MonoBehaviour
|
||||
class InventorySaveData
|
||||
{
|
||||
public SlotData[] slots;
|
||||
public SlotData[] hotbarSlots;
|
||||
}
|
||||
|
||||
[System.Serializable]
|
||||
@@ -623,6 +652,16 @@ public class InventoryManager : MonoBehaviour
|
||||
public int amount;
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
if (PlayerInput.Instance != null)
|
||||
{
|
||||
PlayerInput.Instance.OnToggleInventory -= ToggleInventory;
|
||||
PlayerInput.Instance.OnDropPressed -= DropSelectedItem;
|
||||
PlayerInput.Instance.OnHotbarSelect -= SelectHotbarSlot;
|
||||
}
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
SaveToFile();
|
||||
|
||||
Reference in New Issue
Block a user