2025-07-25 19:16:58 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Base;
|
|
|
|
|
using Prefab;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace Managers
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public GameObject entityLevel;
|
|
|
|
|
public EntityPrefab entityPrefab;
|
2025-08-11 12:45:52 +08:00
|
|
|
|
public GameObject buildingLevel;
|
|
|
|
|
public EntityPrefab buildingPrefab;
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public GameObject bulletLevel;
|
|
|
|
|
public EntityPrefab bulletPrefab;
|
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public EntityPrefab defaultEntityPrefab;
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-08-17 12:21:46 +08:00
|
|
|
|
|
|
|
|
|
private List<Tuple<string,EntityPrefab>> pendingAdditions;
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
2025-08-07 16:44:43 +08:00
|
|
|
|
{
|
|
|
|
|
if (factionEntities.TryGetValue(factionKey, out var entities))
|
|
|
|
|
{
|
|
|
|
|
return entities; // 如果找到,返回对应的实体列表
|
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
|
|
|
|
return new(); // 如果未找到,返回一个空列表
|
2025-08-07 16:44:43 +08:00
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public void Tick()
|
|
|
|
|
{
|
|
|
|
|
foreach (var faction in factionEntities)
|
|
|
|
|
{
|
2025-08-07 16:44:43 +08:00
|
|
|
|
var entitiesToRemove = new List<EntityPrefab>();
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
|
|
|
|
foreach (var entityPrefab in faction.Value)
|
|
|
|
|
{
|
|
|
|
|
if (entityPrefab.entity.IsDead)
|
|
|
|
|
{
|
|
|
|
|
entitiesToRemove.Add(entityPrefab);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ITick itike = entityPrefab.entity;
|
|
|
|
|
itike.Tick();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
// 删除所有标记为死亡的实体
|
|
|
|
|
foreach (var entityToRemove in entitiesToRemove)
|
|
|
|
|
{
|
|
|
|
|
faction.Value.Remove(entityToRemove);
|
|
|
|
|
Destroy(entityToRemove.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-17 12:21:46 +08:00
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-08-17 11:16:55 +08:00
|
|
|
|
/// 根据给定的Def生成实体对象(内部通用方法)。
|
2025-07-25 19:16:58 +08:00
|
|
|
|
/// </summary>
|
2025-08-17 11:16:55 +08:00
|
|
|
|
/// <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)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
GameObject instantiatedEntity = null;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 实例化实体
|
|
|
|
|
instantiatedEntity = Instantiate(prefab, pos, Quaternion.identity, parent);
|
2025-07-25 19:16:58 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 获取并验证EntityPrefab组件
|
2025-08-07 16:44:43 +08:00
|
|
|
|
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
2025-08-11 12:45:52 +08:00
|
|
|
|
if (!entityComponent)
|
2025-07-25 19:16:58 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"EntityPrefab component missing on: {instantiatedEntity.name}");
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 初始化核心数据
|
|
|
|
|
entityComponent.Init(def);
|
|
|
|
|
|
|
|
|
|
// 执行类型特有的额外初始化
|
|
|
|
|
extraInit?.Invoke(entityComponent);
|
|
|
|
|
|
|
|
|
|
// 管理派系列表
|
|
|
|
|
var factionKey = def.attributes.label ?? "default";
|
2025-08-17 12:21:46 +08:00
|
|
|
|
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-08-17 12:21:46 +08:00
|
|
|
|
// if (!factionEntities.ContainsKey(factionKey))
|
|
|
|
|
// {
|
|
|
|
|
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// factionEntities[factionKey].AddLast(entityComponent);
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
|
|
|
|
return entityComponent;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 清理失败实例
|
|
|
|
|
if (instantiatedEntity) Destroy(instantiatedEntity);
|
2025-08-07 16:44:43 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
Debug.LogError($"Entity generation failed: {ex.Message}\n{ex.StackTrace}");
|
|
|
|
|
return null;
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据PawnDef生成普通实体
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void GenerateEntity(Data.EntityDef entityDef, Vector3 pos)
|
2025-08-11 12:45:52 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 验证关键参数
|
2025-08-11 12:45:52 +08:00
|
|
|
|
if (!entityPrefab)
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
Debug.LogError("entityPrefab is null! Assign a valid prefab.");
|
2025-08-11 12:45:52 +08:00
|
|
|
|
GenerateDefaultEntity(pos);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
if (entityDef == null)
|
2025-08-11 12:45:52 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
Debug.LogError("EntityDef is null! Cannot generate entity.");
|
2025-08-11 12:45:52 +08:00
|
|
|
|
GenerateDefaultEntity(pos);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
// 调用通用生成逻辑
|
|
|
|
|
var result = GenerateEntityInternal(
|
|
|
|
|
entityPrefab.gameObject,
|
|
|
|
|
entityLevel.transform,
|
|
|
|
|
pos,
|
|
|
|
|
entityDef
|
|
|
|
|
);
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
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)
|
2025-08-11 12:45:52 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
Debug.LogError("BuildingDef is null! Cannot generate building.");
|
|
|
|
|
GenerateDefaultEntity(pos);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
var worldPos = new Vector3(pos.x, pos.y, pos.z);
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
var result = GenerateEntityInternal(
|
|
|
|
|
buildingPrefab.gameObject,
|
|
|
|
|
buildingLevel.transform,
|
|
|
|
|
worldPos,
|
|
|
|
|
buildingDef
|
|
|
|
|
);
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
if (!result) GenerateDefaultEntity(pos);
|
|
|
|
|
}
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成子弹实体(含方向设置)
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void GenerateBulletEntity(Data.BulletDef bulletDef, Vector3 pos, Vector3 dir)
|
|
|
|
|
{
|
|
|
|
|
// 修正:检查正确的预制体 (bulletPrefab)
|
|
|
|
|
if (!bulletPrefab)
|
2025-08-11 12:45:52 +08:00
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
Debug.LogError("bulletPrefab is null! Assign a valid prefab.");
|
|
|
|
|
GenerateDefaultEntity(pos);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-11 12:45:52 +08:00
|
|
|
|
|
2025-08-17 11:16:55 +08:00
|
|
|
|
if (bulletDef == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("BulletDef is null! Cannot generate bullet.");
|
2025-08-11 12:45:52 +08:00
|
|
|
|
GenerateDefaultEntity(pos);
|
2025-08-17 11:16:55 +08:00
|
|
|
|
return;
|
2025-08-11 12:45:52 +08:00
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
|
|
|
|
var result = GenerateEntityInternal(
|
|
|
|
|
bulletPrefab.gameObject,
|
|
|
|
|
bulletLevel.transform,
|
|
|
|
|
pos,
|
|
|
|
|
bulletDef,
|
|
|
|
|
// 子弹特有的方向设置
|
|
|
|
|
entityComponent => entityComponent.entity.SetTarget(pos + dir)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!result) GenerateDefaultEntity(pos);
|
2025-08-11 12:45:52 +08:00
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 生成默认实体(错误回退)
|
|
|
|
|
/// </summary>
|
2025-07-25 19:16:58 +08:00
|
|
|
|
public void GenerateDefaultEntity(Vector3 pos)
|
|
|
|
|
{
|
2025-08-17 11:16:55 +08:00
|
|
|
|
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevel.transform);
|
2025-07-25 19:16:58 +08:00
|
|
|
|
var entityComponent = entity.GetComponent<EntityPrefab>();
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
const string factionKey = "default";
|
2025-08-17 12:21:46 +08:00
|
|
|
|
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
|
|
|
|
// if (!factionEntities.ContainsKey(factionKey))
|
|
|
|
|
// {
|
|
|
|
|
// factionEntities[factionKey] = new LinkedList<EntityPrefab>();
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// entityComponent.DefaultInit();
|
|
|
|
|
// factionEntities[factionKey].AddLast(entityComponent);
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
protected override void OnStart()
|
|
|
|
|
{
|
|
|
|
|
factionEntities.Clear();
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
|
|
|
|
|
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
|
2025-08-17 11:16:55 +08:00
|
|
|
|
|
2025-07-25 19:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|