(client) feat:实现血条显示,实现攻击交互,添加碰撞体;fix:修复部分朝向贴图加载失败的问题;chore:规范工作类和行为获取类命名
This commit is contained in:
70
Client/Assets/Scripts/Entity/Inventory.cs
Normal file
70
Client/Assets/Scripts/Entity/Inventory.cs
Normal file
@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using Item;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Inventory
|
||||
{
|
||||
public Entity from; // 物品所属实体
|
||||
public List<ItemBase> items = new List<ItemBase>(); // 背包中的物品列表
|
||||
|
||||
/// <summary>
|
||||
/// 添加物品到背包
|
||||
/// </summary>
|
||||
/// <param name="item">要添加的物品</param>
|
||||
/// <param name="count">添加的数量</param>
|
||||
public void AddItem(ItemResource resource, int count)
|
||||
{
|
||||
if (count <= 0) return; // 如果数量小于等于0,直接返回
|
||||
|
||||
// 检查背包中是否已存在相同物品
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.resource.Equals(resource))
|
||||
{
|
||||
item.count += count; // 增加数量
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有找到相同物品,则创建新物品并添加到背包
|
||||
var newItem = new ItemBase { resource = resource, count = count };
|
||||
items.Add(newItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从背包中取出物品
|
||||
/// </summary>
|
||||
/// <param name="itemName">物品名称</param>
|
||||
/// <param name="count">取出的数量</param>
|
||||
/// <returns>是否成功取出</returns>
|
||||
public bool RemoveItem(string itemName, int count)
|
||||
{
|
||||
if (count <= 0) return false; // 如果数量小于等于0,直接返回失败
|
||||
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (item.resource.name == itemName)
|
||||
{
|
||||
if (item.count >= count)
|
||||
{
|
||||
item.count -= count; // 减少数量
|
||||
if (item.count == 0)
|
||||
{
|
||||
items.Remove(item); // 如果数量为0,则移除该物品
|
||||
}
|
||||
|
||||
return true; // 成功取出
|
||||
}
|
||||
else
|
||||
{
|
||||
return false; // 数量不足
|
||||
}
|
||||
}
|
||||
}
|
||||
return false; // 未找到物品
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user