From 20d03d8917ba38568fc56dc53576efb22c1d459c Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 3 Sep 2017 19:59:32 +0200 Subject: Variables initialized in constructor --- not/Selector.lua | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index ef78778..5e54f6e 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -1,5 +1,3 @@ -require "not.Element" - --- `Selector` -- Used in Menu for selecting various things from list. Works for each Controller set or globally. --[[ @@ -14,29 +12,19 @@ selector:new(menu) :set("global", false) -- true: single selector; false: selector for each controller set present :init() ]] -Selector = Element:extends() - -Selector.width = 0 -Selector.height = 0 -Selector.margin = 0 -Selector.focused = false -Selector.global = false -Selector.delay = 2 -Selector.first = false -Selector.list = --[[]]nil -Selector.sets = --[[]]nil -Selector.locks = --[[]]nil -Selector.selections = --[[]]nil -Selector.shape = "portrait" -Selector.sprite = --[[]]nil -Selector.quads = --[[]]nil -Selector.icons_i = --[[]]nil -Selector.icons_q = --[[]]nil +Selector = require "not.Element":extends() --- Constructor function Selector:new (parent) Selector.__super.new(self, parent) self.sprite, self.quads = parent:getSheet() + self.width = 0 + self.height = 0 + self.margin = 0 + self.focused = false + self.global = false + self.delay = 2 + self.first = false + self.shape = "portrait" end -- Size of single block -- cgit v1.1 From ecf44e285df0dc5bfae26db50b274cf49ce46576 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 3 Sep 2017 20:01:34 +0200 Subject: Default delay static for Selector --- not/Selector.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 5e54f6e..11e88d3 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -14,6 +14,8 @@ selector:new(menu) ]] Selector = require "not.Element":extends() +Selector.DEFAULT_DELAY = 2 + function Selector:new (parent) Selector.__super.new(self, parent) self.sprite, self.quads = parent:getSheet() @@ -22,7 +24,7 @@ function Selector:new (parent) self.margin = 0 self.focused = false self.global = false - self.delay = 2 + self.delay = Selector.DEFAULT_DELAY self.first = false self.shape = "portrait" end @@ -234,8 +236,8 @@ function Selector:draw (scale) end function Selector:update (dt) self.delay = self.delay + dt - if self.delay > Selector.delay then -- Selector.delay is initial - self.delay = self.delay - Selector.delay + if self.delay > Selector.DEFAULT_DELAY then + self.delay = self.delay - Selector.DEFAULT_DELAY end end -- cgit v1.1 From 4f0a5df7c5a681a153967aca5142bdef65102a62 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 21:03:04 +0200 Subject: First steps in Selector rewrite --- not/Selector.lua | 288 ++++++++++--------------------------------------------- 1 file changed, 52 insertions(+), 236 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 11e88d3..896f98f 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -1,272 +1,88 @@ --- `Selector` --- Used in Menu for selecting various things from list. Works for each Controller set or globally. ---[[ -How to use `Selector` in `Menu` config file? -selector:new(menu) - :setPosition(x, y) - :setMargin(8) -- each block has marigin on both sides; they do stack - :setSize(32, 32) -- size of single graphics frame - :set("list", require "nautslist") - :set("icons_i", love.graphics.newImage("assets/portraits.png")) - :set("icons_q", require "portraits") - :set("global", false) -- true: single selector; false: selector for each controller set present - :init() -]] +-- Element for selecting variable from list. Selector = require "not.Element":extends() Selector.DEFAULT_DELAY = 2 +Selector.SHAPE_PORTRAIT = 1 +Selector.SHAPE_PANORAMA = 2 -function Selector:new (parent) +function Selector:new (group, parent) Selector.__super.new(self, parent) - self.sprite, self.quads = parent:getSheet() - self.width = 0 - self.height = 0 - self.margin = 0 - self.focused = false - self.global = false + self.atlas, self.quads = parent:getSheet() + self.group = group self.delay = Selector.DEFAULT_DELAY - self.first = false - self.shape = "portrait" + self.shape = Selector.SHAPE_PORTRAIT + self.focused = false + self.locked = false + self.index = 1 end --- Size of single block +-- TODO: See `not/Element@getSize`. function Selector:getSize () - return self.width, self.height -end -function Selector:setSize (width, height) - self.width, self.height = width, height - return self -end - --- Spacing between two blocks -function Selector:getMargin () - return self.margin -end -function Selector:setMargin (margin) - self.margin = margin - return self -end - --- Initialize Selector with current settings. -function Selector:init () - -- Make sure that there is list present - if self.list == nil then - self.list = {} + if self.shape == Selector.SHAPE_PORTRAIT then + return 32, 32 end - -- Initialize global Selector - if self.global then - self.sets = {} - self.locks = {false} - self.selections = {1} - -- Initialize Selector for Controllers - else - self.sets = Controller.getSets() - self.locks = {} - self.selections = {} - for n=1,#self.sets do - self.locks[n] = false - self.selections[n] = 1 - end + if self.shape == Selector.SHAPE_PANORAMA then + return 80, 42 end - return self -end - --- Cycle through list on given number -function Selector:next (n) - local current = self.selections[n] - self:setSelection(n, current + 1) -end -function Selector:previous (n) - local current = self.selections[n] - self:setSelection(n, current - 1) end --- Get number associated with a given set -function Selector:checkNumber (set) - if self.global then return 1 end -- For global Selector - for n,check in pairs(self.sets) do - if check == set then return n end +--- Makes sure that n is in <1, total> range. +local +function limit (n, total) + if n > total then + return limit(n - total, total) end -end - --- Check if given number is locked -function Selector:isLocked (n) - local n = n or 1 - return self.locks[n] -end - --- Sets value of selection of given number. Returns old. -function Selector:setSelection (n, new) - -- Functception. It sounds like fun but it isn't. - local function limit(new, total) - if new > total then - return limit(new - total, total) - elseif new < 1 then - return limit(total + new, total) - else - return new - end + if n < 1 then + return limit(n + total, total) end - local n = n or 1 - local old = self.selections[n] - self.selections[n] = limit(new, #self.list) - return old + return n end --- Get value of selection of given number -function Selector:getSelection (n) - local n = n or 1 - return self.selections[n] -end - --- Get value from list by selection -function Selector:getListValue (i) - return self.list[i] -end - --- Checks if selection of given number is unique within Selector scope. -function Selector:isUnique (n) - local selection = self:getSelection(n) - for fn,v in pairs(self.selections) do - if fn ~= n and self:isLocked(fn) and v == selection then - return false - end - end - return true +--- Chooses item with an index. +-- @param index selected item's index +-- @return old index +function Selector:setIndex (index) + local old = self.index + self.index = limit(index, #self.list) + return old end --- Get list of selections, checks if not locked are allowed. -function Selector:getFullSelection (allowed) - local allowed = allowed - if allowed == nil then allowed = false end - local t = {} - for n,v in pairs(self.selections) do - local name = self:getListValue(self:getSelection(n)) - local locked = self:isLocked(n) - if locked or allowed then - local a = {name} - if self.sets[n] then table.insert(a, self.sets[n]) end - table.insert(t, a) - end - end - return t +--- Returns selected item's value. +-- @return item selected from list +function Selector:getSelected () + return self.list[self.index] end --- Rolls and returns random selection from list that is not locked. -function Selector:rollRandom (avoids) - -- Me: You should make it simpler. - -- Inner me: Nah, it works. Leave it. - -- Me: Ok, let's leave it as it is. - local avoids = avoids or {} - local total = #self.list - local random = love.math.random(1, total) - local eligible = true - for _,avoid in ipairs(avoids) do - if random == avoid then - eligible = false - break - end - end - if not eligible or self:isLocked(random) then - table.insert(avoids, random) - return self:rollRandom(avoid) - else - return random +--- Checks if selection is locked and returns item's value. +-- @return false if not locked, value from list if locked +function Selector:getLocked () + if self.locked then + return self:getSelected() end + return false end --- Draw single block of Selector -function Selector:drawBlock (n, x, y, scale) - if self.quads == nil or self.sprite == nil then return end - local x, y = x or 0, y or 0 - local name = self:getListValue(self:getSelection(n)) - local locked = self:isLocked(n) - local sprite = self.sprite - local quad = self.quads - local icon = self.icons_i - local iconq = self.icons_q[name] - local w,h = self:getSize() - local unique = self:isUnique(n) - if unique then - love.graphics.setColor(255, 255, 255, 255) - else - love.graphics.setColor(140, 140, 140, 255) - end - if not locked then - love.graphics.draw(sprite, quad[self.shape].normal, x*scale, y*scale, 0, scale, scale) - else - love.graphics.draw(sprite, quad[self.shape].active, x*scale, y*scale, 0, scale, scale) - end - love.graphics.draw(icon, iconq, (x+2)*scale, (y+3)*scale, 0, scale, scale) - if self.focused then - local dy = (h-6)/2 - if not locked then - love.graphics.draw(sprite, quad.arrow_l, (x+0-2-math.floor(self.delay))* scale, (y+dy)*scale, 0, scale, scale) - love.graphics.draw(sprite, quad.arrow_r, (x+w-4+math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) - else - love.graphics.draw(sprite, quad.arrow_r, (x+0-2-math.floor(self.delay))* scale, (y+dy)*scale, 0, scale, scale) - love.graphics.draw(sprite, quad.arrow_l, (x+w-4+math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) +--- Checks if Selected value is unique in group's scope. +function Selector:isUnique () + if self.group then + local locked = group:callEachBut(self, "getLocked") + for _,lock in pairs(locked) do + if lock then + return false + end end end - if (self:getSelection(n) ~= 1 or self.first) then - love.graphics.setFont(Font) - love.graphics.setColor(255, 255, 255, 255) - love.graphics.printf(string.upper(name), (x-w)*scale, (y+h+1)*scale, w*3, "center", 0, scale, scale) - end + return true end --- Menu callbacks -function Selector:focus () -- Called when Element gains focus +function Selector:focus () self.focused = true return true -end -function Selector:blur () -- Called when Element loses focus - self.focused = false -end - --- LÖVE2D callbacks -function Selector:draw (scale) - local x,y = self:getPosition() - local margin = self:getMargin() - local width = self:getSize() - x = x - #self.selections*0.5*(margin+margin+width) - for n=1,#self.selections do - self:drawBlock(n, x+(margin+width)*(n-1)+margin*n, y, scale) - end -end -function Selector:update (dt) - self.delay = self.delay + dt - if self.delay > Selector.DEFAULT_DELAY then - self.delay = self.delay - Selector.DEFAULT_DELAY - end end --- Controller callbacks --- TODO: Add action to perform when key is pressed and selector is locked in e.g. to move into character selection from map selection. -function Selector:controlpressed (set, action, key) - if set and self.focused then - local n = self:checkNumber(set) - local locked = self:isLocked(n) - if action == "left" and not locked then self:previous(n) end - if action == "right" and not locked then self:next(n) end - if action == "attack" then - local name = self:getListValue(self:getSelection(n)) - if name == "random" then - self:setSelection(n, self:rollRandom({1,2})) -- avoid empty naut - self.locks[n] = true - else - -- If not empty or if first is allowed. Additionaly must be unique selection. - if (self:getSelection(n) ~= 1 or self.first) and self:isUnique(n) then - self.locks[n] = true - end - end - end - if action == "jump" then - if locked then - self.locks[n] = false - end - end - end +function Selector:blur () + self.focused = false end return Selector -- cgit v1.1 From 87e0b1eb45117ced574df07534f6666470f218a7 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 21:10:43 +0200 Subject: Minor getLocked and isUnique changes --- not/Selector.lua | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 896f98f..fa00286 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -55,22 +55,21 @@ function Selector:getSelected () end --- Checks if selection is locked and returns item's value. --- @return false if not locked, value from list if locked +-- Nil returning part is there to clarify method's behaviour. +-- @return nil if not locked, value from list if locked function Selector:getLocked () if self.locked then return self:getSelected() end - return false + return nil end --- Checks if Selected value is unique in group's scope. function Selector:isUnique () if self.group then - local locked = group:callEachBut(self, "getLocked") - for _,lock in pairs(locked) do - if lock then - return false - end + -- In this case next is used to determine if table returned by call is empty. + if next(group:callEachBut(self, "getLocked")) then + return false end end return true -- cgit v1.1 From 3f353552da6bb67aa1e7018399e62c7eace9b8c2 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 21:14:36 +0200 Subject: Clearing up getLocked, docs changes --- not/Selector.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index fa00286..9083b24 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -49,22 +49,21 @@ function Selector:setIndex (index) end --- Returns selected item's value. --- @return item selected from list +-- @return item selected from the list function Selector:getSelected () return self.list[self.index] end --- Checks if selection is locked and returns item's value. --- Nil returning part is there to clarify method's behaviour. --- @return nil if not locked, value from list if locked +-- @return item selected from the list if locked, nil otherwise function Selector:getLocked () if self.locked then return self:getSelected() end - return nil end --- Checks if Selected value is unique in group's scope. +-- @return boolean answering question function Selector:isUnique () if self.group then -- In this case next is used to determine if table returned by call is empty. -- cgit v1.1 From fd07ad0d87bc836f14c7be4bcb4042db78afdd95 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 22:00:58 +0200 Subject: Basic draw for Selector --- not/Selector.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 9083b24..1c13e76 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -74,6 +74,10 @@ function Selector:isUnique () return true end +function Selector:getText () + return tostring(self:getSelected()) +end + function Selector:focus () self.focused = true return true @@ -83,4 +87,23 @@ function Selector:blur () self.focused = false end +-- TODO: Temporary function to determine quad to use. Will be obsolete when BoxElement will be done. See also `not/Element@getSize`. +function Selector:getShapeString () + if self.shape == Selector.SHAPE_PORTRAIT then + return "portrait" + end + if self.shape == Selector.SHAPE_PANORAMA then + return "panorama" + end +end + +function Selector:draw (scale) + local x, y = self:getPosition() + local w, h = self:getSize() + love.graphics.setColor(255, 255, 255, 255) + love.graphics.draw(self.atlas, self.quads[self:getShapeString()].normal, x*scale, y*scale, 0, scale, scale) + love.graphics.setFont(Font) + love.graphics.printf(self:getText(), (x-w)*scale, (y+h+1)*scale, w*3, "center", 0, scale, scale) +end + return Selector -- cgit v1.1 From 48f8e2e9b19a074306a9db896b84b27b21a2cc99 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 22:23:32 +0200 Subject: Testing new Selector and added update, locked drawing --- not/Selector.lua | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 1c13e76..d96c2f9 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -6,10 +6,11 @@ Selector.DEFAULT_DELAY = 2 Selector.SHAPE_PORTRAIT = 1 Selector.SHAPE_PANORAMA = 2 -function Selector:new (group, parent) +function Selector:new (list, group, parent) Selector.__super.new(self, parent) self.atlas, self.quads = parent:getSheet() self.group = group + self.list = list self.delay = Selector.DEFAULT_DELAY self.shape = Selector.SHAPE_PORTRAIT self.focused = false @@ -100,10 +101,21 @@ end function Selector:draw (scale) local x, y = self:getPosition() local w, h = self:getSize() + local boxType = "normal" + if self:getLocked() then + boxType = "active" + end love.graphics.setColor(255, 255, 255, 255) - love.graphics.draw(self.atlas, self.quads[self:getShapeString()].normal, x*scale, y*scale, 0, scale, scale) + love.graphics.draw(self.atlas, self.quads[self:getShapeString()][boxType], x*scale, y*scale, 0, scale, scale) love.graphics.setFont(Font) love.graphics.printf(self:getText(), (x-w)*scale, (y+h+1)*scale, w*3, "center", 0, scale, scale) end +function Selector:update (dt) + self.delay = self.delay + dt + if self.delay > Selector.DEFAULT_DELAY then + self.delay = self.delay - Selector.DEFAULT_DELAY + end +end + return Selector -- cgit v1.1 From 6d81b4c2e65593218bb2a8ee322d083e362c7ab1 Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 22:37:47 +0200 Subject: Basic interaction and changes to lock mechanic --- not/Selector.lua | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index d96c2f9..00cf5a0 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -14,7 +14,7 @@ function Selector:new (list, group, parent) self.delay = Selector.DEFAULT_DELAY self.shape = Selector.SHAPE_PORTRAIT self.focused = false - self.locked = false + self.lock = false self.index = 1 end @@ -56,9 +56,9 @@ function Selector:getSelected () end --- Checks if selection is locked and returns item's value. --- @return item selected from the list if locked, nil otherwise +-- @return item selected from the list if Selector is locked, nil otherwise function Selector:getLocked () - if self.locked then + if self.lock then return self:getSelected() end end @@ -118,4 +118,24 @@ function Selector:update (dt) end end +function Selector:controlpressed (set, action, key) + if set and self.focused then + if not self.lock then + if action == "left" then + self:setIndex(self.index - 1) + end + if action == "right" then + self:setIndex(self.index + 1) + end + if action == "attack" then + self.lock = true + end + end + + if action == "jump" then + self.lock = false + end + end +end + return Selector -- cgit v1.1 From 1392fe0404f19decab197064c88ca4dc0ad9c12d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 4 Sep 2017 22:58:37 +0200 Subject: Minor interaction changes; draw arrows --- not/Selector.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index 00cf5a0..c6ab810 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -98,15 +98,30 @@ function Selector:getShapeString () end end +-- TODO: Selector draw is missing box content drawing. function Selector:draw (scale) local x, y = self:getPosition() local w, h = self:getSize() + local boxType = "normal" if self:getLocked() then boxType = "active" end + love.graphics.setColor(255, 255, 255, 255) love.graphics.draw(self.atlas, self.quads[self:getShapeString()][boxType], x*scale, y*scale, 0, scale, scale) + + if self.focused then + local dy = (h-6)/2 + local al, ar = self.quads.arrow_r, self.quads.arrow_l + if self.lock then + al, ar = ar, al + end + + love.graphics.draw(self.atlas, ar, (x+0-2-math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) + love.graphics.draw(self.atlas, al, (x+w-4+math.floor(self.delay))*scale, (y+dy)*scale, 0, scale, scale) + end + love.graphics.setFont(Font) love.graphics.printf(self:getText(), (x-w)*scale, (y+h+1)*scale, w*3, "center", 0, scale, scale) end @@ -127,6 +142,7 @@ function Selector:controlpressed (set, action, key) if action == "right" then self:setIndex(self.index + 1) end + -- TODO: Extend functionality on attack action in Selector. if action == "attack" then self.lock = true end -- cgit v1.1 From c0589d55340b948f107c7317f0f216e6ef408792 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 00:34:06 +0200 Subject: That will draw content for selector's box for now --- not/Selector.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index c6ab810..d6910fe 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -98,7 +98,6 @@ function Selector:getShapeString () end end --- TODO: Selector draw is missing box content drawing. function Selector:draw (scale) local x, y = self:getPosition() local w, h = self:getSize() @@ -111,6 +110,11 @@ function Selector:draw (scale) love.graphics.setColor(255, 255, 255, 255) love.graphics.draw(self.atlas, self.quads[self:getShapeString()][boxType], x*scale, y*scale, 0, scale, scale) + -- TODO: That is one way to draw icon for selected value. Find better one. See: `config/menus/host`. + if self.icons_atlas and self.icons_quads then + love.graphics.draw(self.icons_atlas, self.icons_quads[self.index], (x+2)*scale, (y+3)*scale, 0, scale, scale) + end + if self.focused then local dy = (h-6)/2 local al, ar = self.quads.arrow_r, self.quads.arrow_l -- cgit v1.1 From 8e51db223ed1e8307d1009e48becc618a924749b Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 00:44:32 +0200 Subject: Darken not unique selections --- not/Selector.lua | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index d6910fe..ed1813f 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -67,9 +67,11 @@ end -- @return boolean answering question function Selector:isUnique () if self.group then - -- In this case next is used to determine if table returned by call is empty. - if next(group:callEachBut(self, "getLocked")) then - return false + local locked = self.group:callEachBut(self, "getLocked") + for _,value in pairs(locked) do + if value == self:getSelected() then + return false + end end end return true @@ -108,13 +110,17 @@ function Selector:draw (scale) end love.graphics.setColor(255, 255, 255, 255) + if not self:isUnique() then + love.graphics.setColor(120, 120, 120, 255) + end love.graphics.draw(self.atlas, self.quads[self:getShapeString()][boxType], x*scale, y*scale, 0, scale, scale) - -- TODO: That is one way to draw icon for selected value. Find better one. See: `config/menus/host`. if self.icons_atlas and self.icons_quads then love.graphics.draw(self.icons_atlas, self.icons_quads[self.index], (x+2)*scale, (y+3)*scale, 0, scale, scale) end + love.graphics.setColor(255, 255, 255, 255) + if self.focused then local dy = (h-6)/2 local al, ar = self.quads.arrow_r, self.quads.arrow_l -- cgit v1.1 From 975e5663c09f0d74b2d0a18a7a5ce1297362a4a2 Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 02:36:08 +0200 Subject: Moved selector's action handlers outside controlpressed method --- not/Selector.lua | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index ed1813f..bd6224b 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -145,23 +145,32 @@ end function Selector:controlpressed (set, action, key) if set and self.focused then - if not self.lock then - if action == "left" then - self:setIndex(self.index - 1) - end - if action == "right" then - self:setIndex(self.index + 1) - end - -- TODO: Extend functionality on attack action in Selector. - if action == "attack" then - self.lock = true - end + local handler = self[action] + if handler then + handler(self) end + end +end - if action == "jump" then - self.lock = false - end +function Selector:left () + if not self.lock then + self:setIndex(self.index - 1) + end +end + +function Selector:right () + if not self.lock then + self:setIndex(self.index + 1) end end +function Selector:attack () + self.lock = true +end + +-- Selector doesn't actually jump, haha, I tricked you! +function Selector:jump () + self.lock = false +end + return Selector -- cgit v1.1 From 84f6ec47be56d397fe140b5707b76078917fe8fb Mon Sep 17 00:00:00 2001 From: Aki Date: Tue, 5 Sep 2017 03:49:33 +0200 Subject: Random naut selection is back! --- not/Selector.lua | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'not/Selector.lua') diff --git a/not/Selector.lua b/not/Selector.lua index bd6224b..ee6f0e3 100644 --- a/not/Selector.lua +++ b/not/Selector.lua @@ -49,6 +49,23 @@ function Selector:setIndex (index) return old end +function Selector:rollRandom (exclude) + local exclude = exclude or {} + local index = love.math.random(1, #self.list) + local elgible = true + for _,i in ipairs(exclude) do + if index == i then + elgible = false + break + end + end + if not elgible or not self:isUnique(self.list[index]) then + table.insert(exclude, index) + return self:rollRandom(exclude) + end + return index +end + --- Returns selected item's value. -- @return item selected from the list function Selector:getSelected () @@ -64,12 +81,14 @@ function Selector:getLocked () end --- Checks if Selected value is unique in group's scope. +-- @param index optional parameter to fill in place of currently selected item -- @return boolean answering question -function Selector:isUnique () +function Selector:isUnique (item) + local item = item or self:getSelected() if self.group then local locked = self.group:callEachBut(self, "getLocked") for _,value in pairs(locked) do - if value == self:getSelected() then + if value == item then return false end end -- cgit v1.1