main.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. local imgui = require("imgui")
  2. local Action = require("node.action")
  3. local Condition = require("node.condition")
  4. local Sprite = require("sprite")
  5. local player = Sprite("Face.png",64, 64)
  6. local player2 = Sprite("Face2.png",240, 64)
  7. local grid_tile = love.graphics.newImage("grid.png")
  8. grid_tile:setFilter("nearest")
  9. local function coro_test (action)
  10. for i=0,100 do
  11. action.progress = i/100
  12. coroutine.yield( )
  13. end
  14. end
  15. local function coro_test_longer (action)
  16. for i=0,400 do
  17. action.progress = i/400
  18. coroutine.yield( )
  19. end
  20. end
  21. local function sign(x)
  22. if x<0 then
  23. return -1
  24. elseif x>0 then
  25. return 1
  26. else
  27. return 0
  28. end
  29. end
  30. function Move(player, dx, dy)
  31. local function move(action)
  32. local start_x = player.x
  33. local start_y = player.y
  34. local target_x = player.x + dx
  35. local target_y = player.y + dy
  36. local pdx = sign(target_x - start_x)
  37. local pdy = sign(target_y - start_y)
  38. repeat
  39. coroutine.yield()
  40. local walked_delta_x = math.abs(player.x - start_x)
  41. local walked_delta_y = math.abs(player.y - start_y)
  42. if walked_delta_x > math.abs(dx) then
  43. player.x = target_x
  44. walked_delta_x = dx
  45. end
  46. if walked_delta_y > math.abs(dy) then
  47. player.y = target_y
  48. walked_delta_y = dy
  49. end
  50. local px = dx ~= 0 and walked_delta_x/math.abs(dx) or 0
  51. local py = dy ~= 0 and walked_delta_y/math.abs(dy) or 0
  52. action.progress = math.max(px, py)
  53. local pdx = sign(target_x - player.x)
  54. local pdy = sign(target_y - player.y)
  55. player.dx = pdx * 60
  56. player.dy = pdy * 60
  57. until player.x == target_x and player.y == target_y
  58. player.dx = 0
  59. player.dy = 0
  60. end
  61. local action = Action(coroutine.create(move))
  62. action.name = "Move ("..dx..";"..dy..") pixels"
  63. return action
  64. end
  65. function Wait(time)
  66. local function wait(action)
  67. local dt = 0
  68. while dt <= time do
  69. coroutine.yield()
  70. dt = dt + love.timer.getDelta()
  71. action.progress = dt / time
  72. end
  73. end
  74. local action = Action(coroutine.create(wait))
  75. action.name = "Wait "..time.." seconds"
  76. return action
  77. end
  78. local array = {
  79. Move(player, 0, 72),
  80. Wait(1),
  81. Move(player, 0, -72),
  82. Wait(1),
  83. Condition("and", {Move(player, 0, 72), Move(player2, 0, 72)}),
  84. Wait(1),
  85. Condition("and", {Move(player, 0, -72), Move(player2, 0, -72)}),
  86. Wait(1),
  87. Condition("and", {Move(player, 45, 0), Move(player2, -45, 0)}),
  88. Wait(0.5),
  89. Condition("and", {Move(player, 0, 72), Move(player2, 0, 72)}),
  90. Wait(0.5),
  91. Condition("and", {Move(player, 0, -72), Move(player2, 0, -72)}),
  92. Wait(0.5),
  93. Condition("and", {Move(player, -45, 0), Move(player2, 45, 0)}),
  94. --[[
  95. Action(coroutine.create( coro_test )),
  96. Action(coroutine.create( coro_test )),
  97. Action(coroutine.create( coro_test )),
  98. Condition("and", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
  99. Condition("and", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
  100. Condition("and", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))}),
  101. Condition("or", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
  102. Condition("or", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
  103. Condition("or", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))})
  104. ]]
  105. }
  106. for i,node in ipairs(array) do node.id = i end
  107. local selected_node = nil
  108. local started = false
  109. function not_in(v,t)
  110. for k,e in ipairs(t) do
  111. if v == e then return false end
  112. end
  113. return true
  114. end
  115. function draw_array()
  116. imgui.Begin("Array")
  117. if imgui.Button("START!") then
  118. started = true
  119. end
  120. for i,node in ipairs(array) do
  121. local current_selected_node = node:draw_debug()
  122. if current_selected_node then
  123. selected_node = current_selected_node
  124. end
  125. end
  126. if selected_node then
  127. imgui.Separator()
  128. imgui.Text(selected_node.name)
  129. imgui.Text(selected_node.id)
  130. imgui.Text("Is finished "..tostring(selected_node:is_finished()))
  131. end
  132. imgui.Separator()
  133. imgui.Text("--Player--")
  134. imgui.Text("X: "..player.x)
  135. imgui.Text("Y: "..player.y)
  136. imgui.End()
  137. end
  138. local iui_state = {
  139. array={cbk = draw_array, show=true}
  140. }
  141. function love.load()
  142. end
  143. function love.update(dt)
  144. imgui.NewFrame()
  145. if started then
  146. for _,node in ipairs(array) do
  147. node:update(dt)
  148. if not node:is_finished() then break end
  149. end
  150. end
  151. player:update(dt)
  152. player2:update(dt)
  153. end
  154. function love.draw()
  155. for x=0,love.graphics.getWidth(), grid_tile:getWidth() do
  156. for y=0,love.graphics.getHeight(), grid_tile:getHeight() do
  157. love.graphics.draw(grid_tile, x, y, 0, 1, 1)
  158. end
  159. end
  160. player:draw()
  161. player2:draw()
  162. if imgui.BeginMainMenuBar() then
  163. if imgui.BeginMenu("Test") then
  164. for k,v in pairs(iui_state) do
  165. if imgui.MenuItem(k, nil, v.show) then
  166. iui_state[k].show = not iui_state[k].show
  167. end
  168. end
  169. imgui.EndMenu()
  170. end
  171. end
  172. imgui.EndMainMenuBar()
  173. for k,v in pairs(iui_state) do
  174. if v.show then v.cbk() end
  175. end
  176. imgui.Render()
  177. end
  178. function love.quit()
  179. imgui.ShutDown()
  180. end
  181. --
  182. -- User inputs
  183. --
  184. function love.textinput(t)
  185. imgui.TextInput(t)
  186. if not imgui.GetWantCaptureKeyboard() then
  187. -- Pass event to the game
  188. end
  189. end
  190. function love.keypressed(key)
  191. imgui.KeyPressed(key)
  192. if not imgui.GetWantCaptureKeyboard() then
  193. -- Pass event to the game
  194. end
  195. end
  196. function love.keyreleased(key)
  197. imgui.KeyReleased(key)
  198. if not imgui.GetWantCaptureKeyboard() then
  199. -- Pass event to the game
  200. end
  201. end
  202. function love.mousemoved(x, y)
  203. imgui.MouseMoved(x, y)
  204. if not imgui.GetWantCaptureMouse() then
  205. -- Pass event to the game
  206. end
  207. end
  208. function love.mousepressed(x, y, button)
  209. imgui.MousePressed(button)
  210. if not imgui.GetWantCaptureMouse() then
  211. -- Pass event to the game
  212. end
  213. end
  214. function love.mousereleased(x, y, button)
  215. imgui.MouseReleased(button)
  216. if not imgui.GetWantCaptureMouse() then
  217. -- Pass event to the game
  218. end
  219. end
  220. function love.wheelmoved(x, y)
  221. imgui.WheelMoved(y)
  222. if not imgui.GetWantCaptureMouse() then
  223. -- Pass event to the game
  224. end
  225. end