Files
Gen_Hack-and-Slash-Roguelit…/Client/Assets/Scripts/Managers/EntityManage.cs

264 lines
8.7 KiB
C#
Raw Normal View History

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-08-17 11:16:55 +08:00
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
public GameObject entityLevel;
public EntityPrefab entityPrefab;
public GameObject buildingLevel;
public EntityPrefab buildingPrefab;
2025-08-17 11:16:55 +08:00
public GameObject bulletLevel;
public EntityPrefab bulletPrefab;
public EntityPrefab defaultEntityPrefab;
2025-08-17 11:16:55 +08:00
private List<Tuple<string,EntityPrefab>> pendingAdditions;
2025-08-17 11:16:55 +08:00
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
{
if (factionEntities.TryGetValue(factionKey, out var entities))
{
return entities; // 如果找到,返回对应的实体列表
}
2025-08-17 11:16:55 +08:00
return new(); // 如果未找到,返回一个空列表
}
2025-08-17 11:16:55 +08:00
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();
}
}
2025-08-17 11:16:55 +08:00
// 删除所有标记为死亡的实体
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>
2025-08-17 11:16:55 +08:00
/// 根据给定的Def生成实体对象内部通用方法
/// </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-08-17 11:16:55 +08:00
GameObject instantiatedEntity = null;
try
{
2025-08-17 11:16:55 +08:00
// 实例化实体
instantiatedEntity = Instantiate(prefab, pos, Quaternion.identity, parent);
2025-08-17 11:16:55 +08:00
// 获取并验证EntityPrefab组件
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
if (!entityComponent)
{
2025-08-17 11:16:55 +08:00
throw new InvalidOperationException(
$"EntityPrefab component missing on: {instantiatedEntity.name}");
}
2025-08-17 11:16:55 +08:00
// 初始化核心数据
entityComponent.Init(def);
// 执行类型特有的额外初始化
extraInit?.Invoke(entityComponent);
// 管理派系列表
var factionKey = def.attributes.label ?? "default";
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
2025-08-17 11:16:55 +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;
}
catch (System.Exception ex)
{
2025-08-17 11:16:55 +08:00
// 清理失败实例
if (instantiatedEntity) Destroy(instantiatedEntity);
2025-08-17 11:16:55 +08:00
Debug.LogError($"Entity generation failed: {ex.Message}\n{ex.StackTrace}");
return null;
}
}
2025-08-17 11:16:55 +08:00
/// <summary>
/// 根据PawnDef生成普通实体
/// </summary>
public void GenerateEntity(Data.EntityDef entityDef, Vector3 pos)
{
2025-08-17 11:16:55 +08:00
// 验证关键参数
if (!entityPrefab)
{
2025-08-17 11:16:55 +08:00
Debug.LogError("entityPrefab is null! Assign a valid prefab.");
GenerateDefaultEntity(pos);
return;
}
2025-08-17 11:16:55 +08:00
if (entityDef == null)
{
2025-08-17 11:16:55 +08:00
Debug.LogError("EntityDef is null! Cannot generate entity.");
GenerateDefaultEntity(pos);
return;
}
2025-08-17 11:16:55 +08:00
// 调用通用生成逻辑
var result = GenerateEntityInternal(
entityPrefab.gameObject,
entityLevel.transform,
pos,
entityDef
);
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-17 11:16:55 +08:00
Debug.LogError("BuildingDef is null! Cannot generate building.");
GenerateDefaultEntity(pos);
return;
}
2025-08-17 11:16:55 +08:00
var worldPos = new Vector3(pos.x, pos.y, pos.z);
2025-08-17 11:16:55 +08:00
var result = GenerateEntityInternal(
buildingPrefab.gameObject,
buildingLevel.transform,
worldPos,
buildingDef
);
2025-08-17 11:16:55 +08:00
if (!result) GenerateDefaultEntity(pos);
}
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-17 11:16:55 +08:00
Debug.LogError("bulletPrefab is null! Assign a valid prefab.");
GenerateDefaultEntity(pos);
return;
}
2025-08-17 11:16:55 +08:00
if (bulletDef == null)
{
Debug.LogError("BulletDef is null! Cannot generate bullet.");
GenerateDefaultEntity(pos);
2025-08-17 11:16:55 +08:00
return;
}
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-17 11:16:55 +08:00
/// <summary>
/// 生成默认实体(错误回退)
/// </summary>
public void GenerateDefaultEntity(Vector3 pos)
{
2025-08-17 11:16:55 +08:00
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevel.transform);
var entityComponent = entity.GetComponent<EntityPrefab>();
2025-08-17 11:16:55 +08:00
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);
}
2025-08-17 11:16:55 +08:00
protected override void OnStart()
{
factionEntities.Clear();
2025-08-17 11:16:55 +08:00
}
private void Start()
{
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
2025-08-17 11:16:55 +08:00
}
}
}