Florian Dormont пре 8 година
родитељ
комит
5dc122caf6
8 измењених фајлова са 189 додато и 27 уклоњено
  1. 19 0
      .vscode/tasks.json
  2. BIN
      Face.png
  3. BIN
      Face2.png
  4. BIN
      grid.png
  5. 122 17
      main.lua
  6. 11 7
      node/action.lua
  7. 2 3
      node/condition.lua
  8. 35 0
      sprite.lua

+ 19 - 0
.vscode/tasks.json

@@ -0,0 +1,19 @@
+{
+    // See https://go.microsoft.com/fwlink/?LinkId=733558
+    // for the documentation about the tasks.json format
+    "version": "2.0.0",
+    "tasks": [
+        {
+            "label": "love",
+            "type": "shell",
+            "command": "lovec",
+            "args": [
+                "."
+            ],
+            "group": {
+                "kind": "build",
+                "isDefault": true
+            }
+        }
+    ]
+}




+ 122 - 17
main.lua

@@ -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

+ 11 - 7
node/action.lua

@@ -10,7 +10,8 @@ end
 
 function Action:update(dt)
     if self:is_finished() then return end
-    coroutine.resume( self.coroutine, self)
+    local res,error = coroutine.resume( self.coroutine, self)
+    if error then print(error) end
 end
 
 function Action:is_finished()
@@ -25,16 +26,19 @@ function Action:draw_debug()
     imgui.PushStyleColor("Header", 1,1,1, alpha)
     imgui.PushStyleColor("HeaderHovered", 1,1,1, alpha + 0.1)
     imgui.PushStyleColor("HeaderActive", 1,1,1, alpha + 0.2)
-    if imgui.TreeNodeEx("Action "..self.id, {"Framed"}) then
+    local name = self.name or "Action "..self.id
+    if imgui.TreeNodeEx(name, {"Framed"}) then
         if self.progress and not is_finished then
             imgui.SameLine()
             imgui.ProgressBar(self.progress)
         end
-        if self.coroutine then
-            imgui.Text(coroutine.status(self.coroutine))
-        else
-            imgui.Text("-No action-")
-        end
+        --[[
+            if self.coroutine then
+                imgui.Text(coroutine.status(self.coroutine))
+            else
+                imgui.Text("-No action-")
+            end
+        ]]
         imgui.TreePop()
     elseif self.progress and not is_finished then
         imgui.SameLine()

+ 2 - 3
node/condition.lua

@@ -54,13 +54,12 @@ function Condition:draw_debug()
     imgui.PushStyleColor("HeaderHovered",color[1],color[2],color[3], alpha+0.1)
     imgui.PushStyleColor("HeaderActive",color[1],color[2],color[3], alpha+0.2)
 
-    if imgui.TreeNodeEx("Condition "..self.id, {"Framed"}) then
+    if imgui.TreeNodeEx(string.upper(self.condition).." Condition "..self.id, {"Framed"}) then
         if #self.nodes > 0 and self.num_done > 0 and not is_finished then
             imgui.SameLine()
             imgui.ProgressBar((self.num_done+0.0)/#self.nodes)
         end
-        imgui.Text("Condition "..string.upper(self.condition))
-        imgui.Text("Finished "..tostring(self:is_finished()))
+        -- imgui.Text("Finished "..tostring(self:is_finished()))
         imgui.PopStyleColor(1)
         for k,node in ipairs(self.nodes) do
             local current_selected_node = node:draw_debug()

+ 35 - 0
sprite.lua

@@ -0,0 +1,35 @@
+local class = require("30log")
+
+local Sprite = class"Sprite"
+
+function Sprite:init(sprite, x, y)
+    local sprite_type = type(sprite)
+    if sprite_type == "string" then
+        self.sprite = love.graphics.newImage(sprite)
+    elseif sprite_type == "userdata" then
+        self.sprite = sprite
+    else
+        self.sprite = nil
+    end
+    if self.sprite then
+        self.sprite:setFilter("nearest")
+    end
+    self.x = x or 0
+    self.y = y or 0
+    self.sx=2
+    self.sy=2
+    self.dx=0
+    self.dy=0
+end
+
+function Sprite:update(dt)
+    self.x = self.x + self.dx * dt
+    self.y = self.y + self.dy * dt
+end
+
+function Sprite:draw()
+    if not self.sprite then return end
+    love.graphics.draw(self.sprite, self.x, self.y, 0, self.sx, self.sy)
+end
+
+return Sprite