diff options
Diffstat (limited to 'Gamestates/Solo.lua')
-rw-r--r-- | Gamestates/Solo.lua | 121 |
1 files changed, 69 insertions, 52 deletions
diff --git a/Gamestates/Solo.lua b/Gamestates/Solo.lua index 8d50ad6..7cd85da 100644 --- a/Gamestates/Solo.lua +++ b/Gamestates/Solo.lua @@ -1,62 +1,57 @@ +--[[ + Gamestates/Solo.lua + Solo mode + by piernov +]]-- + local GUI = { InGame = require("GUI/InGame")} +local Game = require("Gamestates/Game") local Solo = {} -Solo.Keypressed = {{}} -Solo.Answer = {} -Solo.Hints = {} - +function Solo:enter() -- Initialize or reset variables when entering game + Previous = "Gamestates/Menu" + Solo.Keypressed = {{}} -- 2D array for storage of button pressed + Solo.Hints = {} -- Hints displayed + Solo.state = "playing" -- Default to "playing" but it doesn't mean anything -function Solo:checkKeys() - local found = true - Solo.Hints[#Solo.Hints+1] = {} + Solo.Answer = Game.genAnswer() -- Generate answer - for id, num in ipairs(Solo.Keypressed[#Solo.Keypressed]) do - if num == Solo.Answer[id] then -- Right - table.insert(Solo.Hints[#Solo.Hints], { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill", - Position = { x = 0.795+((#Solo.Hints[#Solo.Hints]-1)%2)*0.0325, y = 0.12+math.floor(#Solo.Hints[#Solo.Hints]/2)*0.0325+(#Solo.Keypressed-1)*0.075}, - Dimension = {width = 0.0225, height = 0.025}, Colors = {0, 0, 171, 255} }) - else -- Misplaced or Wrong - found = false - - for k,v in ipairs(Solo.Answer) do - if num == v then -- Misplaced - table.insert(Solo.Hints[#Solo.Hints], { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill", - Position = { x = 0.795+((#Solo.Hints[#Solo.Hints]-1)%2)*0.0325, y = 0.12+math.floor(#Solo.Hints[#Solo.Hints]/2)*0.0325+(#Solo.Keypressed-1)*0.075}, - Dimension = {width = 0.0225, height = 0.025}, Colors = {171, 171, 171, 255} }) - end - end + -- Debug + if Debug == true then + for k,v in ipairs(Solo.Answer) do + print(k,v) end end - return found + Solo:resize() -- Call the function filling the canvas + + Solo.Music = love.audio.newSource("Resources/BlueMind.ogg") + Solo.Music:setLooping(true) + Solo.Music:play() +end + +function Solo:resize() -- Called in :enter() and when the window is resized + Solo.Display = love.graphics.newCanvas(Screen.width, Screen.height) -- Create the canvas containing base interface to avoid drawing it entirely each frame + love.graphics.setCanvas(Solo.Display) + love.graphics.setFont(Fonts[3]) -- Use font with size 3% by default + Solo.Display:clear() + for id, polygon in ipairs(GUI.InGame.Polygons) do + GUI.InGame.drawPolygon(polygon) + end + love.graphics.setCanvas() end function Solo:init() + Game.init() GUI.InGame.loadInterface() - tmpanswer = { 1, 2, 3, 4, 5, 6 } - - if not love.getVersion then -- Check if LÖVE is older than 0.9.1, getVersion() introduced in version 0.9.1 - love.math.random() -- Throw first value since it's not random in LÖVE 0.9.0 - end - - while #Solo.Answer < 4 do - local n = love.math.random(#tmpanswer) - table.insert(Solo.Answer, tmpanswer[n]) - table.remove(tmpanswer, n) -- table.insert(GUI.InGame.Polygons, { Type = "print", Text = n, Position = {x = 0.1+i*0.01, y = 0.5}, Colors = {255, 255, 255, 255}}) - end - - -- Debug - for k,v in ipairs(Solo.Answer) do - print(k,v) - table.insert(GUI.InGame.Polygons, { Type = "print", Text = v, Position = {x = 0.1+k*0.01, y = 0.5}, Colors = {255, 255, 255, 255}}) - end end function Solo:draw() - for id, polygon in ipairs(GUI.InGame.Polygons) do - GUI.InGame.drawPolygon(polygon) - end + love.graphics.setColor(255, 255, 255, 255) + love.graphics.setBlendMode('premultiplied') + love.graphics.draw(Solo.Display) + love.graphics.setBlendMode('alpha') for num, line in ipairs(Solo.Hints) do for k, poly in ipairs(line) do @@ -69,16 +64,32 @@ function Solo:draw() GUI.InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.50+(id-1)*0.075, y = 0.15+(line-1)*0.075-0.025}, Dimension = {width = 0.04, height = 0.04}, Colors = GUI.InGame.Colors[num] }) end end + + -- Debug + if Debug == true then + for id, num in ipairs(Solo.Answer) do + GUI.InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.08+(id-1)*0.075+0.005, y = 0.12+0.005}, Dimension = {width = 0.035, height = 0.04}, Colors = GUI.InGame.Colors[num] }) -- Display answer + end + end + + if Solo.state == "found" then + GUI.InGame.displayPopup("Found !") + end + + GUI.InGame.drawPolygon({ Type = "print", Text = "Solo", Position = { x = 0.15, y = 0.06}, Colors = {0, 0, 171, 255} }) -- Display "Solo" text + GUI.InGame.drawPolygon({ Type = "print", Text = Config.player_name.text, Position = { x = 0.60, y = 0.06}, Colors = {0, 0, 171, 255} }) -- Display local player's name end -function Solo:mousepressed(x, y, b) - x = x/Screen.width +function Solo:mousepressed(x, y, b) -- Handle mouse + if Solo.state == "found" then return end -- Don't do anything if answer is already found + + x = x/Screen.width -- relative coordinates y = y/Screen.height - if 0.875 < y and y < 0.915 then + if 0.875 < y and y < 0.915 then -- lower part of the screen if x > 0.45 then for i = 0,5 do if 0.50+i*0.075 < x and x < 0.50+i*0.075+0.04 then - if #Solo.Keypressed > 7 or #Solo.Keypressed[#Solo.Keypressed] >= 4 then + if #Solo.Keypressed > 7 or #Solo.Keypressed[#Solo.Keypressed] >= 4 then -- Line already full return else for k,v in pairs(Solo.Keypressed[#Solo.Keypressed]) do @@ -87,20 +98,26 @@ function Solo:mousepressed(x, y, b) end end - table.insert(Solo.Keypressed[#Solo.Keypressed], i+1) + table.insert(Solo.Keypressed[#Solo.Keypressed], i+1) -- Click detected on a color-button end end end - elseif 0.25 < x and x < 0.375 and #Solo.Keypressed <= 7 and #Solo.Keypressed[#Solo.Keypressed] == 4 then - if Solo.checkKeys() then - table.insert(GUI.InGame.Polygons, { Type = "print", Text = "FOUND !", Position = {x = 0.5, y = 0.5}, Colors = {255, 255, 255, 255}}) + elseif 0.25 < x and x < 0.375 and #Solo.Keypressed <= 7 and #Solo.Keypressed[#Solo.Keypressed] == 4 then -- Clik on Ok button + local hints = table.concat(Game.checkKeys(Solo.Keypressed[#Solo.Keypressed], Solo.Answer)) + if hints == "1111" then + Solo.state = "found" else + table.insert(Solo.Hints, Game.addHints(hints, #Solo.Keypressed)) Solo.Keypressed[#Solo.Keypressed+1] = {} end - elseif #Solo.Keypressed <= 7 and 0.075 < x and x < 0.200 and #Solo.Keypressed[#Solo.Keypressed] > 0 then + elseif #Solo.Keypressed <= 7 and 0.075 < x and x < 0.200 and #Solo.Keypressed[#Solo.Keypressed] > 0 then -- Click on reset button Solo.Keypressed[#Solo.Keypressed] = {} end end end +function Solo:leave() + Solo.Music:stop() +end + return Solo |