condition.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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("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("Condition "..string.upper(self.condition))
  53. imgui.Text("Finished "..tostring(self:is_finished()))
  54. imgui.PopStyleColor(1)
  55. for k,node in ipairs(self.nodes) do
  56. local current_selected_node = node:draw_debug()
  57. if current_selected_node then
  58. selected_node = current_selected_node
  59. end
  60. end
  61. imgui.TreePop()
  62. elseif #self.nodes > 0 and self.num_done > 0 and not is_finished then
  63. imgui.SameLine()
  64. imgui.ProgressBar(self.num_done/#self.nodes)
  65. end
  66. if imgui.IsItemClicked() then
  67. selected_node = self
  68. end
  69. imgui.PopStyleColor(1)
  70. return selected_node
  71. end
  72. return Condition