(client) feat:右键菜单全局与UI分离,Building生成支持,双层地图重写
This commit is contained in:
@ -13,6 +13,8 @@ namespace Managers
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
public GameObject buildingLevel;
|
||||
public EntityPrefab buildingPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
@ -63,7 +65,7 @@ namespace Managers
|
||||
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
|
||||
{
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (entityPrefab == null)
|
||||
if (!entityPrefab)
|
||||
{
|
||||
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
|
||||
GenerateDefaultEntity(pos);
|
||||
@ -89,7 +91,7 @@ namespace Managers
|
||||
var entityComponent = instantiatedEntity.GetComponent<EntityPrefab>();
|
||||
|
||||
// 检查 EntityPrefab 组件是否存在
|
||||
if (entityComponent == null)
|
||||
if (!entityComponent)
|
||||
{
|
||||
throw new InvalidOperationException($"Error: EntityPrefab component not found on the instantiated object: {instantiatedEntity.name}");
|
||||
}
|
||||
@ -108,7 +110,7 @@ namespace Managers
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity != null)
|
||||
if (instantiatedEntity)
|
||||
{
|
||||
Destroy(instantiatedEntity); // 删除已创建的对象
|
||||
}
|
||||
@ -121,6 +123,67 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
@ -5,30 +5,57 @@ using UnityEngine.Tilemaps;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class TileManager:Utils.Singleton<TileManager>
|
||||
/// <summary>
|
||||
/// 瓦片管理器,用于加载、初始化和管理瓦片资源。
|
||||
/// </summary>
|
||||
public class TileManager : Utils.Singleton<TileManager>
|
||||
{
|
||||
public Dictionary<string,TileBase> tileBaseMapping = new();
|
||||
/// <summary>
|
||||
/// 存储瓦片名称与瓦片对象的映射关系。
|
||||
/// </summary>
|
||||
public Dictionary<string, TileBase> tileBaseMapping = new();
|
||||
|
||||
/// <summary>
|
||||
/// 存储瓦片索引与瓦片对象的映射关系。
|
||||
/// 索引由四个整数组成,表示瓦片的组合方式。
|
||||
/// </summary>
|
||||
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
|
||||
|
||||
/// <summary>
|
||||
/// 存储瓦片名称与唯一 ID 的映射关系。
|
||||
/// </summary>
|
||||
public Dictionary<string, int> tileID = new();
|
||||
|
||||
/// <summary>
|
||||
/// 初始化瓦片管理器。
|
||||
/// 加载所有瓦片定义、纹理映射表,并生成对应的瓦片对象。
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
if (tileToTileBaseMapping.Count > 0)
|
||||
return;
|
||||
|
||||
// 初始化图像包管理器
|
||||
Managers.PackagesImageManager.Instance.Init();
|
||||
var imagePack = Managers.PackagesImageManager.Instance;
|
||||
|
||||
// 获取所有瓦片定义
|
||||
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
|
||||
for (var i = 0; i < tileType.Length; i++)
|
||||
{
|
||||
tileID.Add(tileType[i].name, i);
|
||||
}
|
||||
|
||||
var tileTextureMappingDef=Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
|
||||
// 处理瓦片纹理映射表定义
|
||||
var tileTextureMappingDef = Managers.DefineManager.Instance.QueryDefinesByType<TileMappingTableDef>();
|
||||
foreach (var mappingTableDef in tileTextureMappingDef)
|
||||
{
|
||||
foreach (var keyVal in mappingTableDef.tileDict)
|
||||
{
|
||||
var key = keyVal.Key;
|
||||
var val = keyVal.Value;
|
||||
|
||||
// 检查键值格式是否合法
|
||||
var parts = key.Split('_');
|
||||
if (parts.Length != 4)
|
||||
{
|
||||
@ -37,6 +64,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查键值中是否存在未定义的瓦片名称
|
||||
if (!(tileID.TryGetValue(parts[0], out var k1) &&
|
||||
tileID.TryGetValue(parts[1], out var k2) &&
|
||||
tileID.TryGetValue(parts[2], out var k3) &&
|
||||
@ -47,7 +75,8 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID,val);
|
||||
// 获取对应精灵
|
||||
var sprite = imagePack.GetSprite(mappingTableDef.packID, val);
|
||||
if (sprite == null)
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
@ -55,6 +84,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查是否存在重复索引
|
||||
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
|
||||
{
|
||||
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
|
||||
@ -62,6 +92,7 @@ namespace Managers
|
||||
continue;
|
||||
}
|
||||
|
||||
// 加载瓦片并存储到映射表中
|
||||
var tile = LoadTile(sprite);
|
||||
tileToTileBaseMapping[(k1, k2, k3, k4)] = tile;
|
||||
tileBaseMapping[val] = tile;
|
||||
@ -69,18 +100,28 @@ namespace Managers
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重新加载瓦片管理器。
|
||||
/// 清空当前的瓦片映射表并重新初始化。
|
||||
/// </summary>
|
||||
public void Reload()
|
||||
{
|
||||
tileToTileBaseMapping.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
public TileBase LoadTile(Sprite sprite)
|
||||
/// <summary>
|
||||
/// 将精灵加载为瓦片对象。
|
||||
/// </summary>
|
||||
/// <param name="sprite">要加载的精灵。</param>
|
||||
/// <param name="colliderType">瓦片的碰撞体类型,默认为 None。</param>
|
||||
/// <returns>返回加载成功的瓦片对象。</returns>
|
||||
public TileBase LoadTile(Sprite sprite, Tile.ColliderType colliderType = Tile.ColliderType.None)
|
||||
{
|
||||
var newTile = ScriptableObject.CreateInstance<Tile>();
|
||||
newTile.sprite = sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = Tile.ColliderType.Sprite;
|
||||
newTile.color = Color.white;
|
||||
newTile.colliderType = colliderType;
|
||||
return newTile;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user