GithubHelp home page GithubHelp logo

luabt's Introduction

Lua 行为树

依赖

支持节点类型

  • Failed:总是返回FAIL
  • Invert:反转执行结果
  • Succeed: 总是返回成功
  • Random:随机选择子节点执行
  • Repeat:重复执行子节点,需要注意的是每次重复会跳帧
  • Select:顺序执行,遇到成功返回
  • Sequence:顺序执行,遇到失败返回
  • WSelect:顺序执行,遇到成功返回,子节点按权重先排序
  • WSequence:顺序执行,遇到失败返回,子节点按权重先排序
  • Condition:条件节点,根据条件执行子节点
  • Parallel:并行节点,根据设置的成功和失败条件返回

实现原理

  • 节点状态
luabt = {
    -- Node Status
    WAITING     = 0,    --等待,中间状态,展开子节点返回
    SUCCESS     = 1,    --成功
    FAIL        = 2,    --失败
    RUNNING     = 3,    --运行
}
  • 每次tick从root节点一次展开执行,遇到FAIL或者全部SUCCESS则reset
  • 遇到RUNNING,本次tick结束,下一次从RUNNING继续执行
  • 支持中断机制,中断达成后,回到中断节点继续执行

扩展节点

  • 修饰节点Succeed, Failed, Invert:子节点从框架节点继承,同时实现on_execute接口
function SucceedNode:on_execute(tree)
    return SUCCESS
end
  • 条件节点Condition:子节点从框架节点继承,同时实现on_check接口
function ConditionNode:on_check(tree)
    return true
end
  • 循环节点Repeat:子节点从框架节点继承,同时实现on_check接口控制循环
function RepeatNode:on_check(tree)
    return true
end
  • 普通节点Node:子节点从框架Node节点继承,同时实现run接口
function Flee:run(tree)
    tree.robot.hp = tree.robot.hp + 2
    print(tree.robot.hp, "Flee.....")
    return SUCCESS
end
  • 中断节点:任意节点可以成为中断节点,只需要实现on_interrupt接口
function BtNode:on_interrupt(tree)
    return true
end
  • 组合节点:直接使用系统提供的组合节点

Example

备注: 需要先下载luaoop

lua example/example.lua

luabt's People

Contributors

xiyoo0812 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.