-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Network support: added player and nodes synchronization
Removed unnecessary files
- Loading branch information
Showing
13 changed files
with
327 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
########## | ||
.1.......# | ||
.2.......# | ||
########## |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
local logger = require("log") | ||
|
||
local socket = require "socket" | ||
|
||
client = {} | ||
client.connected = false | ||
client.address, client.port = "localhost", 12345 | ||
|
||
client.updaterate = 0.1 | ||
|
||
-- Some caching data | ||
client.data = {} | ||
|
||
local function log(msg) | ||
logger:write("Client: " .. msg) | ||
end | ||
|
||
function client:update(dt) | ||
if (client.connected) then | ||
client.t = client.t + dt | ||
|
||
if (client.t > client.updaterate) then | ||
if (client.data.prev_x ~= player.x or client.data.prev_y ~= player.y) then | ||
client.udp:send(string.format("%s %s %f %f", client.id, 'move', player.x, player.y)) | ||
|
||
client.data.prev_x = player.x | ||
client.data.prev_y = player.y | ||
end | ||
|
||
client.udp:send(string.format("%s %s %d %d %d %d", client.id, 'update', player.x - 10, player.y - 10, 20, 20)) | ||
client.t = client.t - client.updaterate | ||
end | ||
|
||
repeat | ||
local data, msg = client.udp:receive() | ||
if data then | ||
ent, cmd, parms = data:match("^(%S*) (%S*) (.*)") | ||
if cmd == 'move' then | ||
local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$") | ||
assert(x and y) | ||
x, y = tonumber(x), tonumber(y) | ||
players:get(ent).x = x | ||
players:get(ent).y = y | ||
elseif cmd == "addNode" then | ||
local x, y = parms:match("^(%-?[%d.e]*) (%-?[%d.e]*)$") | ||
assert(x and y) | ||
x, y = tonumber(x), tonumber(y) | ||
nodes:addNode(x, y, ent) | ||
else | ||
log("unrecognised command: " .. cmd) | ||
end | ||
elseif msg ~= 'timeout' then | ||
log("Network error: " .. tostring(msg)) | ||
client:disconnect() | ||
end | ||
until not data | ||
end | ||
end | ||
|
||
function client:connect() | ||
client.udp = socket.udp() | ||
client.udp:settimeout(0) | ||
client.udp:setpeername(client.address, client.port) | ||
log("Connecting to server " .. client.address .. " " .. client.port) | ||
|
||
client.id = tostring(math.random(99999)) | ||
client.t = 0 -- update delay | ||
|
||
client.udp:send(string.format("%s %s $", client.id, 'register')) | ||
|
||
client.data = { | ||
prev_x = player.x, | ||
prev_y = player.y | ||
} | ||
|
||
client.connected = true | ||
end | ||
|
||
function client:disconnect() | ||
client.connected = false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
------------------------------------------------------------------------------- | ||
-- Keeps track of logs | ||
------------------------------------------------------------------------------- | ||
|
||
log = {} | ||
|
||
function log:load() | ||
log.file = love.filesystem.newFile("lost.log") | ||
end | ||
|
||
function log:write(message) | ||
log.file:open("a") | ||
log.file:write(message .. "\n") | ||
log.file:close() | ||
end | ||
|
||
return log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.