From 81015b011baebc7fb606990024e07c3b7e7164d5 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 16 Jun 2019 16:37:22 +0200 Subject: [PATCH] feat(examples): add a pause menu in plateformer example --- examples/gameplay/plateform/actors/player.lua | 6 +- examples/gameplay/plateform/assets/init.lua | 8 +++ examples/gameplay/plateform/init.lua | 21 +++++-- examples/gameplay/plateform/pause.lua | 61 +++++++++++++++++++ 4 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 examples/gameplay/plateform/pause.lua diff --git a/examples/gameplay/plateform/actors/player.lua b/examples/gameplay/plateform/actors/player.lua index 2f18947..add912c 100644 --- a/examples/gameplay/plateform/actors/player.lua +++ b/examples/gameplay/plateform/actors/player.lua @@ -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 diff --git a/examples/gameplay/plateform/assets/init.lua b/examples/gameplay/plateform/assets/init.lua index af6c93b..2d93581 100644 --- a/examples/gameplay/plateform/assets/init.lua +++ b/examples/gameplay/plateform/assets/init.lua @@ -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"}, } } diff --git a/examples/gameplay/plateform/init.lua b/examples/gameplay/plateform/init.lua index bf59362..d8f0381 100644 --- a/examples/gameplay/plateform/init.lua +++ b/examples/gameplay/plateform/init.lua @@ -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 diff --git a/examples/gameplay/plateform/pause.lua b/examples/gameplay/plateform/pause.lua new file mode 100644 index 0000000..1ac3bac --- /dev/null +++ b/examples/gameplay/plateform/pause.lua @@ -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