(client) feat:实现热重载,实现多维度,实现武器,实现掉落物,实现状态UI,实现攻击AI (#44)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-08-27 19:56:49 +08:00
committed by TheRedApricot
parent d91210a6ff
commit 8456b6c162
132 changed files with 18568 additions and 2534 deletions

View File

@ -1,38 +1,100 @@
using System;
using System.Linq;
using AI;
using Base;
using Data;
using Item;
using Managers;
using UnityEngine;
using Utils;
// 添加 System 命名空间以使用 Action
namespace Entity
{
public class Character : Entity
{
public CharacterDef characterDef;
private int _currentSelected; // 私有字段用于存储实际值
/// <summary>
/// 当前选中的背包槽位索引。
/// 当此值被设置时,如果与旧值不同,将触发 OnCurrentSelectedChanged 事件。
/// </summary>
public int CurrentSelected
{
get => _currentSelected;
set
{
var maxIndex = Inventory != null && Inventory.Capacity > 0 ? Inventory.Capacity - 1 : 0;
var clampedValue = Mathf.Clamp(value, 0, maxIndex);
_currentSelected = clampedValue;
}
}
public Inventory Inventory { get; private set; }
private void Start()
public override void Init(EntityDef entityDef)
{
aiTree = new JobGiver_RandomWander();
attributes = new AttributesDef();
base.Init(entityDef);
Inventory = new Inventory(this, 3);
// 初始化 currentSelected。
// 使用属性来设置,确保触发事件和范围检查。
// 如果Inventory.Capacity为0则currentSelected会被钳制到0。
// 如果Inventory.Capacity为3currentSelected=0是有效值。
CurrentSelected = 0;
}
public void Init()
/// <summary>
/// 尝试将指定物品添加到角色的背包中。
/// </summary>
/// <param name="itemResource">要尝试添加的物品资源。</param>
/// <param name="quantity">要尝试添加的数量。</param>
/// <returns>
/// 未成功添加到背包的物品数量。
/// 如果返回0表示所有物品都成功添加
/// 如果返回quantity表示未能添加任何物品
/// 如果返回一个介于0和quantity之间的值表示部分物品被添加。
/// </returns>
public int TryPickupItem(ItemResource itemResource, int quantity)
{
if (characterDef == null)
return;
if (Inventory == null)
{
Debug.LogError($"Character '{name}' inventory is not initialized. Cannot pickup item.");
return quantity; // 如果背包未初始化,则视为未能添加任何物品
}
var remainingQuantity = Inventory.AddItem(itemResource, quantity);
return remainingQuantity;
}
public override void TryAttack()
// public override void TryAttack()
// {
// if (IsAttacking)
// return;
// if (!DefineManager.Instance.defines.TryGetValue(nameof(BulletDef), out var def))
// return;
// // 修正First() 可能会在一个空的 Values 集合上抛出异常。
// // 更好的做法是使用 TryGetValue 或 FirstOrDefault 并检查结果。
// // 这里假设至少有一个 BulletDef 存在,如果不是,需要更复杂的错误处理。
// var bulletDefEntry = def.Values.FirstOrDefault();
// if (bulletDefEntry == null)
// {
// Debug.LogError("No BulletDef found in DefineManager. Cannot attack.");
// return;
// }
//
// var bulletDef = (BulletDef)bulletDefEntry;
//
// Vector3 dir = MousePosition.GetWorldPosition();
// EntityManage.Instance.GenerateBulletEntity(Program.Instance.FocusedDimensionId, bulletDef, Position,
// dir - Position, this);
// }
public override WeaponResource GetCurrentWeapon()
{
if (IsAttacking)
return;
if (!Managers.DefineManager.Instance.defines.TryGetValue(nameof(BulletDef), out var def))
return;
var buttonDef = def.Values.First();
Vector3 dir = Utils.MousePosition.GetWorldPosition();
Managers.EntityManage.Instance.GenerateBulletEntity((BulletDef)buttonDef, Position,
dir - Position, this);
var currentSelectItem = Inventory.GetSlot(CurrentSelected);
return (WeaponResource)currentSelectItem?.Item;
}
}
}