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

212 lines
7.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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, List<EntityPrefab>> factionEntities = new();
public GameObject entityLevel;
public EntityPrefab entityPrefab;
public GameObject buildingLevel;
public EntityPrefab buildingPrefab;
public EntityPrefab defaultEntityPrefab;
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
{
if (factionEntities.TryGetValue(factionKey, out var entities))
{
return entities; // 如果找到,返回对应的实体列表
}
return new List<EntityPrefab>(); // 如果未找到,返回一个空列表
}
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);
}
}
}
/// <summary>
/// 根据给定的PawnDef生成一个实体对象。
/// </summary>
/// <param name="pawnDef">定义实体属性的PawnDef对象。</param>
/// <param name="pos">实体生成的位置。</param>
/// <remarks>
/// 1. 如果entityPrefab或pawnDef为null则不会生成实体。
/// 2. 实体将被创建在entityLevel.transform下。
/// 3. 使用EntityPrefab组件初始化实体。
/// </remarks>
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
{
// 检查 entityPrefab 是否为空
if (!entityPrefab)
{
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
GenerateDefaultEntity(pos);
return;
}
// 检查 pawnDef 是否为空
if (pawnDef == null)
{
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
GenerateDefaultEntity(pos);
return;
}
GameObject instantiatedEntity = null; // 用于跟踪已实例化的对象
try
{
// 实例化实体对象
instantiatedEntity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
// 获取 EntityPrefab 组件
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
// 检查 EntityPrefab 组件是否存在
if (!entityComponent)
{
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
}
// 初始化实体组件
entityComponent.Init(pawnDef);
// 确保派系键存在,并初始化对应的列表
var factionKey = pawnDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
if (!factionEntities.ContainsKey(factionKey))
{
factionEntities[factionKey] = new List<EntityPrefab>();
}
factionEntities[factionKey].Add(entityComponent);
}
catch (System.Exception ex)
{
// 如果有已实例化的对象,则销毁它
if (instantiatedEntity)
{
Destroy(instantiatedEntity); // 删除已创建的对象
}
// 捕获并记录任何异常
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
// 调用默认生成方法
GenerateDefaultEntity(pos);
}
}
public void GenerateBuildingEntity(Data.BuildingDef buildingDef, Vector3Int pos)
{
// 检查 entityPrefab 是否为空
if (!entityPrefab)
{
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
GenerateDefaultEntity(pos);
return;
}
// 检查 pawnDef 是否为空
if (buildingDef == null)
{
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
GenerateDefaultEntity(pos);
return;
}
GameObject instantiatedEntity = null; // 用于跟踪已实例化的对象
try
{
// 实例化实体对象
instantiatedEntity = Instantiate(buildingPrefab.gameObject, pos, Quaternion.identity, buildingLevel.transform);
// 获取 EntityPrefab 组件
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
// 检查 EntityPrefab 组件是否存在
if (!entityComponent)
{
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
}
// 初始化实体组件
entityComponent.Init(buildingDef);
// 确保派系键存在,并初始化对应的列表
var factionKey = buildingDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
if (!factionEntities.ContainsKey(factionKey))
{
factionEntities[factionKey] = new List<EntityPrefab>();
}
factionEntities[factionKey].Add(entityComponent);
}
catch (System.Exception ex)
{
// 如果有已实例化的对象,则销毁它
if (instantiatedEntity)
{
Destroy(instantiatedEntity); // 删除已创建的对象
}
// 捕获并记录任何异常
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
// 调用默认生成方法
GenerateDefaultEntity(pos);
}
}
public void GenerateDefaultEntity(Vector3 pos)
{
var entity = Instantiate(defaultEntityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
var entityComponent = entity.GetComponent<EntityPrefab>();
const string factionKey = "default";
if (!factionEntities.ContainsKey(factionKey))
{
factionEntities[factionKey] = new List<EntityPrefab>();
}
entityComponent.DefaultInit();
factionEntities[factionKey].Add(entityComponent);
}
protected override void OnStart()
{
factionEntities.Clear();
}
private void Start()
{
var pre = Resources.Load<GameObject>("Default/DefaultEntity");
defaultEntityPrefab = pre.GetComponent<EntityPrefab>();
}
}
}