diff options
| author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 01:15:29 +0200 |
|---|---|---|
| committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2026-06-03 01:15:29 +0200 |
| commit | f08683a3775989e749237cd001a8eaf3193d1684 (patch) | |
| tree | fbda564309b9f0f7c66c0bd68a2e8c5f08ff27f3 /main.lua | |
| parent | cc5b2f31a7abe46147284de869368a0a2c4bcff4 (diff) | |
| download | r6p-f08683a3775989e749237cd001a8eaf3193d1684.tar.xz | |
add main menu
Diffstat (limited to 'main.lua')
| -rw-r--r-- | main.lua | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..238b20b --- /dev/null +++ b/main.lua @@ -0,0 +1,154 @@ +function dump(x, idt) + local p = {} + local idt = idt or 0 + for k, v in pairs(x) do + local s + if type(v) == "table" then + s = dump(v, idt+1) + elseif type(v) == "string" then + s = ("\"%s\""):format(v) + else + s = tostring(v) + end + table.insert(p, ("%s%s = %s"):format((" "):rep(4*(idt+1)), k, s)) + end + return ("{\n%s\n%s}"):format(table.concat(p, ", \n"), (" "):rep(4*idt)) +end + +local client = require("client") +local server = require("server") +local main_menu = require("main_menu") +local ui = require("ui") +local color = ui.color + +local mm +local clt +local active_ui +local loading_text, loading_ui +local error_text, error_ui +local connected = false +local last_status + +local function show_error(msg) + error_text.text:set(msg) + active_ui = error_ui +end + +local function join_game(invite) + local c, err = client.join(invite) + if err then + show_error("Invalid invite") + else + clt = c + active_ui = loading_ui + end +end + +local function disconnect(msg) + client.close(clt) + clt = nil + connected = false + if msg then + show_error(msg) + else + active_ui = mm + end +end + +local function loading_status(status) + loading_text.text:set(status) +end + +local function update_client() + client.update(clt) + + local status = client.status(clt) + if status == "timeout_match" then + disconnect("Failed to connect to match server (match server down?)") + elseif status == "fail_match" then + disconnect("Game not found (invalid invite?)") + elseif status == "timeout_server" then + disconnect("Failed to connect to server (NAT punch failure?)") + elseif status == "fail_server" then + disconnect("Incorrect secret (invalid invite?)") + elseif status == "disco" then + disconnect("Lost connection to server (Server shut down?)") + elseif status ~= last_status then + if status == "active" then + loading_status("Connected to server!") + connected = true + elseif status == "wait_match" then + loading_status("Waiting for match server...") + elseif status == "wait_server" then + loading_status("Waiting for server...") + end + end + + last_status = status +end + +function love.load() + love.graphics.setBackgroundColor(color(0xffffff)) + love.window.setTitle("RAINBOW SIX: PANOPTICON") + + mm = main_menu.create({ + join_game = join_game, + }) + + local loading_box = main_menu.box( + "Loading...", + ui.button("Cancel", function() + disconnect() + end) + ) + loading_text = loading_box.box_title + loading_ui = ui.new(ui.center_x(ui.center_y(loading_box))) + + local error_box = main_menu.box( + "Error", + ui.button("Back", function() + active_ui = mm + end) + ) + error_text = error_box.box_title + error_ui = ui.new(ui.center_x(ui.center_y(error_box))) + + active_ui = mm +end + +function love.mousepressed(x, y, button) + if active_ui then + ui.mousepressed(active_ui, x, y, button) + end +end + +function love.mousereleased(x, y, button) + if active_ui then + ui.mousereleased(active_ui, x, y, button) + end +end + +function love.textinput(text) + if active_ui then + ui.textinput(active_ui, text) + end +end + +function love.keypressed(key) + if active_ui then + ui.keypressed(active_ui, key) + end +end + +function love.update() + if clt then + update_client() + end +end + +function love.draw() + if active_ui then + ui.update(active_ui) + ui.render(active_ui) + end +end |
