From 96fef88f56fdcdf95bc5783eb2b3b881ff435ba0 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 14 Jul 2017 21:54:46 +0200 Subject: Background in menu moved to separate class Weird noise added to menu configs to use single MenuBackground instance Initial pause menu added --- config/menus/credits.lua | 9 +++++++-- config/menus/host.lua | 9 +++++++-- config/menus/main.lua | 9 +++++++-- config/menus/pause.lua | 19 ++++++++++++++++++ config/menus/select.lua | 7 ++++++- config/menus/settings.lua | 9 +++++++-- not/Menu.lua | 35 ++++++--------------------------- not/MenuBackground.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++ not/Scene.lua | 4 ++++ 9 files changed, 112 insertions(+), 38 deletions(-) create mode 100644 config/menus/pause.lua create mode 100644 not/MenuBackground.lua diff --git a/config/menus/credits.lua b/config/menus/credits.lua index 6a2ab50..d59b560 100644 --- a/config/menus/credits.lua +++ b/config/menus/credits.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Element = require "not.Element" @@ -6,7 +6,12 @@ local Element = require "not.Element" local width, height = love.graphics.getWidth()/getRealScale(), love.graphics.getHeight()/getRealScale() local bx = width/2-29 +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, Button(menu) :setText("Go back") :setPosition(bx,144) @@ -21,4 +26,4 @@ return { love.graphics.printf("A game by the Awesomenauts community including:\nSeltzy, PlasmaWisp, ParaDoX, MilkingChicken, Burningdillo, Bronkey and Aki.\n\n04font was used.\n\nBased on a game by Jan Willem Nijman, Paul Veer and Bits_Beats XOXO.\n\nAwesomenauts is property of Ronimo Games.", (x-110)*scale, (y+10)*scale, 220, "left", 0, scale, scale) end) , -} \ No newline at end of file +} diff --git a/config/menus/host.lua b/config/menus/host.lua index fff683c..a180736 100644 --- a/config/menus/host.lua +++ b/config/menus/host.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -11,7 +11,12 @@ local map_Selector = Selector(menu) require "iconsList" local icons, maps = getMapsIconsList() +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, map_Selector :setPosition(width/2, 40) :setSize(80, 42) @@ -42,4 +47,4 @@ return { self.parent:open("main") end) , -} \ No newline at end of file +} diff --git a/config/menus/main.lua b/config/menus/main.lua index d65c57d..85e926f 100644 --- a/config/menus/main.lua +++ b/config/menus/main.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Header = require "not.Header" @@ -9,7 +9,12 @@ local bx = width/2-29 local awesometwo = love.graphics.newImage("assets/two.png") +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, Button(menu) :setText("Start") :setPosition(bx, 80) @@ -56,4 +61,4 @@ return { :setText("Roflnauts") :setPosition(width/2,40) , -} \ No newline at end of file +} diff --git a/config/menus/pause.lua b/config/menus/pause.lua new file mode 100644 index 0000000..74dd043 --- /dev/null +++ b/config/menus/pause.lua @@ -0,0 +1,19 @@ +local menu = ... + +local Button = require "not.Button" + +local width, height = love.graphics.getWidth()/getScale(), love.graphics.getHeight()/getScale() +local bx = width/2-29 + +return { + Button(menu) + :setText("Unpause") + :setPosition(bx, height - 36) + :set("active", function () end) + , + Button(menu) + :setText("Exit") + :setPosition(bx, height - 20) + :set("active", function () end) + , +} diff --git a/config/menus/select.lua b/config/menus/select.lua index 037b798..23f9374 100644 --- a/config/menus/select.lua +++ b/config/menus/select.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -13,7 +13,12 @@ local start_Button = Button(menu) require "iconsList" local nautsIcons, nautsList = getNautsIconsList() +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + return { + background, naut_Selector :setPosition(width/2,60) :setMargin(8) diff --git a/config/menus/settings.lua b/config/menus/settings.lua index 96c12cd..59f9234 100644 --- a/config/menus/settings.lua +++ b/config/menus/settings.lua @@ -1,4 +1,4 @@ -local menu = ... +local menu, background = ... local Button = require "not.Button" local Selector = require "not.Selector" @@ -64,7 +64,12 @@ local controlreleased = function(self, set, action, key) end end +if background == nil or not background:is(require "not.MenuBackground") then + background = require "not.MenuBackground"(menu) +end + local a = { + background, Button(menu) :setText("Keyboard 1") :setPosition(bx,80) @@ -111,4 +116,4 @@ local a = { dimmer } -return a \ No newline at end of file +return a diff --git a/not/Menu.lua b/not/Menu.lua index ea47993..a837045 100644 --- a/not/Menu.lua +++ b/not/Menu.lua @@ -8,12 +8,6 @@ Menu.elements = --[[{not.Element}]]nil Menu.active = 1 Menu.music = --[[not.Music]]nil Menu.sprite = --[[love.graphics.newImage]]nil -Menu.background = --[[love.graphics.newImage]]nil -Menu.asteroids = --[[love.graphics.newImage]]nil -Menu.stars = --[[love.graphics.newImage]]nil -Menu.asteroids_bounce = 0 -Menu.stars_frame = 1 -Menu.stars_delay = 0.8 Menu.allowMove = true Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to manage all quads for animations etc. button = { @@ -30,22 +24,16 @@ Menu.quads = { -- TODO: Could be moved to config file or perhaps QuadManager to }, arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130), arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130), - stars = { - love.graphics.newQuad( 0, 0, 320, 200, 640,200), - love.graphics.newQuad(320, 0, 320, 200, 640,200) - }, } function Menu:new (name) -- Load statically. if Menu.sprite == nil then Menu.sprite = love.graphics.newImage("assets/menu.png") - Menu.background = love.graphics.newImage("assets/backgrounds/menu.png") - Menu.asteroids = love.graphics.newImage("assets/asteroids.png") - Menu.stars = love.graphics.newImage("assets/stars.png") end musicPlayer:setTrack("menu.ogg") musicPlayer:play() + self.elements = {} self:open(name) end @@ -54,8 +42,11 @@ function Menu:delete () end function Menu:open (name) local name = name or "main" self.active = Menu.active --Menu.active is initial - self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self) - self.elements[self.active]:focus() + self.elements = love.filesystem.load(string.format("config/menus/%s.lua", name))(self, self.elements[1]) + -- Common with `next` method. + if not self.elements[self.active]:focus() then + self:next() + end end -- Return reference to quads table and menu sprite @@ -88,24 +79,10 @@ function Menu:update (dt) for _,element in pairs(self.elements) do element:update(dt) end - self.asteroids_bounce = self.asteroids_bounce + dt*0.1 - if self.asteroids_bounce > 2 then self.asteroids_bounce = self.asteroids_bounce - 2 end - self.stars_delay = self.stars_delay - dt - if self.stars_delay < 0 then - self.stars_delay = self.stars_delay + Menu.stars_delay --Menu.stars_delay is initial - if self.stars_frame == 2 then - self.stars_frame = 1 - else - self.stars_frame = 2 - end - end end function Menu:draw () local scale = self.scale local scaler = getRealScale() - love.graphics.draw(self.background, 0, 0, 0, scaler, scaler) - love.graphics.draw(self.stars, self.quads.stars[self.stars_frame], 0, 0, 0, scaler, scaler) - love.graphics.draw(self.asteroids, 0, math.floor(64+math.sin(self.asteroids_bounce*math.pi)*4)*scaler, 0, scaler, scaler) love.graphics.setFont(Font) for _,element in pairs(self.elements) do element:draw(scale) diff --git a/not/MenuBackground.lua b/not/MenuBackground.lua new file mode 100644 index 0000000..83b409c --- /dev/null +++ b/not/MenuBackground.lua @@ -0,0 +1,49 @@ +--- `MenuBackground` +-- Represented as space background with blinking stars and moving asteroids. +-- It might be too specific, but whatever. It is still better than hardcoded background in `Menu` class. +MenuBackground = require "not.Element":extends() + +MenuBackground.BASE_STARS_DELAY = .8 +MenuBackground.QUAD_STARS = { + love.graphics.newQuad( 0, 0, 320, 200, 640,200), + love.graphics.newQuad(320, 0, 320, 200, 640,200) +} + +function MenuBackground:new (parent) + MenuBackground.__super.new(parent) + self.starsFrame = 1 + self.starsDelay = self.BASE_STARS_DELAY + self.asteroidsBounce = 0 + -- Load statically. + if MenuBackground.IMAGE_BACKGROUND == nil then + MenuBackground.IMAGE_BACKGROUND = love.graphics.newImage("assets/backgrounds/menu.png") + MenuBackground.IMAGE_ASTEROIDS = love.graphics.newImage("assets/asteroids.png") + MenuBackground.IMAGE_STARS = love.graphics.newImage("assets/stars.png") + end +end + +function MenuBackground:update (dt) + self.asteroidsBounce = self.asteroidsBounce + dt*0.1 + if self.asteroidsBounce > 2 then + self.asteroidsBounce = self.asteroidsBounce - 2 + end + self.starsDelay = self.starsDelay - dt + if self.starsDelay < 0 then + self.starsDelay = self.starsDelay + self.BASE_STARS_DELAY + if self.starsFrame == 2 then + self.starsFrame = 1 + else + self.starsFrame = 2 + end + end +end + +function MenuBackground:draw () + local scale = self.scale + local scaler = getRealScale() + love.graphics.draw(self.IMAGE_BACKGROUND, 0, 0, 0, scaler, scaler) + love.graphics.draw(self.IMAGE_STARS, self.QUAD_STARS[self.starsFrame], 0, 0, 0, scaler, scaler) + love.graphics.draw(self.IMAGE_ASTEROIDS, 0, math.floor(64+math.sin(self.asteroidsBounce*math.pi)*4)*scaler, 0, scaler, scaler) +end + +return MenuBackground diff --git a/not/Scene.lua b/not/Scene.lua index c770fe4..3324284 100644 --- a/not/Scene.lua +++ b/not/Scene.lua @@ -7,6 +7,10 @@ function Scene:new () self.inputDisabled = false end +function Scene:delete () end +function Scene:update (dt) end +function Scene:draw () end + -- Following setters and getters are a little bit too much, I think. But they do follow general coding directions. function Scene:setSleeping (sleeping) self.sleeping = sleeping -- cgit v1.1