提交确定性AI案例

This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2024-11-15 19:21:14 +08:00
parent ae81ee6c84
commit a0a8751aa5
47 changed files with 18242 additions and 4024 deletions

View File

@@ -339,6 +339,12 @@ namespace BehaviorTreeSlayerEditor
GUI.Label(lbRect, Msg);
GUI.BeginGroup(systemRect, "", "box");
int topBtnWidth = 0;
if (GUI.Button(new Rect(topBtnWidth, 0, 120, 30), "Load", TopBtnStyle))
{
LoadFromFile(); // 调用读取文件的方法
}
topBtnWidth += 135;
if (GUI.Button(new Rect(topBtnWidth, 0, 120, 30), "Save", TopBtnStyle))
{
Save();
@@ -393,6 +399,22 @@ namespace BehaviorTreeSlayerEditor
this.Repaint();
}
}
private void LoadFromFile()
{
string path = EditorUtility.OpenFilePanel("Load Behavior Config", "", "txt");
if (!string.IsNullOrEmpty(path))
{
// 获取相对路径
string relativePath = "Assets" + path.Substring(Application.dataPath.Length);
// 使用 AssetDatabase 加载 TextAsset
TextAsset textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(relativePath);
BTreeManager.Ins.Init(textAsset.text);
treeConfig = textAsset;
TryOpen();
Msg = "Loaded from " + path;
}
}
private void Save()
{

View File

@@ -89,7 +89,14 @@ namespace BehaviorTreeSlayer
public Entry Load()
{
if (Entry == null && config != null)
if (Application.isPlaying)
{
if (Entry == null && config != null)
{
Entry = XmlUtils.DeSerialize<Entry>(config.text);
}
}
else
{
Entry = XmlUtils.DeSerialize<Entry>(config.text);
}

View File

@@ -21,6 +21,21 @@ namespace BehaviorTreeSlayer
public virtual void Exit(object args)
{
}
public virtual TaskResult Tick(LFloat dt, object args = null)
{
return TaskResult.OK;
}
public virtual void Reset()
{
state = TaskResult.None;
}
public virtual void VisitTree(TreeNode node, System.Action<TreeNode> action)
{
}
#if UNITY_EDITOR
public TreeNode Copy(TreeNode node)
{
TreeNode t = this.Clone() as TreeNode;
@@ -38,24 +53,12 @@ namespace BehaviorTreeSlayer
}
return t;
}
public virtual TaskResult Tick(LFloat dt, object args = null)
{
return TaskResult.OK;
}
public virtual void Reset()
{
state = TaskResult.None;
}
TreeNode Clone()
{
return Activator.CreateInstance(this.GetType()) as TreeNode;
}
public virtual void VisitTree(TreeNode node, System.Action<TreeNode> action)
{
}
#endif
}
}

View File

@@ -5,6 +5,7 @@ using JNGame.Runtime.Util;
namespace BehaviorTreeSlayer
{
/// <summary>
/// 运行时 行为
/// </summary>
@@ -40,7 +41,39 @@ namespace BehaviorTreeSlayer
/// 事件
/// </summary>
Dictionary<string, Action<object>> actions = new Dictionary<string, Action<object>>();
/// <summary>
/// 参数
/// </summary>
public Dictionary<string, object> Args = new Dictionary<string, object>();
//------------------ 获取参数 ----------------------------------
public object this[string key]
{
get => Args.TryGetValue(key, out object v) ? v : null;
set => Args[key] = value;
}
public T Get<T>(string key)
{
return (T)this[key];
}
public T Get<T>()
{
foreach (var keyValuePair in Args)
{
if (keyValuePair.Value is T value)
{
return value;
}
}
return default;
}
//------------------ 结束获取参数 ----------------------------------
public void OnInit(int seed,string config)
{
@@ -59,19 +92,10 @@ namespace BehaviorTreeSlayer
}
}
public void Regist(string key, Action<object> onEvent)
{
if (actions.ContainsKey(key))
{
actions[key] = onEvent;
}
else
{
actions.Add(key, onEvent);
}
actions[key] = onEvent;
}
public void UnRegist(string key)
@@ -89,9 +113,12 @@ namespace BehaviorTreeSlayer
return RandomFloat(LFloat.L0,LFloat.L1);
}
public void Update()
public void Update(LFloat dt)
{
if (IsInit)
{
Entry.Tick(dt,this);
}
}
}

View File

@@ -43,7 +43,6 @@ namespace BehaviorTreeSlayer
}
static Type[] types;
public static Type[] Types
{
get
@@ -56,6 +55,7 @@ namespace BehaviorTreeSlayer
}
return types;
}
}
// public static string XmlSerialize<T>(T obj)

View File

@@ -57,7 +57,13 @@ namespace GAS.Runtime
public abstract void OnRemove(int frame,int startFrame,int endFrame);
public abstract void OnGameplayEffectActivate();
public abstract void OnGameplayEffectDeactivate();
/// <summary>
/// 只有Cue附加在实体中时会触发
/// </summary>
public abstract void OnTick(int frame,int startFrame,int endFrame);
/// <summary>
/// 如果不在实体中则自己主动调用
/// </summary>
public abstract void OnTick(int deltaTime);
}