(client) feat:主要实现实体的行为树和工作类 (#40)
Co-authored-by: zzdxxz <2079238449@qq.com> Co-committed-by: zzdxxz <2079238449@qq.com>
This commit is contained in:
8
Client/Assets/Scripts/AI.meta
Normal file
8
Client/Assets/Scripts/AI.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5760fa63a6a454c4ba22c0780f247ef2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
67
Client/Assets/Scripts/AI/AIBase.cs
Normal file
67
Client/Assets/Scripts/AI/AIBase.cs
Normal file
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class AIBase
|
||||
{
|
||||
public List<AIBase> children = new();
|
||||
|
||||
public virtual JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
foreach (var aiBase in children)
|
||||
{
|
||||
var job = aiBase.GetJob(target);
|
||||
if (job != null)
|
||||
return job;
|
||||
}
|
||||
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
|
||||
{
|
||||
// 条件函数,返回 true 表示满足条件
|
||||
private Func<Entity.Entity, bool> condition;
|
||||
|
||||
// 构造函数,传入条件函数
|
||||
public ConditionalAI(Func<Entity.Entity, bool> conditionFunc)
|
||||
{
|
||||
condition = conditionFunc;
|
||||
}
|
||||
|
||||
public override JobBase GetJob(Entity.Entity target)
|
||||
{
|
||||
// 检查条件是否满足
|
||||
if (condition != null && condition(target))
|
||||
{
|
||||
// 如果条件满足,继续查找子节点的任务
|
||||
return base.GetJob(target);
|
||||
}
|
||||
|
||||
// 条件不满足,直接返回 null
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/AI/AIBase.cs.meta
Normal file
3
Client/Assets/Scripts/AI/AIBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7dba424df1b6411f91925da8288cb91f
|
||||
timeCreated: 1752983113
|
61
Client/Assets/Scripts/AI/JobBase.cs
Normal file
61
Client/Assets/Scripts/AI/JobBase.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using Base;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AI
|
||||
{
|
||||
public abstract class JobBase
|
||||
{
|
||||
public Entity.Entity entity;
|
||||
private int timeoutTicks = 100;
|
||||
public bool Running=>timeoutTicks > 0;
|
||||
|
||||
public virtual void StartJob(Entity.Entity target)
|
||||
{
|
||||
entity = target;
|
||||
}
|
||||
|
||||
protected abstract void UpdateJob();
|
||||
|
||||
public bool Update()
|
||||
{
|
||||
if(!Running)
|
||||
return false;
|
||||
UpdateJob();
|
||||
timeoutTicks--;
|
||||
if (timeoutTicks <= 0)
|
||||
{
|
||||
StopJob();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public virtual void StopJob()
|
||||
{
|
||||
timeoutTicks = 0;
|
||||
}
|
||||
}
|
||||
public class WanderJob : JobBase
|
||||
{
|
||||
public override void StartJob(Entity.Entity target)
|
||||
{
|
||||
base.StartJob(target);
|
||||
Vector3 move=new(Random.Range(-10,10), Random.Range(-10,10));
|
||||
var targetPosition=entity.transform.position+move;
|
||||
entity.SetTarget(targetPosition);
|
||||
entity.IsChase = false;
|
||||
}
|
||||
|
||||
protected override void UpdateJob()
|
||||
{
|
||||
entity.TryMove();
|
||||
}
|
||||
|
||||
override public void StopJob()
|
||||
{
|
||||
base.StopJob();
|
||||
entity.IsChase = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
2
Client/Assets/Scripts/AI/JobBase.cs.meta
Normal file
2
Client/Assets/Scripts/AI/JobBase.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2497fdaa11d3554287c58d696dab7e9
|
@ -7,23 +7,17 @@ namespace Base
|
||||
{
|
||||
public interface ITick
|
||||
{
|
||||
public void Tick()
|
||||
{
|
||||
}
|
||||
public void Tick();
|
||||
}
|
||||
|
||||
public interface ITickPhysics
|
||||
{
|
||||
public void TickPhysics()
|
||||
{
|
||||
}
|
||||
public void TickPhysics();
|
||||
}
|
||||
|
||||
public interface ITickUI
|
||||
{
|
||||
public void TickUI()
|
||||
{
|
||||
}
|
||||
public void TickUI();
|
||||
}
|
||||
|
||||
public class Clock : MonoSingleton<Clock>
|
||||
|
@ -1,10 +1,189 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Base
|
||||
{
|
||||
public class UIInputControl:Utils.MonoSingleton<UIInputControl>
|
||||
public class UIInputControl : Utils.MonoSingleton<UIInputControl>, ITickUI
|
||||
{
|
||||
// 存储窗口及其激活键的字典
|
||||
public Dictionary<KeyCode, UIBase> UIwindowKeys = new();
|
||||
// 存储没有激活键的窗口列表
|
||||
private List<UIBase> noKeyWindows = new();
|
||||
|
||||
// 每帧更新逻辑
|
||||
public void TickUI()
|
||||
{
|
||||
foreach (var kvp in UIwindowKeys)
|
||||
{
|
||||
if (Input.GetKeyDown(kvp.Key))
|
||||
{
|
||||
HandleWindowActivation(kvp.Value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
UIwindowKeys.Clear();
|
||||
noKeyWindows.Clear();
|
||||
|
||||
var uiInstances = Resources.FindObjectsOfTypeAll<UIBase>();
|
||||
|
||||
foreach (var uiBase in uiInstances)
|
||||
{
|
||||
var key = uiBase.actionButton;
|
||||
if (key == KeyCode.None)
|
||||
{
|
||||
noKeyWindows.Add(uiBase);
|
||||
uiBase.Hide();
|
||||
continue;
|
||||
}
|
||||
if (UIwindowKeys.ContainsKey(key))
|
||||
{
|
||||
Debug.LogWarning($"Key '{key}' is already assigned to another window. Skipping...");
|
||||
continue;
|
||||
}
|
||||
UIwindowKeys[key] = uiBase;
|
||||
uiBase.Hide();
|
||||
}
|
||||
}
|
||||
|
||||
private void HandleWindowActivation(UIBase targetWindow, bool isFunctionCall = false)
|
||||
{
|
||||
bool wasTargetVisible = targetWindow.IsVisible;
|
||||
bool anyOtherWindowOpen = false;
|
||||
|
||||
// 遍历所有窗口(包括有键和无键窗口)
|
||||
foreach (var kvp in UIwindowKeys.Concat(noKeyWindows.Select(w => new KeyValuePair<KeyCode, UIBase>(KeyCode.None, w))))
|
||||
{
|
||||
if (kvp.Value == targetWindow)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (kvp.Value.IsVisible)
|
||||
{
|
||||
if (!wasTargetVisible || isFunctionCall) // 只在目标窗口要打开时才关闭其他窗口
|
||||
{
|
||||
kvp.Value.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
anyOtherWindowOpen = true; // 记录是否有其他窗口打开
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (wasTargetVisible)
|
||||
{
|
||||
targetWindow.Hide();
|
||||
}
|
||||
else
|
||||
{
|
||||
targetWindow.Show();
|
||||
}
|
||||
|
||||
bool currentWindowState = !wasTargetVisible || anyOtherWindowOpen;
|
||||
if (Base.Clock.Instance.Pause != currentWindowState)
|
||||
{
|
||||
Base.Clock.Instance.Pause = currentWindowState;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 模拟按键输入切换窗口
|
||||
/// </summary>
|
||||
/// <param name="keyCode">要模拟的按键</param>
|
||||
public void SimulateKeyPress(KeyCode keyCode)
|
||||
{
|
||||
if (UIwindowKeys.TryGetValue(keyCode, out UIBase targetWindow))
|
||||
{
|
||||
HandleWindowActivation(targetWindow); // 调用内部逻辑处理
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"No window is assigned to the key '{keyCode}'.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开指定的窗口(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要打开的窗口</param>
|
||||
public void OpenWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot open the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭指定的窗口(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要关闭的窗口</param>
|
||||
public void CloseWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot close the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换指定窗口的显示状态(无论是否有激活键)
|
||||
/// </summary>
|
||||
/// <param name="window">要切换的窗口</param>
|
||||
public void ToggleWindow(UIBase window)
|
||||
{
|
||||
if (window == null || !(UIwindowKeys.ContainsValue(window) || noKeyWindows.Contains(window)))
|
||||
{
|
||||
Debug.LogWarning("Cannot toggle the specified window as it is not registered.");
|
||||
return;
|
||||
}
|
||||
|
||||
HandleWindowActivation(window, true); // 调用内部逻辑处理,标记为函数调用
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象销毁时清理事件监听
|
||||
/// </summary>
|
||||
private void OnDestroy()
|
||||
{
|
||||
SceneManager.sceneLoaded -= OnSceneLoaded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在对象启动时初始化
|
||||
/// </summary>
|
||||
protected override void OnStart()
|
||||
{
|
||||
|
||||
// 注册场景加载事件
|
||||
SceneManager.sceneLoaded += OnSceneLoaded;
|
||||
|
||||
// 初始化时调用一次
|
||||
Init();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 场景加载完成后重新初始化
|
||||
/// </summary>
|
||||
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
||||
{
|
||||
// 场景加载完成后调用 Init 方法
|
||||
Init();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/CameraControl.meta
Normal file
3
Client/Assets/Scripts/CameraControl.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f17e95f87b94882a0a6b913f4cd3521
|
||||
timeCreated: 1752978726
|
13
Client/Assets/Scripts/CameraControl/CameraControl.cs
Normal file
13
Client/Assets/Scripts/CameraControl/CameraControl.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace CameraControl
|
||||
{
|
||||
public class CameraControl:MonoBehaviour,Base.ITick
|
||||
{
|
||||
public Entity.Entity focusedEntity=null;
|
||||
public void Tick()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f749a65b381541fab3f5dd5e487a457b
|
||||
timeCreated: 1752977849
|
@ -5,11 +5,24 @@ namespace Data
|
||||
public class AttributesDef : Define
|
||||
{
|
||||
public int health = 10;
|
||||
public int moveSpeed = 1;
|
||||
public float moveSpeed = 1;
|
||||
public int attack = 1;
|
||||
public int defense = 0;
|
||||
public int attackSpeed = 2;
|
||||
public int attackRange = 3;
|
||||
public int attackTargetCount = 1;
|
||||
public AttributesDef Clone()
|
||||
{
|
||||
return new AttributesDef
|
||||
{
|
||||
health = this.health,
|
||||
moveSpeed = this.moveSpeed,
|
||||
attack = this.attack,
|
||||
defense = this.defense,
|
||||
attackSpeed = this.attackSpeed,
|
||||
attackRange = this.attackRange,
|
||||
attackTargetCount = this.attackTargetCount
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -34,17 +34,49 @@ namespace Data
|
||||
base.Init(xmlDef);
|
||||
|
||||
var nodes = xmlDef.Elements("DrawNodeDef");
|
||||
if (nodes.Count() == 0)
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return false;
|
||||
foreach (var node in nodes)
|
||||
{
|
||||
var drawNode = new DrawNodeDef();
|
||||
drawNode.Init(node);
|
||||
drawNodes.Add(drawNode);
|
||||
}
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var drawNode = new DrawNodeDef();
|
||||
drawNode.Init(node);
|
||||
drawNodes.Add(drawNode);
|
||||
}
|
||||
|
||||
return true;;
|
||||
}
|
||||
// 重载 == 运算符
|
||||
public static bool operator ==(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
|
||||
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false
|
||||
|
||||
return AreEqual(a, b);
|
||||
}
|
||||
|
||||
// 重载 != 运算符
|
||||
public static bool operator !=(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
// 判断两个 DrawingOrderDef 是否相等
|
||||
private static bool AreEqual(DrawingOrderDef a, DrawingOrderDef b)
|
||||
{
|
||||
// 比较 drawNodes 的数量
|
||||
if (a.drawNodes.Count != b.drawNodes.Count)
|
||||
return false;
|
||||
|
||||
// 递归比较每个 DrawNodeDef
|
||||
for (int i = 0; i < a.drawNodes.Count; i++)
|
||||
{
|
||||
if (!DrawNodeDef.AreEqual(a.drawNodes[i], b.drawNodes[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public class DrawNodeDef : Define
|
||||
@ -77,16 +109,16 @@ namespace Data
|
||||
public Vector2 StringToVector(string vectorDef)
|
||||
{
|
||||
// 去掉可能存在的括号和多余的空格
|
||||
string cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
|
||||
var cleanedInput = vectorDef.Replace("(", "").Replace(")", "").Trim();
|
||||
|
||||
// 使用正则表达式匹配两个浮点数
|
||||
Match match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
|
||||
var match = Regex.Match(cleanedInput, @"\s*(-?\d+(\.\d*)?)\s*[, ]\s*(-?\d+(\.\d*)?)\s*");
|
||||
|
||||
if (match.Success)
|
||||
{
|
||||
// 提取匹配到的两个浮点数
|
||||
float x = float.Parse(match.Groups[1].Value);
|
||||
float y = float.Parse(match.Groups[3].Value);
|
||||
var x = float.Parse(match.Groups[1].Value);
|
||||
var y = float.Parse(match.Groups[3].Value);
|
||||
|
||||
// 返回 Vector2 对象
|
||||
return new Vector2(x, y);
|
||||
@ -96,6 +128,30 @@ namespace Data
|
||||
return Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
// 判断两个 DrawNodeDef 是否相等
|
||||
public static bool AreEqual(DrawNodeDef a, DrawNodeDef b)
|
||||
{
|
||||
if (ReferenceEquals(a, b)) return true; // 如果是同一个对象,直接返回 true
|
||||
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) return false; // 如果其中一个为 null,返回 false
|
||||
// 比较基本属性
|
||||
if (a.drawNodeType != b.drawNodeType ||
|
||||
a.nodeName != b.nodeName ||
|
||||
a.position != b.position ||
|
||||
Math.Abs(a.FPS - b.FPS) > 0.001f) // 浮点数比较需要考虑精度
|
||||
return false;
|
||||
// 比较 children 的数量
|
||||
if (a.children.Count != b.children.Count)
|
||||
return false;
|
||||
// 递归比较每个子节点
|
||||
for (var i = 0; i < a.children.Count; i++)
|
||||
{
|
||||
if (!AreEqual(a.children[i], b.children[i]))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -279,8 +279,14 @@ namespace Data
|
||||
value = reference;
|
||||
}
|
||||
}
|
||||
else if(field.FieldType.IsArray)
|
||||
{
|
||||
value = ProcessArrayField(field, element);
|
||||
}
|
||||
else
|
||||
{
|
||||
value = Convert.ChangeType(element.Value, field.FieldType);
|
||||
}
|
||||
field.SetValue(define, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@ -289,7 +295,44 @@ namespace Data
|
||||
}
|
||||
}
|
||||
}
|
||||
private static object ProcessArrayField(FieldInfo field, XElement element)
|
||||
{
|
||||
Type elementType = field.FieldType.GetElementType();
|
||||
if (elementType == null) return null;
|
||||
|
||||
var arrayElements = new List<object>();
|
||||
foreach (var liElement in element.Elements())
|
||||
{
|
||||
if (elementType.IsSubclassOf(typeof(Define)))
|
||||
{
|
||||
Define nestedDefine = (Define)Activator.CreateInstance(elementType);
|
||||
DefaultInitDefine(nestedDefine, liElement, elementType);
|
||||
arrayElements.Add(nestedDefine);
|
||||
}
|
||||
else if (elementType.IsArray) // 嵌套数组处理
|
||||
{
|
||||
// 递归处理嵌套数组
|
||||
var nestedArray = ProcessArrayField(
|
||||
new { FieldType = elementType }.GetType().GetField("FieldType"),
|
||||
liElement
|
||||
);
|
||||
arrayElements.Add(nestedArray);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 基本类型处理
|
||||
arrayElements.Add(Convert.ChangeType(liElement.Value, elementType));
|
||||
}
|
||||
}
|
||||
|
||||
// 构建结果数组
|
||||
Array resultArray = Array.CreateInstance(elementType, arrayElements.Count);
|
||||
for (int i = 0; i < arrayElements.Count; i++)
|
||||
{
|
||||
resultArray.SetValue(arrayElements[i], i);
|
||||
}
|
||||
return resultArray;
|
||||
}
|
||||
/// <summary>
|
||||
/// 从 List<c>XDocument</c> 中查找指定根元素名称的文档。
|
||||
/// </summary>
|
||||
|
@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Data
|
||||
{
|
||||
public class PawnDef : Define
|
||||
{
|
||||
public AttributesDef attributes;
|
||||
public string aiController;
|
||||
public string texturePath = null;
|
||||
public DrawingOrderDef
|
||||
@ -11,6 +15,10 @@ namespace Data
|
||||
drawingOrder_up,
|
||||
drawingOrder_left,
|
||||
drawingOrder_right;
|
||||
|
||||
public BehaviorTreeDef behaviorTree;
|
||||
|
||||
|
||||
public DrawingOrderDef GetDrawingOrder(Orientation orientation)
|
||||
{
|
||||
// 定义一个临时变量用于存储结果
|
||||
@ -48,4 +56,33 @@ namespace Data
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class BehaviorTreeDef : Define
|
||||
{
|
||||
public BehaviorTreeDef[] childTree;
|
||||
public string className;
|
||||
|
||||
public override bool Init(XElement xmlDef)
|
||||
{
|
||||
base.Init(xmlDef);
|
||||
|
||||
var nodes = xmlDef.Elements("Node");
|
||||
var xElements = nodes as XElement[] ?? nodes.ToArray();
|
||||
if (!xElements.Any())
|
||||
return false;
|
||||
|
||||
className = xmlDef.Attribute("className")?.Value;
|
||||
List<BehaviorTreeDef> children = new();
|
||||
foreach (var node in xElements)
|
||||
{
|
||||
var childNode = new BehaviorTreeDef();
|
||||
childNode.Init(node);
|
||||
children.Add(childNode);
|
||||
}
|
||||
|
||||
childTree = children.ToArray();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,96 +1,28 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Character : MonoBehaviour
|
||||
public class Character : Entity
|
||||
{
|
||||
public CharacterDef characterDef;
|
||||
public GameObject body;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (Managers.DefineManager.Instance.defines.TryGetValue("DrawingOrderDef",out var typeDict))
|
||||
{
|
||||
GenerateDrawNode(typeDict.Values?.FirstOrDefault()as DrawingOrderDef);
|
||||
}
|
||||
aiTree = new RandomWander();
|
||||
runtimeAttributes = new AttributesDef();
|
||||
}
|
||||
|
||||
public void Init(CharacterDef def)
|
||||
public void Init()
|
||||
{
|
||||
|
||||
if (def == null)
|
||||
if (characterDef == null)
|
||||
return;
|
||||
GenerateDrawNode(def.GetDrawingOrder(Orientation.Down));
|
||||
}
|
||||
|
||||
// 生成图片或动画节点
|
||||
private void GenerateDrawNode(DrawingOrderDef def)
|
||||
{
|
||||
// Debug.Log(def);
|
||||
// 删除现有子节点
|
||||
DeleteAllChildren();
|
||||
|
||||
// 生成根节点下的所有节点
|
||||
foreach (var nodeDef in def.drawNodes) GenerateNode(nodeDef, transform); // 在当前节点下生成
|
||||
}
|
||||
|
||||
// 递归生成节点
|
||||
private void GenerateNode(DrawNodeDef nodeDef, Transform parent)
|
||||
{
|
||||
// Debug.Log(nodeDef.nodeName);
|
||||
// 创建新的 GameObject 表示节点
|
||||
var nodeObject = new GameObject(nodeDef.nodeName);
|
||||
|
||||
// 设置父节点
|
||||
nodeObject.transform.SetParent(parent, false);
|
||||
|
||||
// 根据节点类型生成不同的内容
|
||||
switch (nodeDef.drawNodeType)
|
||||
{
|
||||
case DrawNodeType.Image:
|
||||
CreateImageNode(nodeObject);
|
||||
break;
|
||||
|
||||
case DrawNodeType.Animation:
|
||||
CreateAnimationNode(nodeObject);
|
||||
break;
|
||||
|
||||
default:
|
||||
Debug.LogWarning($"Unsupported node type: {nodeDef.drawNodeType}");
|
||||
break;
|
||||
}
|
||||
|
||||
// 递归生成子节点
|
||||
if (nodeDef.children != null && nodeDef.children.Count > 0)
|
||||
foreach (var childNodeDef in nodeDef.children)
|
||||
GenerateNode(childNodeDef, nodeObject.transform); // 在当前节点下生成子节点
|
||||
}
|
||||
|
||||
// 创建图片节点
|
||||
private void CreateImageNode(GameObject nodeObject)
|
||||
{
|
||||
// 添加 SpriteRenderer 组件表示图片
|
||||
var spriteRenderer = nodeObject.AddComponent<SpriteRenderer>();
|
||||
spriteRenderer.sprite = Resources.Load<Sprite>("DefaultImage"); // 加载默认图片
|
||||
spriteRenderer.color = Color.white; // 设置默认颜色
|
||||
}
|
||||
|
||||
// 创建动画节点
|
||||
private void CreateAnimationNode(GameObject nodeObject)
|
||||
{
|
||||
// 添加 Animator 组件表示动画
|
||||
var animator = nodeObject.AddComponent<Animator>();
|
||||
animator.runtimeAnimatorController =
|
||||
Resources.Load<RuntimeAnimatorController>("DefaultAnimation"); // 加载默认动画控制器
|
||||
}
|
||||
|
||||
private void DeleteAllChildren()
|
||||
{
|
||||
// 获取当前对象的 Transform
|
||||
var parentTransform = transform;
|
||||
|
||||
// 删除所有子对象
|
||||
foreach (Transform child in parentTransform) Destroy(child.gameObject); // 使用 Destroy 方法删除子对象
|
||||
}
|
||||
|
||||
}
|
||||
}
|
143
Client/Assets/Scripts/Entity/Entity.cs
Normal file
143
Client/Assets/Scripts/Entity/Entity.cs
Normal file
@ -0,0 +1,143 @@
|
||||
using System.Collections.Generic;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public abstract class Entity:MonoBehaviour,ITick
|
||||
{
|
||||
public AIBase aiTree;
|
||||
public JobBase currentJob;
|
||||
public AttributesDef runtimeAttributes;
|
||||
public Vector3 direction;
|
||||
|
||||
public bool canSelect = true;
|
||||
public bool IsChase { set; get; } = true;
|
||||
public bool PlayerControlled
|
||||
{
|
||||
set
|
||||
{
|
||||
if (!value)
|
||||
{
|
||||
IsChase = true;
|
||||
}
|
||||
_isPlayerControlled = value;
|
||||
}
|
||||
get => _isPlayerControlled;
|
||||
}
|
||||
|
||||
private bool _isPlayerControlled = false;
|
||||
|
||||
private const int WarningInterval = 5000;
|
||||
private int _warningTicks = 0;
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
if (PlayerControlled)
|
||||
{
|
||||
UpdatePlayerControls();
|
||||
}
|
||||
else
|
||||
{
|
||||
AutoBehave();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void TryAttck()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 往对应朝向移动moveSpeed*deltaTime的距离
|
||||
/// </summary>
|
||||
public virtual void TryMove()
|
||||
{
|
||||
transform.position+=direction * (runtimeAttributes.moveSpeed * Time.deltaTime * (IsChase ? 1 : 0.5f));
|
||||
}
|
||||
|
||||
public virtual void OnHit(Entity from)
|
||||
{
|
||||
var hit = from.runtimeAttributes.attack - runtimeAttributes.defense;
|
||||
if (hit < 0)
|
||||
hit = from.runtimeAttributes.attack / 100;
|
||||
runtimeAttributes.health -= hit;
|
||||
|
||||
currentJob.StopJob();
|
||||
}
|
||||
|
||||
public virtual void SetTarget(Vector3 pos)
|
||||
{
|
||||
direction = (pos - transform.position).normalized;
|
||||
}
|
||||
|
||||
public virtual void Kill(float delay = 0)
|
||||
{
|
||||
Destroy(gameObject,delay);
|
||||
}
|
||||
|
||||
private void AutoBehave()
|
||||
{
|
||||
if (currentJob == null || !currentJob.Running)
|
||||
{
|
||||
currentJob = aiTree.GetJob(this);
|
||||
if (currentJob == null)
|
||||
{
|
||||
if (_warningTicks<=0)
|
||||
{
|
||||
Debug.LogWarning($"{GetType().Name}类型的{name}没有分配到任何工作,给行为树末尾添加等待行为,避免由于没有工作导致无意义的反复查找工作导致性能问题");
|
||||
_warningTicks += WarningInterval;
|
||||
}
|
||||
|
||||
_warningTicks--;
|
||||
return;
|
||||
}
|
||||
currentJob.StartJob(this);
|
||||
}
|
||||
|
||||
currentJob.Update();
|
||||
}
|
||||
|
||||
private void UpdatePlayerControls()
|
||||
{
|
||||
// 获取当前键盘输入状态
|
||||
var inputDirection = new Vector3();
|
||||
|
||||
// 检测 WASD 输入
|
||||
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
|
||||
{
|
||||
inputDirection += Vector3.forward; // 向前移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
|
||||
{
|
||||
inputDirection += Vector3.back; // 向后移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
|
||||
{
|
||||
inputDirection += Vector3.left; // 向左移动
|
||||
}
|
||||
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
|
||||
{
|
||||
inputDirection += Vector3.right; // 向右移动
|
||||
}
|
||||
|
||||
// 如果有输入方向,则设置目标位置并尝试移动
|
||||
if (inputDirection != Vector3.zero)
|
||||
{
|
||||
// 归一化方向向量,确保对角线移动速度一致
|
||||
inputDirection = inputDirection.normalized;
|
||||
|
||||
// 设置目标位置(假设当前位置为 transform.position)
|
||||
Vector3 targetPosition = transform.position + inputDirection;
|
||||
|
||||
// 调用 SetTarget 方法设置目标位置
|
||||
SetTarget(targetPosition);
|
||||
|
||||
// 调用 TryMove 方法处理实际移动逻辑
|
||||
TryMove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Entity/Entity.cs.meta
Normal file
3
Client/Assets/Scripts/Entity/Entity.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fbde354e0bcc4409b3378ee9b698ddc0
|
||||
timeCreated: 1752932072
|
51
Client/Assets/Scripts/Entity/Outline.cs
Normal file
51
Client/Assets/Scripts/Entity/Outline.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Entity
|
||||
{
|
||||
public class Outline:MonoBehaviour
|
||||
{
|
||||
public GameObject body;
|
||||
public SpriteRenderer outlineRenderer;
|
||||
public CapsuleCollider2D outlineCollider;
|
||||
|
||||
public void Init()
|
||||
{
|
||||
var size = GetSize();
|
||||
outlineRenderer.size = size;
|
||||
outlineCollider.direction = size.x > size.y ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
|
||||
outlineCollider.size = size;
|
||||
}
|
||||
public void Show()
|
||||
{
|
||||
outlineRenderer.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
outlineRenderer.gameObject.SetActive(false);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定对象及其所有子对象组成的图像的大小。
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// 返回一个 Vector3 对象,表示对象在世界空间中的总大小(宽度、高度、深度)。
|
||||
/// 如果没有找到任何渲染器,则返回 (-1, -1, -1) 表示无效大小。
|
||||
/// </returns>
|
||||
public Vector3 GetSize()
|
||||
{
|
||||
var renderers = body.GetComponentsInChildren<Renderer>();
|
||||
|
||||
if (renderers.Length == 0)
|
||||
{
|
||||
return new(-1,-1);
|
||||
}
|
||||
var totalBounds = renderers[0].bounds;
|
||||
for (var i = 1; i < renderers.Length; i++)
|
||||
{
|
||||
totalBounds.Encapsulate(renderers[i].bounds);
|
||||
}
|
||||
var size = totalBounds.size;
|
||||
return size;
|
||||
}
|
||||
}
|
||||
}
|
2
Client/Assets/Scripts/Entity/Outline.cs.meta
Normal file
2
Client/Assets/Scripts/Entity/Outline.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acf99b9aa18d4ab9b4ce796d513d476b
|
22
Client/Assets/Scripts/Managers/Generator.cs
Normal file
22
Client/Assets/Scripts/Managers/Generator.cs
Normal file
@ -0,0 +1,22 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Managers/Generator.cs.meta
Normal file
3
Client/Assets/Scripts/Managers/Generator.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d78f1b5a44344a4a987e308d3b9478cc
|
||||
timeCreated: 1752937967
|
78
Client/Assets/Scripts/Prefab/EntityPrefab.cs
Normal file
78
Client/Assets/Scripts/Prefab/EntityPrefab.cs
Normal file
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using AI;
|
||||
using Base;
|
||||
using Data;
|
||||
using Entity;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Prefab
|
||||
{
|
||||
public class EntityPrefab : MonoBehaviour
|
||||
{
|
||||
public Entity.Entity entity;
|
||||
public Outline outline;
|
||||
|
||||
|
||||
public void Init(Data.PawnDef pawnDef)
|
||||
{
|
||||
entity.runtimeAttributes = pawnDef.attributes.Clone();
|
||||
entity.aiTree = ConvertToAIBase(pawnDef.behaviorTree);
|
||||
|
||||
outline.Init();
|
||||
outline.Hide();
|
||||
}
|
||||
public static AIBase ConvertToAIBase(BehaviorTreeDef behaviorTreeDef)
|
||||
{
|
||||
if (behaviorTreeDef == null)
|
||||
return null;
|
||||
AIBase aiBase = CreateAIBaseInstance(behaviorTreeDef.className);
|
||||
if (behaviorTreeDef.childTree != null)
|
||||
{
|
||||
foreach (var child in behaviorTreeDef.childTree)
|
||||
{
|
||||
if (child != null)
|
||||
{
|
||||
aiBase.children.Add(ConvertToAIBase(child));
|
||||
}
|
||||
}
|
||||
}
|
||||
return aiBase;
|
||||
}
|
||||
|
||||
// 使用反射根据 className 创建具体的 AIBase 子类实例
|
||||
private static AIBase CreateAIBaseInstance(string className)
|
||||
{
|
||||
if (string.IsNullOrEmpty(className))
|
||||
throw new ArgumentException("className 不能为空");
|
||||
|
||||
// 定义可能的命名空间列表
|
||||
var possibleNamespaces = new[] { "AI"};
|
||||
|
||||
foreach (var ns in possibleNamespaces)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 获取当前程序集
|
||||
var assembly = typeof(AIBase).Assembly;
|
||||
|
||||
// 尝试查找类型
|
||||
var type = assembly.GetType($"{ns}.{className}");
|
||||
|
||||
if (type != null && typeof(AIBase).IsAssignableFrom(type))
|
||||
{
|
||||
// 如果找到合适的类型,则创建实例并返回
|
||||
return (AIBase)Activator.CreateInstance(type);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略单个命名空间的错误,继续尝试下一个命名空间
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有命名空间都未找到对应的类型,抛出异常
|
||||
throw new InvalidOperationException($"无法找到类型 {className} 或该类型不是 AIBase 的子类");
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Prefab/EntityPrefab.cs.meta
Normal file
3
Client/Assets/Scripts/Prefab/EntityPrefab.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b20b1846b9ef47db83c2ac8c4c4e82cb
|
||||
timeCreated: 1752975550
|
@ -7,7 +7,6 @@ public class Program : MonoBehaviour
|
||||
{
|
||||
UnityLogger.Init();
|
||||
Managers.DefineManager.Instance.Init();
|
||||
Base.UIInputControl.Instance.isGlobal = true;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
|
@ -5,13 +5,13 @@ using UnityEngine.UI;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class DevMenuUI : MonoBehaviour
|
||||
public class DevMenuUI : UIBase
|
||||
{
|
||||
public GameObject menuContent;
|
||||
|
||||
public Prefab.TextPrefab textTemplate;
|
||||
public Prefab.ButtonPrefab buttonTemplate;
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
Init();
|
||||
@ -19,10 +19,12 @@ namespace UI
|
||||
|
||||
void Init()
|
||||
{
|
||||
Managers.DefineManager.Instance.Init();
|
||||
InitEvent();
|
||||
InitCharacter();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void InitEvent()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
@ -32,12 +34,13 @@ namespace UI
|
||||
// var button= InstantiatePrefab(buttonTemplate, menuContent.transform);
|
||||
// button.text.text = i.ToString();
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
void InitCharacter()
|
||||
{
|
||||
var title = InstantiatePrefab(textTemplate, menuContent.transform);
|
||||
title.Label = "点击切换人物";
|
||||
title.Label = "生成人物";
|
||||
|
||||
var defList = Managers.DefineManager.Instance.QueryNamedDefinesByType<Data.CharacterDef>();
|
||||
foreach (var def in defList)
|
||||
|
24
Client/Assets/Scripts/UI/EscUI.cs
Normal file
24
Client/Assets/Scripts/UI/EscUI.cs
Normal file
@ -0,0 +1,24 @@
|
||||
namespace UI
|
||||
{
|
||||
public class EscUI:UIBase
|
||||
{
|
||||
public void ContinueButton()
|
||||
{
|
||||
Base.UIInputControl.Instance.CloseWindow(this);
|
||||
}
|
||||
|
||||
public void ExitButton()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false; // 停止编辑器播放模式
|
||||
#else
|
||||
Application.Quit(); // 关闭游戏
|
||||
#endif
|
||||
}
|
||||
|
||||
public void SettingsButton()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/EscUI.cs.meta
Normal file
3
Client/Assets/Scripts/UI/EscUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7eac7c56e1474c4ea646c0a408d293a3
|
||||
timeCreated: 1752932683
|
8
Client/Assets/Scripts/UI/PauseUI.cs
Normal file
8
Client/Assets/Scripts/UI/PauseUI.cs
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public class PauseUI : UIBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/PauseUI.cs.meta
Normal file
3
Client/Assets/Scripts/UI/PauseUI.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bcc7e9d015ee465684be520b65c2e182
|
||||
timeCreated: 1752932641
|
15
Client/Assets/Scripts/UI/UIBase.cs
Normal file
15
Client/Assets/Scripts/UI/UIBase.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace UI
|
||||
{
|
||||
public abstract class UIBase:MonoBehaviour
|
||||
{
|
||||
public KeyCode actionButton = KeyCode.None;
|
||||
// 显示或隐藏窗口
|
||||
public virtual void Show() { gameObject.SetActive(true); }
|
||||
public virtual void Hide() { gameObject.SetActive(false); }
|
||||
|
||||
// 判断是否可见
|
||||
public bool IsVisible => gameObject.activeInHierarchy;
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/UI/UIBase.cs.meta
Normal file
3
Client/Assets/Scripts/UI/UIBase.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 293a60af40144aafabdf65f49f5c6e44
|
||||
timeCreated: 1752926575
|
131
Client/Assets/Scripts/Utils/Resolver.cs
Normal file
131
Client/Assets/Scripts/Utils/Resolver.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
public static class Resolver
|
||||
{
|
||||
/// <summary>
|
||||
/// 将字符串表达式解析为一个谓词函数,该函数可以用于筛选实体对象。
|
||||
/// </summary>
|
||||
/// <param name="expression">表示条件的字符串表达式。格式示例:"entity.Id > 10" 或 "entity.Name == 'John'"。</param>
|
||||
/// <returns>返回一个 Func<Entity.Entity, bool> 类型的委托,表示解析后的谓词函数。</returns>
|
||||
/// <exception cref="FormatException">当输入表达式的格式不正确时抛出此异常。</exception>
|
||||
/// <exception cref="NotSupportedException">当表达式中包含不支持的操作符或数据类型时抛出此异常。</exception>
|
||||
/// <remarks>
|
||||
/// 表达式的格式必须符合以下规则:
|
||||
/// - 表达式由三部分组成:属性路径、操作符和值,用空格分隔。
|
||||
/// - 属性路径格式为 "entity.PropertyName",其中 PropertyName 是实体类中的一个公共属性或字段。
|
||||
/// - 操作符可以是以下之一:">", "<", ">=", "<=", "==", "!="。
|
||||
/// - 值的类型必须与属性的类型匹配,并且支持以下类型:string, int, long, float, double, decimal, bool, DateTime, Guid 和枚举类型。
|
||||
///
|
||||
/// 注意事项:
|
||||
/// - 字符串值需要用单引号或双引号括起来,例如 'John' 或 "John"。
|
||||
/// - 对于可为空类型(Nullable),会自动处理其底层类型的转换。
|
||||
/// - 字符串比较默认使用不区分大小写的 Equals 方法。
|
||||
/// </remarks>
|
||||
public static Func<Entity.Entity, bool> ParsePredicate(string expression)
|
||||
{
|
||||
// 格式示例:"entity.Id > 10" 或 "entity.Name == 'John'"
|
||||
var parts = expression.Split(new[] { ' ' }, 3, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length != 3)
|
||||
throw new FormatException(
|
||||
"Invalid expression format. Expected format: 'entity.Property Operator Value'");
|
||||
|
||||
// 解析属性和操作符
|
||||
var propPath = parts[0].Split('.')[1]; // "Id" 或 "Name"
|
||||
var op = parts[1]; // ">", "==" 等
|
||||
|
||||
// 创建表达式参数
|
||||
var param = Expression.Parameter(typeof(Entity.Entity), "entity");
|
||||
|
||||
// 获取属性访问表达式
|
||||
var propAccess = propPath.Split('.')
|
||||
.Aggregate<string, Expression>(param, Expression.PropertyOrField);
|
||||
|
||||
// 获取属性类型
|
||||
var propType = propAccess.Type;
|
||||
|
||||
// 解析值并转换为适当类型
|
||||
object value;
|
||||
var valueStr = parts[2].Trim();
|
||||
|
||||
try
|
||||
{
|
||||
if (propType == typeof(string))
|
||||
// 处理字符串值(去除引号)
|
||||
value = valueStr.Trim('\'', '"');
|
||||
else if (propType == typeof(int))
|
||||
value = int.Parse(valueStr);
|
||||
else if (propType == typeof(long))
|
||||
value = long.Parse(valueStr);
|
||||
else if (propType == typeof(float))
|
||||
value = float.Parse(valueStr);
|
||||
else if (propType == typeof(double))
|
||||
value = double.Parse(valueStr);
|
||||
else if (propType == typeof(decimal))
|
||||
value = decimal.Parse(valueStr);
|
||||
else if (propType == typeof(bool))
|
||||
value = bool.Parse(valueStr);
|
||||
else if (propType == typeof(DateTime))
|
||||
value = DateTime.Parse(valueStr);
|
||||
else if (propType == typeof(Guid))
|
||||
value = Guid.Parse(valueStr);
|
||||
else if (propType.IsEnum)
|
||||
value = Enum.Parse(propType, valueStr);
|
||||
else
|
||||
throw new NotSupportedException($"Type {propType.Name} is not supported");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new FormatException($"Failed to parse value '{valueStr}' for type {propType.Name}", ex);
|
||||
}
|
||||
|
||||
// 创建常量表达式(确保类型匹配)
|
||||
var constant = Expression.Constant(value, propType);
|
||||
|
||||
// 处理可为空类型的情况
|
||||
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
|
||||
{
|
||||
var underlyingType = Nullable.GetUnderlyingType(propType);
|
||||
propAccess = Expression.Property(propAccess, "Value");
|
||||
constant = Expression.Constant(Convert.ChangeType(value, underlyingType), underlyingType);
|
||||
}
|
||||
|
||||
// 创建比较表达式
|
||||
Expression comparison;
|
||||
if (propType == typeof(string) && (op == "==" || op == "!="))
|
||||
{
|
||||
// 字符串特殊处理:使用Equals方法进行不区分大小写的比较
|
||||
var equalsMethod =
|
||||
typeof(string).GetMethod("Equals", new[] { typeof(string), typeof(StringComparison) });
|
||||
var methodCall = Expression.Call(
|
||||
propAccess,
|
||||
equalsMethod,
|
||||
constant,
|
||||
Expression.Constant(StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
comparison = op == "==" ? methodCall : Expression.Not(methodCall);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 其他类型使用标准二元运算符
|
||||
comparison = op switch
|
||||
{
|
||||
">" => Expression.GreaterThan(propAccess, constant),
|
||||
"<" => Expression.LessThan(propAccess, constant),
|
||||
">=" => Expression.GreaterThanOrEqual(propAccess, constant),
|
||||
"<=" => Expression.LessThanOrEqual(propAccess, constant),
|
||||
"==" => Expression.Equal(propAccess, constant),
|
||||
"!=" => Expression.NotEqual(propAccess, constant),
|
||||
_ => throw new NotSupportedException($"Operator {op} not supported")
|
||||
};
|
||||
}
|
||||
|
||||
// 编译为委托
|
||||
var lambda = Expression.Lambda<Func<Entity.Entity, bool>>(comparison, param);
|
||||
return lambda.Compile();
|
||||
}
|
||||
}
|
||||
}
|
3
Client/Assets/Scripts/Utils/Resolver.cs.meta
Normal file
3
Client/Assets/Scripts/Utils/Resolver.cs.meta
Normal file
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: acd36958b991488f92c4b9a8b903c0be
|
||||
timeCreated: 1753012536
|
Reference in New Issue
Block a user