poppy-client/lua/network.lua

158 lines
4.3 KiB
Lua
Raw Permalink Normal View History

2021-12-31 22:12:53 +00:00
local player = require("lua.player")
2022-06-23 17:33:05 +00:00
local util = require("server.utils")
2022-06-19 10:41:25 +00:00
local v2_message = {}
v2_message.init = function(clientID, args)
local nickname, args = util.nextStringRecord(args)
local worldID, args = util.nextStringRecord(args)
2022-06-23 17:33:05 +00:00
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 .. "]")
2021-12-31 22:12:53 +00:00
players[clientID] = player.newPlayer()
2022-06-23 17:33:05 +00:00
camera.target = clientID
_G.clientID = clientID
2022-06-19 10:41:25 +00:00
2022-06-23 17:33:05 +00:00
players[clientID].name = nickname
2022-06-19 10:41:25 +00:00
local world = { id = worldID, x = x, y = y }
gameloop.loadworld(world)
2021-12-31 22:12:53 +00:00
end
2021-11-25 19:07:26 +00:00
2022-06-19 10:41:25 +00:00
v2_message.ping = function(clientID)
gameloop.networkSend("pong")
2022-01-08 11:00:31 +00:00
end
2022-01-09 09:20:38 +00:00
2022-06-19 10:41:25 +00:00
v2_message.playerJoin = function(clientID, name)
2022-01-08 20:50:45 +00:00
ui.addChatEntry("", name .. " Joined the game")
2022-01-02 20:26:40 +00:00
players[clientID] = player.newPlayer()
2022-01-08 19:58:41 +00:00
players[clientID].label = love.graphics.newText(ui.smallFont, name)
players[clientID].name = name
2021-12-31 22:12:53 +00:00
end
2021-11-25 19:07:26 +00:00
2021-11-11 19:22:55 +00:00
2022-06-19 10:41:25 +00:00
v2_message.playerLeave = function(clientID)
2022-01-08 21:57:59 +00:00
if players[clientID] then
local name = players[clientID].name
ui.addChatEntry("", name .. " Left the game")
players[clientID] = nil
end
2021-11-11 19:22:55 +00:00
end
2020-08-13 19:13:40 +00:00
2022-06-19 10:41:25 +00:00
v2_message.moveUpdate = function(clientID, args)
2022-01-02 20:26:40 +00:00
if not players[clientID] then return end
2022-01-09 09:47:12 +00:00
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",""))
2022-01-03 17:59:16 +00:00
players[clientID].x = x
players[clientID].y = y
players[clientID].speed.x = speedx
players[clientID].speed.y = speedy
end
2022-06-19 10:41:25 +00:00
v2_message.moveFlying = function(clientID, args)
2022-01-09 09:20:38 +00:00
if not players[clientID] then return end
2022-02-25 20:53:28 +00:00
players[clientID].flying = (args == "true")
2022-01-09 09:20:38 +00:00
end
2022-06-19 10:41:25 +00:00
v2_message.deletePhysics = function(clientID, args)
2022-01-08 11:00:31 +00:00
if clientID == camera.target then return end
2022-01-03 17:59:16 +00:00
if not players[clientID] then return end
2022-02-25 20:53:28 +00:00
physics.remove_solid(util.getIntRecords(4, args))
2022-01-03 17:59:16 +00:00
end
2022-01-08 11:00:31 +00:00
2022-06-19 10:41:25 +00:00
v2_message.drawPhysics = function(clientID, args)
2022-01-08 11:00:31 +00:00
if clientID == camera.target then return end
2022-01-03 17:59:16 +00:00
if not players[clientID] then return end
2022-02-25 20:53:28 +00:00
physics.draw_solid(util.getIntRecords(4, args))
2020-08-13 19:13:40 +00:00
end
2021-12-31 23:07:42 +00:00
2022-06-19 10:41:25 +00:00
v2_message.drawTexture = function(clientID, args)
2022-01-07 22:16:24 +00:00
if clientID == camera.target then return end
2022-01-09 09:47:12 +00:00
local x, args = util.nextIntRecord(args)
local y, args = util.nextIntRecord(args)
2022-02-17 11:19:51 +00:00
local layer, args = util.nextStringRecord(args)
local hash, args = util.nextStringRecord(args)
2022-02-18 22:24:21 +00:00
hash = love.data.decode("string", "base64", hash)
2022-02-17 11:19:51 +00:00
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
2022-06-23 17:33:05 +00:00
if not gameloop.textures[hash] then print("Failed to find hash!") return end
2022-01-09 09:47:12 +00:00
love.graphics.setBlendMode("replace")
2022-01-07 22:16:24 +00:00
if layer == "fg" then
2022-06-23 17:33:05 +00:00
love.graphics.setCanvas(gameloop.buffer.pixel.fg)
2022-01-07 22:16:24 +00:00
elseif layer == "bg" then
2022-06-23 17:33:05 +00:00
love.graphics.setCanvas(gameloop.buffer.pixel.bg)
2022-01-07 22:16:24 +00:00
end
2022-06-23 17:33:05 +00:00
if not gameloop.textures[hash].image then love.graphics.newImage(gameloop.textures[hash].data) end
2022-02-17 11:19:51 +00:00
color_push()
love.graphics.setColor(r, g, b ,a)
2022-06-23 17:33:05 +00:00
love.graphics.draw(gameloop.textures[hash].image, x, y)
2022-02-17 11:19:51 +00:00
color_pop()
2022-01-08 11:00:31 +00:00
love.graphics.setBlendMode("alpha")
love.graphics.setCanvas()
end
2022-06-19 10:41:25 +00:00
v2_message.deleteTexture = function(clientID, args)
2022-01-08 11:00:31 +00:00
if clientID == camera.target then return end
2022-01-09 09:47:12 +00:00
local x, args = util.nextIntRecord(args)
local y, args = util.nextIntRecord(args)
local width, args = util.nextIntRecord(args)
local height, layer = util.nextIntRecord(args)
2022-01-08 11:00:31 +00:00
if layer == "fg" then
2022-06-24 17:54:38 +00:00
love.graphics.setCanvas(gameloop.buffer.pixel.fg)
2022-01-08 11:00:31 +00:00
elseif layer == "bg" then
2022-06-24 17:54:38 +00:00
love.graphics.setCanvas(gameloop.buffer.pixel.bg)
2022-01-08 11:00:31 +00:00
end
love.graphics.setBlendMode("replace")
2022-02-25 20:53:28 +00:00
color_push()
2022-01-08 11:00:31 +00:00
love.graphics.setColor(0,0,0,0)
love.graphics.rectangle("fill", x, y, width, height)
color_pop()
2022-02-25 20:53:28 +00:00
love.graphics.setBlendMode("alpha")
2022-01-07 22:16:24 +00:00
love.graphics.setCanvas()
end
2022-01-08 19:58:41 +00:00
2022-06-19 10:41:25 +00:00
v2_message.chatMessage = function(clientID, args)
2022-01-08 20:45:49 +00:00
local name
if clientID == "0" then
name = ""
else
name = players[clientID].name
end
2022-01-08 19:58:41 +00:00
ui.addChatEntry(players[clientID].name, args)
end
2022-06-23 17:33:05 +00:00
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
2022-06-19 10:41:25 +00:00
return v2_message