完善背包系统,已验证无问题
parent
7eace863c7
commit
3f5a11e290
File diff suppressed because it is too large
Load Diff
|
@ -26,14 +26,14 @@ namespace BeiBao
|
||||||
Item manaPotion = new Item("Mana Potion", 2, 5);
|
Item manaPotion = new Item("Mana Potion", 2, 5);
|
||||||
|
|
||||||
// 添加物品到背包
|
// 添加物品到背包
|
||||||
inventory.AddItem(healthPotion, 15); // 添加 15 个 Health Potion
|
inventory.AddItem(healthPotion, 15, out var count); // 添加 15 个 Health Potion
|
||||||
inventory.AddItem(manaPotion, 7); // 添加 7 个 Mana Potion
|
inventory.AddItem(manaPotion, 7, out var count1); // 添加 7 个 Mana Potion
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.GetKeyDown(KeyCode.D))
|
if (Input.GetKeyDown(KeyCode.D))
|
||||||
{
|
{
|
||||||
// 从背包中取出物品
|
// 从背包中取出物品
|
||||||
inventory.RemoveItem(1, 5);
|
inventory.RemoveItem(1, 5, out var count);
|
||||||
Debug.Log("取出 5 个 Health Potion");
|
Debug.Log("取出 5 个 Health Potion");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,10 @@ namespace BeiBao
|
||||||
|
|
||||||
private List<InventorySlot> slots;
|
private List<InventorySlot> slots;
|
||||||
|
|
||||||
|
public IReadOnlyList<InventorySlot> Slots => slots;
|
||||||
|
|
||||||
|
public Action<IReadOnlyList<InventorySlot>> RefreshInventorySlot;
|
||||||
|
|
||||||
public Inventory(int slotCount)
|
public Inventory(int slotCount)
|
||||||
{
|
{
|
||||||
_slotCount = slotCount;
|
_slotCount = slotCount;
|
||||||
|
@ -28,7 +32,7 @@ namespace BeiBao
|
||||||
}
|
}
|
||||||
|
|
||||||
// 向背包中添加物品
|
// 向背包中添加物品
|
||||||
public bool AddItem(Item item, int quantity)
|
public bool AddItem(Item item, int quantity, out int restCount)
|
||||||
{
|
{
|
||||||
int remainingQuantity = quantity;
|
int remainingQuantity = quantity;
|
||||||
int tmpQuantity = quantity;
|
int tmpQuantity = quantity;
|
||||||
|
@ -37,29 +41,32 @@ namespace BeiBao
|
||||||
{
|
{
|
||||||
if (remainingQuantity == 0) break;
|
if (remainingQuantity == 0) break;
|
||||||
|
|
||||||
var tmpItem = new Item(item.ItemName, item.ItemID, item.MaxStack);
|
|
||||||
|
|
||||||
if (slot.IsEmpty)
|
if (slot.IsEmpty)
|
||||||
{
|
{
|
||||||
|
var tmpItem = new Item(item.ItemName, item.ItemID, item.MaxStack);
|
||||||
// 如果格子为空,直接放入物品
|
// 如果格子为空,直接放入物品
|
||||||
remainingQuantity = tmpItem.CheckAddQuantity(remainingQuantity);
|
remainingQuantity = tmpItem.CheckAddQuantity(remainingQuantity);
|
||||||
slot.AddItem(tmpItem, tmpQuantity - remainingQuantity);
|
slot.AddItem(tmpItem, tmpQuantity - remainingQuantity);
|
||||||
}
|
}
|
||||||
else if (slot.Item.ItemID == tmpItem.ItemID && slot.Item.Quantity < slot.Item.MaxStack)
|
else if (slot.Item.ItemID == item.ItemID && slot.Item.Quantity < slot.Item.MaxStack)
|
||||||
{
|
{
|
||||||
// 如果格子内物品与新物品相同,并且未满堆叠,堆叠物品
|
// 如果格子内物品与新物品相同,并且未满堆叠,堆叠物品
|
||||||
remainingQuantity = slot.Item.CheckAddQuantity(remainingQuantity);
|
remainingQuantity = slot.Item.CheckAddQuantity(remainingQuantity);
|
||||||
|
slot.Item.AddQuantity(tmpQuantity - remainingQuantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpQuantity = remainingQuantity;
|
tmpQuantity = remainingQuantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Log($"向背包中添加{item.ItemName} 数量为:{quantity}");
|
Debug.Log($"向背包中添加{item.ItemName} 数量为:{quantity}");
|
||||||
|
RefreshInventorySlot?.Invoke(Slots);
|
||||||
|
restCount = remainingQuantity;
|
||||||
return remainingQuantity == 0;
|
return remainingQuantity == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从背包中取出物品
|
// 从背包中取出物品
|
||||||
public bool RemoveItem(int itemID, int quantity)
|
public bool RemoveItem(int itemID, int quantity, out int restCount)
|
||||||
{
|
{
|
||||||
int remainingQuantity = quantity;
|
int remainingQuantity = quantity;
|
||||||
int tmpQuantity = quantity;
|
int tmpQuantity = quantity;
|
||||||
|
@ -78,6 +85,8 @@ namespace BeiBao
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Log($"向背包中取出物品id{itemID} 数量为:{quantity}");
|
Debug.Log($"向背包中取出物品id{itemID} 数量为:{quantity}");
|
||||||
|
RefreshInventorySlot?.Invoke(Slots);
|
||||||
|
restCount = remainingQuantity;
|
||||||
return remainingQuantity == 0;
|
return remainingQuantity == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 783ae3de0c1a4fb5a05e42cb5c5cc12a
|
||||||
|
timeCreated: 1739409274
|
|
@ -0,0 +1,28 @@
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace BeiBao.View
|
||||||
|
{
|
||||||
|
public class BeiBaoItem : MonoBehaviour
|
||||||
|
{
|
||||||
|
public Text NameText;
|
||||||
|
public Text CountText;
|
||||||
|
|
||||||
|
InventorySlot inventorySlot;
|
||||||
|
|
||||||
|
public void Refresh(InventorySlot slot)
|
||||||
|
{
|
||||||
|
inventorySlot = slot;
|
||||||
|
if (!inventorySlot.IsEmpty)
|
||||||
|
{
|
||||||
|
NameText.text = inventorySlot.Item.ItemName;
|
||||||
|
CountText.text = inventorySlot.Item.Quantity.ToString();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NameText.text = "";
|
||||||
|
CountText.text = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 35befefca49d4db38a8c0f9c03e50c09
|
||||||
|
timeCreated: 1739411115
|
|
@ -0,0 +1,70 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using TMPro;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UI;
|
||||||
|
|
||||||
|
namespace BeiBao.View
|
||||||
|
{
|
||||||
|
public class BeiBaoView : MonoBehaviour
|
||||||
|
{
|
||||||
|
public TMP_InputField nameText;
|
||||||
|
public TMP_InputField idText;
|
||||||
|
public TMP_InputField countText;
|
||||||
|
public Button Add;
|
||||||
|
public GameObject itemPfb;
|
||||||
|
|
||||||
|
Inventory inventory;
|
||||||
|
List<BeiBaoItem> items = new List<BeiBaoItem>();
|
||||||
|
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
inventory = new Inventory(5);
|
||||||
|
for (var i = 0; i < inventory.SlotCount; i++)
|
||||||
|
{
|
||||||
|
var instantiate = GameObject.Instantiate(itemPfb, itemPfb.transform.parent);
|
||||||
|
instantiate.gameObject.SetActive(true);
|
||||||
|
var beiBaoItem = instantiate.GetComponent<BeiBaoItem>();
|
||||||
|
beiBaoItem.Refresh(inventory.Slots[i]);
|
||||||
|
items.Add(beiBaoItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
inventory.RefreshInventorySlot += Refresh;
|
||||||
|
Add.onClick.AddListener(ClickAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ClickAdd()
|
||||||
|
{
|
||||||
|
var count = int.Parse(countText.text);
|
||||||
|
var id = int.Parse(idText.text);
|
||||||
|
if (count > 0)
|
||||||
|
{
|
||||||
|
// 创建物品
|
||||||
|
Item healthPotion = new Item(nameText.text, id, 99);
|
||||||
|
|
||||||
|
// 添加物品到背包
|
||||||
|
var b = inventory.AddItem(healthPotion, count, out var restCount);
|
||||||
|
if (!b)
|
||||||
|
{
|
||||||
|
Debug.Log($"添加失败!还有 {restCount} 个 {healthPotion.ItemName} 未添加,背包没位置放了,放不了一点");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (count < 0)
|
||||||
|
{
|
||||||
|
var b = inventory.RemoveItem(id, Mathf.Abs(count),out var restCount);
|
||||||
|
if (!b)
|
||||||
|
{
|
||||||
|
Debug.Log($"取出失败!还有 {restCount} 个 {id} 未取出,背包没有东西了,取不了一点");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Refresh(IReadOnlyList<InventorySlot> slot)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < slot.Count; i++)
|
||||||
|
{
|
||||||
|
items[i].Refresh(slot[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 66f950345dea479694e2bd2cc08f1f94
|
||||||
|
timeCreated: 1739409306
|
Loading…
Reference in New Issue