Skip to content

Commit

Permalink
Network support: added player and nodes synchronization
Browse files Browse the repository at this point in the history
Removed unnecessary files
  • Loading branch information
slemonide committed Dec 20, 2017
1 parent d08d9b9 commit 00405f1
Show file tree
Hide file tree
Showing 13 changed files with 327 additions and 190 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ GPLv3 for everything besides the `lib` folder.
## Libraries used
https://github.com/rinqu-eu/love2d-console

https://github.com/adekto/maid64

https://github.com/tanema/light_world.lua

## Textures

https://opengameart.org/content/dawnlike-16x16-universal-rogue-like-tileset-v181
4 changes: 4 additions & 0 deletions assets/menu/online_menu
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
##########
.1.......#
.2.......#
##########
81 changes: 81 additions & 0 deletions client.lua
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
27 changes: 20 additions & 7 deletions generator.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('xy_map')
require('nodes')
require('player')
require('generator_helpers')

-------------------------------------------------------------------------------
-- World generator
Expand Down Expand Up @@ -36,12 +35,28 @@ function generator:generate()
end

function generator:placeWall(x, y)
nodes:safeAddNode(x, y, "stone wall")
nodes:safeAddNode(x, y, "stone_wall")
end

local function numOpenNeighbours(x, y)
local count = 0

for dx = -1, 1 do
for dy = -1, 1 do
if (nodes:isWalkable(x + dx, y + dy)) then
count = count + 1
end
end
end

return count
end

function generator:placeFloor(x, y)
if (math.random() > 0.01) then
nodes:addNode(x, y, "stone floor")
if (math.random() < 0.9 and numOpenNeighbours(x, y) >= 6) then
nodes:addNode(x, y, "spikes")
else
nodes:addNode(x, y, "stone_floor")

if (math.random() > 0.999) then
candles:add(x, y)
Expand All @@ -55,8 +70,6 @@ function generator:placeFloor(x, y)
ghosts:addPinkGhost(x, y)
end
end
else
nodes:addNode(x, y, "spikes")
end
end

Expand Down Expand Up @@ -179,7 +192,7 @@ function generator:addCave(x, y, points)
generator:addCave(x, y, math.pow(2, math.random(60) + 10))
end
else
nodes:safeAddNode(x, y, "stone floor")
nodes:safeAddNode(x, y, "stone_floor")
node.points = points / 2

local generatorOptions = {
Expand Down
39 changes: 0 additions & 39 deletions generator_helpers.lua

This file was deleted.

102 changes: 0 additions & 102 deletions lib/maid64.lua

This file was deleted.

17 changes: 17 additions & 0 deletions log.lua
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
24 changes: 10 additions & 14 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ require('text')
require('menu')
require('triggers')
require('config')
require('client')
require('server')
require('log')

------------------------
-- Load love
Expand All @@ -19,14 +22,13 @@ DEFAULT_SCALE = 2

function love.load()
math.randomseed(os.time())
log:load()
love.graphics.setDefaultFilter("nearest")
config:load()
textures.load()
nodes:load()

menu:main_menu()
--generator:addCave(0, 0)
--text:write("hello", 0, 0)
end

------------------------
Expand All @@ -39,6 +41,9 @@ function love.update(dt)
draw:updateFade(dt)
generator:generate();
triggers:update(dt)

server:update(dt)
client:update(dt)
end

------------------------
Expand Down Expand Up @@ -94,19 +99,7 @@ function love.draw()
-- need this to render textures properly
love.graphics.setColor(255, 255, 255, 255 * draw.fade)

-- TODO: move nodes, candles, ghosts, etc. into one group so it's easier to iterate
-- Draw nodes
nodes:render()
--[[
nodes:forEach(function(x, y, node)
if (math.abs(x - player.x) * SIZE * draw.scale < love.graphics.getWidth()
and math.abs(y - player.y) * SIZE * draw.scale < love.graphics.getHeight()) then
love.graphics.draw(node.texture.image, node.texture.quad, x * SIZE * draw.scale, y * SIZE * draw.scale, 0, draw.scale, draw.scale)
end
end)
--]]

-- Draw text
text:render()


Expand Down Expand Up @@ -134,6 +127,9 @@ function love.draw()
end
end

-- Draw remote players
players:render()

-- Draw player
love.graphics.draw(textures.player.image, textures.player.quad, player.x * SIZE * draw.scale, player.y * SIZE * draw.scale, 0, draw.scale, draw.scale)
else
Expand Down
Loading

0 comments on commit 00405f1

Please sign in to comment.