From c59c08744734a0516a765d792a26950aa5ff73c1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 15 Aug 2016 00:52:25 +0200 Subject: Menu converted to Controller module --- menu.lua | 83 +++++++++++++++++++++++++++++++++++++----------------------- selector.lua | 80 +++++++++++++++++++++++++++++++++------------------------ 2 files changed, 98 insertions(+), 65 deletions(-) diff --git a/menu.lua b/menu.lua index c281d8c..73457b1 100644 --- a/menu.lua +++ b/menu.lua @@ -55,28 +55,50 @@ function Menu:newSelector() end -- Selectors tables getters --- active: with controller; inactive: without controller -function Menu:getSelectorsAll() +-- 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:getController() ~= nil then + 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:getController() == nil then + 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 +end + +-- Tests if Control set is assigned to any selector +function Menu:isSetUsed(set) + for k,selector in pairs(self:getSelectorsActive()) do + if selector:getControlSet() == set then + return true + end + end + return false +end -- Header get bounce move function Menu:getBounce(f) @@ -91,11 +113,11 @@ function Menu:countdownJump() end end --- Get table of nauts currently selected by active selectors +-- Get table of nauts currently selected by locked selectors function Menu:getNauts() local nauts = {} - for _,selector in pairs(self:getSelectorsActive()) do - table.insert(nauts, {selector:getSelectionName(), selector:getController()}) + for _,selector in pairs(self:getSelectorsLocked()) do + table.insert(nauts, {selector:getSelectionName(), selector:getControlSet()}) end return nauts end @@ -109,20 +131,12 @@ end -- LÖVE2D callbacks -- Update function Menu:update(dt) - local state = true - if #self:getSelectorsActive() > 1 then - for _,selector in pairs(self:getSelectorsActive()) do - state = state and selector.state - end - else - state = false - end - if state then + if #self:getSelectorsLocked() > 1 then self.countdown = self.countdown - dt else self.countdown = Menu.countdown -- Menu.countdown is initial end - if state and self.countdown < 0 then + if self.countdown < 0 then self:startGame() end -- Bounce header @@ -140,7 +154,7 @@ function Menu:draw() 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:getSelectorsAll()) do + for _,selector in pairs(self:getSelectors()) do selector:draw() end -- header @@ -161,23 +175,28 @@ end -- Controller callbacks function Menu:controlpressed(set, action, key) - -- pass to selectors - for k,selector in pairs(Menu:getSelectorsAll()) do + -- Pass to active selectors + for k,selector in pairs(self:getSelectorsActive()) do selector:controlpressed(set, action, key) end - -- map selection chaos! - if action == "left" then - if self.map ~= 1 then - self.map = self.map - 1 - else - self.map = #self.maplist + if not self:isSetUsed(set) then + if action == "attack" then + self:getSelectorsInactive()[1]:assignControlSet(set) end - end - if action == "right" then - if self.map ~= #self.maplist then - self.map = self.map + 1 - else - self.map = 1 + -- 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 end end -- speed up the countdown diff --git a/selector.lua b/selector.lua index ae62e13..f869377 100644 --- a/selector.lua +++ b/selector.lua @@ -5,8 +5,8 @@ Selector = { x = 0, y = 0, parent = nil, - controller = nil, - state = false + controlset = nil, + locked = false } function Selector:new(menu) local o = {} @@ -15,6 +15,7 @@ function Selector:new(menu) o.parent = menu return o end +-- Position function Selector:setPosition(x,y) self.x = x self.y = y @@ -22,20 +23,30 @@ end function Selector:getPosition() return self.x, self.y end -function Selector:assignController(controller) - controller:setParent(self) - self.controller = controller +-- Control Sets +function Selector:assignControlSet(set) + self.controlset = set self.naut = 2 end -function Selector:getController() - if self.controller ~= nil then - return self.controller +function Selector:getControlSet() + if self.controlset ~= nil then + return self.controlset end end +-- States +function Selector:getState() + if self:getControlSet() ~= nil then + if self.locked then + return 2 -- has controls and locked + end + return 1 -- has controls but not locked + end + return 0 -- no controls and not locked +end function Selector:clear() - self.controller = nil + self.controlset = nil self.naut = 1 - self.state = false + self.locked = 0 end function Selector:getSelectionName() return self.parent.nauts[self.naut] @@ -53,9 +64,9 @@ function Selector:draw() -- arrows local arrowl = self.parent.portrait_sheet.arrow_left local arrowr = self.parent.portrait_sheet.arrow_right - if not self.state then + if not self.locked then love.graphics.draw(sprite, p.normal, x*scale, y*scale, 0, scale, scale) - if self.controller ~= nil then + if self.controlset ~= nil then love.graphics.draw(sprite, arrowl, (x-2)* scale, (y+13)*scale, 0, scale, scale) love.graphics.draw(sprite, arrowr, (x+30)*scale, (y+13)*scale, 0, scale, scale) end @@ -69,28 +80,31 @@ end -- Controller callbacks function Selector:controlpressed(set, action, key) + -- locals local n = #self.parent.nauts - if control == "left" and not self.state then - if self.naut == 2 or self.naut == 1 then - self.naut = n - else - self.naut = self.naut - 1 - end - elseif control == "right" and not self.state then - if self.naut == n then - self.naut = 2 - else - self.naut = self.naut + 1 - end - elseif control == "attack" then - if self.naut ~= 1 then - self.state = true - end - elseif control == "jump" then - if self.state == true then - self.state = false - else - self.parent:unselectSelector(self) + if set == self:getControlSet() then + if action == "left" and not self.locked then + if self.naut == 2 or self.naut == 1 then + self.naut = n + else + self.naut = self.naut - 1 + end + elseif action == "right" and not self.locked then + if self.naut == n then + self.naut = 2 + else + self.naut = self.naut + 1 + end + elseif action == "attack" then + if self.naut ~= 1 then + self.locked = true + end + elseif action == "jump" then + if self.locked == true then + self.locked = false + else + self.parent:unselectSelector(self) + end end end end -- cgit v1.1