Treesharp C#行为树库 - 从哪里开始?

0 c# tree behavior-tree

最近,我一直在尝试使用apoc发布treesharp库来实现健壮的行为树.我一直在阅读我书中的迭代器和接口,但我仍然无法弄清楚如何测试更不用说使用这个库了.接口如何与彼此连接以及如何用它们实际执行测试/构建树是令我感到困惑的.

通常在这种情况下,我会查找代码示例并从查看其他人的工作中获得启发,但是,对于此库,似乎没有任何示例代码.

任何人都可以帮我弄清楚如何使用这个库开始构建行为树?我很抱歉,如果这个问题非常无趣(我认为可能是这样),但接口中的枚举器和渐进式接口对我来说现在非常难以理解.

小智 12

我是TreeSharp的作者,如果你们有任何问题,请随时给我发一封电子邮件(它包含在标题中的每个源文件中).

您首先需要了解行为树的概念(选择器,序列,装饰器,操作等之间的差异).我还提供了一些"虚荣"复合材料,使事情稍微容易些(比如等待).

基于构造函数的API允许您完全通过ctors定义树(使用在运行时评估的委托来提供决策等)

不幸的是,我从来没有实现"TreeExecutor"类,它处理从Tick()方法执行任意行为分支.最简单的方法(在此示例中使用PrioritySelector,但您可以使用任何复合)如下所示;

    static void Start()
    {
        // Start MUST be called before you can tick the tree.
        Logic.Start(null);
        // do work to spool up a thread, or whatever to call Tick();
    }

    private static void Tick()
    {
        try
        {             
            Logic.Tick(null);
            // If the last status wasn't running, stop the tree, and restart it.
            if (Logic.LastStatus != RunStatus.Running)
            {
                Logic.Stop(null);
                Logic.Start(null);
            }
        }
        catch (Exception e)
        {
            // Restart on any exception.
            Logging.WriteException(e);
            Logic.Stop(null);
            Logic.Start(null);
            throw;
        }
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,提供其使用的"示例"实际上取决于您使用它的内容.(由于它如此通用,很难给出对任何给定项目都有意义的例子.我一直在使用它从事物到AI逻辑,工作流程,到调度过程)

一个可能有所帮助的小例子;

    static Composite CreateFireMissile()
    {
        return new PrioritySelector(
            new Decorator(ret => CurrentShip.CurrentTarget != null,
                new Action(ret => CurrentShip.CurrentTarget.FireMissile())),

            new Decorator(ret => CurrentShip.CurrentTarget == null,
                new Decorator(ret => CurrentShip.NearbyHostiles.Count > 0,
                    new Sequence(
                        new Action(ret => CurrentShip.SetTarget(CurrentShip.NearbyHostiles[0])),
                        new Action(ret => CurrentShip.RotateTo(CurrentShip.CurrentTarget.Location))
                        )
                    )
                )       
        );
    }
Run Code Online (Sandbox Code Playgroud)

同样,这实际上取决于您的要求.该库将允许您对任何复合材料进行子类化,以便更轻松地重复使用复合材料.(例如;您可以创建一个SetTargetAndRotate操作,它可以消除Sequence中的两个操作)

再说一次,如果你们有疑问,请不要犹豫.