From 2b7ef7c276bf8c5a8dc678adf2c4a71c2428f9fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simo-Pekka=20Kerkel=C3=A4?= Date: Fri, 8 Feb 2019 19:11:41 +0200 Subject: [PATCH] main menu --- main.lua | 23 ++++++++++++++++-- scenes.lua | 70 +++++++++++++++++++++++++----------------------------- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/main.lua b/main.lua index c243efc..b07bf59 100644 --- a/main.lua +++ b/main.lua @@ -3,9 +3,28 @@ function love.load() end function love.update(dt) - scenes[#scenes].update(dt) + if #scenes == 0 then + love.event.quit() + end + if scenes[#scenes].update then + scene_ended = scenes[#scenes].update(key) + end +end + +function love.keypressed(key) + if scenes[#scenes].keypressed then + scenes[#scenes].keypressed(key) + end +end + +function love.keyreleased(key) + if scenes[#scenes].keyreleased then + scenes[#scenes].keyreleased(key) + end end function love.draw() - scenes[#scenes].draw() + if scenes[#scenes].draw then + scenes[#scenes].draw() + end end diff --git a/scenes.lua b/scenes.lua index 287e5da..3bc6b88 100644 --- a/scenes.lua +++ b/scenes.lua @@ -1,49 +1,45 @@ scenes = {} -time_since_start = 0 -loops = 0 -main_scene = {} -looping_scene = {} -looping_scene.draw = function() - love.graphics.print("Loopdy loo", 110, 120) -end -looping_scene.update = function(dt) - time_since_start = time_since_start + dt - if time_since_start > 2 then - looping_scene.draw = function() - love.graphics.print("Looping continues", 200, 150) - end - end - if time_since_start > 8 then - time_since_start = 0 - table.remove(scenes) - loops = loops + 1 - end -end +local main = {} +main.selected = 1 +main.options = { + {text = "Start Game"}, + {text = "Quit"} +} -main_scene.update = function(dt) - time_since_start = time_since_start + dt - if time_since_start > 2 then - main_scene.draw = function() - love.graphics.print("Main Scene extended", 400, 300) - end - end - if time_since_start > 6 then - time_since_start = 0 - if loops < 4 then - table.insert(scenes, looping_scene) +main.draw = function() + for i, v in ipairs(main.options) do + if i == main.selected then + love.graphics.print({{255, 255, 0}, v.text}, 200, 100 + (15 * i)) else - main_scene.draw = function() - love.graphics.print("Damn, enough with the looping..", 100, 100) - end + love.graphics.print(v.text, 200, 100 + (15 * i)) end end end -main_scene.draw = function() - love.graphics.print("Main Scene", 400, 300) +main.keypressed = function(key) + if key == "up" then + main.selected = main.selected + 1 + if main.selected > #main.options then + main.selected = 1 + end + elseif key == "down" then + main.selected = main.selected - 1 + if main.selected < 1 then + main.selected = #main.options + end + elseif key == "return" then + local next = main.options[main.selected].next_scene + if next then + table.insert(scenes, next) + else + table.remove(scenes) + end + end end -table.insert(scenes, main_scene) +main.update = function(dt) +end +table.insert(scenes, main) return scenes