diff options
author | piernov <piernov@piernov.org> | 2014-05-03 21:54:48 +0200 |
---|---|---|
committer | piernov <piernov@piernov.org> | 2014-05-03 21:54:48 +0200 |
commit | a9d658bf88c9ea04c1eb8e39914a444b2e75f2e0 (patch) | |
tree | b5e1eac4069c07674a7fae33fa77761305bfd89e | |
parent | 4d1ec298b7286f213702769063f20254ceff3913 (diff) | |
download | Mastermind-a9d658bf88c9ea04c1eb8e39914a444b2e75f2e0.tar.gz Mastermind-a9d658bf88c9ea04c1eb8e39914a444b2e75f2e0.tar.bz2 Mastermind-a9d658bf88c9ea04c1eb8e39914a444b2e75f2e0.tar.xz Mastermind-a9d658bf88c9ea04c1eb8e39914a444b2e75f2e0.zip |
Start working on Multiplayer mode + makes menu button bigger\
Android NSD is used when OS is Android, must be used in conjunction with piernov/android-love-sdl2's android-nsd branch on Android 4.1+\
lua-enet is used for networking, peer:send()'s flag should be reliable
-rw-r--r-- | Gamestates/Menu.lua | 2 | ||||
-rw-r--r-- | Gamestates/Multiplayer.lua | 24 | ||||
-rw-r--r-- | Gamestates/Multiplayer/Internet.lua | 0 | ||||
-rw-r--r-- | Gamestates/Multiplayer/Local.lua | 117 |
4 files changed, 142 insertions, 1 deletions
diff --git a/Gamestates/Menu.lua b/Gamestates/Menu.lua index 7e82857..cf612bd 100644 --- a/Gamestates/Menu.lua +++ b/Gamestates/Menu.lua @@ -7,7 +7,7 @@ local Gamestates = require "Gamestates/Init" 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, 10)}} then + if Gui.Button{text = name, size = {Utils.percentCoordinates(80, 20)}} then Gamestate.switch(Gamestates[name]) end end diff --git a/Gamestates/Multiplayer.lua b/Gamestates/Multiplayer.lua index e69de29..879a100 100644 --- a/Gamestates/Multiplayer.lua +++ b/Gamestates/Multiplayer.lua @@ -0,0 +1,24 @@ +local Multiplayer = { + Local = require "Gamestates/Multiplayer/Local", + Internet = require "Gamestates/Multiplayer/Internet"} + +local Gui = require "Quickie" +local Utils = require "Utils" + +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 + +function Multiplayer:draw() + Gui.core.draw() +end + + +return Multiplayer diff --git a/Gamestates/Multiplayer/Internet.lua b/Gamestates/Multiplayer/Internet.lua new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Gamestates/Multiplayer/Internet.lua diff --git a/Gamestates/Multiplayer/Local.lua b/Gamestates/Multiplayer/Local.lua new file mode 100644 index 0000000..aaa13f6 --- /dev/null +++ b/Gamestates/Multiplayer/Local.lua @@ -0,0 +1,117 @@ +local Local = {} + +require "enet" + +local Gui = require "Quickie" +local Utils = require "Utils" + +local connect, connected = false, false +Local.server_host = { text = "" } +Local.myservice = { name = "Mastermind", ip = "*", port = "5678" } + +if love.system.getOS() == "Android" then + Local.services = {} + + + function love.handlers.serviceregistered(a,b,c,d) + SDL.log("ServiceRegistered: " .. a) + end + + function love.handlers.discoverystarted(a,b,c,d) + SDL.log("Discovery Started: " .. a) + end + + function love.handlers.serviceresolved(a,b,c,d) + SDL.log("Service Discovered: " .. a .. b .. c .. d) + a = a:gsub("\\032", " ") + if a == Local.myservice.name then + Local.myservice.ip = c + Local.myservice.port = d + else + Local.services[a] = { ip = c, port = d} + end + end + + function love.handlers.servicelost(a,b,c,d) + SDL.log("Service Lost: " .. a .. b) + Local.services[a] = nil + end +end + +function Local:init() + Local.host = enet.host_create(Local.myservice.ip .. ":" .. Local.myservice.port) + + if love.system.getOS() == "Android" then + love.android.registerService(Local.myservice.name, "_http._tcp.", Local.myservice.port) + love.android.discoverServices("_http._tcp.") + end +end + +function Local:update(dt) + + if not connect then + 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 + end + 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{} + elseif 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 + elseif event.type == "disconnect" then + print("Disconnect " .. tostring(event.peer)) + end + + event = Local.host:service() + end + end +end + +function Local:draw() + Gui.core.draw() +end + +function Local:keypressed(key, code) + Gui.keyboard.pressed(key) +end + +-- LÖVE 0.9 +function love.textinput(str) + Gui.keyboard.textinput(str) +end + +return Local |