Merge pull request '(client) feat:添加属性相关定义' (#27) from zzdxxz/Gen_Hack-and-Slash-Roguelite-zzdxxz:main into main
This commit is contained in:
37
Client/Assets/Scripts/Data/AttributesDefine.cs
Normal file
37
Client/Assets/Scripts/Data/AttributesDefine.cs
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
|
||||||
|
|
||||||
|
namespace Data
|
||||||
|
{
|
||||||
|
public class CharacterAttributesDef : 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;
|
||||||
|
public int attackRange = 3;
|
||||||
|
public int attackTargetCount = 1;
|
||||||
|
}
|
||||||
|
}
|
@ -2,6 +2,7 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
using UnityEditor.ShaderGraph.Internal;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Data
|
namespace Data
|
||||||
@ -131,4 +132,6 @@ namespace Data
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -1,4 +1,9 @@
|
|||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Xml.Linq;
|
using System.Xml.Linq;
|
||||||
|
|
||||||
@ -38,21 +43,78 @@ namespace Data
|
|||||||
description = xmlDef.Element("description")?.Value;
|
description = xmlDef.Element("description")?.Value;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
// 定义对齐格式(左对齐,固定宽度)
|
|
||||||
const int labelWidth = -15; // 标签左对齐,占15字符
|
|
||||||
const int valueWidth = -30; // 值左对齐,占30字符
|
|
||||||
|
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|
||||||
sb.AppendLine($"{"DefName:",labelWidth}{defName,valueWidth}");
|
// 使用反射获取当前类的所有字段和属性
|
||||||
sb.AppendLine($"{"Label:",labelWidth}{label,valueWidth}");
|
var fieldsAndProperties = GetAllFieldsAndProperties(this);
|
||||||
sb.AppendLine($"{"Description:",labelWidth}{description,valueWidth}");
|
|
||||||
sb.AppendLine($"{"PackID:",labelWidth}{packID,valueWidth}");
|
foreach (var member in fieldsAndProperties)
|
||||||
|
{
|
||||||
|
var name = member.Name;
|
||||||
|
var value = GetValue(member, this);
|
||||||
|
|
||||||
|
if (value is IList list && list.Count > 0) // 如果是列表类型
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{name}:");
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
sb.AppendLine($" - {FormatValue(item)}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (value is Define defineObject) // 如果是继承自 Define 的子类
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(defineObject.defName))
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{name}: {defineObject.defName}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{name}:");
|
||||||
|
sb.AppendLine(Indent(defineObject.ToString(), " "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sb.AppendLine($"{name}: {FormatValue(value)}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<MemberInfo> GetAllFieldsAndProperties(object obj)
|
||||||
|
{
|
||||||
|
var type = obj.GetType();
|
||||||
|
return type.GetFields(BindingFlags.Public | BindingFlags.Instance)
|
||||||
|
.Cast<MemberInfo>()
|
||||||
|
.Concat(type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Cast<MemberInfo>());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static object GetValue(MemberInfo member, object obj)
|
||||||
|
{
|
||||||
|
switch (member)
|
||||||
|
{
|
||||||
|
case FieldInfo field:
|
||||||
|
return field.GetValue(obj);
|
||||||
|
case PropertyInfo property:
|
||||||
|
return property.GetValue(obj);
|
||||||
|
default:
|
||||||
|
throw new ArgumentException("Unsupported member type.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FormatValue(object value)
|
||||||
|
{
|
||||||
|
return value?.ToString() ?? "null";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string Indent(string text, string prefix)
|
||||||
|
{
|
||||||
|
return string.Join(Environment.NewLine, text.Split('\n').Select(line => prefix + line));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user