(client)feat:实现子弹定义以及生成,实现初始化动画,实现血条 (#43)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
164
Client/Assets/Scripts/Managers/AffiliationManager.cs
Normal file
164
Client/Assets/Scripts/Managers/AffiliationManager.cs
Normal file
@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class AffiliationManager:Utils.Singleton<AffiliationManager>
|
||||
{
|
||||
//定义名,阵营定义
|
||||
private readonly Dictionary<string, AffiliationDef> _affiliations = new();
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var affiliationList = Managers.DefineManager.Instance.QueryDefinesByType<AffiliationDef>();
|
||||
if (affiliationList == null ||affiliationList.Length==0)
|
||||
return;
|
||||
foreach (var affiliation in affiliationList)
|
||||
{
|
||||
_affiliations.Add(affiliation.defName, affiliation);
|
||||
}
|
||||
ValidateAndFixRelationships();
|
||||
}
|
||||
|
||||
public string GetAffiliationName(string defName)
|
||||
{
|
||||
return _affiliations[defName].defName;
|
||||
}
|
||||
public Relation GetRelation(AffiliationDef affiliation1, AffiliationDef affiliation2)
|
||||
{
|
||||
if (affiliation1 == null || affiliation2 == null)
|
||||
{
|
||||
return Relation.Neutral; // 如果任一阵营不存在,返回中立关系
|
||||
}
|
||||
return GetRelation(affiliation1.defName, affiliation2.defName);
|
||||
}
|
||||
public Relation GetRelation(string factionName1, string factionName2)
|
||||
{
|
||||
// 如果查询的是同一个派系,默认是友好关系
|
||||
if (factionName1 == factionName2)
|
||||
{
|
||||
return Relation.Friendly;
|
||||
}
|
||||
|
||||
// 尝试获取两个派系的定义
|
||||
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
|
||||
!_affiliations.TryGetValue(factionName2, out _))
|
||||
{
|
||||
if (faction1 != null) return faction1.defaultRelation;
|
||||
return Relation.Neutral;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为敌对
|
||||
if (faction1.hostileFactions != null && faction1.hostileFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Hostile;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为友好
|
||||
if (faction1.friendlyFactions != null && faction1.friendlyFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Friendly;
|
||||
}
|
||||
|
||||
// 检查faction1是否明确将faction2列为中立
|
||||
if (faction1.neutralFactions != null && faction1.neutralFactions.Contains(factionName2))
|
||||
{
|
||||
return Relation.Neutral;
|
||||
}
|
||||
|
||||
// 如果faction1没有明确设置与faction2的关系,则使用faction1的默认关系
|
||||
return faction1.defaultRelation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置两个阵营之间的关系
|
||||
/// </summary>
|
||||
/// <param name="factionName1">第一个阵营名称</param>
|
||||
/// <param name="factionName2">第二个阵营名称</param>
|
||||
/// <param name="relation">要设置的关系</param>
|
||||
public void SetRelation(string factionName1, string factionName2, Relation relation)
|
||||
{
|
||||
// 不能设置自己与自己的关系
|
||||
if (factionName1 == factionName2)
|
||||
{
|
||||
throw new ArgumentException("Cannot set relation between the same faction");
|
||||
}
|
||||
|
||||
// 确保两个阵营都存在
|
||||
if (!_affiliations.TryGetValue(factionName1, out var faction1) ||
|
||||
!_affiliations.TryGetValue(factionName2, out _))
|
||||
{
|
||||
throw new ArgumentException("One or both factions do not exist");
|
||||
}
|
||||
|
||||
// 确保关系列表已初始化
|
||||
faction1.hostileFactions ??= new List<string>();
|
||||
faction1.friendlyFactions ??= new List<string>();
|
||||
faction1.neutralFactions ??= new List<string>();
|
||||
|
||||
// 先移除所有现有关系
|
||||
faction1.hostileFactions.Remove(factionName2);
|
||||
faction1.friendlyFactions.Remove(factionName2);
|
||||
faction1.neutralFactions.Remove(factionName2);
|
||||
|
||||
// 添加新关系
|
||||
switch (relation)
|
||||
{
|
||||
case Relation.Hostile:
|
||||
faction1.hostileFactions.Add(factionName2);
|
||||
break;
|
||||
case Relation.Friendly:
|
||||
faction1.friendlyFactions.Add(factionName2);
|
||||
break;
|
||||
case Relation.Neutral:
|
||||
faction1.neutralFactions.Add(factionName2);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(relation), relation, null);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查并修复派系关系,确保没有冲突(按友好 > 敌对 > 中立 的优先级)
|
||||
/// </summary>
|
||||
private void ValidateAndFixRelationships()
|
||||
{
|
||||
foreach (var faction in _affiliations.Values)
|
||||
{
|
||||
// 确保所有关系列表已初始化
|
||||
faction.hostileFactions ??= new List<string>();
|
||||
faction.friendlyFactions ??= new List<string>();
|
||||
faction.neutralFactions ??= new List<string>();
|
||||
|
||||
// 检查所有敌对派系
|
||||
foreach (var hostileFaction in faction.hostileFactions.ToList())
|
||||
{
|
||||
// 如果敌对派系同时存在于友好列表中,移除敌对关系(友好优先)
|
||||
if (faction.friendlyFactions.Contains(hostileFaction))
|
||||
{
|
||||
faction.hostileFactions.Remove(hostileFaction);
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果敌对派系同时存在于中立列表中,移除中立关系(敌对优先)
|
||||
if (faction.neutralFactions.Contains(hostileFaction))
|
||||
{
|
||||
faction.neutralFactions.Remove(hostileFaction);
|
||||
}
|
||||
}
|
||||
|
||||
// 检查所有中立派系
|
||||
foreach (var neutralFaction in faction.neutralFactions.ToList())
|
||||
{
|
||||
// 如果中立派系同时存在于友好列表中,移除中立关系(友好优先)
|
||||
if (faction.friendlyFactions.Contains(neutralFaction))
|
||||
{
|
||||
faction.neutralFactions.Remove(neutralFaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 31017ce35a6b441fb36e085dd3fd7c27
|
||||
timeCreated: 1755407963
|
@ -158,20 +158,20 @@ namespace Managers
|
||||
{
|
||||
if (defRef.Item1 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item1 为 null!");
|
||||
Debug.LogError("被引用定义为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (defRef.Item2 == null)
|
||||
{
|
||||
Debug.LogError("defRef.Item2 为 null!");
|
||||
Debug.LogError("被引用定义的字段引用为 null!");
|
||||
continue;
|
||||
}
|
||||
|
||||
var value = FindDefine(defRef.Item3.description, defRef.Item3.defName);
|
||||
if (value == null)
|
||||
{
|
||||
Debug.LogError($"FindDefine 返回 null: description={defRef.Item3.description}, defName={defRef.Item3.defName}");
|
||||
Debug.LogError($"未找到引用,出错的定义:定义类型:{defRef.Item1.GetType().Name}, 定义名:{defRef.Item1.defName} ; 类型:{defRef.Item3.description}, 定义名:{defRef.Item3.defName}");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -2,27 +2,36 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Base;
|
||||
using Entity;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class EntityManage:Utils.MonoSingleton<EntityManage>,ITick
|
||||
public class EntityManage : Utils.MonoSingleton<EntityManage>, ITick
|
||||
{
|
||||
public Dictionary<string, List<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public GameObject entityLevel;
|
||||
public EntityPrefab entityPrefab;
|
||||
|
||||
public Dictionary<string, LinkedList<EntityPrefab>> factionEntities = new();
|
||||
|
||||
public EntityPrefab characterPrefab;
|
||||
public EntityPrefab buildingPrefab;
|
||||
public EntityPrefab bulletPrefab;
|
||||
|
||||
public EntityPrefab defaultEntityPrefab;
|
||||
public List<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
|
||||
private Dictionary<string, Transform> layerCache = new Dictionary<string, Transform>();
|
||||
private List<Tuple<string, EntityPrefab>> pendingAdditions = new();
|
||||
|
||||
public LinkedList<EntityPrefab> FindEntitiesByFaction(string factionKey)
|
||||
{
|
||||
if (factionEntities.TryGetValue(factionKey, out var entities))
|
||||
{
|
||||
return entities; // 如果找到,返回对应的实体列表
|
||||
}
|
||||
return new List<EntityPrefab>(); // 如果未找到,返回一个空列表
|
||||
|
||||
return new(); // 如果未找到,返回一个空列表
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
foreach (var faction in factionEntities)
|
||||
@ -41,6 +50,7 @@ namespace Managers
|
||||
itike.Tick();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除所有标记为死亡的实体
|
||||
foreach (var entityToRemove in entitiesToRemove)
|
||||
{
|
||||
@ -48,102 +58,240 @@ namespace Managers
|
||||
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>
|
||||
/// 根据给定的PawnDef生成一个实体对象。
|
||||
/// 根据给定的Def生成实体对象(内部通用方法)。
|
||||
/// </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)
|
||||
/// <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)
|
||||
{
|
||||
// 检查 entityPrefab 是否为空
|
||||
if (entityPrefab == null)
|
||||
{
|
||||
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; // 用于跟踪已实例化的对象
|
||||
|
||||
GameObject instantiatedEntity = null;
|
||||
try
|
||||
{
|
||||
// 实例化实体对象
|
||||
instantiatedEntity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// 实例化实体
|
||||
instantiatedEntity = Instantiate(prefab, pos, Quaternion.identity, parent);
|
||||
|
||||
// 获取 EntityPrefab 组件
|
||||
// 获取并验证EntityPrefab组件
|
||||
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}");
|
||||
throw new InvalidOperationException(
|
||||
$"EntityPrefab component missing on: {instantiatedEntity.name}");
|
||||
}
|
||||
|
||||
// 初始化实体组件
|
||||
entityComponent.Init(pawnDef);
|
||||
// 初始化核心数据
|
||||
entityComponent.Init(def);
|
||||
|
||||
// 确保派系键存在,并初始化对应的列表
|
||||
var factionKey = pawnDef.attributes.label ?? "default"; // 使用 null 合并运算符简化代码
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
factionEntities[factionKey].Add(entityComponent);
|
||||
// 执行类型特有的额外初始化
|
||||
extraInit?.Invoke(entityComponent);
|
||||
|
||||
// 管理派系列表
|
||||
var factionKey = def.attributes.defName ?? "default";
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
return entityComponent;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
// 如果有已实例化的对象,则销毁它
|
||||
if (instantiatedEntity != null)
|
||||
{
|
||||
Destroy(instantiatedEntity); // 删除已创建的对象
|
||||
}
|
||||
// 清理失败实例
|
||||
if (instantiatedEntity) Destroy(instantiatedEntity);
|
||||
|
||||
// 捕获并记录任何异常
|
||||
Debug.LogError($"An error occurred while generating the entity: {ex.Message}\nStack Trace: {ex.StackTrace}");
|
||||
|
||||
// 调用默认生成方法
|
||||
GenerateDefaultEntity(pos);
|
||||
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 (!characterPrefab)
|
||||
{
|
||||
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(
|
||||
characterPrefab.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,
|
||||
Entity.Entity source = null)
|
||||
{
|
||||
// 修正:检查正确的预制体 (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.entity is Bullet bullet)
|
||||
{
|
||||
bullet.bulletSource = source;
|
||||
if (source) bullet.affiliation = source.affiliation;
|
||||
}
|
||||
|
||||
if (!result) GenerateDefaultEntity(pos);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成默认实体(错误回退)
|
||||
/// </summary>
|
||||
public void GenerateDefaultEntity(Vector3 pos)
|
||||
{
|
||||
var entity = Instantiate(defaultEntityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
|
||||
// 确保层存在
|
||||
var entityLevelTransform = EnsureLayerExists("EntityLevel");
|
||||
|
||||
var entity = Instantiate(defaultEntityPrefab, pos, Quaternion.identity, entityLevelTransform);
|
||||
var entityComponent = entity.GetComponent<EntityPrefab>();
|
||||
|
||||
const string factionKey = "default";
|
||||
if (!factionEntities.ContainsKey(factionKey))
|
||||
{
|
||||
factionEntities[factionKey] = new List<EntityPrefab>();
|
||||
}
|
||||
pendingAdditions.Add(Tuple.Create(factionKey, entityComponent));
|
||||
|
||||
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>();
|
||||
|
||||
layerCache.Clear();
|
||||
}
|
||||
}
|
||||
}
|
47
Client/Assets/Scripts/Managers/ItemResourceManager.cs
Normal file
47
Client/Assets/Scripts/Managers/ItemResourceManager.cs
Normal file
@ -0,0 +1,47 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Data;
|
||||
using Item;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class ItemResourceManager:Utils.Singleton<ItemResourceManager>
|
||||
{
|
||||
//定义名,物品
|
||||
public Dictionary<string,Item.ItemResource> items;
|
||||
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var itemDefs = Managers.DefineManager.Instance.QueryDefinesByType<ItemDef>();
|
||||
if(itemDefs==null||itemDefs.Length==0)
|
||||
return;
|
||||
foreach (var itemDef in itemDefs)
|
||||
{
|
||||
var item=new Item.ItemResource();
|
||||
item.name = itemDef.label;
|
||||
item.description = itemDef.description;
|
||||
|
||||
item.icon = Managers.PackagesImageManager.Instance.GetSprite(itemDef.texture);
|
||||
}
|
||||
}
|
||||
|
||||
public ItemResource GetItem(string defName)
|
||||
{
|
||||
return items.GetValueOrDefault(defName,null);
|
||||
}
|
||||
// <summary>
|
||||
/// 按物品名称查找物品
|
||||
/// </summary>
|
||||
/// <param name="itemName">要查找的物品名称</param>
|
||||
/// <returns>找到的物品对象,如果未找到则返回 null</returns>
|
||||
public ItemResource FindItemByName(string itemName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(itemName))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return items.Values.FirstOrDefault(item => item.name == itemName);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: eafd50f3a0594a6e845ecf87b04744ce
|
||||
timeCreated: 1755062360
|
@ -90,16 +90,16 @@ namespace Managers
|
||||
}
|
||||
|
||||
// 判断是否为 Unity 资源路径
|
||||
bool isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
|
||||
string rootPath = packRootSite[drawOrder.packID];
|
||||
var isUnityResource = drawOrder.texturePath.StartsWith("res:", StringComparison.OrdinalIgnoreCase);
|
||||
var rootPath = packRootSite[drawOrder.packID];
|
||||
|
||||
if (isUnityResource)
|
||||
{
|
||||
// 移除 "res:" 前缀并适配 Unity 资源路径规则
|
||||
string resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
|
||||
var resourceFolder = drawOrder.texturePath.Substring(4).TrimStart('/').Replace('\\', '/');
|
||||
|
||||
// 加载文件夹下的所有纹理资源
|
||||
Texture2D[] textures = Resources.LoadAll<Texture2D>(resourceFolder);
|
||||
var textures = Resources.LoadAll<Texture2D>(resourceFolder);
|
||||
if (textures == null || textures.Length == 0)
|
||||
{
|
||||
Debug.LogWarning($"No textures found in Unity resource folder: {resourceFolder}");
|
||||
@ -124,7 +124,6 @@ namespace Managers
|
||||
new Vector2(0.5f, 0.5f), // 中心点
|
||||
drawOrder.pixelsPerUnit
|
||||
);
|
||||
|
||||
var name = image.name;
|
||||
|
||||
// 插入纹理
|
||||
@ -269,7 +268,11 @@ namespace Managers
|
||||
sprites.Clear();
|
||||
Init();
|
||||
}
|
||||
|
||||
|
||||
public Sprite GetSprite(ImageDef ima)
|
||||
{
|
||||
return GetSprite(ima.packID,ima.name);
|
||||
}
|
||||
public Sprite GetSprite(string packID, string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(packID))
|
||||
@ -343,14 +346,14 @@ namespace Managers
|
||||
if (!bodyTexture.TryGetValue(packageName, out var packageDict))
|
||||
{
|
||||
Debug.LogWarning($"Package '{packageName}' not found.");
|
||||
return Array.Empty<Sprite>();
|
||||
return new[] { defaultSprite };
|
||||
}
|
||||
|
||||
// 检查文件路径是否存在
|
||||
if (!packageDict.TryGetValue(filePath, out var pathDict))
|
||||
{
|
||||
Debug.LogWarning($"File path '{filePath}' not found in package '{packageName}'.");
|
||||
return Array.Empty<Sprite>();
|
||||
return new[] { defaultSprite };
|
||||
}
|
||||
|
||||
// 收集所有匹配的Sprite
|
||||
@ -374,7 +377,7 @@ namespace Managers
|
||||
// 尝试解析编号
|
||||
if (int.TryParse(suffix, out var number))
|
||||
{
|
||||
sprites.Add((number, Value: value));
|
||||
sprites.Add((number, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
Client/Assets/Scripts/Managers/RightMenuManager.cs
Normal file
63
Client/Assets/Scripts/Managers/RightMenuManager.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System.Collections.Generic;
|
||||
using Prefab;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
|
||||
namespace Managers
|
||||
{
|
||||
public class RightMenuManager:Utils.MonoSingleton<RightMenuManager>
|
||||
{
|
||||
[SerializeField]
|
||||
private GameObject _canvas;
|
||||
|
||||
[SerializeField]
|
||||
private RightMenuPrefab _rightMenuPrefab;
|
||||
|
||||
public GameObject Canvas
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_canvas == null)
|
||||
{
|
||||
_canvas = GameObject.Find("Canvas"); // 根据你的实际场景修改查找条件
|
||||
if (_canvas == null)
|
||||
{
|
||||
Debug.LogError("RightMenu Canvas not found in scene!");
|
||||
}
|
||||
}
|
||||
return _canvas;
|
||||
}
|
||||
}
|
||||
|
||||
public RightMenuPrefab RightMenuPrefab
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_rightMenuPrefab == null)
|
||||
{
|
||||
_rightMenuPrefab = Resources.Load<RightMenuPrefab>("Prefab/RightMenu");
|
||||
if (_rightMenuPrefab == null)
|
||||
{
|
||||
Debug.LogError("RightMenuPrefab not found in Resources!");
|
||||
}
|
||||
}
|
||||
return _rightMenuPrefab;
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateRightMenu(List<(string name, UnityAction callback)> buttons,Vector3 position)
|
||||
{
|
||||
var rightMenuObj = Instantiate(RightMenuManager.Instance.RightMenuPrefab.gameObject,
|
||||
RightMenuManager.Instance.Canvas.transform);
|
||||
var rightMenu=rightMenuObj.GetComponent<RightMenuPrefab>();
|
||||
rightMenu.Init(buttons);
|
||||
rightMenu.transform.position = position;
|
||||
rightMenu.Show();
|
||||
}
|
||||
|
||||
protected override void OnStart()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/RightMenuManager.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/RightMenuManager.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 409b8017bbd6443eb2dde17ea6fd5e29
|
||||
timeCreated: 1755526878
|
@ -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