action.lua 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. coroutine.resume( self.coroutine, self)
  11. end
  12. function Action:is_finished()
  13. if not self.coroutine then return true end
  14. return coroutine.status(self.coroutine) == "dead"
  15. end
  16. function Action:draw_debug()
  17. local selected_node = nil
  18. local is_finished = self:is_finished()
  19. local alpha = is_finished and 0.1 or 0.3
  20. imgui.PushStyleColor("Header", 1,1,1, alpha)
  21. imgui.PushStyleColor("HeaderHovered", 1,1,1, alpha + 0.1)
  22. imgui.PushStyleColor("HeaderActive", 1,1,1, alpha + 0.2)
  23. if imgui.TreeNodeEx("Action "..self.id, {"Framed"}) then
  24. if self.progress and not is_finished then
  25. imgui.SameLine()
  26. imgui.ProgressBar(self.progress)
  27. end
  28. if self.coroutine then
  29. imgui.Text(coroutine.status(self.coroutine))
  30. else
  31. imgui.Text("-No action-")
  32. end
  33. imgui.TreePop()
  34. elseif self.progress and not is_finished then
  35. imgui.SameLine()
  36. imgui.ProgressBar(self.progress)
  37. end
  38. imgui.PopStyleColor(2)
  39. return selected_node
  40. end
  41. return Action