From da89a077ca192db7da09713a5db4408c4bc591c0 Mon Sep 17 00:00:00 2001 From: Entarno54 Date: Sun, 1 Jun 2025 14:12:37 +0700 Subject: [PATCH] Caching of images ( holy fps ) --- characters/tankman.json | 4 +- main.lua | 42 +++++++++----- modules/states/playstate.lua | 106 ++++++++++++++--------------------- modules/types.lua | 16 +++--- 4 files changed, 79 insertions(+), 89 deletions(-) diff --git a/characters/tankman.json b/characters/tankman.json index 92505d6..2bda0f3 100644 --- a/characters/tankman.json +++ b/characters/tankman.json @@ -61,7 +61,7 @@ -8 ], "loop": false, - "fps": 50, + "fps": 24, "anim": "singUP-alt", "indices": [], "name": "TANKMAN UGH " @@ -72,7 +72,7 @@ 16 ], "loop": false, - "fps": 50, + "fps": 24, "anim": "singDOWN-alt", "indices": [], "name": "PRETTY GOOD tankman " diff --git a/main.lua b/main.lua index c2e46b3..4486395 100644 --- a/main.lua +++ b/main.lua @@ -35,22 +35,9 @@ function love.update(dt) end elseif not curState then if love.keyboard.isDown("return") then - curState = state(curSong, curDiff) - curState.quit = function() - curState = nil - stateLoaded = false - myTypes.destroyAllSprites() - end - curState.load() + elseif love.keyboard.isDown("down") and not pressed[1] then - curSong = next(songs, curSong) - if not curSong then - curSong = next(songs) - curDiffList = songs[curSong] - else - curDiffList = songs[curSong] - end - curDiff = curDiffList[1] + pressed[1] = true elseif love.keyboard.isDown("right") and not pressed[2] then local curDiffNum = 0 @@ -90,4 +77,29 @@ function love.draw() end end +function love.keypressed(key, un, is) + if curState and stateLoaded then + curState.keypressed(key, un, is) + else + if key == "return" then + curState = state(curSong, curDiff) + curState.quit = function() + curState = nil + stateLoaded = false + myTypes.destroyAllSprites() + end + curState.load() + elseif key == "down" then + curSong = next(songs, curSong) + if not curSong then + curSong = next(songs) + curDiffList = songs[curSong] + else + curDiffList = songs[curSong] + end + curDiff = curDiffList[1] + end + end +end + love.window.setMode(1280, 720, { fullscreen = false , resizable = true}) \ No newline at end of file diff --git a/modules/states/playstate.lua b/modules/states/playstate.lua index fe46049..a1a9f85 100644 --- a/modules/states/playstate.lua +++ b/modules/states/playstate.lua @@ -73,6 +73,8 @@ local function state(songName, songDifficulty) false } + local fps = 60 -- for the counter + local receptors = {} local keyBinds = {} -- loaded from settings.json, if anything's wrong then check your settings.json @@ -100,26 +102,23 @@ local function state(songName, songDifficulty) end local function checkNote(dir) - if pressed[dir] or holded[dir] then - pressed[dir] = false - holded[dir] = true - if receptors[dir].animation ~= string.lower(directions[dir]).." confirm" then - receptors[dir]:PlayAnimation(string.lower(directions[dir]).." press", 25, false) - end - return -- You dont check if you are already holding and not just pressing - end - pressed[dir] = true + local closestNote + local closestIndex for index, note in next, notes do if note.position - conductor.songPosition < 200 then - if note.mustPress and not note.pressed and note.direction == dir then - characters.bf:PlayAnimation("sing"..directions[note.direction]) - note.pressed = true - receptors[dir]:PlayAnimation(string.lower(directions[dir]).." confirm", 25, false) - notes[index] = nil - note:destroy() + if note.mustPress and not note.pressed and note.direction == dir and (not closestNote or closestNote and note.position < closestNote.position) then + closestNote = note + closestIndex = index end end end + if closestNote then + characters.bf:PlayAnimation("sing"..directions[closestNote.direction]) + closestNote.pressed = true + receptors[dir]:PlayAnimation(string.lower(directions[dir]).." confirm", 5, false) + notes[closestIndex] = nil + closestNote:destroy() + end end local elapsed = 0 @@ -129,54 +128,6 @@ local function state(songName, songDifficulty) if not playing then return end -- playing isn't supposed to work like "paused", it's there to keep the game from working during loading - if love.keyboard.isDown(keyBinds[1]) then - checkNote(1) - else - holded[1] = false - pressed[1] = false - end - if love.keyboard.isDown(keyBinds[2]) then - checkNote(2) - else - holded[2] = false - pressed[2] = false - end - if love.keyboard.isDown(keyBinds[3]) then - checkNote(3) - else - holded[3] = false - pressed[3] = false - end - if love.keyboard.isDown(keyBinds[4]) then - checkNote(4) - else - holded[4] = false - pressed[4] = false - end - - if love.keyboard.isDown("escape") then - quit() - end - - if love.keyboard.isDown("space") then - if pressed[5] then goto evilContinue end - pressed[5] = true - paused = not paused - - if paused then - inst:pause() - voices:pause() - pauseStart = socket.gettime() * 1000 - else - inst:play() - voices:play() - pauseTime = pauseTime + (socket.gettime() * 1000 - pauseStart) - end - ::evilContinue:: - else - pressed[5] = false - end - if paused then goto continue end -- if paused then skip this cycle local currentTime = socket.gettime() @@ -275,6 +226,8 @@ local function state(songName, songDifficulty) zoom = myMath.lerp(zoom, 1, .05) + fps = 1000 / dt + ::continue:: end @@ -294,6 +247,7 @@ local function state(songName, songDifficulty) love.graphics.draw(mainCanvas, (love.graphics:getHeight() * (4/3) - (love.graphics:getHeight()* (4/3)) * zoom) / 2, (love.graphics:getHeight()- love.graphics:getHeight() * zoom) / 2, 0, ((love.graphics:getHeight() / 1080) * (4/3)) * zoom, (love.graphics:getHeight() / 1080) * zoom) + love.graphics.print({{0,0,0,1}, string.format("FPS: %s", love.timer.getFPS())}) end love.window.setMode(1280, 720, { fullscreen = false , resizable = true}) @@ -358,6 +312,32 @@ local function state(songName, songDifficulty) startTime = socket.gettime() end + function state.keypressed(key, un, is) + if key == "space" then + paused = not paused + + if paused then + inst:pause() + voices:pause() + pauseStart = socket.gettime() * 1000 + else + inst:play() + voices:play() + pauseTime = pauseTime + (socket.gettime() * 1000 - pauseStart) + end + elseif key == "escape" then + quit() + elseif key == keyBinds[1] then + checkNote(1) + elseif key == keyBinds[2] then + checkNote(2) + elseif key == keyBinds[3] then + checkNote(3) + elseif key == keyBinds[4] then + checkNote(4) + end + end + return state end diff --git a/modules/types.lua b/modules/types.lua index edcd658..1ad9efd 100644 --- a/modules/types.lua +++ b/modules/types.lua @@ -45,8 +45,6 @@ end function module.Sprite(image, sheet) if not cachedQuads[sheet] then local sheetString = files.read_file(sheet) - - if not sheetString then error("Failed to load", sheet) end @@ -90,7 +88,7 @@ function module.Sprite(image, sheet) end newSprite.quads = quads - cachedQuads[sheet] = quads + cachedQuads[sheet] = {quads = quads, image = newSprite.image} Sprites[#Sprites+1] = newSprite @@ -98,8 +96,8 @@ function module.Sprite(image, sheet) else local newSprite = setmetatable({ - image = love.graphics.newImage(image), - quads = cachedQuads[sheet], + image = cachedQuads[sheet].image, + quads = cachedQuads[sheet].quads, elapsed = 0, fps = 10, animation = nil, @@ -219,7 +217,7 @@ end function module.Rect(image, sheet) if not cachedQuads[sheet] then local sheetString = files.read_file(sheet) - + if not sheetString then error("Failed to load", sheet) end @@ -263,15 +261,15 @@ function module.Rect(image, sheet) end newSprite.quads = quads - cachedQuads[sheet] = quads + cachedQuads[sheet] = {quads = quads, image = newSprite.image} Rects[#Rects+1] = newSprite return newSprite else local newSprite = setmetatable({ - image = love.graphics.newImage(image), - quads = cachedQuads[sheet], + image = cachedQuads[sheet].image, + quads = cachedQuads[sheet].quads, elapsed = 0, fps = 10, animation = nil,