BehaviorTree.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ---
  2. --- Generated by EmmyLua(https://github.com/EmmyLua)
  3. --- Created by xxn.
  4. --- DateTime: 2021/1/7 14:30
  5. ---
  6. ---@class BehaviorTree
  7. BehaviorTree = class()
  8. local this = BehaviorTree
  9. ---@type table<number,BehaviorTree>
  10. this.bts = {}
  11. ---@param root BehaviorNode
  12. function BehaviorTree:ctor(root, owner)
  13. self.root = root --行为树根节点
  14. self.root:SetOwner(owner)
  15. end
  16. function this.UpdateAll()
  17. for k, v in pairs(this.bts) do
  18. v:Update()
  19. end
  20. end
  21. function BehaviorTree:Update()
  22. self.root:Visit()
  23. self:HandleStatus()
  24. self.root:SaveStatus()
  25. self.root:Step()
  26. end
  27. function BehaviorTree:Reset()
  28. self.root:Reset()
  29. end
  30. function BehaviorTree:Stop()
  31. self.root:Stop()
  32. end
  33. function BehaviorTree:HandleStatus()
  34. if self.statusCallback and self.root.status == self.status then
  35. self.statusCallback()
  36. end
  37. end
  38. function BehaviorTree:SetStateCallback(callback, status)
  39. self.statusCallback = callback
  40. self.status = status
  41. end
  42. function BehaviorTree:Start()
  43. --local function StartBehaviorTree()
  44. --
  45. -- while true do
  46. -- self:Update()
  47. -- Coroutine.WaitForEndOfFrame()
  48. -- end
  49. --
  50. --end
  51. if not table.contains(this.bts,self) then
  52. table.insert(this.bts,self)
  53. end
  54. self.isStart = true
  55. --if self.loopBehavior == nil then
  56. -- self.loopBehavior = Coroutine.Start(StartBehaviorTree)
  57. --end
  58. end
  59. function BehaviorTree:Cancel()
  60. self:Reset()
  61. self.isStart = false
  62. --if self.loopBehavior then Coroutine.Stop(self.loopBehavior) self.loopBehavior = nil end
  63. table.removeByValue(this.bts,self)
  64. end
  65. function BehaviorTree:IsUpdateIn()
  66. --return self.loopBehavior and true or false
  67. return self.isStart
  68. end