aboutsummaryrefslogtreecommitdiffstats
path: root/Gamestates/Multiplayer
diff options
context:
space:
mode:
Diffstat (limited to 'Gamestates/Multiplayer')
-rw-r--r--Gamestates/Multiplayer/Internet.lua0
-rw-r--r--Gamestates/Multiplayer/Local.lua117
2 files changed, 117 insertions, 0 deletions
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