(client) feat:完成实体生成函数,修复行为树加载错误,改进Define打印缩进

This commit is contained in:
m0_75251201
2025-07-22 14:40:24 +08:00
parent 506d0a68a8
commit a6dfbd7c68
26 changed files with 835 additions and 527 deletions

View File

@ -8,7 +8,12 @@ namespace AI
{
public List<AIBase> children = new();
public virtual JobBase GetJob(Entity.Entity target)
public abstract JobBase GetJob(Entity.Entity target);
}
public class Selector : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
foreach (var aiBase in children)
{
@ -19,28 +24,7 @@ namespace AI
return null;
}
}
public class ContinuousMove : AIBase
{
override public JobBase GetJob(Entity.Entity target)
{
return null;
}
}
public class TrackPlayer : AIBase
{
}
public class RandomWander : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
return new WanderJob();
}
}
public class ConditionalAI : AIBase
public class ConditionalAI : Selector
{
// 条件函数,返回 true 表示满足条件
private Func<Entity.Entity, bool> condition;
@ -64,4 +48,56 @@ namespace AI
return null;
}
}
public class SequentialAI : Selector
{
private int currentIndex = 0; // 当前正在尝试的子节点索引
public override JobBase GetJob(Entity.Entity target)
{
// 如果当前索引超出了子节点范围,重置索引并返回 null
if (currentIndex >= children.Count)
{
ResetIndex();
return null;
}
// 获取当前子节点的任务
var currentChild = children[currentIndex];
var job = currentChild.GetJob(target);
// 移动到下一个子节点
currentIndex++;
return job;
}
// 重置当前索引(用于重新开始遍历)
private void ResetIndex()
{
currentIndex = 0;
}
}
public class ContinuousMove : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
return new MoveJob();
}
}
public class TrackPlayer : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
throw new NotImplementedException();
}
}
public class RandomWander : AIBase
{
public override JobBase GetJob(Entity.Entity target)
{
return new WanderJob();
}
}
}

View File

@ -7,7 +7,7 @@ namespace AI
public abstract class JobBase
{
public Entity.Entity entity;
private int timeoutTicks = 100;
protected int timeoutTicks = 300;
public bool Running=>timeoutTicks > 0;
public virtual void StartJob(Entity.Entity target)
@ -57,5 +57,22 @@ namespace AI
}
}
public class IdleJob:JobBase
{
override public void StartJob(Entity.Entity target)
{
base.StartJob(target);
timeoutTicks = 500;
}
protected override void UpdateJob()
{
}
}
public class MoveJob : JobBase
{
protected override void UpdateJob()
{
entity.TryMove();
}
}
}

View File

@ -105,5 +105,40 @@ namespace Base
if (obj is ITickUI uiObj) tickUIs.Add(uiObj);
}
}
public static void AddTick(ITick tick)
{
if (Instance != null && !Instance.ticks.Contains(tick))
Instance.ticks.Add(tick);
}
public static void RemoveTick(ITick tick)
{
if (Instance != null)
Instance.ticks.Remove(tick);
}
public static void AddTickPhysics(ITickPhysics physics)
{
if (Instance != null && !Instance.tickPhysics.Contains(physics))
Instance.tickPhysics.Add(physics);
}
public static void RemoveTickPhysics(ITickPhysics physics)
{
if (Instance != null)
Instance.tickPhysics.Remove(physics);
}
public static void AddTickUI(ITickUI ui)
{
if (Instance != null && !Instance.tickUIs.Contains(ui))
Instance.tickUIs.Add(ui);
}
public static void RemoveTickUI(ITickUI ui)
{
if (Instance != null)
Instance.tickUIs.Remove(ui);
}
}
}

View File

@ -15,7 +15,7 @@ namespace Data
public string label;
public string packID;
public bool isReferene=false;
public bool isReferene = false;
/// <summary>
/// 初始化方法,根据传入的 XML 元素 (<paramref name="xmlDef" />) 进行处理。
@ -50,69 +50,93 @@ namespace Data
{
var sb = new StringBuilder();
// 使用反射获取当前类的所有字段和属性
var fieldsAndProperties = GetAllFieldsAndProperties(this);
// 显示基类中的基本信息
sb.AppendLine($"Define {{");
sb.AppendLine($"\tdefName: {defName}");
sb.AppendLine($"\tdescription: {description}");
sb.AppendLine($"\tlabel: {label}");
foreach (var member in fieldsAndProperties)
// 获取当前对象的所有字段
var fields = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance);
foreach (var field in fields)
{
var name = member.Name;
var value = GetValue(member, this);
if (field.Name == "defName" || field.Name == "description" || field.Name == "label")
continue; // 已经显示过的基本信息跳过
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)}");
}
sb.Append("\t");
sb.Append(field.Name);
sb.Append(": ");
AppendFieldInfo(sb, field.GetValue(this), "\t\t");
sb.AppendLine();
}
sb.Append("}");
return sb.ToString();
}
private static IEnumerable<MemberInfo> GetAllFieldsAndProperties(object obj)
private void AppendFieldInfo(StringBuilder sb, object value, string indent)
{
var type = obj.GetType();
return type.GetFields(BindingFlags.Public | BindingFlags.Instance)
.Concat(type.GetProperties(BindingFlags.Public | BindingFlags.Instance).Cast<MemberInfo>());
}
private static object GetValue(MemberInfo member, object obj)
{
switch (member)
if (value == null)
{
case FieldInfo field:
return field.GetValue(obj);
case PropertyInfo property:
return property.GetValue(obj);
default:
throw new ArgumentException("Unsupported member type.");
sb.Append("null");
return;
}
var type = value.GetType();
// 如果是简单类型(如 int, string, bool 等)
if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
{
sb.Append(value);
}
// 如果是集合类型(如 List<T>, Dictionary<K,V> 等)
else if (typeof(IEnumerable).IsAssignableFrom(type) && !(value is string))
{
sb.AppendLine(" [");
var isFirst = true;
foreach (var item in (IEnumerable)value)
{
if (!isFirst)
sb.AppendLine(",");
isFirst = false;
sb.Append(indent);
AppendFieldInfo(sb, item, indent + "\t");
}
if (!isFirst) // 如果集合不为空,则添加逗号后的换行
sb.AppendLine();
sb.Append(indent.TrimEnd('\t')); // 回退一级缩进
sb.Append("]");
}
// 如果是自定义对象类型
else
{
sb.AppendLine(" {");
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
var isFirst = true;
foreach (var field in fields)
{
if (!isFirst)
sb.AppendLine(",");
isFirst = false;
sb.Append(indent);
sb.Append(field.Name);
sb.Append(": ");
AppendFieldInfo(sb, field.GetValue(value), indent + "\t");
}
if (!isFirst) // 如果对象有字段,则添加逗号后的换行
sb.AppendLine();
sb.Append(indent.TrimEnd('\t')); // 回退一级缩进
sb.Append("}");
}
}
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));
}
}
}

View File

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

View File

@ -17,6 +17,7 @@ namespace Data
drawingOrder_right;
public BehaviorTreeDef behaviorTree;
public AffiliationDef affiliation;
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
@ -60,29 +61,38 @@ namespace Data
public class BehaviorTreeDef : Define
{
public BehaviorTreeDef[] childTree;
public string className;
public string className="Selector";
public string condition;
public override bool Init(XElement xmlDef)
{
base.Init(xmlDef);
// 从当前节点获取className和condition属性
className = xmlDef.Attribute("className")?.Value ?? className;
condition = xmlDef.Attribute("condition")?.Value;
var nodes = xmlDef.Elements("Node");
var xElements = nodes as XElement[] ?? nodes.ToArray();
if (!xElements.Any())
return false;
if (!nodes.Any())
return true; // 没有子节点也是有效的
className = xmlDef.Attribute("className")?.Value;
List<BehaviorTreeDef> children = new();
foreach (var node in xElements)
foreach (var node in nodes)
{
var childNode = new BehaviorTreeDef();
childNode.Init(node);
if (!childNode.Init(node))
return false;
children.Add(childNode);
}
childTree = children.ToArray();
return true;
}
}
public class AffiliationDef : Define
{
}
}

View File

@ -0,0 +1,10 @@
namespace Entity
{
public class BuildingBase:Entity
{
public override void TryMove()
{
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: eb881a08fe004eb4ab0a6fa9d5d86d33
timeCreated: 1753100586

View File

@ -10,12 +10,11 @@ namespace Entity
public class Character : Entity
{
public CharacterDef characterDef;
public GameObject body;
private void Start()
{
aiTree = new RandomWander();
runtimeAttributes = new AttributesDef();
attributes = new AttributesDef();
}
public void Init()

View File

@ -7,12 +7,13 @@ using UnityEngine.Serialization;
namespace Entity
{
public abstract class Entity:MonoBehaviour,ITick
public class Entity:MonoBehaviour,ITick
{
public AIBase aiTree;
public JobBase currentJob;
public AttributesDef runtimeAttributes;
public AttributesDef attributes;
public Vector3 direction;
public GameObject body;
public bool canSelect = true;
public bool IsChase { set; get; } = true;
@ -53,15 +54,15 @@ namespace Entity
/// </summary>
public virtual void TryMove()
{
transform.position+=direction * (runtimeAttributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
transform.position+=direction * (attributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
}
public virtual void OnHit(Entity from)
{
var hit = from.runtimeAttributes.attack - runtimeAttributes.defense;
var hit = from.attributes.attack - attributes.defense;
if (hit < 0)
hit = from.runtimeAttributes.attack / 100;
runtimeAttributes.health -= hit;
hit = from.attributes.attack / 100;
attributes.health -= hit;
currentJob.StopJob();
}

View File

@ -44,16 +44,14 @@ namespace Managers
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)
void ProcessDefine(Define def)
{
if (def == null || def.isReferene || processedDefines.Contains(def))
if (def == null || def.isReferene)
return;
processedDefines.Add(def);
// 如果字段信息已经缓存,则直接使用缓存
if (!fieldCache.TryGetValue(def.GetType(), out var defineFields))
{
@ -73,7 +71,7 @@ namespace Managers
continue;
if (defRef.isReferene)
{
defineCache.Add(new Tuple<Define, FieldInfo, Define>(parentDef, parentField, defRef));
defineCache.Add(new Tuple<Define, FieldInfo, Define>(def, defineField, defRef));
}
else
{
@ -84,33 +82,54 @@ namespace Managers
anonymousDefines.Add(typeName, new List<Define>());
anonymousDefines[typeName].Add(defRef);
}
ProcessDefine(defRef, def, defineField);
ProcessDefine(defRef);
}
}
}
foreach (var pack in packs)
{
foreach (var define in pack.Value.defines)
foreach (var (typeName, defList) in pack.Value.defines)
{
var typeName = define.Key;
var defList = define.Value;
if (!defines.ContainsKey(typeName))
defines[typeName] = new Dictionary<string, Define>();
foreach (var def in defList)
{
defines[typeName][def.defName] = def;
// 处理顶层 Define
ProcessDefine(def, null, null);
ProcessDefine(def);
}
}
}
foreach (var defRef in defineCache)
{
defRef.Item2.SetValue(defRef.Item1, FindDefine(defRef.Item3.description, defRef.Item3.defName));
if (defRef.Item1 == null)
{
Debug.LogError("defRef.Item1 为 null");
continue;
}
if (defRef.Item2 == null)
{
Debug.LogError("defRef.Item2 为 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}");
continue;
}
try
{
defRef.Item2.SetValue(defRef.Item1, value);
}
catch (Exception ex)
{
Debug.LogError($"SetValue 出错: {ex.Message}");
}
}
}
@ -138,7 +157,23 @@ namespace Managers
}
return null;
}
/// <summary>
/// 使用模板查找并返回指定类型的 Define 对象。
/// </summary>
/// <typeparam name="T">目标类型</typeparam>
/// <param name="defineName">定义名</param>
/// <returns>如果找到,返回转换为目标类型的 Define 对象;否则返回 null。</returns>
public T FindDefine<T>(string defineName) where T : Define
{
foreach (var typeDict in defines.Values)
{
if (typeDict.TryGetValue(defineName, out var define) && define is T result)
{
return result;
}
}
return null;
}
public DefinePack GetDefinePackage(Define define)
{
if (define == null || define.packID == null)

View File

@ -0,0 +1,88 @@
using System.Collections.Generic;
using System.Linq;
using Prefab;
using UnityEngine;
namespace Managers
{
public class EntityManage:MonoBehaviour
{
public Dictionary<string, List<EntityPrefab>> factionEntities = new();
public GameObject entityLevel;
public EntityPrefab entityPrefab;
void Update()
{
// 检测鼠标左键是否按下
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // 获取从相机发出的射线
if (Physics.Raycast(ray, out var hit)) // 检测射线是否击中物体
{
Debug.Log("点击了物体: " + hit.collider.gameObject.name);
}
}
}
/// <summary>
/// 根据给定的PawnDef生成一个实体对象。
/// </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)
{
// 检查entityPrefab是否为空
if (entityPrefab == null)
{
Debug.LogError("Error: entityPrefab is null. Please assign a valid prefab.");
return;
}
// 检查pawnDef是否为空
if (pawnDef == null)
{
Debug.LogError("Error: PawnDef is null. Cannot generate entity without a valid PawnDef.");
return;
}
try
{
// 实例化实体对象
var entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
// 获取EntityPrefab组件
var entityComponent = entity.GetComponent<EntityPrefab>();
// 检查EntityPrefab组件是否存在
if (entityComponent == null)
{
Debug.LogError($"Error: EntityPrefab component not found on the instantiated object: {entity.name}");
return;
}
// 初始化实体组件
entityComponent.Init(pawnDef);
// 确保派系键存在,并初始化对应的列表
var factionKey = pawnDef.attributes.label == null ? "default" : pawnDef.attributes.label;
if (!factionEntities.ContainsKey(factionKey))
{
factionEntities[factionKey] = new List<EntityPrefab>();
}
factionEntities[factionKey].Add(entityComponent);
Base.Clock.AddTick(entity.GetComponent<Entity.Entity>());
}
catch (System.Exception ex)
{
// 捕获并记录任何异常
Debug.LogError($"An error occurred while generating the entity: {ex.Message}");
}
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa378b7511b04429b8b6b0efbcce825a
timeCreated: 1753149728

View File

@ -1,22 +0,0 @@
using Prefab;
using UnityEngine;
namespace Managers
{
public class Generator:MonoBehaviour
{
public GameObject entityLevel;
public EntityPrefab entityPrefab;
public void GenerateEntity(Data.PawnDef pawnDef, Vector3 pos)
{
if (entityPrefab == null || pawnDef == null)
return;
GameObject entity = Instantiate(entityPrefab.gameObject, pos, Quaternion.identity, entityLevel.transform);
// entity.name = pawnDef.name;
var entityComponent = entity.GetComponent<EntityPrefab>();
entityComponent?.Init(pawnDef);
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: d78f1b5a44344a4a987e308d3b9478cc
timeCreated: 1752937967

View File

@ -31,7 +31,7 @@ namespace Managers
if (!packagesImages.ContainsKey(packId))
packagesImages[packId] = new Dictionary<string, Texture2D>();
packagesImages[packId].Add(ima.name, texture);
SplitTextureIntoSprites(packId, ima.name, texture, ima.hCount, ima.wCount, ima.pixelsPerUnit);
}
}
@ -44,15 +44,31 @@ namespace Managers
int cols,
int pixelsPerUnit)
{
if (texture == null || rows <= 0 || cols <= 0)
if (texture == null)
{
Debug.LogError("Invalid parameters for splitting texture.");
Debug.LogError("Texture is null.");
return;
}
// 如果行数或列数小于1则设为1不分割
rows = Mathf.Max(1, rows);
cols = Mathf.Max(1, cols);
var textureWidth = texture.width;
var textureHeight = texture.height;
// 如果不分割rows和cols都为1直接创建单个Sprite
if (rows == 1 && cols == 1)
{
if (!sprites.ContainsKey(packId))
sprites[packId] = new Dictionary<string, Sprite>();
Rect spriteRect = new Rect(0, 0, textureWidth, textureHeight);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
sprites[packId][baseName] = sprite;
return;
}
var tileWidth = textureWidth / cols;
var tileHeight = textureHeight / rows;
@ -61,7 +77,7 @@ namespace Managers
Debug.LogError("Texture dimensions are not divisible by the specified rows and columns.");
return;
}
if (!sprites.ContainsKey(packId))
sprites[packId] = new Dictionary<string, Sprite>();
@ -71,7 +87,7 @@ namespace Managers
{
Rect spriteRect = new(col * tileWidth, row * tileHeight, tileWidth, tileHeight);
var sprite = Sprite.Create(texture, spriteRect, new Vector2(0.5f, 0.5f), pixelsPerUnit);
var index = (rows - row - 1) * cols + col;
var spriteName = $"{baseName}_{index}";

View File

@ -16,7 +16,7 @@ namespace Prefab
public void Init(Data.PawnDef pawnDef)
{
entity.runtimeAttributes = pawnDef.attributes.Clone();
entity.attributes = pawnDef.attributes.Clone();
entity.aiTree = ConvertToAIBase(pawnDef.behaviorTree);
outline.Init();
@ -45,7 +45,10 @@ namespace Prefab
{
if (string.IsNullOrEmpty(className))
throw new ArgumentException("className 不能为空");
if (className.Equals("AIBase", StringComparison.OrdinalIgnoreCase))
{
return (AIBase)Activator.CreateInstance(typeof(AIBase));
}
// 定义可能的命名空间列表
var possibleNamespaces = new[] { "AI"};

View File

@ -1,31 +0,0 @@
using Base;
using UnityEngine;
namespace Test
{
public class ClockTest : MonoBehaviour
{
//private static float timer = 0;
// Start is called once before the first execution of Update after the MonoBehaviour is created
private void Start()
{
var clock = Clock.Instance;
}
// Update is called once per frame
//void Update()
//{
// if (Input.GetKeyUp(KeyCode.W))
// {
// SceneManager.LoadScene("SampleScene");
// }
// if (timer > 1)
// {
// timer -= 1;
// Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӳ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>");
// }
// timer += Time.deltaTime;
//}
}
}

View File

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

View File

@ -1,3 +1,6 @@
using System;
using Data;
using Managers;
using UnityEngine;
namespace Test
@ -6,11 +9,17 @@ namespace Test
public class TestDefine : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
public EntityManage entityManager;
void Awake()
{
Managers.DefineManager.Instance.Init();
Debug.Log(Managers.DefineManager.Instance);
}
private void Start()
{
var chicken = Managers.DefineManager.Instance.FindDefine<CharacterDef>("testPawn");
entityManager.GenerateEntity(chicken,Vector3.zero);
Debug.Log(chicken);
}
// Update is called once per frame