(client) feat:添加角色定义,为匿名定义建立索引,修复一些数据定义中的错误 (#39)

Co-authored-by: zzdxxz <2079238449@qq.com>
Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
2025-07-19 19:03:53 +08:00
committed by TheRedApricot
parent 44cfb55985
commit 389376ec47
43 changed files with 3081 additions and 607 deletions

View File

@ -0,0 +1,10 @@
namespace Base
{
public class UIInputControl:Utils.MonoSingleton<UIInputControl>
{
protected override void OnStart()
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 66ec6c65c80a43e799c1cb67fc9f7691
timeCreated: 1752922425

View File

@ -2,32 +2,10 @@
namespace Data
{
public class CharacterAttributesDef : Define
public class AttributesDef : Define
{
public int health = 10;
public int moveSpeed = 1;
}
public class WeaponAttributesDef : Define
{
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
public class MonsterAttributesDef : Define
{
public int health = 10;
public int moveSpeed = 1;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;
public int attackRange = 3;
public int attackTargetCount = 1;
}
public class BuildingAttributesDef : Define
{
public int health = 10;
public int attack = 1;
public int defense = 0;
public int attackSpeed = 2;

View File

@ -22,54 +22,13 @@ namespace Data
Animation
}
public class CharacterDef : Define
public class CharacterDef : PawnDef
{
public CharacterAttributesDef attributes;
public string texturePath = null;
public DrawingOrderDef
drawingOrder_down,
drawingOrder_up,
drawingOrder_left,
drawingOrder_right;
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
{
// 定义一个临时变量用于存储结果
DrawingOrderDef result = null;
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
switch (orientation)
{
case Orientation.Down:
result = drawingOrder_down;
break;
case Orientation.Up:
result = drawingOrder_up;
break;
case Orientation.Left:
result = drawingOrder_left;
break;
case Orientation.Right:
result = drawingOrder_right;
break;
default:
throw new ArgumentException("Invalid orientation value.");
}
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
if (result == null) result = drawingOrder_down;
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
return result;
}
}
public class DrawingOrderDef : Define
{
public List<DrawNodeDef> drawNodes = new();
public override bool Init(XElement xmlDef)
{
base.Init(xmlDef);

View File

@ -0,0 +1,8 @@
namespace Data
{
public class ItemDefine:Define
{
public ImageDef texture;
public AttributesDef attributes;
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c4d338c5c7b24cd5a496acb98ebf5e3c
timeCreated: 1752760211

View File

@ -0,0 +1,51 @@
using System;
namespace Data
{
public class PawnDef : Define
{
public string aiController;
public string texturePath = null;
public DrawingOrderDef
drawingOrder_down,
drawingOrder_up,
drawingOrder_left,
drawingOrder_right;
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
{
// 定义一个临时变量用于存储结果
DrawingOrderDef result = null;
// 根据传入的 Orientation 获取对应的 DrawingOrderDef
switch (orientation)
{
case Orientation.Down:
result = drawingOrder_down;
break;
case Orientation.Up:
result = drawingOrder_up;
break;
case Orientation.Left:
result = drawingOrder_left;
break;
case Orientation.Right:
result = drawingOrder_right;
break;
default:
throw new ArgumentException("Invalid orientation value.");
}
// 如果当前方向的结果为空,则尝试用 drawingOrder_down 填充
if (result == null) result = drawingOrder_down;
// 如果 drawingOrder_down 仍然为空,则尝试用其他非空方向填充
if (result == null) result = drawingOrder_up ?? drawingOrder_left ?? drawingOrder_right;
return result;
}
}
public class MonsterDef:PawnDef
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0a89bc126c0a466799f6ccb963267419
timeCreated: 1752751832

View File

@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Data;
using UnityEngine;
using Utils;
@ -11,10 +13,11 @@ namespace Managers
{
public class DefineManager : Singleton<DefineManager>
{
private static readonly string[] dataSetFilePath = { "Data", "Mod" };
private static readonly string[] dataSetFilePath = { "Data", "Mods" };
public Dictionary<string, Dictionary<string, Define>> defines = new();
public Dictionary<string, DefinePack> packs = new();
public Dictionary<string, List<Define>> anonymousDefines = new();
/// <summary>
/// 初始化定义管理器,加载所有定义包并构建定义字典。
/// </summary>
@ -26,17 +29,66 @@ namespace Managers
/// </remarks>
public void Init()
{
if (packs.Count > 0)
return;
// 单线程
var packFolder = Configs.ConfigProcessor.GetSubFolders(new(dataSetFilePath));
foreach (var folder in packFolder)
{
var pack = new DefinePack();
if (pack.LoadPack(folder)) packs.Add(pack.packID, pack);
if (pack.LoadPack(folder))
{
packs.Add(pack.packID, pack);
}
}
Dictionary<Type, FieldInfo[]> fieldCache = new();
//不优化到循环里面是因为要先建立索引再链接
List<Tuple<Define, FieldInfo, Define>> defineCache = new();
HashSet<Define> processedDefines = new(); // 用于跟踪已处理的 Define 对象
void ProcessDefine(Define def, Define parentDef, FieldInfo parentField)
{
if (def == null || def.isReferene || processedDefines.Contains(def))
return;
processedDefines.Add(def);
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{
// 获取所有字段类型为 Define 或其派生类型的字段
defineFields = def.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
.ToArray();
// 缓存字段信息
fieldCache[def.GetType()] = defineFields;
}
foreach (var defineField in defineFields)
{
var defRef = (Define)defineField.GetValue(def);
if (defRef == null)
continue;
if (defRef.isReferene)
{
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, defRef));
}
else
{
if (string.IsNullOrEmpty(defRef.defName))
{
var typeName = defRef.GetType().Name;
if (!anonymousDefines.ContainsKey(typeName))
anonymousDefines.Add(typeName, new List<Define>());
anonymousDefines[typeName].Add(defRef);
}
ProcessDefine(defRef, def, defineField);
}
}
}
foreach (var pack in packs)
{
foreach (var define in pack.Value.defines)
@ -51,34 +103,23 @@ namespace Managers
{
defines[typeName][def.defName] = def;
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{
// 获取所有字段类型为 Define 或其派生类型的字段
defineFields = def.GetType()
.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => typeof(Define).IsAssignableFrom(field.FieldType))
.ToArray();
// 缓存字段信息
fieldCache[def.GetType()] = defineFields;
}
foreach (var defineField in defineFields)
{
var defRef=(Define)defineField.GetValue(def);
if (defRef==null || !defRef.isReferene)
continue;
defineCache.Add(new(def, defineField, defRef));
}
// 处理顶层 Define
ProcessDefine(def, null, null);
}
}
}
foreach (var defRef in defineCache)
{
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
}
}
public void Reload()
{
defines.Clear();
packs.Clear();
anonymousDefines.Clear();
Init();
}
/// <summary>
/// 查找指定定义类型的定义名对应的 Define 对象。
@ -126,41 +167,16 @@ namespace Managers
{
defineList.AddRange(define.Values);
}
foreach (var anonymousDefine in anonymousDefines)
{
defineList.AddRange(anonymousDefine.Value);
}
return defineList.ToArray();
}
/// <summary>
/// 查询 Define 对象。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <param name="defineName">定义名(内层字典的键)。</param>
/// <returns>如果找到,则返回 Define 对象;否则返回 null。</returns>
public Define QueryDefine(string defineType, string defineName)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
if (string.IsNullOrEmpty(defineName))
{
Debug.LogError("查询失败:定义名参数不能为空!");
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
if (!typeDefinitions.TryGetValue(defineName, out var targetDefine))
{
Debug.LogWarning($"查询失败:定义类型 '{defineType}' 中未找到定义名 '{defineName}'");
return null;
}
return targetDefine;
}
/// <summary>
/// 查询指定类型下的所有 Define 对象。
/// 查询指定类型下的所有 Define 对象。(包括匿名定义)
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
@ -172,39 +188,49 @@ namespace Managers
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
List<Define> result = new List<Define>();
// 从命名定义中查询
if (defines.TryGetValue(defineType, out var namedDefinitions))
{
result.AddRange(namedDefinitions.Values);
}
// 从匿名定义中查询
if (anonymousDefines.TryGetValue(defineType, out var anonymousDefinitionList))
{
result.AddRange(anonymousDefinitionList);
}
// 如果结果为空,则返回 null
if (result.Count == 0)
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
return typeDefinitions.Values.ToArray();
return result.ToArray();
}
/// <summary>
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型。(包括匿名定义)
/// </summary>
/// <typeparam name="T">目标类型。</typeparam>
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
public T[] QueryDefinesByType<T>()
{
var defineType = typeof(T).Name;
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
if (!defines.TryGetValue(defineType, out var typeDefinitions))
List<Define> allDefines = QueryDefinesByType(defineType)?.ToList();
if (allDefines == null || allDefines.Count == 0)
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}'");
return null;
}
try
{
// 获取所有值并尝试转换为目标类型
// 尝试将所有 Define 对象转换为目标类型 T
var result = new List<T>();
foreach (var item in typeDefinitions.Values)
foreach (var item in allDefines)
{
if (item is T converted)
{
@ -216,6 +242,79 @@ namespace Managers
return null;
}
}
return result.ToArray();
}
catch (Exception ex)
{
Debug.LogError($"类型转换失败:从 Define 转换为 {typeof(T).Name} 时出错。错误信息:{ex.Message}");
return null;
}
}
/// <summary>
/// 查询指定类型下的所有 Define 对象(仅包含命名定义,不包括匿名定义)。
/// </summary>
/// <param name="defineType">定义类型(外层字典的键)。</param>
/// <returns>该类型下的 Define 数组,如果未找到则返回 null。</returns>
public Define[] QueryNamedDefinesByType(string defineType)
{
if (string.IsNullOrEmpty(defineType))
{
Debug.LogError("查询失败:定义类型参数不能为空!");
return null;
}
List<Define> result = new List<Define>();
// 仅从命名定义中查询
if (defines.TryGetValue(defineType, out var namedDefinitions))
{
result.AddRange(namedDefinitions.Values);
}
// 如果结果为空,则返回 null
if (result.Count == 0)
{
Debug.LogWarning($"查询失败:未找到定义类型 '{defineType}' 的命名定义");
return null;
}
return result.ToArray();
}
/// <summary>
/// 查询指定类型下的所有 Define 对象,并尝试转换为目标类型(仅包含命名定义,不包括匿名定义)。
/// </summary>
/// <typeparam name="T">目标类型。</typeparam>
/// <returns>转换后的目标类型数组,如果未找到或转换失败则返回 null。</returns>
public T[] QueryNamedDefinesByType<T>()
{
var defineType = typeof(T).Name;
List<Define> allDefines = QueryNamedDefinesByType(defineType)?.ToList();
if (allDefines == null || allDefines.Count == 0)
{
return null;
}
try
{
// 尝试将所有 Define 对象转换为目标类型 T
var result = new List<T>();
foreach (var item in allDefines)
{
if (item is T converted)
{
result.Add(converted);
}
else
{
Debug.LogError($"类型转换失败:无法将 {item.GetType().Name} 转换为 {typeof(T).Name}");
return null;
}
}
return result.ToArray();
}
catch (Exception ex)

View File

@ -61,7 +61,7 @@ namespace Managers
// 创建Sprite
var sprite = Sprite.Create(texture, (Rect)spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
var index = row * cols + col;
var index = (rows - row - 1) * cols + col;
sprites[name + $"_{index}"] = sprite;
}
}

View File

@ -16,27 +16,93 @@ namespace Map
void Start()
{
TileManager.Instance.Init();
var mapSize = 10;
float noiseScale = 0.1f;
for (int x = 0; x < mapSize; x++)
{
List<int> col = new();
for (int y = 0; y < mapSize; y++)
{
// 计算柏林噪声值
float noiseValue = Mathf.PerlinNoise(x * noiseScale, y * noiseScale);
if (noiseValue < 0.5f) // 小于 0.5 表示 Dirt
{
col.Add(TileManager.Instance.tileID.GetValueOrDefault("Dirt"));
}
else // 大于等于 0.5 表示 Grass
{
col.Add(TileManager.Instance.tileID.GetValueOrDefault("Grass"));
}
}
mapData.Add(col);
UpdateTexture();
}
}
public void UpdateTexture()
{
for (int i = 0; i < mapData.Count; i++)
{
for (int j = 0; j < mapData[i].Count; j++)
{
UpdateTexture(i, j);
}
}
}
public int GetTile(int x, int y)
{
if (x < 0 || x >= mapData.Count)
{
return 0;
}
var col = mapData[x];
if (y < 0 || y >= mapData.Count)
{
return 0;
}
return col[y];
}
public void SetTile(int x, int y, string tileName)
{
SetTile(x,y,TileManager.Instance.tileID.GetValueOrDefault(tileName));
}
public void SetTile(int x, int y, int id)
{
mapData[x][y] = id;
UpdateTexture(x, y);
UpdateTexture(x, y-1);
UpdateTexture(x-1, y);
UpdateTexture(x-1, y-1);
}
/// <summary>
/// 更新对应坐标的贴图
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public void UpdateTexture(int x, int y)
{
var lt = GetTile(x, y + 1);
var rt = GetTile(x + 1, y + 1);
var lb = GetTile(x, y);
var rb = GetTile(x + 1, y);
if (TileManager.Instance.tileToTileBaseMapping.ContainsKey((lt, rt, lb, rb)))
{
textureLevel.SetTile(new(x,y),TileManager.Instance.tileToTileBaseMapping[(lt, rt, lb, rb)]);
}
}
}
public class TileManager:Utils.Singleton<TileManager>
{
public Dictionary<string,TileBase> tileBaseMapping = new();
public Dictionary<(int, int, int, int), TileBase> tileToTileBaseMapping = new();
public Dictionary<string, int> tileID = new();
public void Init()
@ -46,7 +112,7 @@ namespace Map
Managers.PackagesImageManager.Instance.Init();
var imagePack = Managers.PackagesImageManager.Instance;
var tileType = Managers.DefineManager.Instance.QueryDefinesByType<TileDef>();
for (var i = 1; i < tileType.Length; i++)
for (var i = 0; i < tileType.Length; i++)
{
tileID.Add(tileType[i].name, i);
}
@ -57,7 +123,7 @@ namespace Map
foreach (var keyVal in mappingTableDef.tileDict)
{
var key = keyVal.Key;
var val=keyVal.Value;
var val = keyVal.Value;
var parts = key.Split('_');
if (parts.Length != 4)
{
@ -67,14 +133,15 @@ namespace Map
}
if (!(tileID.TryGetValue(parts[0], out var k1) &&
tileID.TryGetValue(parts[1], out var k2) &&
tileID.TryGetValue(parts[2], out var k3) &&
tileID.TryGetValue(parts[3], out var k4)))
tileID.TryGetValue(parts[1], out var k2) &&
tileID.TryGetValue(parts[2], out var k3) &&
tileID.TryGetValue(parts[3], out var k4)))
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{key}中存在未定义的瓦片名称");
continue;
}
var sprite = imagePack.GetSprite(val);
if (sprite == null)
{
@ -82,14 +149,17 @@ namespace Map
Debug.LogError($"来自{packName}定义的TileMappingTableDef键值{val}中存在未定义的图片名称");
continue;
}
if (tileToTileBaseMapping.ContainsKey((k1, k2, k3, k4)))
{
var packName = Managers.DefineManager.Instance.GetDefinePackageName(mappingTableDef);
Debug.LogWarning($"来自{packName}定义的TileMappingTableDef键值{(k1, k2, k3, k4)}存在重复索引,将忽略重复项");
continue;
}
var tile = LoadTile(sprite);
tileToTileBaseMapping.Add((k1, k2, k3, k4), tile);
tileToTileBaseMapping[(k1, k2, k3, k4)] = tile;
tileBaseMapping[val] = tile;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 765878e48a7349a98e57309179eebe4d
timeCreated: 1752918159

View File

@ -0,0 +1,26 @@
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
namespace Prefab
{
public class ButtonPrefab:MonoBehaviour
{
public Button button;
public TMP_Text text;
public string Label
{
get{return text.text;}
set{text.text = value;}
}
public void AddListener(UnityAction callback)
{
button.onClick.AddListener(callback);
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 229699c7a1dc4caa84d46e6cfba6a8d8
timeCreated: 1752918181

View File

@ -0,0 +1,17 @@
using TMPro;
using UnityEngine;
namespace Prefab
{
public class TextPrefab : MonoBehaviour
{
public TMP_Text text;
public string Label
{
get{return text.text;}
set{text.text = value;}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 13c16a6a94284487838eb5c60f53ed0d
timeCreated: 1752918417

View File

@ -6,6 +6,8 @@ public class Program : MonoBehaviour
private void Awake()
{
UnityLogger.Init();
Managers.DefineManager.Instance.Init();
Base.UIInputControl.Instance.isGlobal = true;
}
private void Start()

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6303ff87d5e946649be1abf4e7feb895
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,80 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
namespace UI
{
public class DevMenuUI : MonoBehaviour
{
public GameObject menuContent;
public Prefab.TextPrefab textTemplate;
public Prefab.ButtonPrefab buttonTemplate;
void Start()
{
Init();
}
void Init()
{
Managers.DefineManager.Instance.Init();
InitEvent();
InitCharacter();
}
void InitEvent()
{
var title = InstantiatePrefab(textTemplate, menuContent.transform);
title.Label = "事件菜单";
// for (int i = 0; i < 30; i++)
// {
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
// button.text.text = i.ToString();
// }
}
void InitCharacter()
{
var title = InstantiatePrefab(textTemplate, menuContent.transform);
title.Label = "点击切换人物";
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
foreach (var def in defList)
{
var button=InstantiatePrefab(buttonTemplate, menuContent.transform);
button.Label = def.label;
}
}
/// <summary>
/// 通用的实例化函数,返回实例化的预制件脚本组件。
/// </summary>
/// <typeparam name="T">预制件脚本的类型</typeparam>
/// <param name="prefab">要实例化的预制件</param>
/// <param name="parent">实例化对象的父对象</param>
/// <returns>实例化的预制件脚本组件</returns>
T InstantiatePrefab<T>(T prefab, Transform parent) where T : Component
{
if (prefab == null || parent == null)
{
Debug.LogError("Prefab or parent is null!");
return null;
}
// 实例化预制件
GameObject instance = Instantiate(prefab.gameObject, parent);
// 获取实例化对象的脚本组件
T instantiatedComponent = instance.GetComponent<T>();
if (instantiatedComponent == null)
{
Debug.LogError($"Failed to get component of type {typeof(T).Name} from the instantiated prefab!");
}
return instantiatedComponent;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c83e72721411938449d92dd48c76480d