I'm trying to wrap my brain around Hump's gamestate functions. I've got them working in that I can switch from my main menu to my "gameLevel1" and back again. The issue is, every time I switch back and forth, it seems like all my enemy and pickup generators are doubled. If I switch back and forth a few times, my screen is basically covered with bad guys!
I'm guessing it's because a few variables I've declared in gameLevel1 are global, and are not getting wiped out when I change gamestates. I'm not sure how to get around this though, while still having access to those variables in more than just the gameLevel1.lua
So for example in the code below, UpdateList is global, but I reference it in Enemies, Particles, and Pickups.
Also, enemyController, particleController, and pickupController are all global, but the same thing: they are referenced in other areas.
Do I have to make everything here local and pass it all into everything that needs it? Or am I barking up the wrong tree entirely?
gameLevel1.lua:
Code: Select all
Gamestate = require 'libs.hump.gamestate'
UpdateList = require 'utils.UpdateList'
Timer = require "libs.hump.timer"
local Backgrounds = require 'controllers.Backgrounds'
local Enemies = require 'controllers.Enemies'
local Particles = require 'controllers.Particles'
local Pickups = require 'controllers.Pickups'
local gameLevel1 = {}
local Player = require 'entities.player'
local src1 = love.audio.newSource('assets/seafloor.mp3')
debugtext = {}
local times = {}
function gameLevel1:enter()
UpdateList:enter()
enemyController = Enemies()
pickupController = Pickups()
backgroundController = Backgrounds()
local player = Player(100, 50, exp)
UpdateList:add(player,2)
-- Do particles last so they end up on top of layers visually
particleController = Particles()
src1:play()
end
function gameLevel1:leave()
UpdateList:RemoveAll()
end
function gameLevel1:update(dt)
UpdateList:update(dt)
Timer.update(dt)
-- Debug Info
while #debugtext > 40 do
table.remove(debugtext, 1)
end
-- debugtext[#text+1] = string.format("Enemy Count: %s, %s",#enemyController.yellowPlane.list, #enemyController.redPlane.list )
-- debugtext[#text+1] = string.format("Audio Position: %.2f",src1:tell("samples"))
end
function gameLevel1:draw()
UpdateList:draw()
for i = 1,#debugtext do
love.graphics.setColor(0,0,0, 255 - (i-1) * 6)
love.graphics.print(debugtext[#debugtext - (i-1)], 10, i * 15)
end
love.graphics.setColor(255,255,255)
end
function gameLevel1:quit()
if times[1] then
local timefile = io.open("output/output.csv","w")
local st = ""
for i, t in pairs(times) do
st = st .. t .. ","
end
timefile:write(st)
timefile:close()
end
end
function gameLevel1:keypressed(key)
if key == "space" then
table.insert(times, src1:tell("samples"))
end
end
return gameLevel1