tidy up scene management

This commit is contained in:
wan-may 2023-09-04 15:31:47 -03:00
parent bef5a5e6c7
commit beb5d71f03
4 changed files with 29 additions and 28 deletions

View File

@ -1,5 +1,5 @@
local client = assert( client )
local lg = assert( love.graphics ) local lg = assert( love.graphics )
local scene = assert( require 'client.scene' )
local browser = {} local browser = {}
function browser.draw() function browser.draw()
@ -14,5 +14,4 @@ function browser.onLoad( )
lg.setColor( 1, 1, 1, 1 ) lg.setColor( 1, 1, 1, 1 )
end end
client.addScene( browser, "browser" ) return scene.add( browser, 'browser' )
return browser

View File

@ -1,6 +1,6 @@
--Main menu for client. --Main menu for client.
local lg = assert( love.graphics ) local lg = assert( love.graphics )
local client = assert( client ) local scene = assert( require 'client.scene' )
local strings = assert( require 'client.strings' ) local strings = assert( require 'client.strings' )
local menu = {} local menu = {}
@ -50,13 +50,11 @@ function menu.mousemoved( x, y, dx, dy, istouch )
end end
function menu.mousepressed( x, y, button, istouch, presses ) function menu.mousepressed( x, y, button, istouch, presses )
client.loadScene( "browser" ) scene.load( 'browser' )
end end
function menu.keypressed( key, code, isrepeat ) function menu.keypressed( key, code, isrepeat )
end end
client.addScene( menu, "menu" ) return scene.add( menu, 'menu' )
return menu

20
src/client/scene.lua Normal file
View File

@ -0,0 +1,20 @@
local scene = {}
local love = assert( love )
local mt = {}
function mt.load( name )
print( "Loading Scene:", name )
local scene = assert( scene[name] )
for k, v in pairs( scene ) do
love[k] = v
end
scene.onLoad()
end
function mt.add( t, name )
print( "Adding Scene:", t.name or name )
scene[name] = t
return t
end
return setmetatable( scene, {__index = mt } )

View File

@ -1,26 +1,10 @@
--Client starts here! --Client starts here!
client = { scenes = {} }
local client = client
local shared = assert( require 'shared' ) local shared = assert( require 'shared' )
local love = assert( love ) local love = assert( love )
function client.loadScene( name )
print( "Loading Scene:", name )
local scene = assert( client.scenes[name] )
for k, v in pairs( scene ) do
love[k] = v
end
scene.onLoad()
end
function client.addScene( t, name )
print( "Adding Scene:", name )
client.scenes[name] = t
end
function love.load() function love.load()
local scenes = assert( require 'client.scene' )
assert( require 'client.menu' ) assert( require 'client.menu' )
assert( require 'client.browser' ) assert( require 'client.browser' )
scenes.load( 'menu' )
client.loadScene( "menu" )
end end