|
|
@@ -1,6 +1,12 @@
|
|
|
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
|
|
|
@@ -16,21 +22,102 @@ local function coro_test_longer (action)
|
|
|
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 = {
|
|
|
- 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))})
|
|
|
+ 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
|
|
|
@@ -41,22 +128,25 @@ end
|
|
|
|
|
|
function draw_array()
|
|
|
imgui.Begin("Array")
|
|
|
- imgui.BeginChild("", 0,0, true)
|
|
|
- imgui.Columns(2)
|
|
|
+ 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
|
|
|
- imgui.NextColumn()
|
|
|
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.Button("OK")
|
|
|
- imgui.EndChild()
|
|
|
+ imgui.Separator()
|
|
|
+ imgui.Text("--Player--")
|
|
|
+ imgui.Text("X: "..player.x)
|
|
|
+ imgui.Text("Y: "..player.y)
|
|
|
imgui.End()
|
|
|
end
|
|
|
|
|
|
@@ -72,13 +162,28 @@ end
|
|
|
|
|
|
function love.update(dt)
|
|
|
imgui.NewFrame()
|
|
|
- for _,node in ipairs(array) do
|
|
|
- node:update(dt)
|
|
|
- if not node:is_finished() then break end
|
|
|
+ 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
|