mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-20 11:37:12 +08:00
(client) feat:健康给予,路径优化,结算界面,商店界面 (#60)
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using EventWorkClass;
|
||||
@@ -10,8 +9,8 @@ namespace Utils
|
||||
public static class StringUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// 尝试从字符串中解析出指定数量的浮点数组件。
|
||||
/// 这是StringToVector2和StringToVector3的核心辅助方法。
|
||||
/// 尝试从字符串中解析出指定数量的浮点数组件。
|
||||
/// 这是StringToVector2和StringToVector3的核心辅助方法。
|
||||
/// </summary>
|
||||
/// <param name="source">要解析的源字符串。</param>
|
||||
/// <param name="expectedComponentCount">期望的组件数量(例如,Vector2为2,Vector3为3)。</param>
|
||||
@@ -27,12 +26,10 @@ namespace Utils
|
||||
parsedComponents = new float[expectedComponentCount];
|
||||
if (string.IsNullOrWhiteSpace(source))
|
||||
{
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
parsedComponents[i] = defaultComponentValue;
|
||||
}
|
||||
for (var i = 0; i < expectedComponentCount; i++) parsedComponents[i] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 移除所有可能存在的括号或方括号
|
||||
// 采用Replace而不是Regex是因为简单字符替换的性能优势
|
||||
var cleanedSource = source
|
||||
@@ -45,75 +42,58 @@ namespace Utils
|
||||
char[] delimiters = { ',', ' ', ';' };
|
||||
var componentStrings = cleanedSource.Split(
|
||||
delimiters,
|
||||
System.StringSplitOptions.RemoveEmptyEntries);
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
if (componentStrings.Length < expectedComponentCount)
|
||||
{
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
parsedComponents[i] = defaultComponentValue;
|
||||
}
|
||||
for (var i = 0; i < expectedComponentCount; i++) parsedComponents[i] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
// 尝试解析每个组件,并去除前后空格
|
||||
if (!float.TryParse(componentStrings[i].Trim(), out parsedComponents[i]))
|
||||
{
|
||||
for (var j = 0; j < expectedComponentCount; j++)
|
||||
{
|
||||
parsedComponents[j] = defaultComponentValue;
|
||||
}
|
||||
for (var j = 0; j < expectedComponentCount; j++) parsedComponents[j] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true; // 所有组件都成功解析
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为 Unity 的 Vector2。
|
||||
/// 支持格式如 "1,2", "[1 2]", "(1;2)",并能处理空格。
|
||||
/// 将字符串转换为 Unity 的 Vector2。
|
||||
/// 支持格式如 "1,2", "[1 2]", "(1;2)",并能处理空格。
|
||||
/// </summary>
|
||||
/// <param name="str">要转换的字符串。</param>
|
||||
/// <param name="defaultValue">转换失败时返回的默认 Vector2 值。</param>
|
||||
/// <returns>转换后的 Vector2 值或默认值。</returns>
|
||||
public static Vector2 StringToVector2(string str, Vector2 defaultValue = default)
|
||||
{
|
||||
if (defaultValue == default)
|
||||
{
|
||||
defaultValue = Vector2.zero; // 如果未指定,则使用 Vector2.zero 作为默认值
|
||||
}
|
||||
if (TryParseVectorComponents(str, 2, out var components))
|
||||
{
|
||||
return new Vector2(components[0], components[1]);
|
||||
}
|
||||
if (defaultValue == default) defaultValue = Vector2.zero; // 如果未指定,则使用 Vector2.zero 作为默认值
|
||||
if (TryParseVectorComponents(str, 2, out var components)) return new Vector2(components[0], components[1]);
|
||||
Debug.LogWarning($"将字符串 \"{str}\" 解析为 Vector2 失败。返回默认值 {defaultValue}。");
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为 Unity 的 Vector3。
|
||||
/// 支持格式如 "1,2,3", "[1 2 3]", "(1;2;3)",并能处理空格。
|
||||
/// 将字符串转换为 Unity 的 Vector3。
|
||||
/// 支持格式如 "1,2,3", "[1 2 3]", "(1;2;3)",并能处理空格。
|
||||
/// </summary>
|
||||
/// <param name="str">要转换的字符串。</param>
|
||||
/// <param name="defaultValue">转换失败时返回的默认 Vector3 值。</param>
|
||||
/// <returns>转换后的 Vector3 值或默认值。</returns>
|
||||
public static Vector3 StringToVector3(string str, Vector3 defaultValue = default)
|
||||
{
|
||||
if (defaultValue == default)
|
||||
{
|
||||
defaultValue = Vector3.zero; // 如果未指定,则使用 Vector3.zero 作为默认值
|
||||
}
|
||||
if (defaultValue == default) defaultValue = Vector3.zero; // 如果未指定,则使用 Vector3.zero 作为默认值
|
||||
if (TryParseVectorComponents(str, 3, out var components))
|
||||
{
|
||||
return new Vector3(components[0], components[1], components[2]);
|
||||
}
|
||||
Debug.LogWarning($"将字符串 \"{str}\" 解析为 Vector3 失败。返回默认值 {defaultValue}。");
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 尝试从字符串中解析出指定数量的整数组件。
|
||||
/// 这是StringToVector3Int的核心辅助方法。
|
||||
/// 尝试从字符串中解析出指定数量的整数组件。
|
||||
/// 这是StringToVector3Int的核心辅助方法。
|
||||
/// </summary>
|
||||
/// <param name="source">要解析的源字符串。</param>
|
||||
/// <param name="expectedComponentCount">期望的组件数量(例如,Vector3Int为3)。</param>
|
||||
@@ -129,12 +109,10 @@ namespace Utils
|
||||
parsedComponents = new int[expectedComponentCount];
|
||||
if (string.IsNullOrWhiteSpace(source))
|
||||
{
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
parsedComponents[i] = defaultComponentValue;
|
||||
}
|
||||
for (var i = 0; i < expectedComponentCount; i++) parsedComponents[i] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
// 移除所有可能存在的括号或方括号
|
||||
var cleanedSource = source
|
||||
.Replace("[", "")
|
||||
@@ -145,54 +123,43 @@ namespace Utils
|
||||
char[] delimiters = { ',', ' ', ';' };
|
||||
var componentStrings = cleanedSource.Split(
|
||||
delimiters,
|
||||
System.StringSplitOptions.RemoveEmptyEntries);
|
||||
StringSplitOptions.RemoveEmptyEntries);
|
||||
if (componentStrings.Length < expectedComponentCount)
|
||||
{
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
parsedComponents[i] = defaultComponentValue;
|
||||
}
|
||||
for (var i = 0; i < expectedComponentCount; i++) parsedComponents[i] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (var i = 0; i < expectedComponentCount; i++)
|
||||
{
|
||||
// 尝试解析每个组件,并去除前后空格
|
||||
if (!int.TryParse(componentStrings[i].Trim(), out parsedComponents[i]))
|
||||
{
|
||||
for (var j = 0; j < expectedComponentCount; j++)
|
||||
{
|
||||
parsedComponents[j] = defaultComponentValue;
|
||||
}
|
||||
for (var j = 0; j < expectedComponentCount; j++) parsedComponents[j] = defaultComponentValue;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true; // 所有组件都成功解析
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将字符串转换为 Unity 的 Vector3Int。
|
||||
/// 支持格式如 "1,2,3", "[1 2 3]", "(1;2;3)",并能处理空格。
|
||||
/// 将字符串转换为 Unity 的 Vector3Int。
|
||||
/// 支持格式如 "1,2,3", "[1 2 3]", "(1;2;3)",并能处理空格。
|
||||
/// </summary>
|
||||
/// <param name="str">要转换的字符串。</param>
|
||||
/// <param name="defaultValue">转换失败时返回的默认 Vector3Int 值。</param>
|
||||
/// <returns>转换后的 Vector3Int 值或默认值。</returns>
|
||||
public static Vector3Int StringToVector3Int(string str, Vector3Int defaultValue = default)
|
||||
{
|
||||
if (defaultValue == default)
|
||||
{
|
||||
defaultValue = Vector3Int.zero; // 如果未指定,则使用 Vector3Int.zero 作为默认值
|
||||
}
|
||||
if (defaultValue == default) defaultValue = Vector3Int.zero; // 如果未指定,则使用 Vector3Int.zero 作为默认值
|
||||
if (TryParseVectorIntComponents(str, 3, out var components))
|
||||
{
|
||||
return new Vector3Int(components[0], components[1], components[2]);
|
||||
}
|
||||
Debug.LogWarning($"将字符串 \"{str}\" 解析为 Vector3Int 失败。返回默认值 {defaultValue}。");
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 根据类名字符串创建并返回一个继承自 MapGeneratorWorkClassBase 的实例。
|
||||
/// 根据类名字符串创建并返回一个继承自 MapGeneratorWorkClassBase 的实例。
|
||||
/// </summary>
|
||||
/// <param name="className">不含命名空间的类名。</param>
|
||||
/// <returns>对应的 MapGeneratorWorkClassBase 实例,如果找不到或类型不匹配则返回 null。</returns>
|
||||
@@ -215,7 +182,6 @@ namespace Utils
|
||||
|
||||
// 如果在优先命名空间中没有找到,则进行第二遍:在所有命名空间中查找(不指定命名空间,即只用className)
|
||||
if (foundType == null)
|
||||
{
|
||||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
|
||||
{
|
||||
var type = assembly.GetType(className);
|
||||
@@ -234,7 +200,6 @@ namespace Utils
|
||||
break; // 找到了一个,先用这个。
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 对找到的类型进行有效性检查和实例创建
|
||||
if (foundType != null)
|
||||
@@ -246,7 +211,6 @@ namespace Utils
|
||||
if (typeof(MapGeneratorWorkClassBase).IsAssignableFrom(foundType) &&
|
||||
!foundType.IsAbstract &&
|
||||
foundType.GetConstructor(Type.EmptyTypes) != null) // 检查是否有无参构造函数
|
||||
{
|
||||
try
|
||||
{
|
||||
// 使用 Activator.CreateInstance 创建实例
|
||||
@@ -257,7 +221,6 @@ namespace Utils
|
||||
Debug.LogError($"Error creating instance of {foundType.FullName}: {ex.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
var foundTypeName = foundType.FullName;
|
||||
Debug.LogError($"Type '{foundTypeName}' found but does not meet criteria:" +
|
||||
@@ -273,12 +236,12 @@ namespace Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据类名从指定命名空间和程序集下获取并实例化一个 <see cref="EventWorkClassBase"/> 的子类。
|
||||
/// 根据类名从指定命名空间和程序集下获取并实例化一个 <see cref="EventWorkClassBase" /> 的子类。
|
||||
/// </summary>
|
||||
/// <param name="className">要实例化的类的短名称(不包含命名空间)。</param>
|
||||
/// <param name="targetNamespace">目标类所在的完整命名空间。</param>
|
||||
/// <param name="assemblyToSearch">要搜索的程序集。如果为 null,将搜索 <see cref="EventWorkClassBase"/> 所在的程序集。</param>
|
||||
/// <returns>实例化后的 <see cref="EventWorkClassBase"/> 对象,如果找不到或不符合条件则返回 null。</returns>
|
||||
/// <param name="assemblyToSearch">要搜索的程序集。如果为 null,将搜索 <see cref="EventWorkClassBase" /> 所在的程序集。</param>
|
||||
/// <returns>实例化后的 <see cref="EventWorkClassBase" /> 对象,如果找不到或不符合条件则返回 null。</returns>
|
||||
public static EventWorkClassBase GetAndInstantiateEventWorker(
|
||||
string className,
|
||||
string targetNamespace = "EventWorkClass", // 默认命名空间
|
||||
@@ -286,10 +249,8 @@ namespace Utils
|
||||
{
|
||||
// 1. 确定要搜索的程序集。
|
||||
if (assemblyToSearch == null)
|
||||
{
|
||||
// 默认从 EventWorkClassBase 所在的程序集查找,通常其实现类也位于此程序集。
|
||||
assemblyToSearch = typeof(EventWorkClassBase).Assembly;
|
||||
}
|
||||
|
||||
// 2. 构造完整的类型名称。
|
||||
var fullTypeName = $"{targetNamespace}.{className}";
|
||||
@@ -338,4 +299,4 @@ namespace Utils
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user