poppy-client/lua/commands.lua

95 lines
2.7 KiB
Lua

local player = require("lua.player")
local utils = require("shared.utils")
local fonts = require("shared.fonts")
return function(commands)
-- arg1 here is the clientID, but the server suplies 0
-- as this is the "origin"
-- don't want to overload/specialcase this, so the clientID is passed
-- as first argument instead.
commands.init = function(_, clientID, nickname, avatarHash, worldID, x, y)
print("Connected as " .. clientID .. " with name " .. nickname)
print("New world is " .. worldID .. " Size is [" .. x .. "|" .. y .. "]")
participants[clientID] = player.newPlayer(
{name = nickname, avatar = levelloop.avatars[avatarHash].image, y=y})
participants[clientID].y = y - participants[clientID].avatar:getHeight()
camera.target = clientID
levelloop.clientID = clientID
local world = { id = worldID, x = x, y = y }
levelloop.loadworld(world)
camera.setplayer()
end
commands.serverQuit = function(clientID, message)
menu.init(message)
end
commands.ping = function(clientID)
levelloop.networkSend("pong")
end
commands.saveWorld = function(clientID, status, savename)
if status == false then
ui.addChatEntry("SERVER", "Failed to save world!")
elseif status == "sucess" then
ui.addChatEntry("SERVER", "saved world. " .. savename)
end
end
commands.playerJoin = function(_, clientID, nickname, avatarHash)
ui.addChatEntry("", nickname .. " Joined the game")
local avatar = { image = love.graphics.newImage("textures/player/nephele/fallback.png") }
if levelloop.avatars[avatarHash] then
avatar = levelloop.avatars[avatarHash]
end
participants[clientID] = player.newPlayer({name = nickname, avatar = avatar.image})
participants[clientID].label = love.graphics.newText(fonts.smallFont, nickname)
end
commands.playerLeave = function(_, clientID)
if participants[clientID] then
local name = participants[clientID].name
ui.addChatEntry("", name .. " Left the game")
participants[clientID] = nil
end
end
commands.deletePhysics = function(clientID, x, y, width, height)
if clientID == camera.target then return end
if not participants[clientID] then return end
physics.remove_solid(x, y, width, height)
end
commands.drawPhysics = function(clientID, x, y, width, height)
if clientID == camera.target then return end
if not participants[clientID] then return end
physics.draw_solid(x, y, width, height)
end
commands.chatMessage = function(clientID, message)
local name
if clientID == "0" then
name = ""
else
if participants[clientID] and participants[clientID].name then
name = participants[clientID].name
else
name = "UNKNOWN?"
end
end
ui.addChatEntry(name, message)
end
end