(client) feat:做了初始化加载动画。 fix:修复定义加载列表和加载枚举时的错误识别

This commit is contained in:
m0_75251201
2025-08-17 23:01:43 +08:00
parent dea6cd5fa3
commit f67aca0804
18 changed files with 1247 additions and 39 deletions

View File

@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Data;
namespace Managers
{
public class AffiliationManager:Utils.Singleton<AffiliationManager>
{
//定义名,阵营定义
private readonly Dictionary<string, AffiliationDef> _affiliations = new();
public void Init()
{
var affiliationList = Managers.DefineManager.Instance.QueryDefinesByType<AffiliationDef>();
if (affiliationList == null ||affiliationList.Length==0)
return;
foreach (var affiliation in affiliationList)
{
_affiliations.Add(affiliation.defName, affiliation);
}
ValidateAndFixRelationships();
}
public string GetAffiliationName(string defName)
{
return _affiliations[defName].defName;
}
public Relation GetRelation(string factionName1, string factionName2)
{
// 如果查询的是同一个派系,默认是友好关系
if (factionName1 == factionName2)
{
return Relation.Friendly;
}
// 尝试获取两个派系的定义
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
!_affiliations.TryGetValue(factionName2, out _))
{
if (faction1 != null) return faction1.defaultRelation;
return Relation.Neutral;
}
// 检查faction1是否明确将faction2列为敌对
if (faction1.hostileFactions != null && faction1.hostileFactions.Contains(factionName2))
{
return Relation.Hostile;
}
// 检查faction1是否明确将faction2列为友好
if (faction1.friendlyFactions != null && faction1.friendlyFactions.Contains(factionName2))
{
return Relation.Friendly;
}
// 检查faction1是否明确将faction2列为中立
if (faction1.neutralFactions != null && faction1.neutralFactions.Contains(factionName2))
{
return Relation.Neutral;
}
// 如果faction1没有明确设置与faction2的关系则使用faction1的默认关系
return faction1.defaultRelation;
}
/// <summary>
/// 设置两个阵营之间的关系
/// </summary>
/// <param name="factionName1">第一个阵营名称</param>
/// <param name="factionName2">第二个阵营名称</param>
/// <param name="relation">要设置的关系</param>
public void SetRelation(string factionName1, string factionName2, Relation relation)
{
// 不能设置自己与自己的关系
if (factionName1 == factionName2)
{
throw new ArgumentException("Cannot set relation between the same faction");
}
// 确保两个阵营都存在
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
!_affiliations.TryGetValue(factionName2, out _))
{
throw new ArgumentException("One or both factions do not exist");
}
// 确保关系列表已初始化
faction1.hostileFactions ??= new List<string>();
faction1.friendlyFactions ??= new List<string>();
faction1.neutralFactions ??= new List<string>();
// 先移除所有现有关系
faction1.hostileFactions.Remove(factionName2);
faction1.friendlyFactions.Remove(factionName2);
faction1.neutralFactions.Remove(factionName2);
// 添加新关系
switch (relation)
{
case Relation.Hostile:
faction1.hostileFactions.Add(factionName2);
break;
case Relation.Friendly:
faction1.friendlyFactions.Add(factionName2);
break;
case Relation.Neutral:
faction1.neutralFactions.Add(factionName2);
break;
default:
throw new ArgumentOutOfRangeException(nameof(relation), relation, null);
}
}
/// <summary>
/// 检查并修复派系关系,确保没有冲突(按友好 > 敌对 > 中立 的优先级)
/// </summary>
private void ValidateAndFixRelationships()
{
foreach (var faction in _affiliations.Values)
{
// 确保所有关系列表已初始化
faction.hostileFactions ??= new List<string>();
faction.friendlyFactions ??= new List<string>();
faction.neutralFactions ??= new List<string>();
// 检查所有敌对派系
foreach (var hostileFaction in faction.hostileFactions.ToList())
{
// 如果敌对派系同时存在于友好列表中,移除敌对关系(友好优先)
if (faction.friendlyFactions.Contains(hostileFaction))
{
faction.hostileFactions.Remove(hostileFaction);
continue;
}
// 如果敌对派系同时存在于中立列表中,移除中立关系(敌对优先)
if (faction.neutralFactions.Contains(hostileFaction))
{
faction.neutralFactions.Remove(hostileFaction);
}
}
// 检查所有中立派系
foreach (var neutralFaction in faction.neutralFactions.ToList())
{
// 如果中立派系同时存在于友好列表中,移除中立关系(友好优先)
if (faction.friendlyFactions.Contains(neutralFaction))
{
faction.neutralFactions.Remove(neutralFaction);
}
}
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 31017ce35a6b441fb36e085dd3fd7c27
timeCreated: 1755407963

View File

@ -10,18 +10,15 @@ namespace Managers
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
{
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
public GameObject entityLevel;
public EntityPrefab entityPrefab;
public GameObject buildingLevel;
public EntityPrefab buildingPrefab;
public GameObject bulletLevel;
public EntityPrefab bulletPrefab;
public EntityPrefab defaultEntityPrefab;
private List<Tuple<string,EntityPrefab>> pendingAdditions;
private Dictionary<string, Transform> layerCache = new Dictionary<string, Transform>();
private List<Tuple<string, EntityPrefab>> pendingAdditions;
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
{
@ -71,6 +68,7 @@ namespace Managers
factionEntities[entity.Item1].AddLast(entity.Item2);
}
pendingAdditions.Clear();
}
}
@ -112,16 +110,8 @@ namespace Managers
extraInit?.Invoke(entityComponent);
// 管理派系列表
var factionKey = def.attributes.label ?? "default";
var factionKey = def.attributes.defName ?? "default";
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
// if (!factionEntities.ContainsKey(factionKey))
// {
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
// }
// factionEntities[factionKey].AddLast(entityComponent);
return entityComponent;
}
catch (System.Exception ex)
@ -134,6 +124,34 @@ namespace Managers
}
}
/// <summary>
/// 动态创建层(如果层不存在)
/// </summary>
private Transform EnsureLayerExists(string layerName)
{
// 先从缓存中查找
if (layerCache.TryGetValue(layerName, out var layerTransform))
{
return layerTransform;
}
// 如果缓存中没有,尝试通过 transform.Find 查找
layerTransform = transform.Find(layerName);
if (!layerTransform)
{
// 如果层不存在,动态创建
var layerObject = new GameObject(layerName);
layerTransform = layerObject.transform;
layerTransform.SetParent(transform, false); // 将层附加到当前管理器下
}
// 将新创建的层加入缓存
layerCache[layerName] = layerTransform;
return layerTransform;
}
/// <summary>
/// 根据PawnDef生成普通实体
/// </summary>
@ -154,10 +172,13 @@ namespace Managers
return;
}
// 确保层存在
var entityLevelTransform = EnsureLayerExists("EntityLevel");
// 调用通用生成逻辑
var result = GenerateEntityInternal(
entityPrefab.gameObject,
entityLevel.transform,
entityLevelTransform,
pos,
entityDef
);
@ -187,14 +208,17 @@ namespace Managers
var worldPos = new Vector3(pos.x, pos.y, pos.z);
// 确保层存在
var buildingLevelTransform = EnsureLayerExists("BuildingLevel");
var result = GenerateEntityInternal(
buildingPrefab.gameObject,
buildingLevel.transform,
buildingLevelTransform,
worldPos,
buildingDef
);
if (!result) GenerateDefaultEntity(pos);
if (!result) GenerateDefaultEntity(worldPos);
}
/// <summary>
@ -217,9 +241,12 @@ namespace Managers
return;
}
// 确保层存在
var bulletLevelTransform = EnsureLayerExists("BulletLevel");
var result = GenerateEntityInternal(
bulletPrefab.gameObject,
bulletLevel.transform,
bulletLevelTransform,
pos,
bulletDef,
// 子弹特有的方向设置
@ -234,18 +261,16 @@ namespace Managers
/// </summary>
public void GenerateDefaultEntity(Vector3 pos)
{
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevel.transform);
// 确保层存在
var entityLevelTransform = EnsureLayerExists("EntityLevel");
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevelTransform);
var entityComponent = entity.GetComponent<EntityPrefab>();
const string factionKey = "default";
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
// if (!factionEntities.ContainsKey(factionKey))
// {
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
// }
// entityComponent.DefaultInit();
// factionEntities[factionKey].AddLast(entityComponent);
entityComponent.DefaultInit();
}
protected override void OnStart()
@ -258,7 +283,7 @@ namespace Managers
{
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
layerCache.Clear();
}
}
}

View File

@ -14,6 +14,8 @@ namespace Managers
public void Init()
{
var itemDefs = Managers.DefineManager.Instance.QueryDefinesByType<ItemDef>();
if(itemDefs==null||itemDefs.Length==0)
return;
foreach (var itemDef in itemDefs)
{
var item=new Item.ItemResource();