From e72226dbf8f61dacc2fe4d18297807faa27b207f Mon Sep 17 00:00:00 2001 From: piernov Date: Mon, 12 May 2014 13:00:01 +0200 Subject: Multiplayer mode + various improvement --- GUI/Common.lua | 30 ------ GUI/InGame.lua | 134 ++++++++++++++---------- GUI/Menu.lua | 8 ++ Gamestates/About.lua | 30 ++++++ Gamestates/Config.lua | 64 ++++++++++++ Gamestates/Game.lua | 65 ++++++++++++ Gamestates/Init.lua | 4 - Gamestates/Menu.lua | 48 ++++++++- Gamestates/Multiplayer.lua | 18 +++- Gamestates/Multiplayer/InGame.lua | 201 ++++++++++++++++++++++++++++++++++++ Gamestates/Multiplayer/Internet.lua | 0 Gamestates/Multiplayer/Local.lua | 165 +++++++++++++++++++++-------- Gamestates/Multiplayer/Protocol.lua | 35 +++++++ Gamestates/Solo.lua | 121 ++++++++++++---------- Resources/BlueMind midlife.ogg | Bin 0 -> 1886704 bytes Resources/BlueMind.ogg | Bin 0 -> 4337148 bytes Resources/BlueMind.png | Bin 0 -> 551 bytes Resources/vermin_vibes_1989.ttf | Bin 0 -> 13248 bytes Utils.lua | 12 ++- conf.lua | 12 ++- main.lua | 55 ++++++++-- 21 files changed, 801 insertions(+), 201 deletions(-) delete mode 100644 GUI/Common.lua create mode 100644 Gamestates/About.lua create mode 100644 Gamestates/Config.lua create mode 100644 Gamestates/Game.lua delete mode 100644 Gamestates/Init.lua create mode 100644 Gamestates/Multiplayer/InGame.lua delete mode 100644 Gamestates/Multiplayer/Internet.lua create mode 100644 Gamestates/Multiplayer/Protocol.lua create mode 100644 Resources/BlueMind midlife.ogg create mode 100644 Resources/BlueMind.ogg create mode 100644 Resources/BlueMind.png create mode 100644 Resources/vermin_vibes_1989.ttf diff --git a/GUI/Common.lua b/GUI/Common.lua deleted file mode 100644 index 5b62ed1..0000000 --- a/GUI/Common.lua +++ /dev/null @@ -1,30 +0,0 @@ -local GUI = {} - -function GUI.drawPolygon(polygon) - if polygon.Colors then - love.graphics.setColor(unpack(polygon.Colors)) - end - - if polygon.LineWidth then - love.graphics.setLineWidth(polygon.LineWidth*(Window.width+Window.height)/2) - end - - if polygon.Type == "rectangle" then - love.graphics.rectangle(polygon.DrawMode, polygon.Position.x*Window.width, polygon.Position.y*Window.height, - polygon.Dimension.width*Window.width, polygon.Dimension.height*Window.height) - - elseif polygon.Type == "polygon" then - love.graphics.polygon(polygon.DrawMode, polygon.Position.x1*Window.width, polygon.Position.y1*Window.height, - polygon.Position.x2*Window.width, polygon.Position.y2*Window.height, - polygon.Position.x3*Window.width, polygon.Position.y3*Window.height, - polygon.Position.x4*Window.width, polygon.Position.y4*Window.height) - - elseif polygon.Type == "line" then - love.graphics.line(polygon.Position.x1*Window.width, polygon.Position.y1*Window.height, - polygon.Position.x2*Window.width, polygon.Position.y2*Window.height) - elseif polygon.Type == "print" then - love.graphics.print(polygon.Text, polygon.Position.x*Window.width, polygon.Position.y*Window.height) - end -end - -return GUI diff --git a/GUI/InGame.lua b/GUI/InGame.lua index 17b1ffd..0f63cb3 100644 --- a/GUI/InGame.lua +++ b/GUI/InGame.lua @@ -1,10 +1,16 @@ +--[[ + GUI/InGame.lua + Game interface + by Roildan & piernov +]]-- + local InGame = {} -InGame.Polygons = {} +InGame.Polygons = {} -- Tableau utilisé pour la fonction InGame.drawPolygon(polygon) -InGame.Colors = { {255, 0, 0, 255}, {255, 128, 0, 255}, {255, 255, 0, 255}, {0, 255, 0, 255}, {128, 128, 128, 255}, {255, 255, 255, 255} } +InGame.Colors = { {255, 0, 0, 255}, {255, 128, 0, 255}, {255, 255, 0, 255}, {0, 255, 0, 255}, {128, 128, 128, 255}, {255, 255, 255, 255} } -- Tableau de 6 couleurs. -function InGame.drawPolygon(polygon) +function InGame.drawPolygon(polygon) -- Fonction pour dessiner. if polygon.Colors then love.graphics.setColor(unpack(polygon.Colors)) end @@ -31,7 +37,15 @@ function InGame.drawPolygon(polygon) end end -function InGame.loadInterface() +function InGame.displayPopup(text) -- Display a popup on the center of the screen + love.graphics.setFont(Fonts[4]) + InGame.drawPolygon({ Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.30, y = 0.40}, Dimension = {width = 0.40, height = 0.20}, Colors = {0, 0, 255, 255}}) + InGame.drawPolygon({ Type = "rectangle", DrawMode = "fill", Position = { x = 0.30, y = 0.40}, Dimension = {width = 0.40, height = 0.20}, Colors = {0, 0, 0, 255}}) + InGame.drawPolygon({ Type = "print", Text = text, Position = {x = 0.45, y = 0.48}, Colors = {255, 255, 255, 255}}) + love.graphics.setFont(Fonts[3]) +end + +function InGame.loadInterface() -- Interface used in both Solo and Multiplayer mode love.graphics.setBackgroundColor(0, 0, 0) -- Inner window border @@ -47,16 +61,76 @@ function InGame.loadInterface() table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "fill", Position = { x = 0.05, y = 0.05}, Dimension = {width = 0.35, height = 0.90}, Colors = {0, 0, 0, 255} }) -- Rectangle plein (noir) permettant de cacher les barres diagonales. table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", Position = { x = 0.05, y = 0.05}, Dimension = {width = 0.35, height = 0.90}, Colors = {0, 0, 171, 255} }) -- Rectangle de la partie gauche. table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.05, y1 = 0.1, x2 = 0.4, y2 = 0.1}, Colors = {0, 0, 171, 255} }) -- Ligne séparant le rectangle précédent en 2 partie. - + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.05, y1 = 0.85, x2 = 0.4, y2 = 0.85}, Colors = {0, 0, 171, 255} })-- Ligne séparant le rectangle précédent en 2 partie. + -- Left box buttons table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", Position = { x = 0.075, y = 0.875}, Dimension = {width = 0.125, height = 0.05}, Colors = {0, 0, 171, 255} }) -- Rectangle du boutton "Reset". - table.insert(InGame.Polygons, { Type = "print", Text = "Reset", Position = { x = 0.075+0.035, y = 0.875+0.01 }, Colors = { 255, 255, 255, 255}}) + table.insert(InGame.Polygons, { Type = "print", Text = "Reset", Position = { x = 0.075+0.025, y = 0.875+0.01 }, Colors = { 255, 255, 255, 255}}) table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", Position = { x = 0.25, y = 0.875}, Dimension = {width = 0.125, height = 0.05}, Colors = {0, 0, 171, 255} }) -- Rectangle du boutton "Valider" - table.insert(InGame.Polygons, { Type = "print", Text = "Valider", Position = { x = 0.25+0.035, y = 0.875+0.01 }, Colors = { 255, 255, 255, 255}}) + table.insert(InGame.Polygons, { Type = "print", Text = "Ok", Position = { x = 0.25+0.04, y = 0.875+0.01 }, Colors = { 255, 255, 255, 255}}) + + -- Right top box + table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.005, DrawMode = "line", Position = { x = 0.45, y = 0.05}, Dimension = {width = 0.50, height = 0.75}, Colors = {0, 0, 171, 255} }) -- Rectangle supérieur de la partie droite. + table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.45, y1 = 0.1, x2 = 0.95, y2 = 0.1}, Colors = {0, 0, 171, 255} }) -- Ligne séparant le rectangle précédent en 2 partie. + + -- Right top box content + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.475-0.0025, y1 = 0.75, x2 = 0.52+5*0.075+0.0025, y2 = 0.75}, Colors = {0, 0, 171, 255} }) -- Barre horizontale en bas. + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.475, y1 = 0.75, x2 = 0.475, y2 = 0.15-0.0025}, Colors = {0, 0, 171, 255} }) -- Grande barre verticale à gauche. + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.9, y1 = 0.15-0.0025, x2 = 0.9, y2 = 0.7}, Colors = {0, 0, 171, 255} }) -- Grande barre verticale à droite. + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.03, Position = { x1 = 0.795, y1 = 0.7-0.01, x2 = 0.9+0.005, y2 = 0.7-0.01}, Colors = {0, 0, 171, 255} }) -- Barre reliant la barre précédente et le rectangle "sécurité". + table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.52, y = 0.66}, Dimension = {width = 0.275, height = 0.05}, Colors = {0, 0, 171, 255} }) -- Rectangle de la "vie" du joueur. + + for o = 0, 8 do + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.5475+0.0275*o, y1 = 0.66, x2 = 0.5475+0.0275*o, y2 = 0.71}, Colors = {0, 0, 171, 255} }) -- Petites séparant le rectangle de la "vie" du joueur. + end + + for j = 0, 6 do + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.77, y1 = 0.15+j*0.075, x2 = 0.785, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) -- 7 petites barres reliant les derniers grands carrés et les petits groupes de 4 petits carrés. + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.86, y1 = 0.15+j*0.075, x2 = 0.9, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) -- 7 barres reiant les groupes de 4 petits carrés avec la grande barrre verticale. + + for i = 0, 3 do + table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.497+i*0.075, y = 0.12+j*0.075}, Dimension = {width = 0.045, height = 0.05}, Colors = {0, 0, 171, 255} }) -- 4 grands rectangles. + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.471+i*0.075, y1 = 0.15+j*0.075, x2 = 0.497+i*0.075, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) -- 4 petites barres reliant entre eux les 4 rectangles précédents. + end + + for k = 0, 1 do + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.785+k*0.075, y1 = 0.1275+j*0.075-0.0005, x2 = 0.785+k*0.075, y2 = 0.1725+j*0.075+0.0005}, Colors = {0, 0, 171, 255} }) -- 14 barres verticales encadrant les 7 groupes de 4 petits carrés. + for l = 0, 1 do + table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "line", Position = { x = 0.795+k*0.0325, y = 0.12+l*0.0325+j*0.075}, Dimension = {width = 0.0225, height = 0.025}, Colors = {0, 0, 171, 255} }) -- 7 groupes de 4 petits rectangles. + end + + for m = 0, 2 do + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.0025, Position = { x1 = 0.785+m*0.0325, y1 = 0.1275+k*0.045+j*0.075, x2 = 0.795+m*0.0325, y2 = 0.1275+k*0.045+j*0.075}, Colors = {0, 0, 171, 255} }) -- 42 petites barres horizontales reliant entre eux les groupes de 4 petits carrés. + end + end + end + -- Right bottom box + table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.005, DrawMode = "fill", Position = { x = 0.45, y = 0.85}, Dimension = {width = 0.50, height = 0.10}, Colors = {0, 0, 0, 255} }) -- Rectangle plein (noir) permettant de cacher les barres diagonales. + table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", Position = { x = 0.45, y = 0.85}, Dimension = {width = 0.50, height = 0.10}, Colors = {0, 0, 171, 255} }) -- Rectangle de la parie droite en bas. + + -- Right bottom box buttons + for i = 0, 5 do + table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "fill", Position = { x = 0.50+i*0.075, y = 0.875}, Dimension = {width = 0.04, height = 0.04}, Colors = InGame.Colors[i+1] }) -- Remplissage des 6 carrés par les couleurs du tableau "InGame.Colors". + table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", LineWidth = 0.01, Position = { x = 0.50+i*0.075, y = 0.875}, Dimension = {width = 0.04, height = 0.04}, Colors = {0, 0, 171, 255} }) -- 6 carrés. + table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.52+i*0.075, y1 = 0.75, x2 = 0.52+i*0.075, y2 = 0.875}, Colors = {0, 0, 171, 255} }) -- 6 barres verticales reliant les 6 carrés avec la barre horizontale plus haut. + if i < 5 then + table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.5+i*0.075+0.04, y1 = 0.895, x2 = 0.52+(i+1)*0.075, y2 = 0.895}, Colors = {0, 0, 171, 255} }) -- 5 barres horizontales reliant les 6 carrées précédents entre eux. + end + end +end + +function InGame.loadMultiplayerInterface() -- Addition elements for Multiplayer -- Left box content table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.375, y1 = 0.15-0.005, x2 = 0.375, y2 = 0.75+0.005}, Colors = {0, 0, 171, 255} }) -- Grande barre verticale à droite. table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.06+0.0025, y1 = 0.15, x2 = 0.08, y2 = 0.15}, Colors = {0, 0, 171, 255} }) -- Petite barre horizontale à gauche du dernier grand carré du haut (plus petite que les autres sinon elle dépassait de la case). + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.15+0.0025, y1 = 0.55, x2 = 0.19, y2 = 0.55}, Colors = {0, 0, 171, 255} }) -- Petite barre horizontale + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.02, Position = { x1 = 0.16, y1 = 0.55, x2 = 0.16, y2 = 0.6}, Colors = {0, 0, 171, 255} }) -- Barre verticale reliant au rectangle suivant. + table.insert(InGame.Polygons, { Type = "rectangle",LineWidth = 0.01, DrawMode = "line", Position = { x = 0.135, y = 0.6}, Dimension = {width = 0.05, height = 0.2}, Colors = {0, 0, 171, 255} }) -- Rectangle de la "vie" du joueur. + + for o = 0, 8 do + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.135, y1 = 0.62+0.02*o, x2 = 0.185, y2 = 0.62+0.02*o}, Colors = {0, 0, 171, 255} }) -- Petites séparant le rectangle de la "vie" du joueur. + end for i = 0, 3 do table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.08+i*0.075, y = 0.12}, Dimension = {width = 0.045, height = 0.05}, Colors = {0, 0, 171, 255} }) -- 4 grands carrés du haut. @@ -84,53 +158,9 @@ function InGame.loadInterface() end end end - - -- Right top box - table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.005, DrawMode = "line", Position = { x = 0.45, y = 0.05}, Dimension = {width = 0.50, height = 0.75}, Colors = {0, 0, 171, 255} }) -- Rectangle supérieur de la partie droite. - table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.45, y1 = 0.1, x2 = 0.95, y2 = 0.1}, Colors = {0, 0, 171, 255} }) -- Ligne séparant le rectangle précédent en 2 partie. -- Right top box content - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.375-0.0025, y1 = 0.75, x2 = 0.52+5*0.075+0.0025, y2 = 0.75}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.475, y1 = 0.75, x2 = 0.475, y2 = 0.15-0.0025}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.9, y1 = 0.15-0.0025, x2 = 0.9, y2 = 0.7}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.03, Position = { x1 = 0.795, y1 = 0.7-0.01, x2 = 0.9+0.005, y2 = 0.7-0.01}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.52, y = 0.66}, Dimension = {width = 0.275, height = 0.05}, Colors = {0, 0, 171, 255} }) - - for j = 0, 6 do - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.77, y1 = 0.15+j*0.075, x2 = 0.785, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.785, y1 = 0.1275+j*0.075-0.0005, x2 = 0.785, y2 = 0.1725+j*0.075+0.0005}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.86, y1 = 0.1275+j*0.075, x2 = 0.86, y2 = 0.1725+j*0.075}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.005, Position = { x1 = 0.86, y1 = 0.15+j*0.075, x2 = 0.9, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) - - for i = 0, 3 do - table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.01, DrawMode = "line", Position = { x = 0.497+i*0.075, y = 0.12+j*0.075}, Dimension = {width = 0.045, height = 0.05}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.471+i*0.075, y1 = 0.15+j*0.075, x2 = 0.497+i*0.075, y2 = 0.15+j*0.075}, Colors = {0, 0, 171, 255} }) - end - - for k = 0, 1 do - for l = 0, 1 do - table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "line", Position = { x = 0.795+k*0.0325, y = 0.12+l*0.0325+j*0.075}, Dimension = {width = 0.0225, height = 0.025}, Colors = {0, 0, 171, 255} }) - end - - for m = 0, 2 do - table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.0025, Position = { x1 = 0.785+m*0.0325, y1 = 0.1275+k*0.045+j*0.075, x2 = 0.795+m*0.0325, y2 = 0.1275+k*0.045+j*0.075}, Colors = {0, 0, 171, 255} }) - end - end - end - - -- Right bottom box - table.insert(InGame.Polygons, { Type = "rectangle", LineWidth = 0.005, DrawMode = "fill", Position = { x = 0.45, y = 0.85}, Dimension = {width = 0.50, height = 0.10}, Colors = {0, 0, 0, 255} }) - table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", Position = { x = 0.45, y = 0.85}, Dimension = {width = 0.50, height = 0.10}, Colors = {0, 0, 171, 255} }) - - -- Right bottom box buttons - for i = 0, 5 do - table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "fill", Position = { x = 0.50+i*0.075, y = 0.875}, Dimension = {width = 0.04, height = 0.04}, Colors = InGame.Colors[i+1] }) - table.insert(InGame.Polygons, { Type = "rectangle", DrawMode = "line", LineWidth = 0.01, Position = { x = 0.50+i*0.075, y = 0.875}, Dimension = {width = 0.04, height = 0.04}, Colors = {0, 0, 171, 255} }) - table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.52+i*0.075, y1 = 0.75, x2 = 0.52+i*0.075, y2 = 0.875}, Colors = {0, 0, 171, 255} }) - if i < 5 then - table.insert(InGame.Polygons, { Type = "line", Position = { x1 = 0.5+i*0.075+0.04, y1 = 0.895, x2 = 0.52+(i+1)*0.075, y2 = 0.895}, Colors = {0, 0, 171, 255} }) - end - end + table.insert(InGame.Polygons, { Type = "line", LineWidth = 0.01, Position = { x1 = 0.375-0.0025, y1 = 0.75, x2 = 0.52+5*0.075+0.0025, y2 = 0.75}, Colors = {0, 0, 171, 255} }) -- Barre horizontale en bas. end return InGame diff --git a/GUI/Menu.lua b/GUI/Menu.lua index 29f3386..d5bb455 100644 --- a/GUI/Menu.lua +++ b/GUI/Menu.lua @@ -1,3 +1,11 @@ +--[[ + GUI/Menu.lua + Main menu button list + by piernov + + Comment: seems useless too… +]]-- + return { Buttons = { "Solo", diff --git a/Gamestates/About.lua b/Gamestates/About.lua new file mode 100644 index 0000000..d65b245 --- /dev/null +++ b/Gamestates/About.lua @@ -0,0 +1,30 @@ +--[[ + Gamestates/About.lua + About page + by piernov +]]-- +local Gui = require "Quickie" +local Utils = require "Utils" + +local About = {} + +function About:enter() + Previous = "Gamestates/Menu" +end + +function About:update(dt) + Gui.group{grow = "down", pos = {Utils.percentCoordinates(10, 10)}, function() + Gui.Label{text = "BlueMind", size = {Utils.percentCoordinates(10, 10)}} + Gui.Label{text = "A Mastermind(tm)-like game", size = {Utils.percentCoordinates(10, 10)}} + Gui.Label{text = "by piernov and Roildan", size = {Utils.percentCoordinates(10, 10)}} + Gui.Label{text = "Music by Choucka", size = {Utils.percentCoordinates(10, 10)}} + + end} +end + +function About:draw() + Gui.core.draw() +end + + +return About diff --git a/Gamestates/Config.lua b/Gamestates/Config.lua new file mode 100644 index 0000000..394b115 --- /dev/null +++ b/Gamestates/Config.lua @@ -0,0 +1,64 @@ +--[[ + Gamestates/Config.lua + User defined configuration + by piernov + + Comment: currently only used for player_name, but maybe allowing other parameters like sound or display settings will be implemented. + A similar file could be used to implement an "High score" feature. +]]-- + +Config = {} +Config.player_name = {} + +local Gui = require "Quickie" +local Utils = require "Utils" + +function Config:loadUserConfig() -- Load user config file + if not love.filesystem.exists("userConfig") then + love.filesystem.write( "userConfig", "") -- Create the save file if it doesn't exists + else + for line in love.filesystem.lines("userConfig") do -- Read line by line + var, arg = line:match("(.*): (.*)") -- Split following the ": " schema + if var == "PLAYERNAME" then + Config.player_name.text = arg -- Handle player_name + end + end + end + if not Config.player_name.text then + Config.player_name.text = "BlueMind" -- Set default player_name if nothing was found in the user config file + end +end + +function Config:enter() + Previous = "Gamestates/Menu" +end + +function Config:update(dt) + Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(20, 20)}} + Gui.Label{text = "Player name", size = {Utils.percentCoordinates(10, 10)}} + Gui.Input{info = Config.player_name, size = {Utils.percentCoordinates(50, 10)}} + if Gui.Button{text = "Save", size = {Utils.percentCoordinates(60, 10)}} then + love.filesystem.write( "userConfig", "PLAYERNAME: " .. Config.player_name.text) -- Write the new name to the config file + end + Gui.group.pop{} +end + +-- Quickie's functions +function Config:draw() + Gui.core.draw() +end + +function Config:keypressed(key, code) + Gui.keyboard.pressed(key) +end + +function love.textinput(str) + Gui.keyboard.textinput(str) +end +-- End + +function Config:leave() + love.keyboard.setTextInput(false) +end + +return Config diff --git a/Gamestates/Game.lua b/Gamestates/Game.lua new file mode 100644 index 0000000..3bd870a --- /dev/null +++ b/Gamestates/Game.lua @@ -0,0 +1,65 @@ +--[[ + Gamestates/Game.lua + Shared game functions between Solo and Multiplayer mode + by piernov + + Comment: this should even get more functions shared like mousepressed. +]]-- + +local Game = {} + +function Game.checkKeys(received, answer) -- Check the submitter answer against the real one + local hints = {} + + for id, num in ipairs(received) do + if tonumber(num) == answer[id] then -- Right + table.insert(hints, 1) + else -- Misplaced or Wrong + for k,v in ipairs(answer) do + if tonumber(num) == v then -- Misplaced + table.insert(hints, 0) + end + end + end + end + + return hints +end + +function Game.addHints(hints, lineNumber) -- Generate hints rectangles from the checkKeys()-returned table + local polygons = {} + for i=1,#hints do + if tonumber(hints:sub(i, i)) == 1 then -- Show "misplaced" hint + table.insert(polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill", + Position = { x = 0.795+((#polygons-1)%2)*0.0325, y = 0.12+math.floor(#polygons/2)*0.0325+(lineNumber-1)*0.075}, + Dimension = {width = 0.0225, height = 0.025}, Colors = {255, 0, 171, 255} }) + else -- Show "well placed" hint + table.insert(polygons, { Type = "rectangle", LineWidth = 0.0025, DrawMode = "fill", + Position = { x = 0.795+((#polygons-1)%2)*0.0325, y = 0.12+math.floor(#polygons/2)*0.0325+(lineNumber-1)*0.075}, + Dimension = {width = 0.0225, height = 0.025}, Colors = {171, 171, 171, 255} }) + end + end + return polygons +end + + +function Game.genAnswer() -- Generate a random answer + local allowedNumbers = { 1, 2, 3, 4, 5, 6 } -- Select between those numbers, only 6 possibility + local tempAnswer = {} + + while #tempAnswer < 4 do -- Pick 4 numbers + local n = love.math.random(#allowedNumbers) -- Randomly in allowedNumbers + table.insert(tempAnswer, allowedNumbers[n]) -- Add the number to the answer + table.remove(allowedNumbers, n) -- And delete it from allowedNumbers to prevent picking the same number a second time + end + + return tempAnswer +end + +function Game.init() + 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 +end + +return Game diff --git a/Gamestates/Init.lua b/Gamestates/Init.lua deleted file mode 100644 index 5f42661..0000000 --- a/Gamestates/Init.lua +++ /dev/null @@ -1,4 +0,0 @@ -return { - Solo = require("Gamestates/Solo"), - Multiplayer = require("Gamestates/Multiplayer") -} diff --git a/Gamestates/Menu.lua b/Gamestates/Menu.lua index cf612bd..37e69a4 100644 --- a/Gamestates/Menu.lua +++ b/Gamestates/Menu.lua @@ -1,13 +1,57 @@ +--[[ + Gamestates/Menu.lua + Main menu + by piernov + + Comment: might not be a good idea to immediately load both Solo and Multiplayer mode. +]]-- + local Menu = {} local Gui = require "Quickie" local Utils = require "Utils" local GUI = { Menu = require("GUI/Menu")} -local Gamestates = require "Gamestates/Init" +local Gamestates = { + Solo = require("Gamestates/Solo"), + Multiplayer = require("Gamestates/Multiplayer"), + About = require("Gamestates/About") +} +Gamestates.Options = Config + + +function Menu:enter() + Previous = nil -- Menu's root + +-- Redefine Quickie's colors + Gui.core.style.color.normal.fg[1] = 0 + Gui.core.style.color.normal.fg[2] = 0 + Gui.core.style.color.normal.fg[3] = 255 + + Gui.core.style.color.normal.bg[1] = 0 + Gui.core.style.color.normal.bg[2] = 0 + Gui.core.style.color.normal.bg[3] = 0 + + Gui.core.style.color.hot.fg[1] = 0 + Gui.core.style.color.hot.fg[2] = 0 + Gui.core.style.color.hot.fg[3] = 255 + + Gui.core.style.color.hot.bg[1] = 48 + Gui.core.style.color.hot.bg[2] = 48 + Gui.core.style.color.hot.bg[3] = 48 + + Gui.core.style.color.active.fg[1] = 0 + Gui.core.style.color.active.fg[2] = 0 + Gui.core.style.color.active.fg[3] = 255 + + Gui.core.style.color.active.bg[1] = 24 + Gui.core.style.color.active.bg[2] = 24 + Gui.core.style.color.active.bg[3] = 24 +-- End +end function Menu:update(dt) Gui.group{grow = "down", pos = {Utils.percentCoordinates(10, 10)}, function() for _, name in ipairs(GUI.Menu.Buttons) do - if Gui.Button{text = name, size = {Utils.percentCoordinates(80, 20)}} then + if Gui.Button{text = name, size = {Utils.percentCoordinates(80, 20)}} then -- Display main menu buttons and switch gamestate if clicked Gamestate.switch(Gamestates[name]) end end diff --git a/Gamestates/Multiplayer.lua b/Gamestates/Multiplayer.lua index 879a100..0347238 100644 --- a/Gamestates/Multiplayer.lua +++ b/Gamestates/Multiplayer.lua @@ -1,18 +1,26 @@ +--[[ + Gamestates/Multiplayer.lua + Multiplayer menu + by piernov + + Comment: created for an hypothetical Internet mode. +]]-- + local Multiplayer = { - Local = require "Gamestates/Multiplayer/Local", - Internet = require "Gamestates/Multiplayer/Internet"} + Local = require "Gamestates/Multiplayer/Local"} local Gui = require "Quickie" local Utils = require "Utils" +function Multiplayer:enter() + Previous = "Gamestates/Menu" +end + function Multiplayer:update(dt) Gui.group{grow = "down", pos = {Utils.percentCoordinates(10, 10)}, function() if Gui.Button{text = "Local", size = {Utils.percentCoordinates(80, 20)}} then Gamestate.switch(Multiplayer.Local) end - if Gui.Button{text = "Internet", size = {Utils.percentCoordinates(80, 20)}} then - Gamestate.switch(Multiplayer.Internet) - end end} end diff --git a/Gamestates/Multiplayer/InGame.lua b/Gamestates/Multiplayer/InGame.lua new file mode 100644 index 0000000..f94be12 --- /dev/null +++ b/Gamestates/Multiplayer/InGame.lua @@ -0,0 +1,201 @@ +--[[ + Gamestates/Multiplayer/InGame.lua + Multiplayer mode + by piernov +]]-- +local GUI = { InGame = require("GUI/InGame")} +local Game = require("Gamestates/Game") +local Protocol = require("Gamestates/Multiplayer/Protocol") + +local InGame = {} + +function InGame:enter(Local) -- Initialize or reset variables when entering game + Previous = "Gamestates/Multiplayer/Local" + InGame.Keypressed = {{}} -- 2D array for storage of button pressed + InGame.Hints = {} -- Hints displayed for local player + InGame.opponentHints = {} -- Hints displayed for remote player + InGame.state = "playing" -- Default to "playing" but it doesn't mean anything + InGame.localLife = {} + InGame.remoteLife = {} + + InGame.host = Local.host + InGame.client = Local.client + + InGame.opponentAnswer = Game.genAnswer() -- Generate answer for the opponent + + for o = 0, 9 do + table.insert(InGame.remoteLife, { Type = "line", LineWidth = 0.015, Position = { x1 = 0.135, y1 = 0.61+0.02*o, x2 = 0.185, y2 = 0.61+0.02*o}, Colors = {171, 0, 0, 255} }) -- Display life bar content + end + + for o = 0, 9 do + table.insert(InGame.localLife, { Type = "line", LineWidth = 0.022, Position = { x1 = 0.535+0.0275*o, y1 = 0.66, x2 = 0.535+0.0275*o, y2 = 0.71}, Colors = {171, 0, 0, 255} }) -- Display life bar content + end + + InGame:resize() -- Call the function filling the canvas + + InGame.Musiclowlife = love.audio.newSource("Resources/BlueMind midlife.ogg") + InGame.Music = love.audio.newSource("Resources/BlueMind.ogg") + InGame.Music:setLooping(true) + InGame.Music:play() +end + +function InGame:resize() -- Called in :enter() and when the window is resized + InGame.Display = love.graphics.newCanvas(Screen.width, Screen.height) -- Create the canvas containing base interface to avoid drawing it entirely each frame + love.graphics.setCanvas(InGame.Display) + love.graphics.setFont(Fonts[3]) -- Use font with size 3% by default + InGame.Display:clear() + for id, polygon in ipairs(GUI.InGame.Polygons) do + GUI.InGame.drawPolygon(polygon) + end + love.graphics.setCanvas() +end + +function InGame:init(host, client) + Game.init() + GUI.InGame.loadInterface() + GUI.InGame.loadMultiplayerInterface() +end + +function InGame:draw() + love.graphics.setColor(255, 255, 255, 255) + love.graphics.setBlendMode('premultiplied') + love.graphics.draw(InGame.Display) + love.graphics.setBlendMode('alpha') + + for num, line in ipairs(InGame.Hints) do + for k, poly in ipairs(line) do + GUI.InGame.drawPolygon(poly) + end + end + + for line, keys in ipairs(InGame.Keypressed) do + for id, num in ipairs(keys) do + 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 + + for num, poly in ipairs(InGame.localLife) do + GUI.InGame.drawPolygon(poly) + end + + for num, poly in ipairs(InGame.remoteLife) do + GUI.InGame.drawPolygon(poly) + end + + for id, num in ipairs(InGame.opponentAnswer) 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] }) -- Top left boxes content displaying opponent answer + end + +-- Display popup for win/loose and found answer + if InGame.state == "found" then + GUI.InGame.displayPopup("Found !") + elseif InGame.state == "not-found" then + GUI.InGame.displayPopup("Not found...") + elseif InGame.state == "win" then + GUI.InGame.displayPopup("You win !") + elseif InGame.state == "loose" then + GUI.InGame.displayPopup("You loose...") + end +-- End + + GUI.InGame.drawPolygon({ Type = "print", Text = InGame.client.name, Position = { x = 0.15, y = 0.06}, Colors = {0, 0, 171, 255} }) -- Display remote player's name + 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 InGame:update(dt) + local event = InGame.host:service() + + while event ~= nil do -- Handle network events + Protocol.parseGlobalEvents(event) + + if event.type == "receive" then + command, args = event.data:match("(.*): (.*)") -- Split received message : ": " + if command == "ANSWER" then + local hints = table.concat(Game.checkKeys({args:match("(%d)(%d)(%d)(%d)")}, InGame.opponentAnswer)) -- Process received answers + if hints == "1111" then -- Only well-placed hints = answer found + event.peer:send("FOUND: TRUE", 0, "reliable") + table.remove(InGame.localLife, 1) -- Loss of 1 life point + if #InGame.localLife < 1 then -- Empty life + InGame.state = "loose" + else + if #InGame.localLife == 2 then + InGame.Music:stop() + InGame.Musiclowlife:setLooping(true) + InGame.Musiclowlife:play() + end + InGame.opponentAnswer = Game.genAnswer() -- Generate a new answer + InGame.opponentHints = {} + end + else + event.peer:send("HINTS: " .. hints, 0, "reliable") + end + elseif command == "FOUND" and args == "TRUE" then + table.remove(InGame.remoteLife, 1) -- Opponent loose 1 life point + if #InGame.remoteLife < 1 then -- Opponent's life is empty, we won + InGame.state = "win" + else + InGame.Keypressed = {{}} + InGame.Hints = {} + InGame.state = "found" + end + elseif command == "FOUND" and args == "FALSE" then + table.remove(InGame.remoteLife, 1) -- Opponent loose 1 life point + InGame.opponentAnswer = Game.genAnswer() -- Generate a new answer + InGame.opponentHints = {} + elseif command == "HINTS" then + table.insert(InGame.Hints, Game.addHints(args, #InGame.Keypressed)) + InGame.Keypressed[#InGame.Keypressed+1] = {} + InGame.state = "playing" + if #InGame.Keypressed > 7 then + table.remove(InGame.localLife, 1) -- Loss of 1 life point + InGame.Keypressed = {{}} + InGame.Hints = {} + InGame.state = "not-found" + event.peer:send("FOUND: FALSE", 0, "reliable") + end + end + end + event = InGame.host:service() -- Process next event + end +end + +function InGame:mousepressed(x, y, b) -- Handle mouse + if InGame.state == "found" or InGame.state == "not-found" then -- Click anywhere to hide the "found" popup message + InGame.state = "playing" + end + + x = x/Screen.width -- relative coordinates + y = y/Screen.height + if 0.875 < y and y < 0.915 and InGame.state ~= "validating" then -- lower part of the screen, and don't do anything if we're waiting for hints from peer + 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 #InGame.Keypressed > 7 or #InGame.Keypressed[#InGame.Keypressed] >= 4 then -- Line already full + return + else + for k,v in pairs(InGame.Keypressed[#InGame.Keypressed]) do + if v == i+1 then + return -- Disallow multiple identical colors in same line + end + end + + table.insert(InGame.Keypressed[#InGame.Keypressed], i+1) -- Click detected on a color-button + end + end + end + elseif 0.25 < x and x < 0.375 and #InGame.Keypressed <= 7 and #InGame.Keypressed[#InGame.Keypressed] == 4 then -- Clik on Ok button + local peer = InGame.host:get_peer(InGame.client.index) + InGame.state = "validating" + peer:send("ANSWER: " .. table.concat(InGame.Keypressed[#InGame.Keypressed]), 0, "reliable") + elseif #InGame.Keypressed <= 7 and 0.075 < x and x < 0.200 and #InGame.Keypressed[#InGame.Keypressed] > 0 then -- Click on reset button + InGame.Keypressed[#InGame.Keypressed] = {} + end + end +end + +function InGame:leave() + InGame.Music:stop() + InGame.Musiclowlife:stop() +end + +return InGame diff --git a/Gamestates/Multiplayer/Internet.lua b/Gamestates/Multiplayer/Internet.lua deleted file mode 100644 index e69de29..0000000 diff --git a/Gamestates/Multiplayer/Local.lua b/Gamestates/Multiplayer/Local.lua index aaa13f6..7158866 100644 --- a/Gamestates/Multiplayer/Local.lua +++ b/Gamestates/Multiplayer/Local.lua @@ -1,3 +1,11 @@ +--[[ + Gamestates/Multiplayer/Local.lua + Local connect menu + by piernov + + Comment: we really miss a Bonjour/Avahi Lua binding, only Android NSD is (barely) supported… +]]-- + local Local = {} require "enet" @@ -5,16 +13,20 @@ require "enet" local Gui = require "Quickie" local Utils = require "Utils" +local Protocol = require "Gamestates/Multiplayer/Protocol" + local connect, connected = false, false Local.server_host = { text = "" } -Local.myservice = { name = "Mastermind", ip = "*", port = "5678" } +Local.myservice = { name = "BlueMind", ip = "*", port = 5678 } +Local.client = {} -if love.system.getOS() == "Android" then +-- Android NSD events callback +if love.system.getOS() == "Android" then -- Won't work on other OS… Local.services = {} - function love.handlers.serviceregistered(a,b,c,d) SDL.log("ServiceRegistered: " .. a) + Local.myservice.name = a end function love.handlers.discoverystarted(a,b,c,d) @@ -37,67 +49,128 @@ if love.system.getOS() == "Android" then Local.services[a] = nil end end +-- End + +function Local:enter() + Previous = "Gamestates/Multiplayer" + connect, connected = false, false +end function Local:init() - Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port) + Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port) -- Open network socket + local i = Local.myservice.port + while not Local.host do -- unable to create socket: most likely port is already used + if i > Local.myservice.port+20 then + print("Error while creating socket") + return 1 + end + Local.myservice.port = Local.myservice.port+1 -- try next port + Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port) + end + + Local.myservice.name = Config.player_name.text -- Get player_name from user config - if love.system.getOS() == "Android" then - love.android.registerService(Local.myservice.name, "_http._tcp.", Local.myservice.port) - love.android.discoverServices("_http._tcp.") + if love.system.getOS() == "Android" then -- Start Android NSD discovery and publish + love.android.registerService(Local.myservice.name, "_bluemind._tcp.", Local.myservice.port) + love.android.discoverServices("_bluemind._tcp.") end end function Local:update(dt) - if not connect then - Gui.group.push{grow = "right"} + Gui.group.push{grow = "right"} - Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}} - if love.system.getOS() == "Android" then - for name, host in pairs(Local.services) do - if Gui.Button{text = name .. " " .. host.ip .. ":" .. host.port, size = {Utils.percentCoordinates(60, 10)}} then - Local.server_host.text = host.ip.. ":" .. host.port - connect = true - return + Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}} + if love.system.getOS() == "Android" then -- List discovered services + for name, host in pairs(Local.services) do + if Gui.Button{text = name .. " " .. host.ip .. ":" .. host.port, size = {Utils.percentCoordinates(60, 10)}} then + Local.server_host.text = host.ip.. ":" .. host.port + if connected then + Local.host:disconnect() + connected = false end + connect = true + return end end + end + +-- Manually entering peer's address + Gui.Label{text = "Host", size = {Utils.percentCoordinates(10, 10)}} + Gui.Input{info = Local.server_host, size = {Utils.percentCoordinates(50, 10)}} + if Gui.Button{text = "Connect", size = {Utils.percentCoordinates(60, 10)}} then + connect = true + end +-- End + +-- Handle connection and display a proposition message + if Local.client.name and not connect then + Gui.Label{text = args .. " connected to you. Play a game ?"} + + Gui.group.push{grow = "right"} + if Gui.Button{text = "Yes", size = {Utils.percentCoordinates(10, 10)}} then + Protocol.acceptGame(Local.host, Local.client.index, true) + Gamestate.switch(require("Gamestates/Multiplayer/InGame")) + end + if Gui.Button{text = "No", size = {Utils.percentCoordinates(10, 10)}} then + Protocol.acceptGame(Local.host, Local.client.index, false) + Local.client = {} + end + Gui.group.pop{} + end +-- End - Gui.Label{text = "Host", size = {Utils.percentCoordinates(10, 10)}} - Gui.Input{info = Local.server_host, size = {Utils.percentCoordinates(50, 10)}} - if Gui.Button{text = "Connect", size = {Utils.percentCoordinates(60, 10)}} then - connect = true - end - Gui.group.pop{} - Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}} - Gui.Label{text = "My infos"} - Gui.Label{text = "Name: " .. Local.myservice.name} - Gui.Label{text = "Host: " .. Local.myservice.ip .. ":" .. Local.myservice.port} - Gui.group.pop{} + Gui.group.pop{} - Gui.group.pop{} - elseif not connected then +-- Show local player name and host + love.graphics.setFont(Fonts[3]) + Gui.group.push{grow = "down", pos = {Utils.percentCoordinates(10, 0)}} + Gui.Label{text = "My infos"} + Gui.Label{text = "Name: " .. Local.myservice.name} + Gui.Label{text = "Host: " .. Local.myservice.ip .. ":" .. Local.myservice.port} + Gui.group.pop{} + love.graphics.setFont(Fonts[4]) +-- End + + Gui.group.pop{} + + if connect and not connected then Local.host:connect(Local.server_host.text) connected = true - else - local event = Local.host:service() - - while event ~= nil do - if event.type == "receive" then - print("Receive ", event.data, event.peer) - elseif event.type == "connect" then - if event.peer:index() > 1 then - print("Already connected") - else - print("Local " .. tostring(event.peer) .. " " .. event.peer:state() .. " " .. tostring(event.peer:connect_id())) + end + + local event = Local.host:service() + + while event ~= nil do -- Process network events + Protocol.parseGlobalEvents(event) + + if event.type == "receive" then + command, args = event.data:match("(.*): (.*)") + if command == "CONNECT" then + Local.client.name = args + event.peer:send("CONNECTED: ".. Local.myservice.name, 0, "reliable") + elseif command == "CONNECTED" then + Local.client.name = args + elseif command == "ACCEPT" then + if args == "YES" then + Gamestate.switch(require("Gamestates/Multiplayer/InGame")) + elseif args == "NO" then + event.peer:disconnect() + connect, connected = false, false + Local.client = {} end - elseif event.type == "disconnect" then - print("Disconnect " .. tostring(event.peer)) end - - event = Local.host:service() + elseif event.type == "connect" then + if event.peer:index() <= 1 then + Local.client.index = event.peer:index() + end + if connected then + event.peer:send("CONNECT: ".. Local.myservice.name, 0, "reliable") + end end + + event = Local.host:service() end end @@ -114,4 +187,8 @@ function love.textinput(str) Gui.keyboard.textinput(str) end +function Local:leave() + love.keyboard.setTextInput(false) +end + return Local diff --git a/Gamestates/Multiplayer/Protocol.lua b/Gamestates/Multiplayer/Protocol.lua new file mode 100644 index 0000000..6f3b739 --- /dev/null +++ b/Gamestates/Multiplayer/Protocol.lua @@ -0,0 +1,35 @@ +--[[ + Gamestates/Multiplayer/Protocol.lua + Some functions involved in talking between 2 clients + by piernov + + Comment: maybe useless. +]]-- +local Protocol = {} + +function Protocol.acceptGame(host, peer_id, accept) -- Send an answer to a connection from another player + local peer = host:get_peer(peer_id) + if accept then + peer:send("ACCEPT: YES", 0, "reliable") + else + peer:send("ACCEPT: NO", 0, "reliable") + peer:disconnect_later() + end +end + +function Protocol.parseGlobalEvents(event) -- Shared function between local menu and game which disallow multiple connections and handle disconnect + if event.type == "connect" then + if event.peer:index() > 1 then + print("Already connected") + event.peer:disconnect(2) + end + elseif event.type == "disconnect" then + print("Disconnect " .. tostring(event.peer)) + if event.data == 2 then + print("Peer is busy") + event.peer:disconnect() + end + end +end + +return Protocol 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 diff --git a/Resources/BlueMind midlife.ogg b/Resources/BlueMind midlife.ogg new file mode 100644 index 0000000..78bf0e0 Binary files /dev/null and b/Resources/BlueMind midlife.ogg differ diff --git a/Resources/BlueMind.ogg b/Resources/BlueMind.ogg new file mode 100644 index 0000000..b80d792 Binary files /dev/null and b/Resources/BlueMind.ogg differ diff --git a/Resources/BlueMind.png b/Resources/BlueMind.png new file mode 100644 index 0000000..d3f7f16 Binary files /dev/null and b/Resources/BlueMind.png differ diff --git a/Resources/vermin_vibes_1989.ttf b/Resources/vermin_vibes_1989.ttf new file mode 100644 index 0000000..b3873ba Binary files /dev/null and b/Resources/vermin_vibes_1989.ttf differ diff --git a/Utils.lua b/Utils.lua index c811e1b..bbae94d 100644 --- a/Utils.lua +++ b/Utils.lua @@ -1,9 +1,17 @@ +--[[ + Utils.lua + Used for unclassified functions + by piernov + + Comment: a bit useless, but still a more convenient than nothing for Quickie's functions +]]-- + local Utils = {} -function Utils.percentCoordinates(x, y) +function Utils.percentCoordinates(x, y) -- Convert percentage to absolute coordinates x = (x/100)*Screen.width y = (y/100)*Screen.height return x, y end -return Utils \ No newline at end of file +return Utils diff --git a/conf.lua b/conf.lua index fa40609..17f7461 100644 --- a/conf.lua +++ b/conf.lua @@ -1,11 +1,17 @@ +--[[ + conf.lua + Core configuration file + by piernov +]]-- + function love.conf(t) t.identity = nil -- The name of the save directory (string) t.version = "0.9.0" -- The LÖVE version this game was made for (string) t.console = false -- Attach a console (boolean, Windows only) - t.window.title = "Mastermind" -- The window title (string) - t.window.icon = nil -- Filepath to an image to use as the window's icon (string) + t.window.title = "BlueMind" -- The window title (string) + t.window.icon = "Resources/BlueMind.png" -- Filepath to an image to use as the window's icon (string) t.window.width = 640 -- The window width (number) t.window.height = 640 -- The window height (number) t.window.borderless = false -- Remove all border visuals from the window (boolean) @@ -15,7 +21,7 @@ function love.conf(t) t.window.fullscreen = false -- Enable fullscreen (boolean) t.window.fullscreentype = "normal" -- Standard fullscreen or desktop fullscreen mode (string) t.window.vsync = true -- Enable vertical sync (boolean) - t.window.fsaa = 4 -- The number of samples to use with multi-sampled antialiasing (number) + t.window.fsaa = 0 -- The number of samples to use with multi-sampled antialiasing (number) t.window.display = 1 -- Index of the monitor to show the window in (number) t.window.highdpi = false -- Enable high-dpi mode for the window on a Retina display (boolean). Added in 0.9.1 t.window.srgb = false -- Enable sRGB gamma correction when drawing to the screen (boolean). Added in 0.9.1 diff --git a/main.lua b/main.lua index d6b4170..b34509a 100644 --- a/main.lua +++ b/main.lua @@ -1,21 +1,62 @@ -require 'love2d-fakecanvas/fakecanvas' +--[[ + main.lua + Main file used by LÖVE to start the game + by piernov +]]-- -Screen = {} +require 'love2d-fakecanvas/fakecanvas' -- Load canvas emulation library + +Debug = false -- Set to true to view the answer in Solo mode + +Screen = {} -- Contains window width and height +Fonts = {} -- Used to store preloaded fonts Gamestate = require "hump.gamestate" +Config = require "Gamestates/Config" Menu = require "Gamestates/Menu" +local min_dt = 1/25 -- Cap FPS to 25, avoid extensive CPU use, snippet from LÖVE Wiki under love.timer.sleep() +local next_time = 0 + function love.load() - Gamestate.registerEvents() - Gamestate.switch(Menu) + Config:loadUserConfig() -- Read user configuration file to load player_name + Screen.width, Screen.height = love.window.getDimensions() + love.resize(Screen.width, Screen.height) -- Call love.resize() in order to preload fonts + love.graphics.setFont(Fonts[4]) -- Use font with size 4% by default + + Gamestate.registerEvents() -- Initialize hump Gamestate library + Gamestate.switch(Menu) -- Switch to menu + + next_time = love.timer.getTime() -- FPS limiting end +function love.resize(w, h) + Screen.width, Screen.height = w, h + Fonts[4] = love.graphics.newFont("Resources/vermin_vibes_1989.ttf", 0.04*Screen.height) + Fonts[3] = love.graphics.newFont("Resources/vermin_vibes_1989.ttf", 0.03*Screen.height) +end + +-- FPS limiting function love.update(dt) - Screen.width, Screen.height = love.window.getDimensions() + next_time = next_time + min_dt +end + +function love.draw() + local cur_time = love.timer.getTime() + if next_time <= cur_time then + next_time = cur_time + return + end + love.timer.sleep(next_time - cur_time) end +-- End function love.keypressed(key) - if key == "escape" then - love.event.quit() + if key == "escape" then -- Handle return key on Android or Escape key on regular keyboard + if Previous then -- Switch to previous entry if we aren't a the menu's root, otherwise quit + Gamestate.switch(require(Previous)) + else + love.event.quit() + end end end -- cgit v1.2.3-54-g00ecf