(client)feat:实现按武器释放技能
This commit is contained in:
@ -0,0 +1,125 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!1001 &575440246232574994
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
serializedVersion: 2
|
||||
m_Modification:
|
||||
serializedVersion: 3
|
||||
m_TransformParent: {fileID: 0}
|
||||
m_Modifications:
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalPosition.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalPosition.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalPosition.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalRotation.w
|
||||
value: 1
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalRotation.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalRotation.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalRotation.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.x
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.y
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 697189026367054479, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_LocalEulerAnglesHint.z
|
||||
value: 0
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 887327274103887133, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: m_Name
|
||||
value: MonsterPrefab Variant
|
||||
objectReference: {fileID: 0}
|
||||
- target: {fileID: 1550000129210799929, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: entity
|
||||
value:
|
||||
objectReference: {fileID: 1684624059175526519}
|
||||
- target: {fileID: 3332598847335032684, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
propertyPath: entity
|
||||
value:
|
||||
objectReference: {fileID: 1684624059175526519}
|
||||
m_RemovedComponents:
|
||||
- {fileID: 5281521133329697568, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
m_RemovedGameObjects: []
|
||||
m_AddedGameObjects: []
|
||||
m_AddedComponents:
|
||||
- targetCorrespondingSourceObject: {fileID: 887327274103887133, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
insertIndex: 3
|
||||
addedObject: {fileID: 1684624059175526519}
|
||||
m_SourcePrefab: {fileID: 100100000, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
--- !u!1 &841105155399989519 stripped
|
||||
GameObject:
|
||||
m_CorrespondingSourceObject: {fileID: 887327274103887133, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
m_PrefabInstance: {fileID: 575440246232574994}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
--- !u!114 &1684624059175526519
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 841105155399989519}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 32995e4c8e0d40dfa39990f0671a3733, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
animatorPrefab: {fileID: 2113064398104960506, guid: ea9af70ce0f4c8b4a9de58ac63074156, type: 3}
|
||||
imagePrefab: {fileID: 1922746734790246249, guid: a6657f26d735fab4690c8185980fda29, type: 3}
|
||||
healthBarPrefab: {fileID: 8409907229673894003}
|
||||
entityPrefab: {fileID: 3009419556014787454}
|
||||
direction: {x: 0, y: 0, z: 0}
|
||||
body: {fileID: 2350608668399211852}
|
||||
affiliation:
|
||||
canSelect: 1
|
||||
currentDimensionId:
|
||||
_hitBarUIShowTime: 5
|
||||
--- !u!1 &2350608668399211852 stripped
|
||||
GameObject:
|
||||
m_CorrespondingSourceObject: {fileID: 2838206730318674270, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
m_PrefabInstance: {fileID: 575440246232574994}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
--- !u!114 &3009419556014787454 stripped
|
||||
MonoBehaviour:
|
||||
m_CorrespondingSourceObject: {fileID: 3332598847335032684, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
m_PrefabInstance: {fileID: 575440246232574994}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 841105155399989519}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: b20b1846b9ef47db83c2ac8c4c4e82cb, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
--- !u!114 &8409907229673894003 stripped
|
||||
MonoBehaviour:
|
||||
m_CorrespondingSourceObject: {fileID: 8307348883874536545, guid: 6cd8b01a0f57372438dc30c864ae1530, type: 3}
|
||||
m_PrefabInstance: {fileID: 575440246232574994}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 0}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 605f185650fe46d89a6e0d60fb8fb11c, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 90f616eda22821c49bd8cb9c3e897f05
|
||||
PrefabImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1799,6 +1799,7 @@ MonoBehaviour:
|
||||
m_EditorClassIdentifier:
|
||||
isGlobal: 1
|
||||
characterPrefab: {fileID: 3420474218334607780, guid: 5218adfb8e855a9459df63de8b2f323c, type: 3}
|
||||
monsterPrefab: {fileID: 3009419556014787454, guid: 90f616eda22821c49bd8cb9c3e897f05, type: 3}
|
||||
buildingPrefab: {fileID: 5615006624229444611, guid: d7b9277d8e6ac4541800044bdb0da063, type: 3}
|
||||
bulletPrefab: {fileID: 8687677644466399534, guid: 29b2450a8636a104586e36333878f4d9, type: 3}
|
||||
pickupPrefab: {fileID: 1096543602564338806, guid: ec84100729cd1bd4699fc15b3a09134c, type: 3}
|
||||
|
@ -21,6 +21,7 @@ namespace AI
|
||||
{
|
||||
entity = target;
|
||||
}
|
||||
|
||||
public bool Update()
|
||||
{
|
||||
if (!Running)
|
||||
@ -29,11 +30,13 @@ namespace AI
|
||||
timeoutTicks--;
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void StopJob()
|
||||
{
|
||||
timeoutTicks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public class WanderJob : JobBase
|
||||
{
|
||||
public override void StartJob(Entity.Entity target)
|
||||
@ -57,6 +60,7 @@ namespace AI
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class IdleJob : JobBase
|
||||
{
|
||||
override public void StartJob(Entity.Entity target)
|
||||
@ -64,10 +68,12 @@ namespace AI
|
||||
base.StartJob(target);
|
||||
timeoutTicks = 500;
|
||||
}
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class MoveJob : JobBase
|
||||
{
|
||||
protected override void UpdateJob()
|
||||
@ -79,19 +85,200 @@ namespace AI
|
||||
public class AttackJob : JobBase
|
||||
{
|
||||
private Entity.Entity attackTarget;
|
||||
protected override void UpdateJob()
|
||||
|
||||
// StartJob 方法:用于初始化任务,寻找初始攻击目标
|
||||
override public void StartJob(Entity.Entity performerEntityContext) // 参数名更明确,通常是发起任务的实体
|
||||
{
|
||||
entity.TryMove();
|
||||
base.StartJob(performerEntityContext);
|
||||
// 1. 任务执行者自身有效性检查
|
||||
if (entity == null)
|
||||
{
|
||||
StopJob(); // 调用StopJob来结束任务
|
||||
return;
|
||||
}
|
||||
|
||||
attackTarget = FindNewHostileTarget();
|
||||
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob(); // 调用StopJob来结束任务
|
||||
}
|
||||
}
|
||||
|
||||
override public void StartJob(Entity.Entity target)
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
base.StartJob(target);
|
||||
attackTarget =
|
||||
EntityManage.Instance
|
||||
.FindNearestEntityByRelation(target.currentDimensionId, target.entityPrefab, Relation.Hostile)
|
||||
?.entity;
|
||||
// 1. 任务执行者的基本检查
|
||||
if (entity == null || entity.IsDead)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
|
||||
if (attackTarget == null || attackTarget.IsDead)
|
||||
{
|
||||
attackTarget = FindNewHostileTarget(); // 尝试寻找新的攻击目标
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var weapon = entity.GetCurrentWeapon();
|
||||
var attackRange = 0f;
|
||||
if (weapon != null)
|
||||
{
|
||||
attackRange = weapon.Attributes.attackRange;
|
||||
}
|
||||
|
||||
var distanceSq = (entity.Position - attackTarget.Position).sqrMagnitude;
|
||||
var effectiveAttackRangeSq = attackRange * attackRange; // 将攻击范围也平方
|
||||
|
||||
entity.SetTarget(attackTarget.Position);
|
||||
|
||||
if (weapon != null && distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.TryAttack();
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找执行实体最近的敌对目标。
|
||||
/// </summary>
|
||||
/// <returns>找到的敌对实体,如果没有则返回null。</returns>
|
||||
private Entity.Entity FindNewHostileTarget()
|
||||
{
|
||||
if (!entity) return null;
|
||||
return EntityManage.Instance.FindNearestEntityByRelation(
|
||||
entity.currentDimensionId, // 搜索维度ID
|
||||
entity.entityPrefab, // 执行实体的Prefab ID,用于关系判断
|
||||
Relation.Hostile)?.entity; // 寻找敌对关系的目标
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class AdvancedAttackJob : JobBase
|
||||
{
|
||||
private Entity.Entity attackTarget;
|
||||
|
||||
// 常量:用于配置远程AI行为的风筝参数
|
||||
private const float KITING_THRESHOLD_MULTIPLIER = 0.5f; // 当目标距离小于 (攻击范围 * 此乘数) 时,远程单位开始尝试远离
|
||||
|
||||
private const float KITING_BUFFER_DISTANCE = 5.0f; // 当远程单位远离时,目标点会是当前位置向反方向偏移此距离
|
||||
|
||||
// StartJob 方法:用于初始化任务,寻找初始攻击目标
|
||||
override public void StartJob(Entity.Entity performerEntityContext)
|
||||
{
|
||||
base.StartJob(performerEntityContext);
|
||||
if (entity == null)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
|
||||
attackTarget = FindNewHostileTarget();
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob();
|
||||
}
|
||||
}
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
// 1. 任务执行者的基本检查
|
||||
if (entity == null || entity.IsDead)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 攻击目标检查
|
||||
if (attackTarget == null || attackTarget.IsDead)
|
||||
{
|
||||
attackTarget = FindNewHostileTarget(); // 尝试寻找新的攻击目标
|
||||
if (attackTarget == null)
|
||||
{
|
||||
StopJob();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 获取武器和其属性
|
||||
var weapon = entity.GetCurrentWeapon();
|
||||
var attackRange = 0f;
|
||||
var isRangedWeapon = false; // 标识是否为远程武器
|
||||
if (weapon != null)
|
||||
{
|
||||
if (weapon.Attributes != null)
|
||||
{
|
||||
attackRange = weapon.Attributes.attackRange;
|
||||
}
|
||||
|
||||
// 使用 WeaponType 来判断武器类型,更明确
|
||||
isRangedWeapon = weapon.Type == WeaponType.Ranged;
|
||||
}
|
||||
|
||||
var distanceSq = (entity.Position - attackTarget.Position).sqrMagnitude;
|
||||
var effectiveAttackRangeSq = attackRange * attackRange; // 将攻击范围平方
|
||||
// ---- 核心AI行为决策 ( AdvancedAttackJob 的智能之处) ----
|
||||
if (isRangedWeapon)
|
||||
{
|
||||
// 远程单位的风筝(Kiting)逻辑
|
||||
var kitingDistance = attackRange * KITING_THRESHOLD_MULTIPLIER;
|
||||
var kitingThresholdSq = kitingDistance * kitingDistance; // 过近距离的平方
|
||||
// 1. 如果目标过于接近 (小于风筝阈值),尝试远离
|
||||
if (distanceSq < kitingThresholdSq)
|
||||
{
|
||||
// 计算一个远离目标的点作为移动目标
|
||||
var directionAway = (entity.Position - attackTarget.Position).normalized;
|
||||
var fleePosition = entity.Position + directionAway * KITING_BUFFER_DISTANCE;
|
||||
entity.SetTarget(fleePosition); // 设置远离点为新的移动目标
|
||||
entity.TryMove(); // 优先执行移动操作以拉开距离
|
||||
// 在此状态下不进行攻击,专注于 reposition
|
||||
}
|
||||
// 2. 如果目标在最佳攻击范围内 (即在风筝阈值和有效攻击范围之间),则停止移动并攻击
|
||||
else if (distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.SetTarget(entity.Position); // 设定目标为当前位置,使其停止移动,专注于攻击
|
||||
entity.TryAttack();
|
||||
}
|
||||
// 3. 如果目标太远 (超出有效攻击范围),则移动靠近目标
|
||||
else
|
||||
{
|
||||
entity.SetTarget(attackTarget.Position); // 设置目标位置为移动目标
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
else // 近战单位或没有武器的单位
|
||||
{
|
||||
entity.SetTarget(attackTarget.Position);
|
||||
if (weapon != null && distanceSq <= effectiveAttackRangeSq)
|
||||
{
|
||||
entity.TryAttack();
|
||||
}
|
||||
else
|
||||
{
|
||||
entity.TryMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找执行实体最近的敌对目标。
|
||||
/// </summary>
|
||||
/// <returns>找到的敌对实体,如果没有则返回null。</returns>
|
||||
private Entity.Entity FindNewHostileTarget()
|
||||
{
|
||||
if (!entity) return null;
|
||||
return EntityManage.Instance.FindNearestEntityByRelation(
|
||||
entity.currentDimensionId,
|
||||
entity.entityPrefab,
|
||||
Relation.Hostile)?.entity;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -24,4 +24,18 @@ namespace AI
|
||||
}
|
||||
}
|
||||
|
||||
public class JobGiver_AttackJob : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return Managers.EntityManage.Instance.ExistsHostile(target.currentDimensionId, target.entityPrefab) ? new AttackJob() : null;
|
||||
}
|
||||
}
|
||||
public class JobGiver_AdvancedAttackJob : AIBase
|
||||
{
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
return Managers.EntityManage.Instance.ExistsHostile(target.currentDimensionId, target.entityPrefab) ? new AdvancedAttackJob() : null;
|
||||
}
|
||||
}
|
||||
}
|
@ -20,11 +20,12 @@ namespace Data
|
||||
value = xmlDef.Attribute("value")?.Value;
|
||||
|
||||
var nodes = xmlDef.Elements("Node");
|
||||
if (!nodes.Any())
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return true; // 没有子节点也是有效的
|
||||
|
||||
List<BehaviorTreeDef> children = new();
|
||||
foreach (var node in nodes)
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var childNode = new BehaviorTreeDef();
|
||||
if (!childNode.Init(node))
|
||||
|
@ -262,7 +262,35 @@ namespace Data
|
||||
|
||||
return Vector2.zero;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算动画执行一个周期的总时间(包括子对象)。
|
||||
/// 如果自身没有纹理,自身动画时间为0。
|
||||
/// 总周期取自身动画时间和所有子动画周期中的最大值。
|
||||
/// </summary>
|
||||
/// <returns>动画执行一个周期的总时间(秒)。</returns>
|
||||
public float GetAnimationCycleDuration()
|
||||
{
|
||||
if (FPS < 0.01)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
float ownDuration = 0f;
|
||||
// 计算当前节点自身的动画周期时间
|
||||
// 由于 Init 方法已经处理了 FPS 的校验,FPS 保证大于 0
|
||||
if (textures.Count > 0)
|
||||
{
|
||||
ownDuration = textures.Count / FPS;
|
||||
}
|
||||
// 递归计算所有子节点的动画周期,并取其中最长的
|
||||
float maxChildDuration = 0f;
|
||||
foreach (var childNode in nodes)
|
||||
{
|
||||
float childDuration = childNode.GetAnimationCycleDuration();
|
||||
maxChildDuration = Math.Max(maxChildDuration, childDuration);
|
||||
}
|
||||
// 整个 DrawNodeDef 的动画周期是自身动画周期和所有子动画周期中的最大值
|
||||
return Math.Max(ownDuration, maxChildDuration);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,15 +7,15 @@ namespace Data
|
||||
{
|
||||
public class EntityDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public AttributesDef attributes = new();
|
||||
public DrawingOrderDef drawingOrder;
|
||||
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
public AffiliationDef affiliation;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -2,6 +2,6 @@ namespace Data
|
||||
{
|
||||
public class MonsterDef:EntityDef
|
||||
{
|
||||
|
||||
public WeaponDef weapon;
|
||||
}
|
||||
}
|
@ -9,6 +9,9 @@ namespace Data
|
||||
{
|
||||
public WeaponType type = WeaponType.Melee;
|
||||
public AttributesDef attributes;
|
||||
public BulletDef bullet;
|
||||
public DrawNodeDef attackAnimation;
|
||||
public float attackDetectionTime = 0;
|
||||
public WeaponDef() // 构造函数,用于设置武器的默认属性
|
||||
{
|
||||
maxStack = 1; // 武器默认最大堆叠为1
|
||||
|
@ -68,33 +68,33 @@ namespace Entity
|
||||
return remainingQuantity;
|
||||
}
|
||||
|
||||
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 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()
|
||||
{
|
||||
var currentSelectItem = Inventory.GetSlot(CurrentSelected);
|
||||
return (WeaponResource)currentSelectItem.Item;
|
||||
return (WeaponResource)currentSelectItem?.Item;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using Item;
|
||||
using Managers;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
|
||||
@ -28,10 +29,10 @@ namespace Entity
|
||||
public ImagePrefab imagePrefab;
|
||||
|
||||
public ProgressBarPrefab healthBarPrefab;
|
||||
|
||||
|
||||
public EntityPrefab entityPrefab;
|
||||
public EntityDef entityDef;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 人工智能行为树,定义实体的行为逻辑。
|
||||
/// </summary>
|
||||
@ -45,7 +46,8 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 实体的属性定义,包括生命值、攻击力、防御力等。
|
||||
/// </summary>
|
||||
public Attributes attributes=new();
|
||||
public Attributes attributes = new();
|
||||
|
||||
/// <summary>
|
||||
/// 实体当前的移动方向。
|
||||
/// </summary>
|
||||
@ -72,8 +74,8 @@ namespace Entity
|
||||
public bool IsChase { set; get; } = true;
|
||||
|
||||
public string currentDimensionId = null;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 表示实体是否由玩家控制。
|
||||
@ -91,6 +93,7 @@ namespace Entity
|
||||
{
|
||||
Program.Instance.FocusedEntity.PlayerControlled = false;
|
||||
}
|
||||
|
||||
Program.Instance.SetFocusedEntity(this);
|
||||
}
|
||||
// 逻辑修改:确保只有当自身是焦点实体时才取消焦点,避免不必要的逻辑执行
|
||||
@ -114,49 +117,49 @@ namespace Entity
|
||||
/// 表示实体是否已经死亡(生命值小于等于零)。
|
||||
/// </summary>
|
||||
public bool IsDead => attributes.health <= 0;
|
||||
public bool IsShowingHealthBarUI=>_hitBarUIShowTimer > 0;
|
||||
|
||||
public bool IsShowingHealthBarUI => _hitBarUIShowTimer > 0;
|
||||
public bool IsAttacking => _attackCoroutine != null;
|
||||
|
||||
/// <summary>
|
||||
/// 当实体受到伤害时触发的事件。
|
||||
/// 可以订阅此事件来响应实体的生命值变化,例如更新UI或播放受击特效。
|
||||
/// </summary>
|
||||
public event Action<EntityHitEventArgs> OnEntityHit;
|
||||
|
||||
/// <summary>
|
||||
/// 当实体死亡时触发的事件。
|
||||
/// 只在实体首次进入死亡状态时触发一次。
|
||||
/// </summary>
|
||||
public event Action<Entity> OnEntityDied;
|
||||
|
||||
private bool _warning = false;
|
||||
|
||||
/// <summary>
|
||||
/// 存储不同朝向下的动画节点集合。
|
||||
/// </summary>
|
||||
public Dictionary<EntityState, Dictionary<Orientation, ITick[]>> bodyAnimationNode = new();
|
||||
|
||||
private ITick[] _currentAnimatorCache;
|
||||
|
||||
private GameObject wearponAttackAnimationNodeRoot = null;
|
||||
private ITick[] wearponAttackAnimationNodeList;
|
||||
|
||||
/// <summary>
|
||||
/// 存储不同朝向下的身体节点对象。
|
||||
/// </summary>
|
||||
protected Dictionary<EntityState, Dictionary<Orientation,GameObject>> bodyNodes = new();
|
||||
protected Dictionary<EntityState, Dictionary<Orientation, GameObject>> bodyNodes = new();
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的朝向。
|
||||
/// </summary>
|
||||
private Orientation _currentOrientation = Orientation.Down;
|
||||
|
||||
/// <summary>
|
||||
/// 当前实体的状态
|
||||
/// </summary>
|
||||
private EntityState _currentState = EntityState.Idle;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 攻击动画的持续时间(秒)。
|
||||
/// </summary>
|
||||
private const float AttackAnimationDuration = 0.1f;
|
||||
|
||||
/// <summary>
|
||||
/// 抖动的偏移量。
|
||||
/// </summary>
|
||||
private const float ShakeOffset = 0.5f;
|
||||
// 协程引用
|
||||
private Coroutine _attackCoroutine;
|
||||
|
||||
@ -178,7 +181,7 @@ namespace Entity
|
||||
affiliation = entityDef.affiliation?.defName;
|
||||
InitBody(entityDef.drawingOrder);
|
||||
this.entityDef = entityDef;
|
||||
|
||||
|
||||
HideHealthBar();
|
||||
}
|
||||
|
||||
@ -191,7 +194,6 @@ namespace Entity
|
||||
// 预缓存枚举值(避免每次循环重复调用 Enum.GetValues)
|
||||
var states = Enum.GetValues(typeof(EntityState)).Cast<EntityState>().ToArray();
|
||||
var orientations = Enum.GetValues(typeof(Orientation)).Cast<Orientation>().ToArray();
|
||||
|
||||
// 预初始化字典结构(减少内层循环的字典检查)
|
||||
foreach (var state in states)
|
||||
{
|
||||
@ -204,45 +206,77 @@ namespace Entity
|
||||
{
|
||||
var stateBodyNodes = bodyNodes[state];
|
||||
var stateAnimNodes = bodyAnimationNode[state];
|
||||
|
||||
foreach (var orientation in orientations)
|
||||
{
|
||||
// 获取节点定义(避免重复调用)
|
||||
var nodeDef = drawingOrder.GetDrawNodeDef(state, orientation, out var original);
|
||||
|
||||
// 处理空节点定义(直接创建空对象)
|
||||
GameObject targetObj = null;
|
||||
// --- 修改点一:处理空节点定义(增加默认精灵显示) ---
|
||||
if (nodeDef == null)
|
||||
{
|
||||
var obj = new GameObject { name = $"{state}_Empty" };
|
||||
obj.transform.SetParent(body.transform, false);
|
||||
stateBodyNodes[orientation] = obj;
|
||||
continue; // 跳过后续动画处理
|
||||
}
|
||||
|
||||
// 处理有效节点定义
|
||||
GameObject targetObj;
|
||||
if (original.HasValue && stateBodyNodes.TryGetValue(original.Value, out var reusedObj))
|
||||
{
|
||||
targetObj = reusedObj; // 复用已有对象
|
||||
if (imagePrefab != null && Managers.PackagesImageManager.Instance.defaultSprite != null)
|
||||
{
|
||||
// 实例化imagePrefab作为默认占位符
|
||||
targetObj = Instantiate(imagePrefab.gameObject, body.transform);
|
||||
targetObj.name = $"{state}_{orientation}_Default";
|
||||
targetObj.transform.localPosition=Vector3.zero;
|
||||
var imagePrefabCom = targetObj.GetComponent<ImagePrefab>();
|
||||
if (imagePrefabCom != null)
|
||||
{
|
||||
imagePrefabCom.SetSprite(Managers.PackagesImageManager.Instance.defaultSprite);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"InitBody: 默认ImagePrefab中无法获取ImagePrefab组件!状态: {state}, 朝向: {orientation}");
|
||||
// 降级为普通GameObject
|
||||
targetObj = new GameObject { name = $"{state}_{orientation}_Empty" };
|
||||
targetObj.transform.SetParent(body.transform, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有imagePrefab或defaultSprite,则创建空GameObject
|
||||
targetObj = new GameObject { name = $"{state}_{orientation}_Empty" };
|
||||
targetObj.transform.SetParent(body.transform, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
targetObj = InitBodyPart(nodeDef, body); // 创建新对象
|
||||
// 处理有效节点定义
|
||||
if (original.HasValue && stateBodyNodes.TryGetValue(original.Value, out var reusedObj))
|
||||
{
|
||||
targetObj = reusedObj; // 复用已有对象
|
||||
}
|
||||
else
|
||||
{
|
||||
targetObj = InitBodyPart(nodeDef, body); // 创建新对象
|
||||
}
|
||||
}
|
||||
|
||||
stateBodyNodes[orientation] = targetObj;
|
||||
|
||||
// 逻辑修改:确保 stateAnimNodes[orientation] 总是被初始化为一个列表
|
||||
var animatorsForOrientation = new List<ITick>(); // 总是创建一个新的列表
|
||||
var animators = targetObj.GetComponentsInChildren<SpriteAnimator>();
|
||||
if (animators.Length > 0)
|
||||
if (targetObj != null)
|
||||
{
|
||||
animatorsForOrientation.AddRange(animators);
|
||||
stateBodyNodes[orientation] = targetObj;
|
||||
// 逻辑说明:确保 stateAnimNodes[orientation] 总是被初始化为一个列表
|
||||
var animatorsForOrientation = new List<ITick>(); // 总是创建一个新的列表
|
||||
var animators = targetObj.GetComponentsInChildren<SpriteAnimator>();
|
||||
if (animators.Length > 0)
|
||||
{
|
||||
animatorsForOrientation.AddRange(animators);
|
||||
}
|
||||
|
||||
stateAnimNodes[orientation] = animatorsForOrientation.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"InitBody: 无法为状态 {state}, 朝向 {orientation} 创建或找到有效的GameObject。");
|
||||
stateBodyNodes[orientation] = new GameObject($"ErrorNode_{state}_{orientation}"); // 提供一个错误占位符
|
||||
stateBodyNodes[orientation].transform.SetParent(body.transform, false);
|
||||
stateAnimNodes[orientation] = Array.Empty<ITick>();
|
||||
}
|
||||
stateAnimNodes[orientation] = animatorsForOrientation.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 批量隐藏所有节点(使用字典值集合直接操作)
|
||||
foreach (var nodeDict in bodyNodes.Values)
|
||||
{
|
||||
@ -252,9 +286,8 @@ namespace Entity
|
||||
}
|
||||
}
|
||||
|
||||
SetBodyTexture(EntityState.Idle,Orientation.Down); // 激活默认朝向
|
||||
SetBodyTexture(EntityState.Idle, Orientation.Down); // 激活默认朝向
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 递归初始化单个绘图节点及其子节点,具有更强的健壮性和错误处理。
|
||||
@ -288,7 +321,6 @@ namespace Entity
|
||||
nodeObject = new GameObject(drawNode.nodeName);
|
||||
nodeObject.transform.SetParent(parent.transform, false);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// 单纹理节点
|
||||
if (imagePrefab == null)
|
||||
@ -299,9 +331,8 @@ namespace Entity
|
||||
|
||||
nodeObject = Instantiate(imagePrefab.gameObject, parent.transform);
|
||||
var texture =
|
||||
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID,
|
||||
drawNode.textures[0]);
|
||||
|
||||
Managers.PackagesImageManager.Instance.GetSprite(drawNode.packID,
|
||||
drawNode.textures[0]); // --- 修改点二:移除 ?. ---
|
||||
if (!texture)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
@ -319,7 +350,6 @@ namespace Entity
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// 多纹理动画节点
|
||||
if (!animatorPrefab)
|
||||
@ -330,7 +360,6 @@ namespace Entity
|
||||
|
||||
nodeObject = Instantiate(animatorPrefab.gameObject, parent.transform);
|
||||
var animator = nodeObject.GetComponent<SpriteAnimator>();
|
||||
|
||||
if (animator == null)
|
||||
{
|
||||
Debug.LogWarning($"InitBodyPart: 无法获取SpriteAnimator组件 (节点名: {drawNode.nodeName})");
|
||||
@ -344,7 +373,8 @@ namespace Entity
|
||||
try
|
||||
{
|
||||
var sprite =
|
||||
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID, textureId);
|
||||
Managers.PackagesImageManager.Instance.GetSprite(drawNode.packID,
|
||||
textureId); // --- 修改点二:移除 ?. ---
|
||||
if (sprite != null)
|
||||
{
|
||||
animatedSprites.Add(sprite);
|
||||
@ -361,7 +391,7 @@ namespace Entity
|
||||
$"InitBodyPart: 加载动画纹理时出错 (节点名: {drawNode.nodeName}, 纹理ID: {textureId}): {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (animatedSprites.Count > 0)
|
||||
{
|
||||
animator.SetSprites(animatedSprites.ToArray());
|
||||
@ -376,10 +406,8 @@ namespace Entity
|
||||
|
||||
// 设置节点属性
|
||||
if (!nodeObject) return nodeObject;
|
||||
|
||||
nodeObject.transform.localPosition = drawNode.position;
|
||||
nodeObject.name = drawNode.nodeName ?? "UnnamedNode";
|
||||
|
||||
// 递归初始化子节点
|
||||
if (drawNode.nodes == null) return nodeObject;
|
||||
foreach (var child in drawNode.nodes)
|
||||
@ -393,7 +421,6 @@ namespace Entity
|
||||
Debug.LogError($"InitBodyPart: 初始化子节点失败 (父节点: {drawNode.nodeName}): {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return nodeObject;
|
||||
}
|
||||
@ -417,6 +444,7 @@ namespace Entity
|
||||
SetBodyTexture(EntityState.Idle, _currentOrientation);
|
||||
}
|
||||
}
|
||||
|
||||
if (PlayerControlled)
|
||||
{
|
||||
UpdatePlayerControls();
|
||||
@ -426,7 +454,7 @@ namespace Entity
|
||||
AutoBehave();
|
||||
}
|
||||
|
||||
if (_currentAnimatorCache!=null)
|
||||
if (_currentAnimatorCache != null)
|
||||
{
|
||||
foreach (var animator in _currentAnimatorCache)
|
||||
{
|
||||
@ -434,6 +462,13 @@ namespace Entity
|
||||
}
|
||||
}
|
||||
|
||||
if (wearponAttackAnimationNodeList != null)
|
||||
{
|
||||
foreach (var tick in wearponAttackAnimationNodeList)
|
||||
{
|
||||
tick.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
if (IsShowingHealthBarUI)
|
||||
{
|
||||
@ -448,12 +483,27 @@ namespace Entity
|
||||
/// <summary>
|
||||
/// 尝试攻击目标实体。
|
||||
/// </summary>
|
||||
public virtual void TryAttack()
|
||||
public virtual void TryAttack() // 使用override允许子类重写
|
||||
{
|
||||
if(!IsAttacking)
|
||||
_attackCoroutine = StartCoroutine(AttackFlow());
|
||||
if (IsAttacking || IsDead) return; // 死亡时无法攻击
|
||||
|
||||
// 尝试获取当前武器
|
||||
WeaponResource currentWeapon = GetCurrentWeapon();
|
||||
|
||||
// 如果没有武器,可以选择进行徒手攻击或者直接返回
|
||||
// 暂时设定为:如果没有武器,则不进行攻击
|
||||
if (currentWeapon == null)
|
||||
{
|
||||
// 可以在这里添加一个默认的徒手攻击逻辑,或者播放一个“不能攻击”的提示
|
||||
Debug.Log($"{name} 没有装备武器,无法攻击。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 启动基于武器的攻击协程
|
||||
_attackCoroutine = StartCoroutine(AttackFlow(currentWeapon));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public virtual void SetBodyTexture(EntityState state, Orientation orientation)
|
||||
{
|
||||
if (bodyNodes.TryGetValue(_currentState, out var stateNode))
|
||||
@ -474,7 +524,7 @@ namespace Entity
|
||||
|
||||
_currentState = state;
|
||||
_currentOrientation = orientation;
|
||||
|
||||
|
||||
if (bodyAnimationNode.TryGetValue(_currentState, out var animationNode) &&
|
||||
animationNode.TryGetValue(_currentOrientation, out var value))
|
||||
{
|
||||
@ -482,7 +532,7 @@ namespace Entity
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentAnimatorCache = new ITick[]{}; // 如果没有找到动画,则使用空列表
|
||||
_currentAnimatorCache = new ITick[] { }; // 如果没有找到动画,则使用空列表
|
||||
}
|
||||
}
|
||||
|
||||
@ -494,10 +544,10 @@ namespace Entity
|
||||
if (IsAttacking)
|
||||
return;
|
||||
transform.position += direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
SetBodyTexture(EntityState.Walking,_currentOrientation);
|
||||
SetBodyTexture(EntityState.Walking, _currentOrientation);
|
||||
_walkingTimer = 2;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 处理实体受到攻击的逻辑。
|
||||
/// </summary>
|
||||
@ -509,6 +559,7 @@ namespace Entity
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var hit = from.attributes.attack - attributes.defense;
|
||||
if (hit < 0)
|
||||
hit = from.attributes.attack / 100;
|
||||
@ -525,24 +576,24 @@ namespace Entity
|
||||
// 如果是首次死亡,则触发 OnEntityDied 事件
|
||||
// MODIFIED: 停止所有活动,包括当前工作
|
||||
currentJob = null; // 清除当前工作
|
||||
OnEntityDied?.Invoke(this);
|
||||
OnEntityDied?.Invoke(this);
|
||||
}
|
||||
|
||||
|
||||
ShowHealthBar(); // 无论是否死亡,都更新血条UI
|
||||
}
|
||||
|
||||
public void ShowHealthBar()
|
||||
{
|
||||
if(!healthBarPrefab)
|
||||
if (!healthBarPrefab)
|
||||
return;
|
||||
healthBarPrefab.gameObject.SetActive(true);
|
||||
healthBarPrefab.Progress = (float)attributes.health / entityDef.attributes.health;
|
||||
_hitBarUIShowTimer=_hitBarUIShowTime;
|
||||
_hitBarUIShowTimer = _hitBarUIShowTime;
|
||||
}
|
||||
|
||||
public void HideHealthBar()
|
||||
{
|
||||
if(!healthBarPrefab)
|
||||
if (!healthBarPrefab)
|
||||
return;
|
||||
healthBarPrefab.gameObject.SetActive(false);
|
||||
}
|
||||
@ -556,6 +607,7 @@ namespace Entity
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
attributes.health = 0; // 直接设置生命值为0
|
||||
// MODIFIED: 停止所有活动,包括当前工作
|
||||
currentJob?.StopJob();
|
||||
@ -605,14 +657,16 @@ namespace Entity
|
||||
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
||||
_warning = true;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
currentJob.StartJob(this);
|
||||
}
|
||||
|
||||
currentJob.Update();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 更新玩家控制的逻辑,处理输入和移动。
|
||||
@ -622,7 +676,7 @@ namespace Entity
|
||||
// 检测 Shift 键状态
|
||||
var isHoldingShift = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
|
||||
IsChase = !isHoldingShift; // 按住 Shift 时 IsChase = false,否则 true
|
||||
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
||||
// 获取当前键盘输入状态(2D 移动,只使用 X 和 Y 轴)
|
||||
var inputDirection = Vector2.zero;
|
||||
|
||||
// 检测 WASD 或方向键输入
|
||||
@ -630,22 +684,27 @@ namespace Entity
|
||||
{
|
||||
inputDirection += Vector2.up; // 向上移动(Y 轴正方向)
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
||||
{
|
||||
inputDirection += Vector2.down; // 向下移动(Y 轴负方向)
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
inputDirection += Vector2.left; // 向左移动(X 轴负方向)
|
||||
}
|
||||
|
||||
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
inputDirection += Vector2.right; // 向右移动(X 轴正方向)
|
||||
}
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
TryAttack();
|
||||
}
|
||||
|
||||
// 如果有输入方向,则设置目标位置并尝试移动
|
||||
if (inputDirection == Vector2.zero) return;
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
@ -662,68 +721,166 @@ namespace Entity
|
||||
|
||||
|
||||
}
|
||||
// 攻击流程协程
|
||||
IEnumerator AttackFlow()
|
||||
{
|
||||
// 播放攻击动画并获取动画持续时间
|
||||
var animationDuration = PlayAttackAnimation();
|
||||
// 等待动画执行完毕
|
||||
yield return new WaitForSeconds(animationDuration);
|
||||
// 调用检测并攻击敌人的方法
|
||||
DetectAndAttackEnemies();
|
||||
// 攻击流程结束,清理协程引用
|
||||
_attackCoroutine = null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 播放攻击动画。
|
||||
/// </summary>
|
||||
/// <returns>开始检测攻击范围内敌人的时间。</returns>
|
||||
public float PlayAttackAnimation()
|
||||
{
|
||||
// 启动协程来执行攻击动画
|
||||
StartCoroutine(ShakeInDirectionCoroutine());
|
||||
|
||||
// 返回检测敌人的起始时间
|
||||
return AttackAnimationDuration;
|
||||
}
|
||||
|
||||
private IEnumerator ShakeInDirectionCoroutine()
|
||||
// NEW: ShakeInDirectionCoroutine 签名修改以接收持续时间
|
||||
private IEnumerator ShakeInDirectionCoroutine(float duration)
|
||||
{
|
||||
var originalPosition = transform.position; // 记录原始位置
|
||||
transform.position += direction * ShakeOffset;
|
||||
yield return new WaitForSeconds(AttackAnimationDuration);
|
||||
// 在攻击动画持续时间内进行抖动效果
|
||||
transform.position += direction * 0.5f;
|
||||
yield return new WaitForSeconds(duration);
|
||||
transform.position = originalPosition;
|
||||
}
|
||||
|
||||
public void DetectAndAttackEnemies()
|
||||
// NEW: AttackFlow 现在接收 WeaponResource 参数
|
||||
protected IEnumerator AttackFlow(WeaponResource weapon) // 将可见性改为 protected,允许子类访问
|
||||
{
|
||||
var attackCount = attributes.attackTargetCount;
|
||||
// 获取攻击范围内的所有碰撞体
|
||||
// STEP 1: 激活武器动画节点
|
||||
if (weapon.AttackAnimationDef != null)
|
||||
{
|
||||
var animation = weapon.InstantiateAttackAnimation(body.transform);
|
||||
wearponAttackAnimationNodeRoot = animation.root;
|
||||
wearponAttackAnimationNodeList = animation.animationComponents;
|
||||
}
|
||||
|
||||
// STEP 4: 等待到攻击判定时间
|
||||
float elapsedTime = 0f;
|
||||
while (elapsedTime < weapon.AttackDetectionTime)
|
||||
{
|
||||
if (IsDead)
|
||||
{
|
||||
/* 如果实体在此期间死亡,立刻中断 */
|
||||
break;
|
||||
}
|
||||
|
||||
elapsedTime += Time.deltaTime;
|
||||
yield return null; // 等待一帧
|
||||
}
|
||||
|
||||
// 如果实体在等待期间死亡,清理并退出
|
||||
if (IsDead)
|
||||
{
|
||||
CleanupAttack(weapon);
|
||||
yield break;
|
||||
}
|
||||
|
||||
ExecuteWeaponAction(weapon);
|
||||
|
||||
float remainingAnimationTime = weapon.AttackAnimationTime - elapsedTime;
|
||||
if (remainingAnimationTime > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(remainingAnimationTime);
|
||||
}
|
||||
else if (weapon.AttackAnimationTime > 0)
|
||||
{
|
||||
yield return new WaitForSeconds(weapon.AttackAnimationTime - elapsedTime);
|
||||
}
|
||||
|
||||
// STEP 7: 清理攻击状态
|
||||
CleanupAttack(weapon);
|
||||
}
|
||||
|
||||
private void CleanupAttack(WeaponResource weapon)
|
||||
{
|
||||
if (wearponAttackAnimationNodeRoot)
|
||||
{
|
||||
Destroy(wearponAttackAnimationNodeRoot);
|
||||
wearponAttackAnimationNodeRoot = null;
|
||||
}
|
||||
|
||||
wearponAttackAnimationNodeList = null;
|
||||
_attackCoroutine = null;
|
||||
}
|
||||
|
||||
protected virtual void ExecuteWeaponAction(WeaponResource weapon) // 将可见性改为 protected,允许子类重写
|
||||
{
|
||||
if (weapon == null) return; // 安全检查
|
||||
|
||||
switch (weapon.Type)
|
||||
{
|
||||
case WeaponType.Melee:
|
||||
ExecuteMeleeAttack(weapon);
|
||||
break;
|
||||
case WeaponType.Ranged:
|
||||
ExecuteRangedAttack(weapon);
|
||||
break;
|
||||
default:
|
||||
Debug.LogWarning($"未知武器类型: {weapon.Type} for {name}");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void ExecuteMeleeAttack(WeaponResource weapon)
|
||||
{
|
||||
if (weapon.Attributes == null)
|
||||
{
|
||||
Debug.LogWarning($"武器 {weapon.DefName} 没有定义Attributes,无法执行近战攻击。");
|
||||
return;
|
||||
}
|
||||
|
||||
var attackRange = weapon.Attributes.attackRange;
|
||||
var attackTargetCount = weapon.Attributes.attackTargetCount;
|
||||
|
||||
var hits = Physics2D.OverlapCircleAll(
|
||||
transform.position,
|
||||
attributes.attackRange,
|
||||
transform.position,
|
||||
attackRange,
|
||||
LayerMask.GetMask("Entity"));
|
||||
|
||||
foreach (var hit in hits)
|
||||
{
|
||||
if (attackCount <= 0) break;
|
||||
if (attackTargetCount <= 0) break; // 已达到最大攻击目标数
|
||||
if (hit.gameObject == gameObject) continue; // 不攻击自己
|
||||
|
||||
// 检查是否是自身(额外安全措施)
|
||||
if (hit.gameObject == this.gameObject) continue;
|
||||
|
||||
// 获取Entity组件
|
||||
var entity = hit.GetComponent<Entity>();
|
||||
if (!entity) continue;
|
||||
|
||||
// 执行攻击
|
||||
entity.OnHit(this);
|
||||
attackCount--;
|
||||
if (entity != null && entity.affiliation != affiliation) // 确保是敌对实体
|
||||
{
|
||||
entity.OnHit(this); // 攻击时将自身作为攻击来源
|
||||
attackTargetCount--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// NEW: 辅助方法用于执行远程攻击
|
||||
private void ExecuteRangedAttack(WeaponResource weapon)
|
||||
{
|
||||
if (weapon.Bullet == null)
|
||||
{
|
||||
Debug.LogWarning($"远程武器 {weapon.DefName} 没有定义Bullet,无法发射子弹。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取子弹方向。这里使用实体当前的移动方向作为子弹发射方向
|
||||
// 更复杂的逻辑可能根据鼠标位置、目标位置等确定
|
||||
Vector3 bulletDirection = direction; // 实体当前的朝向
|
||||
if (PlayerControlled && Input.GetMouseButton(0)) // 玩家控制时,如果鼠标按下,尝试朝鼠标方向发射
|
||||
{
|
||||
Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
mouseWorldPos.z = transform.position.z; // 保持Z轴一致
|
||||
bulletDirection = (mouseWorldPos - transform.position).normalized;
|
||||
}
|
||||
|
||||
// 如果没有明确的方向,给一个默认值以防万一
|
||||
if (bulletDirection == Vector3.zero) bulletDirection = Vector3.down;
|
||||
|
||||
|
||||
// 假设 EntityManage.Instance.GenerateBulletEntity 方法存在
|
||||
// (需要一个 EntityManage 单例来实现子弹生成)
|
||||
if (EntityManage.Instance != null && Program.Instance != null)
|
||||
{
|
||||
EntityManage.Instance.GenerateBulletEntity(
|
||||
Program.Instance.FocusedDimensionId,
|
||||
weapon.Bullet,
|
||||
transform.position, // 子弹的生成位置
|
||||
bulletDirection, // 子弹的初始方向
|
||||
this); // 子弹的发射者
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("EntityManage.Instance 或 Program.Instance 为空,无法生成子弹。请确保它们已正确初始化。");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public virtual WeaponResource GetCurrentWeapon()
|
||||
{
|
||||
return null;
|
||||
|
@ -1,9 +1,26 @@
|
||||
using Data;
|
||||
using Item;
|
||||
using Managers;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Monster:Entity
|
||||
{
|
||||
|
||||
private WeaponResource weapon;
|
||||
public override void Init(EntityDef entityDef)
|
||||
{
|
||||
base.Init(entityDef);
|
||||
var monsterDef = entityDef as MonsterDef;
|
||||
if (monsterDef != null)
|
||||
{
|
||||
weapon = (WeaponResource)ItemResourceManager.Instance.GetItem(monsterDef.weapon.defName);
|
||||
}
|
||||
}
|
||||
|
||||
public override WeaponResource GetCurrentWeapon()
|
||||
{
|
||||
return weapon;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -63,7 +63,6 @@ namespace Entity
|
||||
if (entity == null) return;
|
||||
if (entity.TryPickupItem(itemResource, 1) <= 0)
|
||||
{
|
||||
Debug.Log("Kill");
|
||||
Kill();
|
||||
}
|
||||
}
|
||||
|
@ -100,5 +100,19 @@ namespace Item
|
||||
/// 物品的帧率(如果适用)。
|
||||
/// </summary>
|
||||
public float FPS { get; protected set; }
|
||||
|
||||
public static ItemResource GetDefault()
|
||||
{
|
||||
ItemDef defaultDef = new()
|
||||
{
|
||||
defName = "default",
|
||||
label = "错误物品",
|
||||
description = "你看到这个物品表示加载出错了",
|
||||
rarity = ItemRarity.Uncommon,
|
||||
maxStack = 1,
|
||||
ssEquippable = false
|
||||
};
|
||||
return new ItemResource(defaultDef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq; // Added for LINQ
|
||||
using Base;
|
||||
using Data;
|
||||
using Entity;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Item
|
||||
{
|
||||
public class WeaponResource : ItemResource
|
||||
{
|
||||
public Attributes Attributes { get; private set; }
|
||||
|
||||
public BulletDef Bullet { get; private set; }
|
||||
public WeaponType Type { get; private set; }
|
||||
|
||||
public DrawNodeDef AttackAnimationDef { get; private set; }
|
||||
public float AttackAnimationTime{get; private set;}
|
||||
|
||||
public float AttackDetectionTime{get;private set;}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数:通过 WeaponDef 对象初始化 WeaponResource。
|
||||
@ -24,10 +35,205 @@ namespace Item
|
||||
throw new ArgumentNullException(nameof(def), "创建 WeaponResource 时,WeaponDef 不能为 null。");
|
||||
}
|
||||
|
||||
Bullet = def.bullet;
|
||||
// 初始化武器的属性。Attributes 对象通过 WeaponDef 中的属性数据进行构建。
|
||||
Attributes = new Attributes(def.attributes);
|
||||
// 初始化武器类型,直接从 WeaponDef 定义中获取。
|
||||
Type = def.type;
|
||||
AttackDetectionTime = def.attackDetectionTime;
|
||||
|
||||
// 逻辑修改说明 1:存储 DrawNodeDef,而不是直接创建 GameObject。
|
||||
AttackAnimationDef = def.attackAnimation;
|
||||
AttackAnimationTime = AttackAnimationDef?.GetAnimationCycleDuration() ?? 0.1f;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑修改说明 2:新增公共方法:根据 WeaponResource 的定义创建攻击动画的运行时 GameObject 实例。
|
||||
/// 这是 WeaponResource 的工厂方法,用于按需创建动画对象。
|
||||
/// </summary>
|
||||
/// <param name="parent">动画 GameObject 的父 Transform。</param>
|
||||
/// <returns>包含动画根 GameObject 和所有 ITick 组件的元组。</returns>
|
||||
public (GameObject root, ITick[] animationComponents) InstantiateAttackAnimation(Transform parent)
|
||||
{
|
||||
if (AttackAnimationDef == null)
|
||||
{
|
||||
Debug.LogWarning($"WeaponResource: 尝试实例化攻击动画但 _attackAnimationDef 为 null. 武器类型: {Type}");
|
||||
return (null, Array.Empty<ITick>());
|
||||
}
|
||||
|
||||
// 逻辑修改说明 4:在此处统一加载 ImagePrefab 和 SpriteAnimator,避免性能损耗。
|
||||
var imagePrefab = Resources.Load<ImagePrefab>("Prefab/Image");
|
||||
var animatorPrefab = Resources.Load<SpriteAnimator>("Prefab/Animation");
|
||||
|
||||
if (imagePrefab == null)
|
||||
{
|
||||
Debug.LogError("InstantiateAttackAnimation: 无法加载 Prefab/Image.");
|
||||
return (null, Array.Empty<ITick>());
|
||||
}
|
||||
if (animatorPrefab == null)
|
||||
{
|
||||
Debug.LogError("InstantiateAttackAnimation: 无法加载 Prefab/Animation.");
|
||||
return (null, Array.Empty<ITick>());
|
||||
}
|
||||
|
||||
// 逻辑修改说明 2:调用修改后的私有辅助方法来创建 GameObject 层次结构。
|
||||
var animationRoot = _CreateBodyPartGameObject(
|
||||
AttackAnimationDef,
|
||||
parent,
|
||||
imagePrefab,
|
||||
animatorPrefab);
|
||||
|
||||
if (animationRoot == null)
|
||||
{
|
||||
Debug.LogWarning($"WeaponResource: 创建攻击动画根节点失败. 武器类型: {Type}");
|
||||
return (null, Array.Empty<ITick>());
|
||||
}
|
||||
|
||||
// 逻辑修改说明 3:收集所有实现了 ITick 接口的组件。
|
||||
var tickComponents = animationRoot.GetComponentsInChildren<ITick>(true);
|
||||
// GetComponentsInChildren(true) 会查找包括自身在内的所有子对象上的组件,即使它们是处于非活动状态。
|
||||
|
||||
return (animationRoot, tickComponents);
|
||||
}
|
||||
|
||||
// 逻辑修改说明 4:修改 InitBodyPart 为 _CreateBodyPartGameObject,并设为 private static 方法。
|
||||
// 它现在接收 ImagePrefab 和 SpriteAnimator 作为参数,不再在内部加载。
|
||||
private static GameObject _CreateBodyPartGameObject(DrawNodeDef drawNode, Transform parent, ImagePrefab imagePrefab, SpriteAnimator animatorPrefab)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 参数验证
|
||||
if (drawNode == null)
|
||||
{
|
||||
Debug.LogWarning("CreateBodyPartGameObject: drawNode参数为null");
|
||||
return null;
|
||||
}
|
||||
|
||||
if (parent == null)
|
||||
{
|
||||
// 逻辑修改说明:这里直接返回 null 更合适,因为没有父节点无法创建实例。
|
||||
Debug.LogWarning($"CreateBodyPartGameObject: 父节点为null (节点名: {drawNode.nodeName ?? "Unnamed DrawNode"})");
|
||||
return null;
|
||||
}
|
||||
|
||||
GameObject nodeObject = null;
|
||||
// 根据纹理数量创建不同类型的节点
|
||||
switch (drawNode.textures?.Count ?? 0)
|
||||
{
|
||||
case 0:
|
||||
// 无纹理节点
|
||||
nodeObject = new GameObject(drawNode.nodeName);
|
||||
nodeObject.transform.SetParent(parent.transform, false);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
// 单纹理节点
|
||||
// imagePrefab 已作为参数传入,无需再次加载
|
||||
nodeObject = Object.Instantiate(imagePrefab.gameObject, parent.transform);
|
||||
nodeObject.name = drawNode.nodeName ?? "UnnamedNode"; // 提前设置名称以在警告中显示
|
||||
|
||||
var texture =
|
||||
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID,
|
||||
drawNode.textures[0]);
|
||||
|
||||
if (!texture)
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"CreateBodyPartGameObject: 无法获取纹理 (节点名: {nodeObject.name}, 纹理ID: {drawNode.textures[0]})");
|
||||
}
|
||||
|
||||
var imagePrefabCom = nodeObject.GetComponent<ImagePrefab>();
|
||||
if (imagePrefabCom != null)
|
||||
{
|
||||
imagePrefabCom.SetSprite(texture);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"CreateBodyPartGameObject: 无法获取ImagePrefab组件 (节点名: {nodeObject.name})");
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
// 多纹理动画节点
|
||||
// animatorPrefab 已作为参数传入,无需再次加载
|
||||
nodeObject = Object.Instantiate(animatorPrefab.gameObject, parent.transform);
|
||||
nodeObject.name = drawNode.nodeName ?? "UnnamedNode"; // 提前设置名称以在警告中显示
|
||||
|
||||
var animator = nodeObject.GetComponent<SpriteAnimator>();
|
||||
|
||||
if (animator == null)
|
||||
{
|
||||
Debug.LogWarning($"CreateBodyPartGameObject: 无法获取SpriteAnimator组件 (节点名: {nodeObject.name})");
|
||||
break;
|
||||
}
|
||||
|
||||
animator.SetFPS(drawNode.FPS);
|
||||
var animatedSprites = new List<Sprite>();
|
||||
foreach (var textureId in drawNode.textures)
|
||||
{
|
||||
try
|
||||
{
|
||||
var sprite =
|
||||
Managers.PackagesImageManager.Instance?.GetSprite(drawNode.packID, textureId);
|
||||
if (sprite != null)
|
||||
{
|
||||
animatedSprites.Add(sprite);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(
|
||||
$"CreateBodyPartGameObject: 无法获取动画纹理 (节点名: {nodeObject.name}, 纹理ID: {textureId})");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError(
|
||||
$"CreateBodyPartGameObject: 加载动画纹理时出错 (节点名: {nodeObject.name}, 纹理ID: {textureId}): {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
if (animatedSprites.Count > 0)
|
||||
{
|
||||
animator.SetSprites(animatedSprites.ToArray());
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"CreateBodyPartGameObject: 没有有效的动画纹理 (节点名: {nodeObject.name})");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// 设置节点属性 (对于case 1, default 已经在上面设置了名称,这里可以作为 fallback 或统一处理)
|
||||
if (nodeObject == null) return null; // 如果nodeObject在上面某些分支没有成功创建,提前返回
|
||||
|
||||
nodeObject.transform.localPosition = drawNode.position;
|
||||
// nodeObject.name = drawNode.nodeName ?? "UnnamedNode"; // 不再需要,已在上面处理
|
||||
|
||||
// 递归初始化子节点
|
||||
if (drawNode.nodes == null) return nodeObject;
|
||||
foreach (var child in drawNode.nodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 逻辑修改说明 4:递归调用时,传递 prefab 参数。
|
||||
_CreateBodyPartGameObject(child, nodeObject.transform, imagePrefab, animatorPrefab);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"CreateBodyPartGameObject: 初始化子节点失败 (父节点: {nodeObject.name ?? drawNode.nodeName ?? "Unnamed Parent"}): {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return nodeObject;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.LogError($"CreateBodyPartGameObject: 初始化节点时发生未处理的异常 (节点名: {drawNode?.nodeName ?? "Unknown DrawNode"}): {ex}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ namespace Managers
|
||||
|
||||
/// <summary> 角色实体的预制体。 </summary>
|
||||
public EntityPrefab characterPrefab;
|
||||
|
||||
public EntityPrefab monsterPrefab;
|
||||
/// <summary> 建筑实体的预制体。 </summary>
|
||||
public EntityPrefab buildingPrefab;
|
||||
|
||||
@ -369,7 +369,30 @@ namespace Managers
|
||||
);
|
||||
if (result == null) GenerateDefaultEntity(dimensionId, pos);
|
||||
}
|
||||
public void GenerateMonsterEntity(string dimensionId, MonsterDef monsterDef, Vector3 pos)
|
||||
{
|
||||
if (!monsterPrefab)
|
||||
{
|
||||
Debug.LogError("实体管理器:monsterPrefab 为空!请分配一个有效的预制体。");
|
||||
GenerateDefaultEntity(dimensionId, pos);
|
||||
return;
|
||||
}
|
||||
|
||||
if (monsterDef == null)
|
||||
{
|
||||
Debug.LogError("实体管理器:monsterDef 为空!无法生成实体。");
|
||||
GenerateDefaultEntity(dimensionId, pos);
|
||||
return;
|
||||
}
|
||||
|
||||
var result = GenerateEntityInternal(
|
||||
dimensionId,
|
||||
monsterPrefab.gameObject,
|
||||
pos,
|
||||
monsterDef
|
||||
);
|
||||
if (result == null) GenerateDefaultEntity(dimensionId, pos);
|
||||
}
|
||||
/// <summary>
|
||||
/// 在指定维度和网格位置生成一个建筑实体。
|
||||
/// </summary>
|
||||
|
@ -8,6 +8,7 @@ namespace Managers
|
||||
{
|
||||
public class ItemResourceManager : Utils.Singleton<ItemResourceManager>,ILaunchManager
|
||||
{
|
||||
private ItemResource defaultItem;
|
||||
private readonly Dictionary<string, Item.ItemResource> _items = new();
|
||||
private readonly Dictionary<string, List<Item.ItemResource>> _itemsByName = new(); // 保持按显示名称查找的字典
|
||||
|
||||
@ -15,6 +16,7 @@ namespace Managers
|
||||
|
||||
public void Init()
|
||||
{
|
||||
defaultItem=ItemResource.GetDefault();
|
||||
var baseItemDefs = Managers.DefineManager.Instance.QueryDefinesByType<ItemDef>();
|
||||
var weaponDefs = Managers.DefineManager.Instance.QueryDefinesByType<WeaponDef>();
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using Base;
|
||||
using UnityEngine;
|
||||
|
||||
@ -101,6 +102,13 @@ namespace Prefab
|
||||
// 重置帧计时器,以确保从头开始播放
|
||||
_frameTimer = 0f;
|
||||
}
|
||||
|
||||
public void Restore()
|
||||
{
|
||||
_currentFrameIndex = 0;
|
||||
_renderer.sprite = _sprites[_currentFrameIndex];
|
||||
}
|
||||
|
||||
public void SetFPS(float newFPS) => _fps = Mathf.Max(0.1f, newFPS);
|
||||
public void SetStaticSprite(Sprite sprite) => _staticSprite = sprite;
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ namespace UI
|
||||
InitMonster();
|
||||
InitBuilding();
|
||||
InitItem();
|
||||
InitWeapon();
|
||||
}
|
||||
|
||||
private void InitReloadGameButton()
|
||||
@ -101,7 +102,7 @@ namespace UI
|
||||
"生成怪物",
|
||||
"未定义任何怪物",
|
||||
def => def.label,
|
||||
GenerateEntityCallback);
|
||||
GenerateMonsterEntityCallback);
|
||||
}
|
||||
|
||||
private void InitBuilding()
|
||||
@ -170,7 +171,16 @@ namespace UI
|
||||
entityPlacementUI.snapEnabled = false;
|
||||
UIInputControl.Instance.Show(entityPlacementUI);
|
||||
}
|
||||
|
||||
private void GenerateMonsterEntityCallback(MonsterDef monsterDef)
|
||||
{
|
||||
entityPlacementUI.currentAction = () =>
|
||||
{
|
||||
Managers.EntityManage.Instance.GenerateMonsterEntity(Program.Instance.FocusedDimensionId,monsterDef, Utils.MousePosition.GetWorldPosition());
|
||||
};
|
||||
entityPlacementUI.Prompt = $"当前生成器:\n名称:{monsterDef.label}\n描述:{monsterDef.description}";
|
||||
entityPlacementUI.snapEnabled = false;
|
||||
UIInputControl.Instance.Show(entityPlacementUI);
|
||||
}
|
||||
private void GenerateBuildingCallback(BuildingDef def)
|
||||
{
|
||||
entityPlacementUI.currentAction = () =>
|
||||
|
@ -1,8 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Base;
|
||||
using Entity;
|
||||
using UnityEngine;
|
||||
// 确保 Character 类在此命名空间下
|
||||
|
||||
namespace UI
|
||||
{
|
||||
@ -10,26 +10,27 @@ namespace UI
|
||||
/// 负责管理和显示角色的装备用户界面。
|
||||
/// 该组件会监听当前关注实体的变化,并根据所关注角色的库存数据动态更新装备槽位的显示,
|
||||
/// 同时采用对象池技术高效管理 ItemUI 实例的创建和复用。
|
||||
/// 除了显示,现在还支持通过滚轮选择物品,并同步更新焦点角色的 CurrentSelected 字段。
|
||||
/// </summary>
|
||||
public class EquipmentUI : MonoBehaviour,ITick
|
||||
public class EquipmentUI : MonoBehaviour, ITick
|
||||
{
|
||||
[SerializeField]
|
||||
[Tooltip("所有 ItemUI 实例的父级 GameObject,用于布局。")]
|
||||
[SerializeField]
|
||||
[Tooltip("所有 ItemUI 实例的父级 GameObject,用于布局。")]
|
||||
private GameObject uiParent;
|
||||
|
||||
[SerializeField]
|
||||
[Tooltip("用于实例化装备槽位的 ItemUI 预制件。")]
|
||||
[SerializeField]
|
||||
[Tooltip("用于实例化装备槽位的 ItemUI 预制件。")]
|
||||
private ItemUI itemUIPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// 当前界面所关联和关注的角色实体。
|
||||
/// </summary>
|
||||
private Character focusedEntity = null;
|
||||
private Character focusedEntity;
|
||||
|
||||
/// <summary>
|
||||
/// ItemUI 实例的对象池,用于高效管理和复用 ItemUI。
|
||||
/// </summary>
|
||||
private List<ItemUI> itemUIPool = new List<ItemUI>();
|
||||
private List<ItemUI> itemUIPool = new();
|
||||
|
||||
/// <summary>
|
||||
/// MonoBehaviour 的 Start 生命周期方法。
|
||||
@ -49,10 +50,10 @@ namespace UI
|
||||
private void OnDestroy()
|
||||
{
|
||||
Program.Instance.OnFocusedEntityChanged -= UpdateFocusedEntity;
|
||||
|
||||
|
||||
// 如果当前有关注的角色,取消注册其库存改变事件。
|
||||
// 注意:此处需要确保 focusedEntity 不为 null,否则可能抛出 NullReferenceException。
|
||||
if (focusedEntity != null)
|
||||
// 确保 focusedEntity 不为 null 且 Inventory 不为 null。
|
||||
if (focusedEntity != null && focusedEntity.Inventory != null)
|
||||
{
|
||||
focusedEntity.Inventory.OnInventoryChanged -= UpdateUI;
|
||||
}
|
||||
@ -60,7 +61,7 @@ namespace UI
|
||||
// 销毁对象池中所有 ItemUI 的 GameObject。
|
||||
foreach (var itemUI in itemUIPool)
|
||||
{
|
||||
if (itemUI != null && itemUI.gameObject != null)
|
||||
if (itemUI != null && itemUI.gameObject != null)
|
||||
{
|
||||
Destroy(itemUI.gameObject);
|
||||
}
|
||||
@ -76,8 +77,8 @@ namespace UI
|
||||
private void UpdateFocusedEntity(Entity.Entity entity)
|
||||
{
|
||||
// 如果之前有关注的角色,先取消注册其库存改变事件。
|
||||
// 注意:此处需要确保 focusedEntity 不为 null。
|
||||
if (focusedEntity != null)
|
||||
// 确保 focusedEntity 不为 null 且 Inventory 不为 null。
|
||||
if (focusedEntity != null && focusedEntity.Inventory != null)
|
||||
{
|
||||
focusedEntity.Inventory.OnInventoryChanged -= UpdateUI;
|
||||
}
|
||||
@ -88,17 +89,37 @@ namespace UI
|
||||
{
|
||||
focusedEntity = newCharacter;
|
||||
}
|
||||
// 如果传入的 entity 不是 Character 类型,focusedEntity 将保持其当前值。
|
||||
// 如果希望在非 Character 实体被关注时清除 focusedEntity,则需要在此处添加 `else { focusedEntity = null; }`。
|
||||
else
|
||||
{
|
||||
// 如果传入的 entity 不是 Character 类型,或者为 null,则清除当前的 focusedEntity。
|
||||
focusedEntity = null;
|
||||
}
|
||||
|
||||
// 如果现在有关注的角色,注册其库存改变事件。
|
||||
if (focusedEntity != null)
|
||||
if (focusedEntity != null)
|
||||
{
|
||||
focusedEntity.Inventory.OnInventoryChanged += UpdateUI;
|
||||
}
|
||||
|
||||
|
||||
// 立即更新UI以反映新的关注实体(或没有关注实体)的状态。
|
||||
UpdateUI();
|
||||
UpdateUI();
|
||||
|
||||
// 在更新UI后,确保UI的选中状态与角色当前选中字段同步
|
||||
// 只有当有焦点角色且库存不为空时才更新选中状态
|
||||
if (focusedEntity != null && focusedEntity.Inventory != null && focusedEntity.Inventory.Capacity > 0)
|
||||
{
|
||||
// 确保 CurrentSelected 在有效范围内,否则重置为0
|
||||
if (focusedEntity.CurrentSelected < 0 || focusedEntity.CurrentSelected >= focusedEntity.Inventory.Capacity)
|
||||
{
|
||||
focusedEntity.CurrentSelected = 0;
|
||||
}
|
||||
UpdateSelectionUI(focusedEntity.CurrentSelected);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果没有焦点实体或库存为空,则清空所有选中状态
|
||||
UpdateSelectionUI(-1); // 传入一个无效索引以取消所有选中
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -108,8 +129,7 @@ namespace UI
|
||||
private void UpdateUI()
|
||||
{
|
||||
// 如果没有关注的角色或其库存,则禁用所有 ItemUI。
|
||||
// 注意:此处需要先检查 focusedEntity 是否为 null,以避免 NullReferenceException。
|
||||
if (focusedEntity == null || focusedEntity.Inventory == null)
|
||||
if (focusedEntity == null || focusedEntity.Inventory == null)
|
||||
{
|
||||
foreach (var itemUI in itemUIPool)
|
||||
{
|
||||
@ -127,6 +147,7 @@ namespace UI
|
||||
{
|
||||
Debug.LogError("ItemUIPrefab 未在 EquipmentUI 中指定。无法创建物品用户界面。", this);
|
||||
foreach (var itemUI in itemUIPool) itemUI.gameObject.SetActive(false);
|
||||
uiParent.SetActive(false); // 确保父级也被禁用
|
||||
return;
|
||||
}
|
||||
|
||||
@ -143,16 +164,17 @@ namespace UI
|
||||
}
|
||||
else
|
||||
{
|
||||
var itemObj=Instantiate(itemUIPrefab.gameObject, uiParent.transform);
|
||||
// 使用 Instantiate(GameObject, Transform) 以确保父级设置正确且避免转换问题。
|
||||
var itemObj = Instantiate(itemUIPrefab.gameObject, uiParent.transform);
|
||||
itemUI = itemObj.GetComponent<ItemUI>();
|
||||
itemUIPool.Add(itemUI);
|
||||
currentUIPoolSize++; // 更新池的大小计数。
|
||||
}
|
||||
|
||||
|
||||
// 确保 ItemUI GameObject 处于激活状态,并使用当前物品槽位的数据进行初始化。
|
||||
itemUI.gameObject.SetActive(true);
|
||||
itemUI.Init(focusedEntity.Inventory.GetSlot(i), i);
|
||||
itemUI.Select = false;
|
||||
// 移除此处 itemUI.Select = false; 选中状态将由 UpdateSelectionUI 统一管理
|
||||
}
|
||||
|
||||
// 如果库存槽位数量减少,禁用对象池中多余的 ItemUI 实例。
|
||||
@ -161,14 +183,93 @@ namespace UI
|
||||
if (itemUIPool[i] != null && itemUIPool[i].gameObject != null)
|
||||
{
|
||||
itemUIPool[i].gameObject.SetActive(false);
|
||||
itemUIPool[i].Select = false; // 禁用时也确保清除选中状态
|
||||
}
|
||||
}
|
||||
uiParent.SetActive(true);
|
||||
|
||||
// 首次更新UI时,或者当Inventory改变时,需要确保 CurrentSelected 的UI状态是正确的
|
||||
// 但如果 UpdateFocusedEntity 已经处理了,这里可以省略,或者确保只在必要时调用
|
||||
// 考虑到 UpdateUI 也会被 Inventory.OnInventoryChanged 调用,这里再次确保同步是合理的。
|
||||
if (focusedEntity != null && focusedEntity.Inventory != null && focusedEntity.Inventory.Capacity > 0)
|
||||
{
|
||||
UpdateSelectionUI(focusedEntity.CurrentSelected);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateSelectionUI(-1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当当前选中物品改变时,更新所有 ItemUI 的选中状态。
|
||||
/// </summary>
|
||||
/// <param name="selectedItemIndex">当前选中的物品索引。传入 -1 将取消所有 ItemUI 的选中状态。</param>
|
||||
private void UpdateSelectionUI(int selectedItemIndex)
|
||||
{
|
||||
// 如果对象池为空,则无需更新
|
||||
if (itemUIPool == null || itemUIPool.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < itemUIPool.Count; i++)
|
||||
{
|
||||
ItemUI itemUI = itemUIPool[i];
|
||||
if (itemUI != null && itemUI.gameObject != null)
|
||||
{
|
||||
// 只有在 ItemUI 激活状态下才设置其选中状态,避免对禁用UI的操作
|
||||
// 或者如果传入-1,即使激活也全部设置为false
|
||||
if (itemUI.gameObject.activeSelf || selectedItemIndex == -1) // 确保当取消所有选中时,循环到所有激活的,甚至当前禁用的ItemUI
|
||||
{
|
||||
itemUI.Select = (i == selectedItemIndex && itemUI.gameObject.activeSelf);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每帧调用的更新方法,用于处理滚轮输入以选择物品。
|
||||
/// </summary>
|
||||
public void Tick()
|
||||
{
|
||||
|
||||
// 如果没有焦点实体、没有库存或库存为空,不进行选择操作
|
||||
if (focusedEntity == null || focusedEntity.Inventory == null || focusedEntity.Inventory.Capacity == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float scrollInput = Input.GetAxis("Mouse ScrollWheel");
|
||||
|
||||
if (scrollInput != 0) // 检测到滚轮输入
|
||||
{
|
||||
int currentSelection = focusedEntity.CurrentSelected;
|
||||
int inventoryCapacity = focusedEntity.Inventory.Capacity;
|
||||
|
||||
if (scrollInput > 0) // 滚轮向上,选择前一个
|
||||
{
|
||||
currentSelection--;
|
||||
if (currentSelection < 0)
|
||||
{
|
||||
currentSelection = inventoryCapacity - 1; // 循环到最后一个
|
||||
}
|
||||
}
|
||||
else // 滚轮向下,选择后一个
|
||||
{
|
||||
currentSelection++;
|
||||
if (currentSelection >= inventoryCapacity)
|
||||
{
|
||||
currentSelection = 0; // 循环到第一个
|
||||
}
|
||||
}
|
||||
|
||||
// 如果选择发生变化,则更新焦点实体和UI
|
||||
if (focusedEntity.CurrentSelected != currentSelection)
|
||||
{
|
||||
focusedEntity.CurrentSelected = currentSelection;
|
||||
UpdateSelectionUI(currentSelection); // 更新UI选中状态
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -29,8 +29,10 @@
|
||||
<label>测试枪</label>
|
||||
<description>一把测试用的枪</description>
|
||||
<attributes>TestGun</attributes>
|
||||
<texture>TestGun</texture>
|
||||
<itemTexture>TestGunItem</itemTexture>
|
||||
<type>Ranged</type>
|
||||
<textures>
|
||||
<li>TestGunItem</li>
|
||||
</textures>
|
||||
<bullet>yellowBullet</bullet>
|
||||
</WeaponDef>
|
||||
|
||||
@ -52,12 +54,27 @@
|
||||
</textures>
|
||||
</walk_down>
|
||||
</drawingOrder>
|
||||
<behaviorTree>
|
||||
<Node className="JobGiver_ContinuousMove"/>
|
||||
</behaviorTree>
|
||||
</BulletDef>
|
||||
|
||||
|
||||
<BulletDef>
|
||||
<defName>testGunBullet</defName>
|
||||
<attributes>
|
||||
<health>1</health>
|
||||
<moveSpeed>20</moveSpeed>
|
||||
</attributes>
|
||||
<drawingOrder>
|
||||
<idle_down>
|
||||
<textures>
|
||||
<li>TestGun</li>
|
||||
</textures>
|
||||
</idle_down>
|
||||
<walk_down>
|
||||
<textures>
|
||||
<li>TestGun</li>
|
||||
</textures>
|
||||
</walk_down>
|
||||
</drawingOrder>
|
||||
</BulletDef>
|
||||
|
||||
|
||||
|
||||
@ -73,6 +90,7 @@
|
||||
<WeaponDef>
|
||||
<defName>Claw</defName>
|
||||
<label>爪子</label>
|
||||
<type>Melee</type>
|
||||
<description>爪子</description>
|
||||
<attributes>Claw</attributes>
|
||||
</WeaponDef>
|
||||
|
@ -114,6 +114,10 @@
|
||||
</rangedAttack_right>
|
||||
</drawingOrder>
|
||||
<behaviorTree>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
</CharacterDef>
|
||||
@ -142,6 +146,10 @@
|
||||
</idle_down>
|
||||
</drawingOrder>
|
||||
<behaviorTree>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
</CharacterDef>
|
||||
@ -170,7 +178,11 @@
|
||||
</idle_down>
|
||||
</drawingOrder>
|
||||
<behaviorTree>
|
||||
<Node className="JobGiver_Idel"/>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
</CharacterDef>
|
||||
|
||||
@ -198,7 +210,11 @@
|
||||
</idle_down>
|
||||
</drawingOrder>
|
||||
<behaviorTree>
|
||||
<Node className="JobGiver_Idel"/>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
</CharacterDef>
|
||||
|
||||
|
@ -3,11 +3,20 @@
|
||||
|
||||
<MonsterDef>
|
||||
<defName>chicken</defName>
|
||||
<label>测试鸡</label>
|
||||
<label>不大聪明</label>
|
||||
<behaviorTree>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
<affiliation>monster</affiliation>
|
||||
<attributes>
|
||||
<moveSpeed>1</moveSpeed>
|
||||
<health>123</health>
|
||||
</attributes>
|
||||
<weapon>Claw</weapon>
|
||||
<drawingOrder>
|
||||
<idle_down name="Body" FPS="10">
|
||||
<textures>
|
||||
@ -24,5 +33,36 @@
|
||||
</drawingOrder>
|
||||
</MonsterDef>
|
||||
|
||||
<MonsterDef>
|
||||
<defName>big</defName>
|
||||
<label>大聪明</label>
|
||||
<behaviorTree>
|
||||
<Node className="ThinkNode_Conditional" value="EntityHealth(50)">
|
||||
<Node className="JobGiver_AttackJob"/>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</Node>
|
||||
<Node className="JobGiver_RandomWander"/>
|
||||
</behaviorTree>
|
||||
<affiliation>monster</affiliation>
|
||||
<attributes>
|
||||
<moveSpeed>1</moveSpeed>
|
||||
<health>123</health>
|
||||
</attributes>
|
||||
<weapon>TestGun</weapon>
|
||||
<drawingOrder>
|
||||
<idle_down name="Body" FPS="20">
|
||||
<textures>
|
||||
<li>testPawnAnimation_0</li>
|
||||
<li>testPawnAnimation_1</li>
|
||||
<li>testPawnAnimation_2</li>
|
||||
<li>testPawnAnimation_3</li>
|
||||
<li>testPawnAnimation_4</li>
|
||||
<li>testPawnAnimation_3</li>
|
||||
<li>testPawnAnimation_2</li>
|
||||
<li>testPawnAnimation_1</li>
|
||||
</textures>
|
||||
</idle_down>
|
||||
</drawingOrder>
|
||||
</MonsterDef>
|
||||
|
||||
</Define>
|
Reference in New Issue
Block a user