semi synced multiplayer
This commit is contained in:
@@ -1,29 +1,83 @@
|
||||
local network = require("client")
|
||||
require("client.world")
|
||||
|
||||
local player = {
|
||||
x = 100,
|
||||
y = 100,
|
||||
}
|
||||
function move(player, x, y)
|
||||
local dir = strvec_to_intvec(x,y)
|
||||
player.x = dir.x
|
||||
player.y = dir.y
|
||||
end
|
||||
|
||||
function dir_to_vec(dir)
|
||||
if dir == "up" then
|
||||
return vec(0,-1)
|
||||
elseif dir == "down" then
|
||||
return vec(0,1)
|
||||
elseif dir == "left" then
|
||||
return vec(-1,0)
|
||||
elseif dir == "right" then
|
||||
return vec(1,0)
|
||||
else
|
||||
return vec(0,0)
|
||||
end
|
||||
end
|
||||
|
||||
function strvec_to_intvec(x,y)
|
||||
return vec(tonumber(x), tonumber(y))
|
||||
end
|
||||
|
||||
local client_id = 0
|
||||
|
||||
function love.load()
|
||||
network.connect("127.0.0.1", 9000)
|
||||
|
||||
network.on(network.MSG_ON_CONNECT, function(payload)
|
||||
print("connected to server with id " .. payload)
|
||||
add_player(payload)
|
||||
if client_id == 0 then
|
||||
client_id = payload
|
||||
end
|
||||
end)
|
||||
|
||||
network.on(network.MSG_GAMESTATE, function(payload)
|
||||
print(payload)
|
||||
local cmds = read_command(payload)
|
||||
act_on_command(cmds)
|
||||
end)
|
||||
|
||||
network.on(network.MSG_ON_PLAYER_CONNECT, function(payload)
|
||||
if payload == client_id then return end
|
||||
add_player(payload)
|
||||
for i, v in pairs(world.entities.players) do
|
||||
network.sendPacket(network.MSG_KEY_PRESS, "player/" .. v.client_id .. "/moved/" .. v.x .. "/".. v.y)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function add_player(id)
|
||||
local new_player = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
sprite = 0,
|
||||
client_id = id
|
||||
}
|
||||
|
||||
world.entities.players[new_player.client_id] = new_player
|
||||
|
||||
return new_player
|
||||
end
|
||||
|
||||
function love.keypressed(key)
|
||||
network.sendPacket(network.MSG_KEY_PRESS, client_id .. "/pressed/" .. key)
|
||||
local dir
|
||||
if key == "w" then
|
||||
dir = vec(0,-world.cell_size)
|
||||
elseif key == "a" then
|
||||
dir = vec(-world.cell_size,0)
|
||||
elseif key == "s" then
|
||||
dir = vec(0,world.cell_size)
|
||||
elseif key == "d" then
|
||||
dir = vec(world.cell_size,0)
|
||||
else
|
||||
dir = "right"
|
||||
end
|
||||
network.sendPacket(network.MSG_KEY_PRESS, "player/" .. client_id .. "/moved/" .. world.entities.players[client_id].x + dir.x .. "/".. world.entities.players[client_id].y + dir.y)
|
||||
end
|
||||
|
||||
function love.update()
|
||||
@@ -31,7 +85,46 @@ function love.update()
|
||||
end
|
||||
|
||||
function love.draw()
|
||||
love.graphics.print("Connected as id " .. client_id, 20, 20)
|
||||
for i, v in pairs(world.entities.players) do
|
||||
love.graphics.rectangle("fill", v.x, v.y, world.cell_size, world.cell_size)
|
||||
love.graphics.print(v.client_id, v.x, v.y - world.cell_size - 5)
|
||||
end
|
||||
|
||||
love.graphics.circle("fill", player.x, player.y, 10)
|
||||
end
|
||||
|
||||
function read_command(payload)
|
||||
local cmds = {}
|
||||
local num_cmds = 1
|
||||
|
||||
local s = ""
|
||||
for i in string.gmatch(payload, ".") do
|
||||
if i ~= "/" then
|
||||
s = s .. i
|
||||
else
|
||||
cmds[num_cmds] = s
|
||||
s = ""
|
||||
num_cmds = num_cmds + 1
|
||||
end
|
||||
end
|
||||
cmds[num_cmds] = s
|
||||
return cmds
|
||||
end
|
||||
|
||||
function act_on_command(cmds)
|
||||
if cmds[1] == "player" then
|
||||
-- do player sync
|
||||
for i, v in pairs(world.entities.players) do
|
||||
if v.client_id == cmds[2] then
|
||||
if cmds[3] == "moved" then
|
||||
local player = world.entities.players[cmds[2]]
|
||||
move(player, tonumber(cmds[4]),tonumber(cmds[5]))
|
||||
end
|
||||
end
|
||||
|
||||
if world.entities.players[cmds[2]] == nil then
|
||||
print("adding player")
|
||||
add_player(cmds[2])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user