From 701696160987a2ca74c4518ae3ddecc5c51c62b1 Mon Sep 17 00:00:00 2001 From: Simon Kellet Date: Wed, 24 Jul 2024 15:57:37 +0100 Subject: [PATCH] restart doesnt stop battle music --- restart.lua | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 restart.lua diff --git a/restart.lua b/restart.lua new file mode 100644 index 0000000..02dbb55 --- /dev/null +++ b/restart.lua @@ -0,0 +1,68 @@ +-- Functions to handle restarting +-- all objects +-- Used in map switching and restarting! + +function ClearWalls(walls) + for i = #walls, 1, -1 do + walls[i]:destroy() + end +end + +function StopAllMusic() + -- TODO: maybe find a way + -- to find all 'streaming' + -- sounds and then stop them all! + musicBattle:stop() + musicMenu:stop() + musicPause:stop() + --musicStory:stop() -- Unused +end + +function ClearPlayerCollision() + UserPlayer1.collider:destroy() + UserPlayer2.collider:destroy() +end + +function ClearBullets(bullets) + for i, v in ipairs(bullets) do + v.collider:destroy() + end +end + +local function setNewLevelFromRandom(count) + local level = math.random(1, count) + _G.CUR_LEVEL = level +end + +local function getLevelCount() + local levelcount = 0 + local suf = ".lua" + local files = love.filesystem.getDirectoryItems("maps/") + + for _, file in ipairs(files) do + if string.find(file, suf) then + levelcount = levelcount + 1 + --print(k .. ". " .. file) --outputs something like "1. main.lua" + end + end + return levelcount +end + +function RestartGame() + setNewLevelFromRandom(getLevelCount()) + + -- Stop the music + --StopAllMusic() + + -- Clear the players collision + ClearPlayerCollision() + + -- Work through and delete all bullets + ClearBullets(Bullets1) + ClearBullets(Bullets2) + + --Clear Walls + ClearWalls(Walls) + + -- Done! +end