mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 12:37:12 +08:00
(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Item;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Inventory
|
||||
{
|
||||
public Entity From { get; private set; }
|
||||
private readonly List<InventorySlot> _slots;
|
||||
public int Capacity { get; private set; }
|
||||
|
||||
public event System.Action<Item.ItemResource, int> OnItemAdded;
|
||||
public event System.Action<Item.ItemResource, int> OnItemRemoved;
|
||||
public event System.Action OnInventoryChanged;
|
||||
|
||||
public Inventory(Entity owner, int capacity = 20)
|
||||
{
|
||||
@@ -21,7 +17,17 @@ namespace Entity
|
||||
_slots = new List<InventorySlot>(Capacity);
|
||||
}
|
||||
|
||||
public int AddItem(Item.ItemResource itemResource, int quantity)
|
||||
public Entity From { get; }
|
||||
public int Capacity { get; }
|
||||
|
||||
public int OccupiedSlotsCount => _slots.Count;
|
||||
public int AvailableSlotsCount => Capacity - _slots.Count;
|
||||
|
||||
public event Action<ItemResource, int> OnItemAdded;
|
||||
public event Action<ItemResource, int> OnItemRemoved;
|
||||
public event Action OnInventoryChanged;
|
||||
|
||||
public int AddItem(ItemResource itemResource, int quantity)
|
||||
{
|
||||
if (itemResource == null || quantity <= 0)
|
||||
{
|
||||
@@ -35,7 +41,6 @@ namespace Entity
|
||||
|
||||
// 1. 尝试堆叠到现有槽位 (使用 DefName 进行比较)
|
||||
if (itemResource.MaxStack > 1)
|
||||
{
|
||||
foreach (var slot in _slots.Where(s =>
|
||||
s.Item != null && s.Item.DefName == itemResource.DefName && !s.IsFull))
|
||||
{
|
||||
@@ -44,7 +49,6 @@ namespace Entity
|
||||
addedTotal += addedToSlot;
|
||||
if (remainingQuantity <= 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果还有剩余,尝试添加到新槽位
|
||||
while (remainingQuantity > 0 && _slots.Count < Capacity)
|
||||
@@ -63,15 +67,13 @@ namespace Entity
|
||||
}
|
||||
|
||||
if (remainingQuantity > 0)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Inventory for {From?.ToString() ?? "Unknown"} is full or cannot stack. {remainingQuantity} of {itemResource.Name} (DefName: {itemResource.DefName}) could not be added.");
|
||||
}
|
||||
|
||||
return remainingQuantity;
|
||||
}
|
||||
|
||||
public int RemoveItem(Item.ItemResource itemResource, int quantity)
|
||||
public int RemoveItem(ItemResource itemResource, int quantity)
|
||||
{
|
||||
if (itemResource == null || quantity <= 0)
|
||||
{
|
||||
@@ -94,19 +96,13 @@ namespace Entity
|
||||
remainingQuantity -= removedFromSlot;
|
||||
removedTotal += removedFromSlot;
|
||||
|
||||
if (slot.IsEmpty)
|
||||
{
|
||||
slotsToClean.Add(slot);
|
||||
}
|
||||
if (slot.IsEmpty) slotsToClean.Add(slot);
|
||||
|
||||
if (remainingQuantity <= 0) break;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var slot in slotsToClean)
|
||||
{
|
||||
_slots.Remove(slot);
|
||||
}
|
||||
foreach (var slot in slotsToClean) _slots.Remove(slot);
|
||||
|
||||
if (removedTotal > 0)
|
||||
{
|
||||
@@ -115,21 +111,19 @@ namespace Entity
|
||||
}
|
||||
|
||||
if (remainingQuantity > 0)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"Inventory for {From?.ToString() ?? "Unknown"}: Not enough {itemResource.Name} (DefName: {itemResource.DefName}). {remainingQuantity} could not be removed.");
|
||||
}
|
||||
|
||||
return remainingQuantity;
|
||||
}
|
||||
|
||||
public bool HasItem(Item.ItemResource itemResource, int quantity = 1)
|
||||
public bool HasItem(ItemResource itemResource, int quantity = 1)
|
||||
{
|
||||
if (itemResource == null || quantity <= 0) return false;
|
||||
return GetItemCount(itemResource) >= quantity;
|
||||
}
|
||||
|
||||
public int GetItemCount(Item.ItemResource itemResource)
|
||||
public int GetItemCount(ItemResource itemResource)
|
||||
{
|
||||
if (itemResource == null) return 0;
|
||||
// 使用 DefName 进行计数
|
||||
@@ -141,28 +135,23 @@ namespace Entity
|
||||
{
|
||||
return _slots.AsReadOnly();
|
||||
}
|
||||
public InventorySlot GetSlot(Item.ItemResource itemResource)
|
||||
|
||||
public InventorySlot GetSlot(ItemResource itemResource)
|
||||
{
|
||||
if (itemResource == null) return null;
|
||||
|
||||
foreach (var slot in _slots)
|
||||
{
|
||||
if (slot.Item != null && slot.Item.DefName == itemResource.DefName)
|
||||
{
|
||||
return slot;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public InventorySlot GetSlot(int i)
|
||||
{
|
||||
if (i < 0 || i >= OccupiedSlotsCount)
|
||||
return null;
|
||||
return _slots[i];
|
||||
}
|
||||
|
||||
public int OccupiedSlotsCount => _slots.Count;
|
||||
public int AvailableSlotsCount => Capacity - _slots.Count;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user