From 796730f857fe69011ada33c865fc3b28e61b49c9 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sun, 3 Jul 2022 00:24:53 +0000 Subject: [PATCH] add temporary way of connecting to host --- src/conf.lua | 4 +-- src/states/main-menu.lua | 65 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/conf.lua b/src/conf.lua index 0100748..02cf689 100644 --- a/src/conf.lua +++ b/src/conf.lua @@ -3,8 +3,8 @@ function love.conf(t) t.console = true - t.window.width = 1280 - t.window.height = 720 + t.window.width = 1280/2 + t.window.height = 720/2 t.window.resizable = true t.modules.joystick = false diff --git a/src/states/main-menu.lua b/src/states/main-menu.lua index 120341a..235cc54 100644 --- a/src/states/main-menu.lua +++ b/src/states/main-menu.lua @@ -4,19 +4,45 @@ local rgb = require("helpers.rgb") local darken = require("helpers.darken") local lighten = require("helpers.lighten") +local socket = require("socket") + +local function splitat(str, char) + local index = str:find(char) + return str:sub(1, index-1), str:sub(index+1) +end + function MainMenu:init() self.ui = UI.new{ - font = love.graphics.newFont(48), + font = love.graphics.newFont(32), text_color = rgb(255, 255, 255), bg_color = rgb(60, 60, 60), bg_hover_color = {lighten(rgb(60, 60, 60))}, bg_pressed_color = {darken(rgb(60, 60, 60))}, } - self.ip_textbox = { text = "127.0.0.1" } + self.addr_textbox = { text = "127.0.0.1:54321" } + + self.server_socket = nil + self.client_socket = nil + + self.hosting = false + self.connecting = false end function MainMenu:update() + if (self.hosting or self.connecting) and not self.server_socket then + self.server_socket = socket.bind("*", 0) + self.server_socket:settimeout(0.05) + end + + if not self.client_socket then + if self.hosting then + self.client_socket = self.server_socket:accept() + elseif self.connecting then + local ip, port = splitat(self.addr_textbox.text, ":") + self.client_socket = socket.connect(ip, port) + end + end end function MainMenu:keypressed(...) @@ -29,9 +55,40 @@ end function MainMenu:draw() local w, h = love.graphics.getDimensions() - self.ui:textbox(self.ip_textbox, w/2-350, h/2-30, 400, 60) - if self.ui:button("Connect", w/2+100, h/2-30, 250, 60) then + + if self.server_socket then + local ip, port = self.server_socket:getsockname() + self.ui:label("server", 0, 0) + self.ui:label(ip, 0, 40) + self.ui:label(port, 0, 80) end + + if self.client_socket then + local ip, port = self.client_socket:getsockname() + self.ui:label("client", 200, 0) + self.ui:label(ip, 200, 40) + self.ui:label(port, 200, 80) + + local peerip, peerport = self.client_socket:getpeername() + self.ui:label("peer", 400, 0) + self.ui:label(peerip, 400, 40) + self.ui:label(peerport, 400, 80) + end + + if self.hosting then + self.ui:label("Hosting...", w/2-100, h/2) + elseif self.connecting then + self.ui:label("Connecting...", w/2-100, h/2) + else + self.ui:textbox(self.addr_textbox, w/2-250, h/2-30, 280, 60) + if self.ui:button("Connect", w/2+50, h/2-30, 200, 60) then + self.connecting = true + end + if self.ui:button("Host", w/2+50, h/2+40, 200, 60) then + self.hosting = true + end + end + self.ui:postDraw() end