feat(examples): add a pause menu in plateformer example

This commit is contained in:
Kazhnuz 2019-06-16 16:37:22 +02:00
parent b47e158126
commit 81015b011b
4 changed files with 90 additions and 6 deletions

View file

@ -25,8 +25,10 @@ function Player:updateStart(dt)
end
if self.keys["start"].isPressed then
self.world:switchActivity()
self.assets:switchActivity()
--self.world:switchActivity()
--self.assets:switchActivity()
self.scene.menusystem:activate()
self.scene.menusystem:switchMenu("PauseMenu")
self.scene:flushKeys()
end

View file

@ -3,5 +3,13 @@ return {
{"player", "examples/gameplay/plateform/assets/monkey_lad"},
{"coin", "examples/gameplay/plateform/assets/coin5"},
{"sparkle", "examples/gameplay/plateform/assets/sparkle"}
},
["imagefonts"] = {
{"medium", "examples/mainmenu/assets/medium"}
},
["sfx"] = {
{"navigate", "examples/mainmenu/assets/menu_move.mp3"},
{"confirm", "examples/mainmenu/assets/menu_confirm.mp3"},
{"cancel", "examples/mainmenu/assets/menu_error.mp3"},
}
}

View file

@ -25,6 +25,7 @@ local Scene = require "gamecore.modules.scenes"
local Plateformer = Scene:extend()
local World = require "gamecore.modules.world.world2D"
local Pause = require "examples.gameplay.plateform.pause"
function Plateformer:new()
Plateformer.super.new(self)
@ -35,20 +36,32 @@ function Plateformer:new()
World(self, folder .. ".actors", "examples/gameplay/plateform/assets/platformer.lua")
Pause(self)
self.menusystem:deactivate()
self.menusystem:lockWorldWhenActive(true)
self.menusystem:lockAssetsWhenActive(true)
self.world:loadMap()
end
function Plateformer:restart()
self.menusystem:deactivate()
collectgarbage()
self.world:reset()
end
function Plateformer:update(dt)
if (self.world.isActive == false) and self.sources[1].keys["start"].isPressed then
self.world:switchActivity()
self.assets:switchActivity()
if (self.menusystem.isActive == true) and self.sources[1].keys["start"].isPressed then
self.menusystem:deactivate()
end
end
function Plateformer:draw()
if (self.world.isActive == false) then
local w, h = core.screen:getDimensions()
love.graphics.printf("PAUSE", 0, h/2 - 16, w, "center")
love.graphics.setColor(1, 1, 0, 1)
love.graphics.printf("PAUSE", 0, 24*3, w, "center")
utils.graphics.resetColor()
end
end

View file

@ -0,0 +1,61 @@
local ListMenu = require "gamecore.modules.menusystem.listbox"
local Widget = require "gamecore.modules.menusystem.widgets"
local PauseMenu = ListMenu:extend()
local ResumeWidget = Widget.Text:extend()
local RestartWidget = Widget.Text:extend()
local ExitWidget = Widget.Text:extend()
function PauseMenu:new(scene)
self.scene = scene
local screenHeight, screenWidth = core.screen:getDimensions()
local w, h = 424/4, 240 - 48*4
local x, y = 3*w/2, 24*4
PauseMenu.super.new(self, scene.menusystem, "PauseMenu", x, y, w, h, 3)
ResumeWidget(self)
RestartWidget(self)
ExitWidget(self)
end
-- WIDGETS
-- All widgets used by the pause menu
function ResumeWidget:new(menu)
self.scene = menu.scene
local font = self.scene.assets.fonts["medium"]
local label = "resume"
ResumeWidget.super.new(self, menu, font, label)
end
function ResumeWidget:action()
self.scene.assets:playSFX(self.sfx)
self.scene.menusystem:deactivate()
end
function RestartWidget:new(menu)
self.scene = menu.scene
local font = self.scene.assets.fonts["medium"]
local label = "restart"
RestartWidget.super.new(self, menu, font, label)
end
function RestartWidget:action()
self.scene.assets:playSFX(self.sfx)
self.scene:restart()
end
function ExitWidget:new(menu)
self.scene = menu.scene
local font = self.scene.assets.fonts["medium"]
local label = "exit"
ExitWidget.super.new(self, menu, font, label)
end
function ExitWidget:action()
self.scene.assets:playSFX(self.sfx)
core.scenemanager:setStoredScene("mainmenu")
end
return PauseMenu