OK, there's a lot of work because things from one screen are spread into various files. In particular, many things that belong in the menu or in the game, are actually in introhandler.lua.
This is what the new file states.lua would contain:
Code: Select all
local function activate(self, new)
assert(new, "Attempt to activate a state that does not exist yet")
if self.active and self.active.when_deactivated then self.active:when_deactivated() end
self.active = new
if new.when_activated then new:when_activated() end
end
return {activate = activate, active = false}
This is how main.lua would look like:
Code: Select all
--[=====[-------------- Modules --------------]=====]--
local states = require 'states'
love.window.setFullscreen(true, "desktop")
--[=====[-------------- Events --------------]=====]--
function love.load()
states.introh = require 'introhandler'
states.menu = require 'menu'
states.info = require 'info'
states.game = require 'game'
for k, v in pairs(states) do
if type(v) == "table" and v.load then v.load(args) end
end
states:activate(states.introh)
end
function love.update(dt)
if states.active.update then states.active:update(dt) end
end
function love.draw()
if states.active.draw then states.active:draw() end
end
function love.keypressed(...)
if states.active.keypressed then states.active:keypressed(...) end
end
Remember to add more events if you need them.
You need to create two new files, one for the info screen and one for the game. From the code, it seems to me that you want to have a pause menu; if so, maybe you need a new file for it, or maybe you can do with the existing menu. I've used the latter approach below.
You need to move all code that belongs to the menu (the code that you have in introh.lua under 'if state == "menu" then ...'), to menu.lua. The variable mclick would be in menu.lua as well. The variable 'Info' is not necesary. The variable 'music' is unused because the code is commented out, but it looks like it belongs in menu.lua.
introhandler.lua has no business updating anything other than the splash screen, therefore sprite:update(dt) would be moved to game.lua.
introhandler.lua ends up looking like this:
Code: Select all
--[=====[-------------- Modules --------------]=====]--
local o_ten_one = require "o-ten-one"
local states = require 'states'
local menu = require 'menu'
--[=====[-------------- Variables --------------]=====]--
local introh = {}
--[=====[-------------- Events --------------]=====]--
function introh:load()
splash = o_ten_one({fill = "lighten"})
function splash.onDone()
menu:mainmenu()
end
end
function introh:update(dt)
splash:update(dt)
end
function introh:draw()
splash:draw()
end
function introh:keypressed(key)
if key == "escape" then
-- splash:skip() -- slow to react!
menu:mainmenu()
end
end
return introh
Isn't that clear and tidy?
The variables menuon, menuallow, allow are no longer necessary and can be removed.
The idea is that every file that handles a state takes care ONLY of what is necessary for that state. This keeps the code a lot tidier. When switching states, instead of setting the variable 'state', you use states:activate(states.NEW_STATE). In the case of the menu, instead of doing that, you use menu:mainmenu() for the main menu, and menu:pausemenu() for the in-game pause menu (that will set up the variables to show the correct menu; remember to first do: local menu = require "menu" in the file that calls the menu).
I've attached a .love file with a new states.lua library, new files info.lua and game.lua, and my changes to main.lua, introhandler.lua and menu.lua. Note that the game crashes when it calls menu:allow() because that doesn't exist any longer. You probably want to use states.game:setroom(new room name) instead, which is a function that I have created that I think you might find useful later. That makes the variable 'lobbyexit' unnecessary. You will probably need to clean up some stuff that you were using for handling the states in the way you were doing it.
I think you could use a similar approach for rooms (or maps, if you prefer) as for states; however I haven't implemented that.
The player moves more slowly because you were calling sprite:update(dt) twice, one in main.lua and one in introhandler.lua, and now it is called only once. I think that calling it twice was a bug, so you may need to adjust the player speed.
I've got rid of the 'delay' variable because I wasn't sure what it was for.