289 lines
9.5 KiB
C#
289 lines
9.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Base;
|
||
using Prefab;
|
||
using UnityEngine;
|
||
|
||
namespace Managers
|
||
{
|
||
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
|
||
{
|
||
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
|
||
|
||
public EntityPrefab entityPrefab;
|
||
public EntityPrefab buildingPrefab;
|
||
public EntityPrefab bulletPrefab;
|
||
|
||
public EntityPrefab defaultEntityPrefab;
|
||
|
||
private Dictionary<string, Transform> layerCache = new Dictionary<string, Transform>();
|
||
private List<Tuple<string, EntityPrefab>> pendingAdditions;
|
||
|
||
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||
{
|
||
if (factionEntities.TryGetValue(factionKey, out var entities))
|
||
{
|
||
return entities; // 如果找到,返回对应的实体列表
|
||
}
|
||
|
||
return new(); // 如果未找到,返回一个空列表
|
||
}
|
||
|
||
public void Tick()
|
||
{
|
||
foreach (var faction in factionEntities)
|
||
{
|
||
var entitiesToRemove = new List<EntityPrefab>();
|
||
|
||
foreach (var entityPrefab in faction.Value)
|
||
{
|
||
if (entityPrefab.entity.IsDead)
|
||
{
|
||
entitiesToRemove.Add(entityPrefab);
|
||
}
|
||
else
|
||
{
|
||
ITick itike = entityPrefab.entity;
|
||
itike.Tick();
|
||
}
|
||
}
|
||
|
||
// 删除所有标记为死亡的实体
|
||
foreach (var entityToRemove in entitiesToRemove)
|
||
{
|
||
faction.Value.Remove(entityToRemove);
|
||
Destroy(entityToRemove.gameObject);
|
||
}
|
||
}
|
||
|
||
if (pendingAdditions.Any())
|
||
{
|
||
foreach (var entity in pendingAdditions)
|
||
{
|
||
if (!factionEntities.ContainsKey(entity.Item1))
|
||
{
|
||
factionEntities[entity.Item1] = new LinkedList<EntityPrefab>();
|
||
}
|
||
|
||
factionEntities[entity.Item1].AddLast(entity.Item2);
|
||
}
|
||
|
||
pendingAdditions.Clear();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 根据给定的Def生成实体对象(内部通用方法)。
|
||
/// </summary>
|
||
/// <param name="prefab">要实例化的预制体</param>
|
||
/// <param name="parent">生成的父级Transform</param>
|
||
/// <param name="pos">生成位置</param>
|
||
/// <param name="def">实体定义对象</param>
|
||
/// <param name="extraInit">额外的初始化操作(如子弹方向设置)</param>
|
||
/// <returns>成功时返回EntityPrefab组件,失败时返回null</returns>
|
||
private EntityPrefab GenerateEntityInternal(
|
||
GameObject prefab,
|
||
Transform parent,
|
||
Vector3 pos,
|
||
Data.EntityDef def, // 所有Def类型需继承自BaseDef
|
||
Action<EntityPrefab> extraInit = null)
|
||
{
|
||
GameObject instantiatedEntity = null;
|
||
try
|
||
{
|
||
// 实例化实体
|
||
instantiatedEntity = Instantiate(prefab, pos, Quaternion.identity, parent);
|
||
|
||
// 获取并验证EntityPrefab组件
|
||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||
if (!entityComponent)
|
||
{
|
||
throw new InvalidOperationException(
|
||
$"EntityPrefab component missing on: {instantiatedEntity.name}");
|
||
}
|
||
|
||
// 初始化核心数据
|
||
entityComponent.Init(def);
|
||
|
||
// 执行类型特有的额外初始化
|
||
extraInit?.Invoke(entityComponent);
|
||
|
||
// 管理派系列表
|
||
var factionKey = def.attributes.defName ?? "default";
|
||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||
return entityComponent;
|
||
}
|
||
catch (System.Exception ex)
|
||
{
|
||
// 清理失败实例
|
||
if (instantiatedEntity) Destroy(instantiatedEntity);
|
||
|
||
Debug.LogError($"Entity generation failed: {ex.Message}\n{ex.StackTrace}");
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/// <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>
|
||
public void GenerateEntity(Data.EntityDef entityDef, Vector3 pos)
|
||
{
|
||
// 验证关键参数
|
||
if (!entityPrefab)
|
||
{
|
||
Debug.LogError("entityPrefab is null! Assign a valid prefab.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
if (entityDef == null)
|
||
{
|
||
Debug.LogError("EntityDef is null! Cannot generate entity.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
// 确保层存在
|
||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||
|
||
// 调用通用生成逻辑
|
||
var result = GenerateEntityInternal(
|
||
entityPrefab.gameObject,
|
||
entityLevelTransform,
|
||
pos,
|
||
entityDef
|
||
);
|
||
|
||
if (!result) GenerateDefaultEntity(pos);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成建筑实体(位置使用Vector3Int)
|
||
/// </summary>
|
||
public void GenerateBuildingEntity(Data.BuildingDef buildingDef, Vector3Int pos)
|
||
{
|
||
// 修正:检查正确的预制体 (buildingPrefab)
|
||
if (!buildingPrefab)
|
||
{
|
||
Debug.LogError("buildingPrefab is null! Assign a valid prefab.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
if (buildingDef == null)
|
||
{
|
||
Debug.LogError("BuildingDef is null! Cannot generate building.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
var worldPos = new Vector3(pos.x, pos.y, pos.z);
|
||
|
||
// 确保层存在
|
||
var buildingLevelTransform = EnsureLayerExists("BuildingLevel");
|
||
|
||
var result = GenerateEntityInternal(
|
||
buildingPrefab.gameObject,
|
||
buildingLevelTransform,
|
||
worldPos,
|
||
buildingDef
|
||
);
|
||
|
||
if (!result) GenerateDefaultEntity(worldPos);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成子弹实体(含方向设置)
|
||
/// </summary>
|
||
public void GenerateBulletEntity(Data.BulletDef bulletDef, Vector3 pos, Vector3 dir)
|
||
{
|
||
// 修正:检查正确的预制体 (bulletPrefab)
|
||
if (!bulletPrefab)
|
||
{
|
||
Debug.LogError("bulletPrefab is null! Assign a valid prefab.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
if (bulletDef == null)
|
||
{
|
||
Debug.LogError("BulletDef is null! Cannot generate bullet.");
|
||
GenerateDefaultEntity(pos);
|
||
return;
|
||
}
|
||
|
||
// 确保层存在
|
||
var bulletLevelTransform = EnsureLayerExists("BulletLevel");
|
||
|
||
var result = GenerateEntityInternal(
|
||
bulletPrefab.gameObject,
|
||
bulletLevelTransform,
|
||
pos,
|
||
bulletDef,
|
||
// 子弹特有的方向设置
|
||
entityComponent => entityComponent.entity.SetTarget(pos + dir)
|
||
);
|
||
|
||
if (!result) GenerateDefaultEntity(pos);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生成默认实体(错误回退)
|
||
/// </summary>
|
||
public void GenerateDefaultEntity(Vector3 pos)
|
||
{
|
||
// 确保层存在
|
||
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));
|
||
|
||
entityComponent.DefaultInit();
|
||
}
|
||
|
||
protected override void OnStart()
|
||
{
|
||
factionEntities.Clear();
|
||
|
||
}
|
||
|
||
private void Start()
|
||
{
|
||
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
||
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
||
layerCache.Clear();
|
||
}
|
||
}
|
||
} |