poppy-client/lua/network.lua

158 lines
4.3 KiB
Lua

local player = require("lua.player")
local util = require("server.utils")
local v2_message = {}
v2_message.init = function(clientID, args)
local nickname, args = util.nextStringRecord(args)
local worldID, args = util.nextStringRecord(args)
local x, args = util.nextIntRecord(args)
local y = tonumber(args)
print("Connected as " .. clientID .. " with name " .. nickname)
print("New world is " .. worldID .. " Size is [" .. x .. "|" .. y .. "]")
players[clientID] = player.newPlayer()
camera.target = clientID
_G.clientID = clientID
players[clientID].name = nickname
local world = { id = worldID, x = x, y = y }
gameloop.loadworld(world)
end
v2_message.ping = function(clientID)
gameloop.networkSend("pong")
end
v2_message.playerJoin = function(clientID, name)
ui.addChatEntry("", name .. " Joined the game")
players[clientID] = player.newPlayer()
players[clientID].label = love.graphics.newText(ui.smallFont, name)
players[clientID].name = name
end
v2_message.playerLeave = function(clientID)
if players[clientID] then
local name = players[clientID].name
ui.addChatEntry("", name .. " Left the game")
players[clientID] = nil
end
end
v2_message.moveUpdate = function(clientID, args)
if not players[clientID] then return end
local x, args = util.nextIntRecord(args)
local y, args = util.nextIntRecord(args)
local speedx, args = util.nextIntRecord(args)
local speedy = util.clean(args:gsub("^.-\31",""))
players[clientID].x = x
players[clientID].y = y
players[clientID].speed.x = speedx
players[clientID].speed.y = speedy
end
v2_message.moveFlying = function(clientID, args)
if not players[clientID] then return end
players[clientID].flying = (args == "true")
end
v2_message.deletePhysics = function(clientID, args)
if clientID == camera.target then return end
if not players[clientID] then return end
physics.remove_solid(util.getIntRecords(4, args))
end
v2_message.drawPhysics = function(clientID, args)
if clientID == camera.target then return end
if not players[clientID] then return end
physics.draw_solid(util.getIntRecords(4, args))
end
v2_message.drawTexture = function(clientID, args)
if clientID == camera.target then return end
local x, args = util.nextIntRecord(args)
local y, args = util.nextIntRecord(args)
local layer, args = util.nextStringRecord(args)
local hash, args = util.nextStringRecord(args)
hash = love.data.decode("string", "base64", hash)
local r, args = util.nextIntRecord(args)
local g, args = util.nextIntRecord(args)
local b, a = util.nextIntRecord(args)
a = tonumber(a)
if not r or not g or not b then
r, g, b = 1, 1, 1
end
if not a then
a = 1
end
if not gameloop.textures[hash] then print("Failed to find hash!") return end
love.graphics.setBlendMode("replace")
if layer == "fg" then
love.graphics.setCanvas(gameloop.buffer.pixel.fg)
elseif layer == "bg" then
love.graphics.setCanvas(gameloop.buffer.pixel.bg)
end
if not gameloop.textures[hash].image then love.graphics.newImage(gameloop.textures[hash].data) end
color_push()
love.graphics.setColor(r, g, b ,a)
love.graphics.draw(gameloop.textures[hash].image, x, y)
color_pop()
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas()
end
v2_message.deleteTexture = function(clientID, args)
if clientID == camera.target then return end
local x, args = util.nextIntRecord(args)
local y, args = util.nextIntRecord(args)
local width, args = util.nextIntRecord(args)
local height, layer = util.nextIntRecord(args)
if layer == "fg" then
love.graphics.setCanvas(gameloop.buffer.pixel.fg)
elseif layer == "bg" then
love.graphics.setCanvas(gameloop.buffer.pixel.bg)
end
love.graphics.setBlendMode("replace")
color_push()
love.graphics.setColor(0,0,0,0)
love.graphics.rectangle("fill", x, y, width, height)
color_pop()
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas()
end
v2_message.chatMessage = function(clientID, args)
local name
if clientID == "0" then
name = ""
else
name = players[clientID].name
end
ui.addChatEntry(players[clientID].name, args)
end
v2_message.clearCanvas = function(clientID)
drawing.clearAllCanvases()
end
v2_message.fillCanvas = function(clientID, args)
local layer, hash = util.nextStringRecord(args)
hash = love.data.decode("string", "base64", hash)
drawing.fillCanvas(layer, hash)
end
return v2_message