From 8e9078e929fd8ec4d681e065304c716903bc350b Mon Sep 17 00:00:00 2001 From: lustlion Date: Thu, 17 Feb 2022 23:39:31 +0100 Subject: [PATCH] demo recording and playback, collision table --- code/debug.lua | 3 +- code/demo.lua | 65 ++++ code/game.lua | 16 + code/keybind.lua | 56 ++- code/level.lua | 36 +- code/objects.lua | 35 +- code/require.lua | 3 +- demo/play_demo.lua | 885 +++++++++++++++++++++++++++++++++++++++++++++ main.lua | 6 +- 9 files changed, 1063 insertions(+), 42 deletions(-) create mode 100644 code/demo.lua create mode 100644 demo/play_demo.lua diff --git a/code/debug.lua b/code/debug.lua index b2254d8..37c7ec0 100644 --- a/code/debug.lua +++ b/code/debug.lua @@ -33,7 +33,8 @@ function DebugUI() end function DebugColisions() - LoadedObjects.DrawCollisions() + DrawColisionTable() + -- LoadedObjects.DrawCollisions() end function DebugEntities() diff --git a/code/demo.lua b/code/demo.lua new file mode 100644 index 0000000..1908252 --- /dev/null +++ b/code/demo.lua @@ -0,0 +1,65 @@ +Demo = {} +DemoPlayback = false +DemoRecording = false +DemoAction = nil -- Table of actions +CurrentDemoFrame = nil + +function Demo:Draw() + if DemoRecording then + love.graphics.setColor(1,0,0,1) + elseif DemoPlayback then + love.graphics.setColor(0,0,1,1) + end + love.graphics.rectangle("line",0,0,game.width ,game.height) +end + +function Demo:PlaybackStart() + DemoPlayback = true + CurrentDemoFrame = 0 + dofile("demo/play_demo.lua") +end + +function Demo:PlaybackEnd() + DemoPlayback = false + DemoAction = nil +end + +function Demo:RecordAction(action) + if DemoRecording + and action ~= nil + then + DemoFile:write("\""..action.."\",") + end +end + +function Demo:RecordStart() + -- Make demo stuff + os.execute( "mkdir \"./demo\"" ) + DemoFile = io.open("demo/play_demo.lua", "w+") + --DemoFile = io.open("demo/mothbackDemo_"..os.date("%Y-%m-%d_%H-%M-%S")..".lua", "w+") + DemoFile:write("main_Player.pos.x = "..main_Player.pos.x.."\n") + DemoFile:write("main_Player.pos.y = "..main_Player.pos.y.."\n") + DemoFile:write("DemoAction = {\n") + DemoRecording = true + CurrentDemoFrame = 1 +end + +function Demo:RecordEnd() + DemoFile:write("}\n}") + DemoFile:close() + DemoFile = nil + DemoRecording = false +end + +function Demo:Step() + if DemoRecording then + if CurrentDemoFrame == 1 then + DemoFile:write("\t{") + else + DemoFile:write("},\n\t{") + end + elseif DemoPlayback then + if DemoAction[CurrentDemoFrame + 1] == nil then Demo:PlaybackEnd() end + end + CurrentDemoFrame = CurrentDemoFrame + 1 +end diff --git a/code/game.lua b/code/game.lua index 49a9fcd..813be24 100644 --- a/code/game.lua +++ b/code/game.lua @@ -46,6 +46,22 @@ function GameStep() if Keybind:CheckPressed(Keybind.debug.editor) then editor_mode = true end + + if Keybind:CheckPressed(Keybind.debug.recording) then + if DemoRecording then + Demo:RecordEnd() + else + Demo:RecordStart() + end + end + + if Keybind:CheckPressed(Keybind.debug.playback) then + if DemoPlayback then + Demo:PlaybackEnd() + else + Demo:PlaybackStart() + end + end end function GameDraw() diff --git a/code/keybind.lua b/code/keybind.lua index d561aad..9787bb0 100644 --- a/code/keybind.lua +++ b/code/keybind.lua @@ -1,19 +1,43 @@ Keybind = {} + Keybind.move = {} +Keybind.move.left = { demo = "move_left"} +Keybind.move.right = { demo = "move_right"} +Keybind.move.up = { demo = "move_up"} +Keybind.move.down = { demo = "move_down"} +Keybind.move.jump = { demo = "move_jump"} +Keybind.move.hook = { demo = "move_hook"} +Keybind.move.dash = { demo = "move_dash"} + Keybind.menu = {} +Keybind.menu.pause = { demo = "menu_pause"} +Keybind.menu.confirm = { demo = "menu_confirm"} + Keybind.debug = {} Keybind.editor = {} Keybind.generic = {} function Keybind:CheckDown(action) - for _, keyname in pairs(action.keys) do - if type(keyname) == "string" then - if love.keyboard.isDown(keyname) then return true end - else - if love.mouse.isDown(keyname) then return true end + if DemoPlayback then + for _, demo_action in pairs(DemoAction[CurrentDemoFrame]) do + if demo_action == action.demo then + return true + end end + return false + else + for _, keyname in pairs(action.keys) do + local fn = love.keyboard.isDown + if not type(keyname) == "string" then + local fn = love.mouse.isDown + end + if fn(keyname) then + Demo:RecordAction(action.demo) + return true + end + end + return false end - return false end function Keybind:CheckPressed(action) @@ -51,23 +75,25 @@ end function Keybind:Default() --Menu - Keybind.menu.pause= { keys = {"escape"}} - Keybind.menu.confirm= { keys = {"z", "space", 1}} + Keybind.menu.pause.keys = {"escape"} + Keybind.menu.confirm.keys = {"z", "space", 1} --Move - Keybind.move.left = { keys = {"left", "a"}} - Keybind.move.right = { keys = {"right", "d"}} - Keybind.move.up = { keys = {"up", "w"}} - Keybind.move.down = { keys = {"down", "s"}} - Keybind.move.jump = { keys = {"z", "space"}} - Keybind.move.hook = { keys = {"x", 1}} - Keybind.move.dash = { keys = {"c", 2}} + Keybind.move.left.keys = {"left", "a"} + Keybind.move.right.keys = {"right", "d"} + Keybind.move.up.keys = {"up", "w"} + Keybind.move.down.keys = {"down", "s"} + Keybind.move.jump.keys = {"z", "space"} + Keybind.move.hook.keys = {"x", 1} + Keybind.move.dash.keys = {"c", 2} --Debug Keybind.debug.debug = { keys = {"f1"}} Keybind.debug.reposition = { keys = {"f2"}} Keybind.debug.reload = { keys = {"f3"}} Keybind.debug.editor = { keys = {"f4"}} + Keybind.debug.recording = { keys = {"f5"}} + Keybind.debug.playback = { keys = {"f6"}} -- Editor Keybind.editor.palette = { keys = {"tab"}} diff --git a/code/level.lua b/code/level.lua index 25c8d49..ee137ff 100644 --- a/code/level.lua +++ b/code/level.lua @@ -1,5 +1,5 @@ function LevelLoadTiles() - + math.randomseed(3) LevelData = dofile("data/levels/"..currLevel) --[[ @@ -638,6 +638,7 @@ function TileCreateObjects() end end end + CreateCollisionTable() end function AnimateTiles() @@ -660,6 +661,39 @@ function AnimateTiles() end end +function CreateCollisionTable() + -- init table + CollisionTable = {} + for j=0, 16*LevelGetTileHeight()-1 do + CollisionTable[j] = {} + for i=0, 16*LevelGetTileWidth()-1 do + CollisionTable[j][i] = false + end + end + + for _, collision in pairs(LoadedObjects.Collisions) do + for ci=0, math.floor(collision.width)-1 do + for cj=0, math.floor(collision.height)-1 do + print(ci..","..cj) + CollisionTable[collision.from.y+cj][collision.from.x+ci] = true + end + end + end +end + +function DrawColisionTable() + for j=1, #CollisionTable do + for i=1, #CollisionTable[j] do + if CollisionTable[j][i] then + love.graphics.setColor(0,1,0,1) + else + love.graphics.setColor(1,0,0,1) + end + love.graphics.points(i,j) + end + end +end + function DrawTile(tile,x,y,depth) local Properties = TileData[tile.id] diff --git a/code/objects.lua b/code/objects.lua index 0e5cf88..e0135d7 100644 --- a/code/objects.lua +++ b/code/objects.lua @@ -9,10 +9,6 @@ LoadedObjects = { -- level functions function LoadedObjects.DrawCollisions() - for _, collision in pairs(LoadedObjects.Collisions) do - collision:Draw(1) - end - for _, platform in pairs(LoadedObjects.Platforms) do if platform.disable == true then platform:Draw(2) end if platform.disable == false then platform:Draw(1) end @@ -29,33 +25,26 @@ end -- returns true if theres a collision at that point function isThereObjectAt(x,y,objectType) - for _, collision in pairs(objectType) do - if collision.disable then + for _, object in pairs(objectType) do + if object.disable then -- Dont calculate if dissabled - elseif x >= collision.from.x - and x <= collision.to.x - and y >= collision.from.y - and y <= collision.to.y then - collision.isColliding = true + elseif x >= object.from.x + and x <= object.to.x + and y >= object.from.y + and y <= object.to.y then + object.isColliding = true return true end end return false end -function isThereAnyCollisionAt(x,y) - local Check = { - LoadedObjects.Collisions, - LoadedObjects.Ladders, - LoadedObjects.Platforms - } - for _, type in pairs(Check) do - local result = isThereObjectAt(x,y,type) - if result then - return result - end +function isThereCollisionAt(x,y) + if x >= 0 and x < #CollisionTable + and y >= 0 and y < #CollisionTable[0] then + return CollisionTable[math.floor(y)][math.floor(x)] end - return false + return false end -- flags diff --git a/code/require.lua b/code/require.lua index b71e364..544999a 100644 --- a/code/require.lua +++ b/code/require.lua @@ -21,8 +21,9 @@ require "code/camera" require "code/lights" require "code/objects" --- UI functions +-- functions require "code/debug" +require "code/demo" require "code/keybind" require "code/menu" require "code/ui" diff --git a/demo/play_demo.lua b/demo/play_demo.lua new file mode 100644 index 0000000..8965b10 --- /dev/null +++ b/demo/play_demo.lua @@ -0,0 +1,885 @@ +main_Player.pos.x = 104.9 +main_Player.pos.y = 152.99952723758 +DemoAction = { + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {"move_jump",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_dash","move_up",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_dash","move_up",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {}, + {}, + {}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left","move_dash","move_up","move_left",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_dash",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {"move_left",}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right",}, + {"move_right","move_hook",}, + {"move_right","move_hook",}, + {"move_right","move_hook",}, + {"move_right","move_hook",}, + {"move_right","move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {"move_hook",}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {}, + {} +} \ No newline at end of file diff --git a/main.lua b/main.lua index 2967580..03b33a9 100644 --- a/main.lua +++ b/main.lua @@ -50,7 +50,7 @@ function love.load() language = "ENG" LocaleLoad(language) - gravity = 0.2 + gravity = 0.14 -- Debug and log stuff memoryUsage, dtcount = 0, 0 logPrint("mothback: "..collectgarbage("count").." kB, Loading time: "..os.clock().." seconds") @@ -81,6 +81,8 @@ function love.update(dt) fps_count = fps_count + 1 current_dt = dt + if DemoRecording or DemoPlayback then Demo:Step() end + -- things per second dtcount = dtcount + dt if dtcount >= 1 then @@ -139,4 +141,6 @@ function love.draw() if menu_type ~= nil then MenuDraw(menu_type) end love.graphics.print(game.scale,10,40) + + if DemoRecording or DemoPlayback then Demo:Draw() end end