zhandouxiaojiji / behavior3lua Goto Github PK
View Code? Open in Web Editor NEWbehavior tree for lua
License: MIT License
behavior tree for lua
License: MIT License
我有一个这样的节点,给定一个目标位置,可以寻路到目标点,我发现在执行这段逻辑的时候会有比较频繁的lua gc,我在想是不是因为我对node的set_var和get_var使用不正确导致的?
--移动到目标位置
--input vec3 targetPos
local bret = require "Framework/Behavior3Lua/behavior_ret"
local AStarHelper = require("Common/AStarHelper")
local abs = math.abs
local SPEED = 10
return function (node,targetPos)
local env = node.env
local owner = env.owner
local args = node.args
local vec3Start = owner.isoObjCmp.position
local vec3End = targetPos
if(abs(vec3Start.x - vec3End.x) < 0.01 and abs(vec3Start.y - vec3End.y) < 0.01) then
--已经在目的地了
return bret.SUCCESS
end
local pathList = node:get_var("PATH")
if pathList == nil then
--这里查找路径点
local pathList = AStarHelper.FindPath(owner.tileMap,owner.isoWorldCmp,vec3Start,vec3End)
if(table.length(pathList) == 0) then
--没有路径
node:set_var("PATH", nil)
return bret.SUCCESS
end
node:set_var("PATH", pathList) --记录全部路径列表
node:set_var("INDEX", 0) --记录当前在路径列表的那个下标
node:set_var("COUNT",table.length(pathList)) --记录总的路径列表长度
node:set_var("STEP_START_POS",pathList[0]) --记录当前路径的起点
node:set_var("STEP_SRAT_TIME",Time.time) --记录当前路径的起始时间
local dis = Vector3.Distance(vec3Start,pathList[0])
node:set_var("TOTAL_TIME", dis / SPEED) --记录当前路径移动的总时间
end
pathList = node:get_var("PATH")
local index = node:get_var("INDEX")
local count = node:get_var("COUNT")
local step_start_pos = node:get_var("STEP_START_POS")
local step_start_time = node:get_var("STEP_SRAT_TIME")
local TotalTime = node:get_var("TOTAL_TIME")
local target = pathList[index]
--到达一个目标点,就切换到下一个点
if(abs(owner.isoObjCmp.position.x - target.x) < 0.01 and abs(owner.isoObjCmp.position.y - target.y) < 0.01) then
--print("move cur point reach target")
local index = node:get_var("INDEX")
if index == count then
node:set_var("PATH", nil)
return bret.SUCCESS
else
local current = pathList[index]
index = index + 1
target = pathList[index]
local dis = Vector3.Distance(current,target)
local totalTime = dis / SPEED
node:set_var("INDEX", index)
node:set_var("TOTAL_TIME", totalTime)
node:set_var("STEP_SRAT_TIME",Time.time)
node:set_var("STEP_START_POS",current)
owner.spineCmp:SetFace(target - owner.isoObjCmp.position)
owner.spineCmp:Play("move",true)
return bret.RUNNING
end
end
owner.isoObjCmp.position = Vector3.Lerp(step_start_pos,target,(Time.time - step_start_time) / TotalTime)
return bret.RUNNING
end
能否调整下 目录结构
目前 库文件 都在 lualib 目录下
这种结构 不好 放到工程里使用 还得 改一次路径
能否把他们合在一个 目录里
比如Sequence节点,如果其第一个孩子节点第一次返回是running状态,那本次tick直接返回了,下次tick进来后会拿上一次的返回结果判断,如果是running则又直接return了,那这里岂不是死循环了? 因为后续不会去执行running节点了。
local bret = require 'behavior3.behavior_ret'
local M = {
name = 'Sequence',
type = 'Composite',
desc = '顺序执行',
doc = [[
+ 一直往下执行,有子节点返回成功则返回成功,若全部节点返回失败则返回失败
+ 子节点是或的关系
]]
}
function M.run(node, env)
local last_idx, last_ret = node:resume(env)
if last_idx then
-- print("last", last_idx, last_ret)
if last_ret == bret.FAIL or bret.RUNNING then
return last_ret
elseif last_ret == bret.SUCCESS then
last_idx = last_idx + 1
else
error('wrong ret')
end
else
last_idx = 1
end
for i = last_idx, #node.children do
local child = node.children[i]
local r = child:run(env)
if r == bret.RUNNING then
return node:yield(env, i)
end
if r == bret.FAIL then
return r
end
end
return bret.SUCCESS
end
return M
请问下 coroutine 分支 为什么不合并到主干 感觉 coroutine 比较符合 lua 的风格
行为树中 ifelse and or 节点 挺 常用的 可否 支持 下 ?
local M = {
name = 'IfElse',
type = 'Composite',
desc = 'IfElse',
doc = [[
+ 拥有三个子节点
+ 当第一个子节点返回true的时候执行第二个子节点并返回此子节点的返回值
+ 否则执行第三个节点并返回这个节点的返回值
]]
}
local M = {
name = 'AND',
type = 'Decorator',
desc = '始终返回失败',
doc = [[
+ 拥有两个子节点
+ 同时执行按 AND 处理
]]
}
local M = {
name = 'OR',
type = 'Decorator',
desc = '始终返回失败',
doc = [[
+ 拥有两个子节点
+ 同时执行按 OR 处理
]]
}
可以添加一部分调试部分的逻辑?不太懂你指的在编辑器里怎么打的标志
我感觉可以去递归一下父节点描述+子节点描述+参数 来打不同颜色的日志
例如:Selector (英雄AI)-->FindEnemy(查找敌人范围,args{w=1000,h=500})-->output XEnemy
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.