diff options
author | Aki <nthirtyone@gmail.com> | 2016-12-20 06:05:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-12-20 06:05:56 +0100 |
commit | 493a89fb354ee6ce667d29a7b96bb4fdd0f6697f (patch) | |
tree | d862856d4b62a6c996ea16211ebd321206e236b4 /menu.lua | |
parent | ed6ad6687aeb5359af48bc97c1dbf5dd66842c9c (diff) | |
parent | 73bcd524906c51d43b22b3ea68c64b38f130bea6 (diff) | |
download | roflnauts-1.0-pre.4.zip roflnauts-1.0-pre.4.tar.gz roflnauts-1.0-pre.4.tar.bz2 |
Merge pull request #16 from nthirtyone/menuv1.0-pre.4
Menu
Diffstat (limited to 'menu.lua')
-rw-r--r-- | menu.lua | 244 |
1 files changed, 72 insertions, 172 deletions
@@ -1,215 +1,115 @@ --- `Menu` --- It is one of the last things I will need to mess around with. I'm happy and surprised everything works so far. --- For sure I have learnt a lot about lua during this journey. Still a lot ahead. I will continue writing in the same style though, to not make it even worse. +-- `Menu` (Scene) +-- It creates single screen of a menu +-- I do know that model I used here and in `World` loading configuration files is not flawless but I did not want to rewrite `World`s one but wanted to keep things similar at least in project scope. --- WHOLE CODE HAS FLAG OF "need a cleanup" +require "music" -require "selector" - --- Metatable of `Menu` +-- Here it begins Menu = { - -- move selectors to one table; make functions to retrieve selectors w or w/o controller - selectors = nil, - nauts = require "nautslist", - portrait_sprite = nil, - portrait_sheet = require "portraits", scale = getScale(), - countdown = 10, - maplist = require "maplist", - map = 1, - header_move = 0 + elements, --table + active = 1, + music, + sprite, + background, + allowMove = true, + quads = { + button = { + normal = love.graphics.newQuad(0, 0, 58,15, 80,130), + active = love.graphics.newQuad(0, 0, 58,15, 80,130) + }, + portrait = { + normal = love.graphics.newQuad( 0, 15, 32,32, 80,130), + active = love.graphics.newQuad(32, 15, 32,32, 80,130) + }, + panorama = { + normal = love.graphics.newQuad(0,47, 80,42, 80,130), + active = love.graphics.newQuad(0,88, 80,42, 80,130) + }, + arrow_l = love.graphics.newQuad(68, 0, 6, 6, 80,130), + arrow_r = love.graphics.newQuad(74, 0, 6, 6, 80,130), + } } - --- Constructor of `Menu` -function Menu:new() - -- Meta +function Menu:new(name) local o = {} setmetatable(o, self) self.__index = self - -- initialize - o.selectors = {} - o.portrait_sprite = love.graphics.newImage("assets/portraits.png") - -- selectors - for i=0,3 do - o:newSelector() - end - -- music - o.music = Music:new("ROFLmenu.ogg") + self.sprite = love.graphics.newImage("assets/menu.png") + self.background = love.graphics.newImage("assets/backgrounds/menu.png") + o.elements = {} + o:load(name) + o.music = Music:new("menu.ogg") return o end - --- Destructor function Menu:delete() self.music:delete() end --- Naut selector -function Menu:newSelector() - local selector = Selector:new(self) - local w, h = love.graphics.getWidth()/self.scale, love.graphics.getHeight()/self.scale - local n = #self.selectors - 1 - table.insert(self.selectors, selector) - local x = (w-76)/2+n*44 - local y = h/2-8 - selector:setPosition(x, y) +-- Load menu from file +function Menu:load(name) + local name = "config/" .. (name or "menumain") .. ".lua" + local menu = love.filesystem.load(name) + self.active = 1 + self.elements = menu(self) + self.elements[self.active]:focus() end --- Selectors tables getters --- all of them -function Menu:getSelectors() - return self.selectors -end --- with control set -function Menu:getSelectorsActive() - local t = {} - for _,selector in pairs(self.selectors) do - if selector:getState() ~= 0 then - table.insert(t, selector) - end - end - return t -end --- without control set -function Menu:getSelectorsInactive() - local t = {} - for _,selector in pairs(self.selectors) do - if selector:getState() == 0 then - table.insert(t, selector) - end - end - return t -end --- with locked character -function Menu:getSelectorsLocked() - local t = {} - for _,selector in pairs(self.selectors) do - if selector:getState() == 2 then - table.insert(t, selector) - end - end - return t +-- Return reference to quads table and menu sprite +function Menu:getSheet() + return self.sprite, self.quads end --- Tests if Control set is assigned to any selector (1.) and if it is locked (2.) -function Menu:isSetUsed(set) - for k,selector in pairs(self:getSelectorsActive()) do - if selector:getControlSet() == set then - if selector:getState() == 2 then - return true, true - else - return true, false - end - end +-- Cycle elements +function Menu:next() + self.elements[self.active]:blur() + self.active = (self.active%#self.elements)+1 + if not self.elements[self.active]:focus() then + self:next() end - return false, false -end - --- Header get bounce move -function Menu:getBounce(f) - local f = f or 1 - return math.sin(self.header_move*f*math.pi) end - --- Speed up countdown -function Menu:countdownJump() - if self.countdown ~= Menu.countdown then -- Menu.countdown is initial - self.countdown = self.countdown - 1 +function Menu:previous() + self.elements[self.active]:blur() + if self.active == 1 then + self.active = #self.elements + else + self.active = self.active - 1 end -end - --- Get table of nauts currently selected by locked selectors -function Menu:getNauts() - local nauts = {} - for _,selector in pairs(self:getSelectorsLocked()) do - table.insert(nauts, {selector:getSelectionName(), selector:getControlSet()}) + if not self.elements[self.active]:focus() then + self:previous() end - return nauts -end - --- WARUDO -function Menu:startGame() - local world = World:new(self.maplist[self.map], self:getNauts()) - changeScene(world) end -- LÖVE2D callbacks --- Update function Menu:update(dt) - if #self:getSelectorsLocked() > 1 then - self.countdown = self.countdown - dt - else - self.countdown = Menu.countdown -- Menu.countdown is initial - end - if self.countdown < 0 then - self:startGame() - end - -- Bounce header - self.header_move = self.header_move + dt - if self.header_move > 2 then - self.header_move = self.header_move - 2 + for _,element in pairs(self.elements) do + element:update(dt) end end --- Draw function Menu:draw() - -- locals - local w, h = love.graphics.getWidth()/self.scale, love.graphics.getHeight()/self.scale local scale = self.scale - -- map selection - love.graphics.setFont(Font) - love.graphics.printf("Map: " .. self.maplist[self.map], (w/2)*scale, (h/2-22)*scale, 150, "center", 0, scale, scale, 75, 4) - -- character selection - for _,selector in pairs(self:getSelectors()) do - selector:draw() - end - -- header - love.graphics.setFont(Bold) - local angle = self:getBounce(2) - local dy = self:getBounce()*4 - love.graphics.printf("ROFLNAUTS2",(w/2)*scale,(32+dy)*scale,336,"center",(angle*5)*math.pi/180,scale,scale,168,12) - -- footer + love.graphics.draw(self.background, 0, 0, 0, getRealScale(), getRealScale()) love.graphics.setFont(Font) - love.graphics.printf("Use W,S,A,D,G,H or Arrows,Enter,Rshift or Gamepad\n\nA game by the Awesomenauts community\nSeltzy, PlasmaWisp, ParaDoX, MilkingChicken, Burningdillo, Bronkey, Aki, 04font\nBased on a game by Jan Willem Nijman, Paul Veer and Bits_Beats XOXO", (w/2)*scale, (h-42)*scale, 336, "center", 0, scale, scale, 168, 4) - -- countdown - local countdown = math.floor(self.countdown) - if self.countdown < Menu.countdown then -- Menu.countdown is initial - love.graphics.setFont(Bold) - love.graphics.print(countdown,(w/2-6.5)*self.scale,(h/2+30)*self.scale,0,self.scale,self.scale) + for _,element in pairs(self.elements) do + element:draw(scale) end end -- Controller callbacks function Menu:controlpressed(set, action, key) - local used, locked = self:isSetUsed(set) - -- Pass to active selectors - for k,selector in pairs(self:getSelectorsActive()) do - selector:controlpressed(set, action, key) - end - if not used then - if action == "attack" then - self:getSelectorsInactive()[1]:assignControlSet(set) + if self.allowMove then + if action == "down" then + self:next() end - -- map selection chaos! - if action == "left" then - if self.map ~= 1 then - self.map = self.map - 1 - else - self.map = #self.maplist - end - end - if action == "right" then - if self.map ~= #self.maplist then - self.map = self.map + 1 - else - self.map = 1 - end + if action == "up" then + self:previous() end end - -- speed up the countdown - if action ~= "jump" then - if set == nil or locked then - self:countdownJump() -- that's funny isn't it? if not jump then jump - end + for _,element in pairs(self.elements) do + element:controlpressed(set, action, key) end end function Menu:controlreleased(set, action, key) + for _,element in pairs(self.elements) do + element:controlreleased(set, action, key) + end end
\ No newline at end of file |