(client) chore

This commit is contained in:
m0_75251201
2025-08-26 16:00:58 +08:00
parent efbf4f824a
commit f04c89046b
51 changed files with 2390 additions and 617 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using Base;
using Data;
using Entity;
using Map;
using Prefab;
@ -39,13 +40,16 @@ namespace Managers
/// <summary> 角色实体的预制体。 </summary>
public EntityPrefab characterPrefab;
/// <summary> 建筑实体的预制体。 </summary>
public EntityPrefab buildingPrefab;
/// <summary> 子弹实体的预制体。 </summary>
public EntityPrefab bulletPrefab;
/// <summary> 默认实体的预制体,用于生成失败时的回退。 </summary>
public EntityPrefab defaultEntityPrefab;
// 已移除RegisterDimension 和 UnregisterDimension 方法的相关注释,因为这些方法已不存在且与逻辑无关。
/// <summary>
@ -80,7 +84,7 @@ namespace Managers
public void Tick()
{
// 获取当前管理器中所有活跃维度ID的副本以安全地迭代和移除。
List<string> activeDimensionIdsInManager = _dimensionFactionEntities.Keys.ToList();
var activeDimensionIdsInManager = _dimensionFactionEntities.Keys.ToList();
foreach (var dimensionId in activeDimensionIdsInManager)
{
@ -88,7 +92,8 @@ namespace Managers
var dimension = Program.Instance.GetDimension(dimensionId);
if (dimension == null)
{
Debug.LogWarning($"实体管理器:跳过维度 '{dimensionId}' 的Tick处理因为其维度对象在程序Program中已不再活跃或从未注册。正在清理相关内部数据。");
Debug.LogWarning(
$"实体管理器:跳过维度 '{dimensionId}' 的Tick处理因为其维度对象在程序Program中已不再活跃或从未注册。正在清理相关内部数据。");
// 如果 Program 不再有这个维度说明它已经失效或被注销EntityManage 需要清理自己的相关数据。
_dimensionFactionEntities.Remove(dimensionId);
_dimensionLayerCache.Remove(dimensionId);
@ -96,7 +101,7 @@ namespace Managers
RemovePendingAdditionsForDimension(dimensionId);
continue; // 跳过处理这个不存在的维度
}
// 现在真正处理的是 _dimensionFactionEntities 中该维度ID对应的字典。
var factionDict = _dimensionFactionEntities[dimensionId];
@ -149,7 +154,7 @@ namespace Managers
_dimensionLayerCache.Remove(dimensionId); // 对应的层级缓存也可以清理。
}
}
// 处理待添加实体 (现在维度感知)。
if (_pendingAdditions.Any())
{
@ -163,7 +168,8 @@ namespace Managers
// 再次检查维度是否活跃(在 Program 中注册),防止在添加前维度被 Program 注销。
if (Program.Instance.GetDimension(dimensionId) == null)
{
Debug.LogError($"实体管理器:尝试将实体 '{entityPrefab?.name ?? "null"}' 添加到在程序Program中未注册或不活跃的维度 '{dimensionId}'。该实体将被销毁。");
Debug.LogError(
$"实体管理器:尝试将实体 '{entityPrefab?.name ?? "null"}' 添加到在程序Program中未注册或不活跃的维度 '{dimensionId}'。该实体将被销毁。");
if (entityPrefab != null && entityPrefab.gameObject != null) Destroy(entityPrefab.gameObject);
_pendingAdditions.Remove(pending); // 立即从待添加列表中移除已处理的条目。
continue;
@ -215,6 +221,7 @@ namespace Managers
remainingPendingAdditions.Add(pending);
}
}
_pendingAdditions = remainingPendingAdditions;
}
@ -262,7 +269,7 @@ namespace Managers
throw new InvalidOperationException(
$"在 '{instantiatedEntity.name}' 上缺少 EntityPrefab 组件,无法完成实体初始化。");
}
entityComponent.entity.currentDimensionId=dimensionId;
entityComponent.Init(def);
extraInit?.Invoke(entityComponent);
@ -296,11 +303,9 @@ namespace Managers
{
_dimensionLayerCache[dimensionId] = new Dictionary<string, Transform>();
layerCacheForDimension = _dimensionLayerCache[dimensionId]; // 更新引用。
Debug.LogWarning($"实体管理器:按需为维度 '{dimensionId}' 初始化层级缓存。");
}
else
{
Debug.LogError($"实体管理器:在层级缓存中未找到维度 '{dimensionId}'且在程序Program中也未注册。无法创建层级 '{layerName}'。");
return null;
}
}
@ -490,6 +495,208 @@ namespace Managers
entityComponent.DefaultInit();
}
/// <summary>
/// 在指定维度中,查找与源实体具有特定关系的最近实体。
/// </summary>
/// <param name="dimensionId">要搜索的维度ID。</param>
/// <param name="sourceEntityPrefab">作为参照的源实体预制体。</param>
/// <param name="targetRelationship">目标实体与源实体之间的期望关系例如Friendly, Hostile。</param>
/// <returns>找到的最近实体预制体,如果没有找到符合条件的实体则返回 null。</returns>
public EntityPrefab FindNearestEntityByRelation(string dimensionId, EntityPrefab sourceEntityPrefab,
Relation targetRelationship)
{
// 参数校验:确保输入参数有效,避免空引用异常。
if (sourceEntityPrefab == null || sourceEntityPrefab.entity == null)
{
Debug.LogWarning("实体管理器FindNearestEntityByRelation 方法中,源实体预制体或其内部实体为空。无法执行搜索。");
return null;
}
// 维度数据存在性检查:验证目标维度是否在实体管理器的内部数据结构中被初始化和管理。
if (!_dimensionFactionEntities.TryGetValue(dimensionId, out var factionDict))
{
Debug.LogWarning($"实体管理器FindNearestEntityByRelation 方法中,维度 '{dimensionId}' 未被初始化或未在内部管理实体。");
return null;
}
// 初始化追踪变量:设置初始值,用于在遍历过程中追踪最近的实体及其距离。
// 使用平方距离 (SqrMagnitude) 可以避免在每次距离计算时进行昂贵的开方运算,从而提高性能。
EntityPrefab nearestTarget = null;
var minDistanceSqr = float.MaxValue;
var sourcePos = sourceEntityPrefab.transform.position;
// 关系管理器实例检查:确保 AffiliationManager 可用,它是判断实体关系的核心组件。
var affiliationManager = AffiliationManager.Instance;
if (affiliationManager == null)
{
Debug.LogError("实体管理器FindNearestEntityByRelation 方法中AffiliationManager 实例为空。无法确定实体关系。");
return null;
}
// 遍历所有派系和实体_dimensionFactionEntities 按维度和派系组织,需要遍历所有派系才能找到维度内的所有实体。
foreach (var factionEntities in factionDict.Values) // factionDict.Values 是 LinkedList<EntityPrefab> 的集合
{
foreach (var currentEntityPrefab in factionEntities)
{
// 实体有效性及排除源实体自身:
// 1. 排除无效或已死亡的实体,确保只处理活跃的实体。
// 2. 在寻找“最近”实体时,通常指的是 *除了自身以外* 的实体。
// 如果需要包含自身(例如,当 targetRelationship 是 AffiliationManager.Relation.Self 时),
// 可以根据具体需求调整此逻辑,但默认行为是排除自身。
if (currentEntityPrefab == null || currentEntityPrefab.entity == null ||
currentEntityPrefab.entity.IsDead || currentEntityPrefab == sourceEntityPrefab)
{
continue;
}
// 关系判断:使用 AffiliationManager 提供的接口判断源实体与当前遍历实体之间的关系。
var currentRelation =
affiliationManager.GetRelation(sourceEntityPrefab.entity, currentEntityPrefab.entity);
if (currentRelation == targetRelationship)
{
// 距离计算与最近实体更新:
// 1. 计算与源实体的距离(使用平方距离优化)。
// 2. 如果当前实体更近,则更新 nearestTarget 和 minDistanceSqr。
var distanceSqr = Vector3.SqrMagnitude(currentEntityPrefab.transform.position - sourcePos);
if (distanceSqr < minDistanceSqr)
{
minDistanceSqr = distanceSqr;
nearestTarget = currentEntityPrefab;
}
}
}
}
return nearestTarget; // 返回找到的最近实体
}
/// <summary>
/// 在指定维度中,判断是否存在任何与源实体敌对的活跃实体。
/// </summary>
/// <param name="dimensionId">要搜索的维度ID。</param>
/// <param name="sourceEntityPrefab">作为参照的源实体预制体。</param>
/// <returns>如果存在敌对实体则返回 true否则返回 false。</returns>
public bool ExistsHostile(string dimensionId, EntityPrefab sourceEntityPrefab)
{
// 参数校验:确保输入参数有效,避免空引用异常。
if (sourceEntityPrefab == null || sourceEntityPrefab.entity == null)
{
Debug.LogWarning("实体管理器ExistsHostile 方法中,源实体预制体或其内部实体为空。无法执行搜索。");
return false;
}
// 维度数据存在性检查:验证目标维度是否在实体管理器的内部数据结构中被初始化和管理。
if (!_dimensionFactionEntities.TryGetValue(dimensionId, out var factionDict))
{
Debug.LogWarning($"实体管理器ExistsHostile 方法中,维度 '{dimensionId}' 未被初始化或未在内部管理实体。");
return false;
}
// 关系管理器实例检查:确保 AffiliationManager 可用,它是判断实体关系的核心组件。
var affiliationManager = AffiliationManager.Instance;
if (affiliationManager == null)
{
Debug.LogError("实体管理器ExistsHostile 方法中AffiliationManager 实例为空。无法确定实体关系。");
return false;
}
// 遍历所有实体:遍历维度内的所有派系,再遍历每个派系下的所有实体。
foreach (var factionEntities in factionDict.Values)
{
foreach (var currentEntityPrefab in factionEntities)
{
// 实体有效性及排除源实体自身:
// 1. 排除无效或已死亡的实体。
// 2. 排除源实体自身,避免自身判断为敌对。
if (currentEntityPrefab == null || currentEntityPrefab.entity == null ||
currentEntityPrefab.entity.IsDead || currentEntityPrefab == sourceEntityPrefab)
{
continue;
}
// 关系判断:判断当前实体与源实体是否为“敌对”关系。
if (affiliationManager.GetRelation(sourceEntityPrefab.entity, currentEntityPrefab.entity) ==
Data.Relation.Hostile)
{
return true; // 找到第一个敌对实体即返回 true提高效率。
}
}
}
return false; // 遍历完所有实体都未找到敌对实体。
}
/// <summary>
/// 在指定维度中,判断源实体视野范围内是否存在任何与源实体敌对的活跃实体。
/// </summary>
/// <param name="dimensionId">要搜索的维度ID。</param>
/// <param name="sourceEntityPrefab">作为参照的源实体预制体。</param>
/// <param name="sightRange">视野范围,实体间的距离小于等于此值视为在视野内。</param>
/// <returns>如果视野范围内存在敌对实体则返回 true否则返回 false。</returns>
public bool ExistsHostileInSightRange(string dimensionId, EntityPrefab sourceEntityPrefab, float sightRange)
{
// 参数校验:确保输入参数有效,避免空引用异常及不合理的范围值。
if (sourceEntityPrefab == null || sourceEntityPrefab.entity == null)
{
Debug.LogWarning("实体管理器ExistsHostileInSightRange 方法中,源实体预制体或其内部实体为空。无法执行搜索。");
return false;
}
if (sightRange <= 0)
{
Debug.LogWarning($"实体管理器ExistsHostileInSightRange 方法中,视野范围必须为正值。接收到的值为: {sightRange}。");
return false;
}
// 维度数据存在性检查:验证目标维度是否在实体管理器的内部数据结构中被初始化和管理。
if (!_dimensionFactionEntities.TryGetValue(dimensionId, out var factionDict))
{
Debug.LogWarning($"实体管理器ExistsHostileInSightRange 方法中,维度 '{dimensionId}' 未被初始化或未在内部管理实体。");
return false;
}
// 关系管理器实例检查:确保 AffiliationManager 可用,它是判断实体关系的核心组件。
var affiliationManager = AffiliationManager.Instance;
if (affiliationManager == null)
{
Debug.LogError("实体管理器ExistsHostileInSightRange 方法中AffiliationManager 实例为空。无法确定实体关系。");
return false;
}
// 预计算平方距离:避免在循环内部重复计算平方根,提高性能。
var sightRangeSqr = sightRange * sightRange;
var sourcePos = sourceEntityPrefab.transform.position;
// 遍历所有实体:遍历维度内的所有派系,再遍历每个派系下的所有实体。
foreach (var factionEntities in factionDict.Values)
{
foreach (var currentEntityPrefab in factionEntities)
{
// 实体有效性及排除源实体自身:
// 1. 排除无效或已死亡的实体。
// 2. 排除源实体自身。
if (currentEntityPrefab == null || currentEntityPrefab.entity == null ||
currentEntityPrefab.entity.IsDead || currentEntityPrefab == sourceEntityPrefab)
{
continue;
}
// 关系判断:判断当前实体与源实体是否为“敌对”关系。
if (affiliationManager.GetRelation(sourceEntityPrefab.entity, currentEntityPrefab.entity) ==
Data.Relation.Hostile)
{
// 距离判断:如果关系敌对,进一步判断其是否在视野范围内。
var distanceSqr = Vector3.SqrMagnitude(currentEntityPrefab.transform.position - sourcePos);
if (distanceSqr <= sightRangeSqr)
{
return true; // 找到第一个视野范围内的敌对实体即返回 true。
}
}
}
}
return false; // 遍历完所有实体都未找到视野范围内的敌对实体。
}
/// <summary>
/// 单例管理器启动时调用,订阅场景加载事件。
/// </summary>
@ -513,8 +720,8 @@ namespace Managers
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
_dimensionFactionEntities.Clear(); // 清理所有旧场景的实体数据。
_dimensionLayerCache.Clear(); // 清理所有旧场景的层级缓存。
_dimensionLayerCache.Clear(); // 清理所有旧场景的层级缓存。
// 场景加载时清理待添加列表中的所有实体,因为它们可能属于旧场景或未在任何维度中被处理。
foreach (var pending in _pendingAdditions)
{
@ -523,14 +730,15 @@ namespace Managers
Destroy(pending.Item3.gameObject);
}
}
_pendingAdditions.Clear(); // 清理待添加实体列表。
// 在新场景加载完成后,遍历 Program 中已注册的所有维度,并为每个维度初始化 EntityManage 的内部数据结构。
// 此时 Program.Awake() 应该已经完成,所有 Dimension 组件的 Awake() 也已执行Program.RegisteredDimensions 应该包含了当前场景的所有维度。
foreach (var registeredDimensionEntry in Program.Instance.RegisteredDimensions)
{
string dimensionId = registeredDimensionEntry.Key;
Dimension dimension = registeredDimensionEntry.Value;
var dimensionId = registeredDimensionEntry.Key;
var dimension = registeredDimensionEntry.Value;
if (dimension != null) // 确保维度实例本身不是null。
{
@ -541,7 +749,8 @@ namespace Managers
}
else
{
Debug.LogWarning($"实体管理器在场景加载初始化期间Program 为ID '{dimensionId}' 注册了一个空的 Dimension 对象。这可能表明维度注册存在问题。");
Debug.LogWarning(
$"实体管理器在场景加载初始化期间Program 为ID '{dimensionId}' 注册了一个空的 Dimension 对象。这可能表明维度注册存在问题。");
}
}
}
@ -559,7 +768,8 @@ namespace Managers
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
if (defaultEntityPrefab == null)
{
Debug.LogError($"实体管理器:已加载 DefaultEntity 预制体,但它缺少来自 Resources/Default/DefaultEntity 的 EntityPrefab 组件。请确保它包含该组件。");
Debug.LogError(
$"实体管理器:已加载 DefaultEntity 预制体,但它缺少来自 Resources/Default/DefaultEntity 的 EntityPrefab 组件。请确保它包含该组件。");
}
}
else