(client) chore
This commit is contained in:
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
@ -9,20 +6,74 @@ namespace Entity
|
||||
{
|
||||
public class Character : Entity
|
||||
{
|
||||
public CharacterDef characterDef;
|
||||
|
||||
private void Start()
|
||||
// ------------- 新增 / 修改部分 START -------------
|
||||
|
||||
// 声明 Inventory 字段。更改为 private set 以确保 Inventory 实例只能在 Character 内部设置。
|
||||
public Inventory Inventory { get; private set; }
|
||||
|
||||
public override void Init(EntityDef entityDef)
|
||||
{
|
||||
aiTree = new JobGiver_RandomWander();
|
||||
attributes = new AttributesDef();
|
||||
base.Init(entityDef);
|
||||
|
||||
// 1. 初始化背包:
|
||||
// 在这里创建并初始化 Inventory 实例。
|
||||
// 'this' 指的是当前 Character 实例,作为 Inventory 的拥有者。
|
||||
// 容量可以根据 entityDef 或一个默认值来设定。这里使用默认值 20。
|
||||
Inventory = new Inventory(this, 3);
|
||||
|
||||
// (可选) 可以在此订阅背包事件,以便 Character 对背包变化做出响应,
|
||||
// 例如更新角色状态、播放音效或更新UI。
|
||||
// Inventory.OnItemAdded += HandleItemAddedToInventory;
|
||||
// Inventory.OnItemRemoved += HandleItemRemovedFromInventory;
|
||||
// Inventory.OnInventoryChanged += HandleInventoryChanged;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
/// <summary>
|
||||
/// 尝试将指定物品添加到角色的背包中。
|
||||
/// </summary>
|
||||
/// <param name="itemResource">要尝试添加的物品资源。</param>
|
||||
/// <param name="quantity">要尝试添加的数量。</param>
|
||||
/// <returns>未成功添加到背包的物品数量。
|
||||
/// 如果返回0,表示所有物品都成功添加;
|
||||
/// 如果返回quantity,表示未能添加任何物品;
|
||||
/// 如果返回一个介于0和quantity之间的值,表示部分物品被添加。</returns>
|
||||
public int TryPickupItem(Item.ItemResource itemResource, int quantity)
|
||||
{
|
||||
if (characterDef == null)
|
||||
return;
|
||||
if (Inventory == null)
|
||||
{
|
||||
Debug.LogError($"Character '{this.name}' inventory is not initialized. Cannot pickup item.");
|
||||
return quantity; // 如果背包未初始化,则视为未能添加任何物品
|
||||
}
|
||||
|
||||
// 直接调用 Inventory 实例的 AddItem 方法。
|
||||
// AddItem 方法已经包含了严谨的逻辑来处理物品堆叠、新槽位分配、容量检查以及无效输入的警告。
|
||||
int remainingQuantity = Inventory.AddItem(itemResource, quantity);
|
||||
|
||||
return remainingQuantity;
|
||||
}
|
||||
|
||||
// (可选) 示例事件处理方法,用于展示如何响应背包事件
|
||||
// private void HandleItemAddedToInventory(Item.ItemResource item, int count)
|
||||
// {
|
||||
// Debug.Log($"{DefName} picked up {count} x {item.Name}. Current total: {Inventory.GetItemCount(item)}");
|
||||
// // 例如更新UI、播放音效、完成任务等
|
||||
// }
|
||||
|
||||
// private void HandleItemRemovedFromInventory(Item.ItemResource item, int count)
|
||||
// {
|
||||
// Debug.Log($"{DefName} removed {count} x {item.Name}. Current total: {Inventory.GetItemCount(item)}");
|
||||
// // 例如更新UI、播放音效、更新制作状态等
|
||||
// }
|
||||
|
||||
// private void HandleInventoryChanged()
|
||||
// {
|
||||
// Debug.Log($"{DefName}'s inventory changed. Occupied slots: {Inventory.OccupiedSlotsCount}/{Inventory.Capacity}");
|
||||
// // 例如通知背包UI刷新显示
|
||||
// }
|
||||
|
||||
// ------------- 新增 / 修改部分 END -------------
|
||||
|
||||
|
||||
public override void TryAttack()
|
||||
{
|
||||
if (IsAttacking)
|
||||
@ -35,4 +86,4 @@ namespace Entity
|
||||
dir - Position, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user