demo recording and playback, collision table

This commit is contained in:
lustlion 2022-02-17 23:39:31 +01:00
parent fb375e352b
commit 8e9078e929
9 changed files with 1063 additions and 42 deletions

View File

@ -33,7 +33,8 @@ function DebugUI()
end end
function DebugColisions() function DebugColisions()
LoadedObjects.DrawCollisions() DrawColisionTable()
-- LoadedObjects.DrawCollisions()
end end
function DebugEntities() function DebugEntities()

65
code/demo.lua Normal file
View File

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

View File

@ -46,6 +46,22 @@ function GameStep()
if Keybind:CheckPressed(Keybind.debug.editor) then if Keybind:CheckPressed(Keybind.debug.editor) then
editor_mode = true editor_mode = true
end 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 end
function GameDraw() function GameDraw()

View File

@ -1,19 +1,43 @@
Keybind = {} Keybind = {}
Keybind.move = {} 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 = {}
Keybind.menu.pause = { demo = "menu_pause"}
Keybind.menu.confirm = { demo = "menu_confirm"}
Keybind.debug = {} Keybind.debug = {}
Keybind.editor = {} Keybind.editor = {}
Keybind.generic = {} Keybind.generic = {}
function Keybind:CheckDown(action) function Keybind:CheckDown(action)
for _, keyname in pairs(action.keys) do if DemoPlayback then
if type(keyname) == "string" then for _, demo_action in pairs(DemoAction[CurrentDemoFrame]) do
if love.keyboard.isDown(keyname) then return true end if demo_action == action.demo then
else return true
if love.mouse.isDown(keyname) then return true end end
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 end
return false
end end
function Keybind:CheckPressed(action) function Keybind:CheckPressed(action)
@ -51,23 +75,25 @@ end
function Keybind:Default() function Keybind:Default()
--Menu --Menu
Keybind.menu.pause= { keys = {"escape"}} Keybind.menu.pause.keys = {"escape"}
Keybind.menu.confirm= { keys = {"z", "space", 1}} Keybind.menu.confirm.keys = {"z", "space", 1}
--Move --Move
Keybind.move.left = { keys = {"left", "a"}} Keybind.move.left.keys = {"left", "a"}
Keybind.move.right = { keys = {"right", "d"}} Keybind.move.right.keys = {"right", "d"}
Keybind.move.up = { keys = {"up", "w"}} Keybind.move.up.keys = {"up", "w"}
Keybind.move.down = { keys = {"down", "s"}} Keybind.move.down.keys = {"down", "s"}
Keybind.move.jump = { keys = {"z", "space"}} Keybind.move.jump.keys = {"z", "space"}
Keybind.move.hook = { keys = {"x", 1}} Keybind.move.hook.keys = {"x", 1}
Keybind.move.dash = { keys = {"c", 2}} Keybind.move.dash.keys = {"c", 2}
--Debug --Debug
Keybind.debug.debug = { keys = {"f1"}} Keybind.debug.debug = { keys = {"f1"}}
Keybind.debug.reposition = { keys = {"f2"}} Keybind.debug.reposition = { keys = {"f2"}}
Keybind.debug.reload = { keys = {"f3"}} Keybind.debug.reload = { keys = {"f3"}}
Keybind.debug.editor = { keys = {"f4"}} Keybind.debug.editor = { keys = {"f4"}}
Keybind.debug.recording = { keys = {"f5"}}
Keybind.debug.playback = { keys = {"f6"}}
-- Editor -- Editor
Keybind.editor.palette = { keys = {"tab"}} Keybind.editor.palette = { keys = {"tab"}}

View File

@ -1,5 +1,5 @@
function LevelLoadTiles() function LevelLoadTiles()
math.randomseed(3)
LevelData = dofile("data/levels/"..currLevel) LevelData = dofile("data/levels/"..currLevel)
--[[ --[[
@ -638,6 +638,7 @@ function TileCreateObjects()
end end
end end
end end
CreateCollisionTable()
end end
function AnimateTiles() function AnimateTiles()
@ -660,6 +661,39 @@ function AnimateTiles()
end end
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) function DrawTile(tile,x,y,depth)
local Properties = TileData[tile.id] local Properties = TileData[tile.id]

View File

@ -9,10 +9,6 @@ LoadedObjects = {
-- level functions -- level functions
function LoadedObjects.DrawCollisions() function LoadedObjects.DrawCollisions()
for _, collision in pairs(LoadedObjects.Collisions) do
collision:Draw(1)
end
for _, platform in pairs(LoadedObjects.Platforms) do for _, platform in pairs(LoadedObjects.Platforms) do
if platform.disable == true then platform:Draw(2) end if platform.disable == true then platform:Draw(2) end
if platform.disable == false then platform:Draw(1) end if platform.disable == false then platform:Draw(1) end
@ -29,33 +25,26 @@ end
-- returns true if theres a collision at that point -- returns true if theres a collision at that point
function isThereObjectAt(x,y,objectType) function isThereObjectAt(x,y,objectType)
for _, collision in pairs(objectType) do for _, object in pairs(objectType) do
if collision.disable then if object.disable then
-- Dont calculate if dissabled -- Dont calculate if dissabled
elseif x >= collision.from.x elseif x >= object.from.x
and x <= collision.to.x and x <= object.to.x
and y >= collision.from.y and y >= object.from.y
and y <= collision.to.y then and y <= object.to.y then
collision.isColliding = true object.isColliding = true
return true return true
end end
end end
return false return false
end end
function isThereAnyCollisionAt(x,y) function isThereCollisionAt(x,y)
local Check = { if x >= 0 and x < #CollisionTable
LoadedObjects.Collisions, and y >= 0 and y < #CollisionTable[0] then
LoadedObjects.Ladders, return CollisionTable[math.floor(y)][math.floor(x)]
LoadedObjects.Platforms
}
for _, type in pairs(Check) do
local result = isThereObjectAt(x,y,type)
if result then
return result
end
end end
return false return false
end end
-- flags -- flags

View File

@ -21,8 +21,9 @@ require "code/camera"
require "code/lights" require "code/lights"
require "code/objects" require "code/objects"
-- UI functions -- functions
require "code/debug" require "code/debug"
require "code/demo"
require "code/keybind" require "code/keybind"
require "code/menu" require "code/menu"
require "code/ui" require "code/ui"

885
demo/play_demo.lua Normal file
View File

@ -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",},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{}
}

View File

@ -50,7 +50,7 @@ function love.load()
language = "ENG" language = "ENG"
LocaleLoad(language) LocaleLoad(language)
gravity = 0.2 gravity = 0.14
-- Debug and log stuff -- Debug and log stuff
memoryUsage, dtcount = 0, 0 memoryUsage, dtcount = 0, 0
logPrint("mothback: "..collectgarbage("count").." kB, Loading time: "..os.clock().." seconds") logPrint("mothback: "..collectgarbage("count").." kB, Loading time: "..os.clock().." seconds")
@ -81,6 +81,8 @@ function love.update(dt)
fps_count = fps_count + 1 fps_count = fps_count + 1
current_dt = dt current_dt = dt
if DemoRecording or DemoPlayback then Demo:Step() end
-- things per second -- things per second
dtcount = dtcount + dt dtcount = dtcount + dt
if dtcount >= 1 then if dtcount >= 1 then
@ -139,4 +141,6 @@ function love.draw()
if menu_type ~= nil then MenuDraw(menu_type) end if menu_type ~= nil then MenuDraw(menu_type) end
love.graphics.print(game.scale,10,40) love.graphics.print(game.scale,10,40)
if DemoRecording or DemoPlayback then Demo:Draw() end
end end