condition.lua 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. local Node = require("node")
  2. local Condition = Node:extend"Condition"
  3. local default_routine = function() return true end
  4. local and_routine = function(accumulator, current_node) return accumulator and current_node:is_finished() end
  5. local or_routine = function(accumulator, current_node) return accumulator or current_node:is_finished() end
  6. function Condition:init(condition, nodes)
  7. Node.init(self)
  8. self.nodes = nodes
  9. self.condition = condition
  10. self.selected_condition = default_routine
  11. self.num_done = 0
  12. if self.condition == "or" then self.selected_condition = or_routine
  13. elseif self.condition == "and" then self.selected_condition = and_routine end
  14. end
  15. function Condition:update(dt)
  16. if self:is_finished() then return end
  17. for _,node in ipairs(self.nodes) do
  18. node:update(dt)
  19. end
  20. end
  21. function Condition:is_finished()
  22. if #self.nodes == 0 then return true end
  23. self.num_done = 0
  24. local accumulator = self.nodes[1]:is_finished()
  25. if self.nodes[1]:is_finished() then self.num_done = self.num_done + 1 end
  26. local i = 2
  27. while i <= #self.nodes do
  28. accumulator = self.selected_condition(accumulator, self.nodes[i])
  29. if self.nodes[i]:is_finished() then self.num_done = self.num_done + 1 end
  30. i = i + 1
  31. end
  32. return accumulator
  33. end
  34. function Condition:draw_debug()
  35. local selected_node = nil
  36. local is_finished = self:is_finished()
  37. local alpha = is_finished and 0.1 or 0.3
  38. local color = {0,0,0}
  39. if self.condition == "or" then
  40. color = {1,0,0}
  41. elseif self.condition == "and" then
  42. color = {0,1,0}
  43. end
  44. imgui.PushStyleColor("Header",color[1],color[2],color[3], alpha)
  45. imgui.PushStyleColor("HeaderHovered",color[1],color[2],color[3], alpha+0.1)
  46. imgui.PushStyleColor("HeaderActive",color[1],color[2],color[3], alpha+0.2)
  47. if imgui.TreeNodeEx(string.upper(self.condition).." Condition "..self.id, {"Framed"}) then
  48. if #self.nodes > 0 and self.num_done > 0 and not is_finished then
  49. imgui.SameLine()
  50. imgui.ProgressBar((self.num_done+0.0)/#self.nodes)
  51. end
  52. -- imgui.Text("Finished "..tostring(self:is_finished()))
  53. imgui.PopStyleColor(1)
  54. for k,node in ipairs(self.nodes) do
  55. local current_selected_node = node:draw_debug()
  56. if current_selected_node then
  57. selected_node = current_selected_node
  58. end
  59. end
  60. imgui.TreePop()
  61. elseif #self.nodes > 0 and self.num_done > 0 and not is_finished then
  62. imgui.SameLine()
  63. imgui.ProgressBar(self.num_done/#self.nodes)
  64. end
  65. if imgui.IsItemClicked() then
  66. selected_node = self
  67. end
  68. imgui.PopStyleColor(1)
  69. return selected_node
  70. end
  71. return Condition