| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- local imgui = require("imgui")
- local Action = require("node.action")
- local Condition = require("node.condition")
- local Sprite = require("sprite")
- local player = Sprite("Face.png",64, 64)
- local player2 = Sprite("Face2.png",240, 64)
- local grid_tile = love.graphics.newImage("grid.png")
- grid_tile:setFilter("nearest")
- local function coro_test (action)
- for i=0,100 do
- action.progress = i/100
- coroutine.yield( )
- end
- end
- local function coro_test_longer (action)
- for i=0,400 do
- action.progress = i/400
- coroutine.yield( )
- end
- end
- local function sign(x)
- if x<0 then
- return -1
- elseif x>0 then
- return 1
- else
- return 0
- end
- end
- function Move(player, dx, dy)
- local function move(action)
- local start_x = player.x
- local start_y = player.y
- local target_x = player.x + dx
- local target_y = player.y + dy
- local pdx = sign(target_x - start_x)
- local pdy = sign(target_y - start_y)
- repeat
- coroutine.yield()
- local walked_delta_x = math.abs(player.x - start_x)
- local walked_delta_y = math.abs(player.y - start_y)
- if walked_delta_x > math.abs(dx) then
- player.x = target_x
- walked_delta_x = dx
- end
- if walked_delta_y > math.abs(dy) then
- player.y = target_y
- walked_delta_y = dy
- end
- local px = dx ~= 0 and walked_delta_x/math.abs(dx) or 0
- local py = dy ~= 0 and walked_delta_y/math.abs(dy) or 0
- action.progress = math.max(px, py)
- local pdx = sign(target_x - player.x)
- local pdy = sign(target_y - player.y)
- player.dx = pdx * 60
- player.dy = pdy * 60
- until player.x == target_x and player.y == target_y
- player.dx = 0
- player.dy = 0
- end
- local action = Action(coroutine.create(move))
- action.name = "Move ("..dx..";"..dy..") pixels"
- return action
- end
- function Wait(time)
- local function wait(action)
- local dt = 0
- while dt <= time do
- coroutine.yield()
- dt = dt + love.timer.getDelta()
- action.progress = dt / time
- end
- end
- local action = Action(coroutine.create(wait))
- action.name = "Wait "..time.." seconds"
- return action
- end
- local array = {
- Move(player, 0, 72),
- Wait(1),
- Move(player, 0, -72),
- Wait(1),
- Condition("and", {Move(player, 0, 72), Move(player2, 0, 72)}),
- Wait(1),
- Condition("and", {Move(player, 0, -72), Move(player2, 0, -72)}),
- Wait(1),
- Condition("and", {Move(player, 45, 0), Move(player2, -45, 0)}),
- Wait(0.5),
- Condition("and", {Move(player, 0, 72), Move(player2, 0, 72)}),
- Wait(0.5),
- Condition("and", {Move(player, 0, -72), Move(player2, 0, -72)}),
- Wait(0.5),
- Condition("and", {Move(player, -45, 0), Move(player2, 45, 0)}),
- --[[
- Action(coroutine.create( coro_test )),
- Action(coroutine.create( coro_test )),
- Action(coroutine.create( coro_test )),
- Condition("and", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
- Condition("and", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
- Condition("and", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))}),
- Condition("or", {Action(coroutine.create( coro_test )), Action(coroutine.create( coro_test_longer ))}),
- Condition("or", {Action(), Action(coroutine.create(function() coroutine.yield() end))}),
- Condition("or", {Action(coroutine.create(function() coroutine.yield() end)), Action(coroutine.create(function() coroutine.yield() end))})
- ]]
- }
- for i,node in ipairs(array) do node.id = i end
- local selected_node = nil
- local started = false
- function not_in(v,t)
- for k,e in ipairs(t) do
- if v == e then return false end
- end
- return true
- end
- function draw_array()
- imgui.Begin("Array")
- if imgui.Button("START!") then
- started = true
- end
- for i,node in ipairs(array) do
- local current_selected_node = node:draw_debug()
- if current_selected_node then
- selected_node = current_selected_node
- end
- end
- if selected_node then
- imgui.Separator()
- imgui.Text(selected_node.name)
- imgui.Text(selected_node.id)
- imgui.Text("Is finished "..tostring(selected_node:is_finished()))
- end
- imgui.Separator()
- imgui.Text("--Player--")
- imgui.Text("X: "..player.x)
- imgui.Text("Y: "..player.y)
- imgui.End()
- end
- local iui_state = {
- array={cbk = draw_array, show=true}
- }
- function love.load()
- end
- function love.update(dt)
- imgui.NewFrame()
- if started then
- for _,node in ipairs(array) do
- node:update(dt)
- if not node:is_finished() then break end
- end
- end
- player:update(dt)
- player2:update(dt)
- end
- function love.draw()
- for x=0,love.graphics.getWidth(), grid_tile:getWidth() do
- for y=0,love.graphics.getHeight(), grid_tile:getHeight() do
- love.graphics.draw(grid_tile, x, y, 0, 1, 1)
- end
- end
- player:draw()
- player2:draw()
- if imgui.BeginMainMenuBar() then
- if imgui.BeginMenu("Test") then
- for k,v in pairs(iui_state) do
- if imgui.MenuItem(k, nil, v.show) then
- iui_state[k].show = not iui_state[k].show
- end
- end
- imgui.EndMenu()
- end
- end
- imgui.EndMainMenuBar()
- for k,v in pairs(iui_state) do
- if v.show then v.cbk() end
- end
- imgui.Render()
- end
- function love.quit()
- imgui.ShutDown()
- end
- --
- -- User inputs
- --
- function love.textinput(t)
- imgui.TextInput(t)
- if not imgui.GetWantCaptureKeyboard() then
- -- Pass event to the game
- end
- end
- function love.keypressed(key)
- imgui.KeyPressed(key)
- if not imgui.GetWantCaptureKeyboard() then
- -- Pass event to the game
- end
- end
- function love.keyreleased(key)
- imgui.KeyReleased(key)
- if not imgui.GetWantCaptureKeyboard() then
- -- Pass event to the game
- end
- end
- function love.mousemoved(x, y)
- imgui.MouseMoved(x, y)
- if not imgui.GetWantCaptureMouse() then
- -- Pass event to the game
- end
- end
- function love.mousepressed(x, y, button)
- imgui.MousePressed(button)
- if not imgui.GetWantCaptureMouse() then
- -- Pass event to the game
- end
- end
- function love.mousereleased(x, y, button)
- imgui.MouseReleased(button)
- if not imgui.GetWantCaptureMouse() then
- -- Pass event to the game
- end
- end
- function love.wheelmoved(x, y)
- imgui.WheelMoved(y)
- if not imgui.GetWantCaptureMouse() then
- -- Pass event to the game
- end
- end
|