action.lua 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local Node = require("node")
  2. local Action = Node:extend"Action"
  3. function Action:init(coro)
  4. Node.init(self)
  5. self.coroutine = coro
  6. self.progress = nil -- progress ∈ [0;1]
  7. end
  8. function Action:update(dt)
  9. if self:is_finished() then return end
  10. local res,error = coroutine.resume( self.coroutine, self)
  11. if error then print(error) end
  12. end
  13. function Action:is_finished()
  14. if not self.coroutine then return true end
  15. return coroutine.status(self.coroutine) == "dead"
  16. end
  17. function Action:draw_debug()
  18. local selected_node = nil
  19. local is_finished = self:is_finished()
  20. local alpha = is_finished and 0.1 or 0.3
  21. imgui.PushStyleColor("Header", 1,1,1, alpha)
  22. imgui.PushStyleColor("HeaderHovered", 1,1,1, alpha + 0.1)
  23. imgui.PushStyleColor("HeaderActive", 1,1,1, alpha + 0.2)
  24. local name = self.name or "Action "..self.id
  25. if imgui.TreeNodeEx(name, {"Framed"}) then
  26. if self.progress and not is_finished then
  27. imgui.SameLine()
  28. imgui.ProgressBar(self.progress)
  29. end
  30. --[[
  31. if self.coroutine then
  32. imgui.Text(coroutine.status(self.coroutine))
  33. else
  34. imgui.Text("-No action-")
  35. end
  36. ]]
  37. imgui.TreePop()
  38. elseif self.progress and not is_finished then
  39. imgui.SameLine()
  40. imgui.ProgressBar(self.progress)
  41. end
  42. imgui.PopStyleColor(2)
  43. return selected_node
  44. end
  45. return Action