Compare commits

...

2 Commits

Author SHA1 Message Date
Pascal Abresch bce53de48c adjust default world size 2022-10-10 19:46:38 +02:00
Pascal Abresch 92ea3fe781 Add saving; add escape menu 2022-10-10 19:46:27 +02:00
12 changed files with 193 additions and 58 deletions

View File

@ -1,4 +1,5 @@
love.conf = function(conf)
conf.identity = "Poppy"
conf.window.height = 612
conf.window.minheight = 480
conf.window.width = 1024

View File

@ -4,7 +4,7 @@ local utils = require("shared.utils")
local fonts = require("shared.fonts")
local commands = {}
local sharedCommands = require("lua.sharedCommands")
local sharedCommands = require("shared.commands")
commands.commandsInit = sharedCommands.commandsInit
@ -33,6 +33,16 @@ commands.ping = function(clientID)
end
commands.saveWorld = function(clientID, args)
local status, savename = utils.nextStringRecord(args)
if status == "failed" then
ui.addChatEntry("SERVER", "Failed to save world!")
elseif status == "sucess" then
ui.addChatEntry("SERVER", "saved world. " .. savename)
end
end
commands.playerJoin = function(clientID, args)
local nickname, avatarEncoded = utils.nextStringRecord(args)
avatarHash = love.data.decode("string", "base64", avatarEncoded)

View File

@ -7,10 +7,10 @@ local socket = require "socket"
local utils = require("shared.utils")
local fonts = require("shared.fonts")
local errorHandler = require("shared.error")
local rpc = require("server.rpc")
local constants = require("server.constants")
local errorHandler = require("lua.error")
gameloop.nwChecklist = {}
gameloop.Canvas = {}

View File

@ -1,5 +1,6 @@
local layout = {}
layout.uiState = {}
layout.uiStateMenu = {}
local fonts = require("shared.fonts")
local utils = require("shared.utils")
@ -57,6 +58,15 @@ function layout.cursorSelect(container, x, y, simulate)
end
function layout.cursorSelectMenu(container, x, y, simulate)
x2, y2 = layout.handle(container[1], x, y, simulate)
if not simulate then
table.insert(layout.uiStateMenu, { identifier = container.identifier, kind=container.kind,
x=x, x2=x2, y=y, y2=y2 })
end
return x2, y2
end
function layout.linewidth(container, x, y, simulate)
assert(container.width, "layout.linewidth (container) no container.width provided!")

View File

@ -1,6 +1,5 @@
menu = {}
menu.serverlist = {
{description = "Lenjas server", host = "192.168.178.39", port = "11150"},
{description = "A localhost server", host = "::1", port = "11150"},
{description = "A localhost server [ipv4]", host = "127.0.0.1", port = "11150"}
}

View File

@ -27,10 +27,42 @@ ui.helptext = [[[P] toggle fly
{Cursor keys} Move camera
]]
local menuVisible = false
local textEnabled = false
local textEntry = ""
function ui.keyreleased(key, _)
if key == "escape" then
menuVisible = not menuVisible
if menuVisible then
love.visible = function(visible)
if visible then
love.update = function(dt)
gameloop.networkSync(dt)
physics.update(dt)
camera.update(dt)
love.timer.sleep((1/60) -dt)
end
else
love.update = function(dt)
gameloop.networkSync(dt)
physics.update(dt)
end
end
end
ui.drawMenu(window.x, window.y)
else
love.visible = function(visible)
if visible then
love.update = gameloop.normalVisible
else
love.update = gameloop.reducedVisible
end
end
ui.draw(window.x, window.y)
end
love.visible(true)
return
end
if key == "return" and not love.keyboard.isDown("lalt") then
textEnabled = true
love.keyboard.setTextInput(true)
@ -262,6 +294,21 @@ function ui.helpDrawer(w, h)
}
end
function ui.menuDrawer(w, h)
return {name = "color",
color = { 1, 1, 0.8, 0.8},
{name = "cursorSelectMenu",
identifier = "save",
kind = "button",
{name = "label",
text = "Save world",
font = fonts.bigFont
}
}
}
end
function ui.draw(w, h)
window.x, window.y = w, h
@ -280,52 +327,65 @@ function ui.draw(w, h)
end
function ui.drawMenu(w, h)
window.x, window.y = w, h
layout.uiStateMenu = {}
love.graphics.setCanvas(ui.buffer)
love.graphics.clear()
local menuDrawer = ui.menuDrawer(w, h)
menux, menuy = layout.handle(menuDrawer, 0, 0, true)
layout.handle(menuDrawer, (window.x - menux) /2, (window.y - menuy)/2)
love.graphics.setCanvas()
end
function ui.mousepressed(mousex, mousey)
local textures = gameloop.textures
for i, v in ipairs(layout.uiState) do
if mousex >= v.x and mousex <= v.x2 then
if mousey >= v.y and mousey <= v.y2 then
if v.kind == "picker" then
assert(v.identifier, "No identifier in picker!")
drawing.cursorHash = v.identifier
drawing.cursor = textures[v.identifier]
assert(drawing.cursor, "No cursor texture found!")
ui.draw(window.x, window.y)
return
elseif v.kind == "tab" then
selectedTab = v.identifier
ui.draw(window.x, window.y)
return
elseif v.kind == "colorpicker" then
local scale = 1.0 / granularity
local red = math.floor((mousey - v.y) / v.pointSize) / scale
local green = math.floor((mousex - v.x) / v.pointSize / (scale + 1)) / scale
local blue = (math.floor((mousex - v.x) / v.pointSize) % (scale + 1)) / scale
drawing.color = {red, green, blue, 1}
ui.draw(window.x, window.y)
return
elseif v.kind == "bwColorPicker" then
local scale = 1.0 / granularity
local saturation = math.floor((mousex - v.x) / v.pointSize) / scale
drawing.color = {saturation, saturation, saturation, 1}
ui.draw(window.x, window.y)
return
elseif v.kind == "button" then
if v.identifier == "disconnect" then
gameloop.networkSend(utils.unit("playerLeave"))
menu.init()
return
elseif v.identifier == "increaseUISize" then
ui.sidebarScale = 2
if menuVisible then
for i, v in ipairs(layout.uiStateMenu) do
if mousex >= v.x and mousex <= v.x2 then
if mousey >= v.y and mousey <= v.y2 then
if v.kind == "button" then
if v.identifier == "save" then
gameloop.networkSend("saveWorld")
ui.keyreleased("escape")
end
end
end
end
end
else
for i, v in ipairs(layout.uiState) do
if mousex >= v.x and mousex <= v.x2 then
if mousey >= v.y and mousey <= v.y2 then
if v.kind == "picker" then
assert(v.identifier, "No identifier in picker!")
drawing.cursorHash = v.identifier
drawing.cursor = textures[v.identifier]
assert(drawing.cursor, "No cursor texture found!")
ui.draw(window.x, window.y)
return
elseif v.identifier == "decreaseUISize" then
ui.sidebarScale = 1
elseif v.kind == "tab" then
selectedTab = v.identifier
ui.draw(window.x, window.y)
return
elseif v.kind == "colorpicker" then
local scale = 1.0 / granularity
local red = math.floor((mousey - v.y) / v.pointSize) / scale
local green = math.floor((mousex - v.x) / v.pointSize / (scale + 1)) / scale
local blue = (math.floor((mousex - v.x) / v.pointSize) % (scale + 1)) / scale
drawing.color = {red, green, blue, 1}
ui.draw(window.x, window.y)
return
elseif v.kind == "bwColorPicker" then
local scale = 1.0 / granularity
local saturation = math.floor((mousex - v.x) / v.pointSize) / scale
drawing.color = {saturation, saturation, saturation, 1}
ui.draw(window.x, window.y)
return
end
return
end
return
end
end
end

View File

@ -5,9 +5,11 @@ local rpc = require("server.rpc")
return function(commands)
function commands.drawTexture(clientID, args)
sharedCommands.drawTexture(clientID, args)
broadcast(clientID, util.unit("drawTexture", args))
end
function commands.deleteTexture(clientID, args)
sharedCommands.deleteTexture(clientID, args)
broadcast(clientID, util.unit("deleteTexture", args))
end
function commands.clearCanvas(clientID)

View File

@ -0,0 +1,45 @@
local util = require("shared.utils")
local constants = require("server.constants")
local rpc = require("server.rpc")
local os = require("os")
return function(commands)
function commands.listSaves(clientID, args)
end
function commands.saveWorld(clientID, args)
local handle = io.popen("date -u --rfc-3339=seconds")
local datetime = handle:read("*a")
handle:close()
datetime = datetime:sub(1, -8)
datetime = datetime:gsub("%s+", "_")
datetime = datetime:gsub(":+", "")
print(datetime)
local savename = ""
if args then
savename = args .. "/" .. datetime
else
savename = datetime
end
local directory = "Saves/" .. savename
local result = love.filesystem.createDirectory(directory)
-- TODO check this result...
if result then
serverloop.Canvas.fg:newImageData():encode("png", directory .. "/fg.png")
serverloop.Canvas.bg:newImageData():encode("png", directory .. "/bg.png")
print("saved world. " .. savename)
broadcast("0", util.unit("saveWorld", "sucess", "" .. savename))
return
else
print("Failed to save world!")
return util.unit("saveWorld", "failed", "none")
end
end
function commands.loadWorld(clientID, args)
end
function commands.saveWorldAndLoad(clientID, args)
end
end

View File

@ -4,8 +4,8 @@ return {
protocolVersion = "poppyV003",
world = {
x = 3000,
y = 3000,
x = 768,
y = 768,
id = "NYAAA"
}
}

View File

@ -12,8 +12,11 @@ require("server.commands.players")(commands)
require("server.commands.physics")(commands)
require("server.commands.drawing")(commands)
require("server.commands.chat")(commands)
require("server.commands.saving")(commands)
local errorHandler = require("lua.error")
sharedCommands = require("shared.commands")
local errorHandler = require("shared.error")
local unpriviligedCommands = {}
require("server.unpriviliged_commands.init")(unpriviligedCommands)
@ -107,6 +110,7 @@ function serverloop.init(host)
serverloop.Canvas = {}
serverloop.Canvas.fg = love.graphics.newCanvas(constants.world.x, constants.world.y)
serverloop.Canvas.bg = love.graphics.newCanvas(constants.world.x, constants.world.y)
sharedCommands.commandsInit()
print("Startup!")
love.update = mainloop
love.draw = function()

View File

@ -9,24 +9,27 @@ local textures
function commands.commandsInit()
if gameloop then -- client
CLIENT = true
players = _G.players
textures = gameloop.textures
else
canvas.fg = serverloop.Canvas.fg
canvas.bg = serverloop.Canvas.bg
SERVER = true
players = _G.clients
textures = serverloop.textures
canvas.fg = serverloop.Canvas.fg
canvas.bg = serverloop.Canvas.bg
end
end
function commands.commandsWorldInit()
if gameloop then
if CLIENT then
canvas.fg = gameloop.Canvas.fg
canvas.bg = gameloop.Canvas.bg
end
end
commands.moveUpdate = function(clientID, args)
if not players[clientID] then return end
local x, args = utils.nextIntRecord(args)
@ -48,7 +51,9 @@ end
commands.drawTexture = function(clientID, args)
if clientID == camera.target then return end
if _G.camera then
if clientID == camera.target then return end
end
local x, args = utils.nextIntRecord(args)
local y, args = utils.nextIntRecord(args)
local layer, args = utils.nextStringRecord(args)
@ -58,13 +63,10 @@ commands.drawTexture = function(clientID, args)
local g, args = utils.nextIntRecord(args)
local b, args = utils.nextIntRecord(args)
a = tonumber(args) or 1
if not gameloop.textures[hash] then error("Failed to find hash!") return end
if not textures[hash].image then love.graphics.newImage(textures[hash].data) end
print(x, y, layer, r, g, b, a)
print(hashEncoded)
print(type(canvas))
print(type(canvas[layer]))
if not textures[hash] then error("Failed to find hash!") return end
if not textures[hash].image then
textures[hash].image = love.graphics.newImage(textures[hash].data)
end
love.graphics.setCanvas(canvas[layer])
love.graphics.setBlendMode("alpha")
utils.color_push()
@ -76,7 +78,9 @@ end
commands.deleteTexture = function(clientID, args)
if clientID == camera.target then return end
if _G.camera then
if clientID == camera.target then return end
end
local x, args = utils.nextIntRecord(args)
local y, args = utils.nextIntRecord(args)
local width, args = utils.nextIntRecord(args)