summaryrefslogtreecommitdiff
path: root/client/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'client/main.lua')
-rw-r--r--client/main.lua23
1 files changed, 12 insertions, 11 deletions
diff --git a/client/main.lua b/client/main.lua
index dbef847..089bc08 100644
--- a/client/main.lua
+++ b/client/main.lua
@@ -1,4 +1,5 @@
local G = love.graphics
+local common = require 'common'
local Camera = require'r.camera'
local Pos = require'r.pos'
local class = require'r.class'
@@ -6,18 +7,16 @@ local enet = require'enet'
local pprint = require'pprint'
local json = require'dkjson'
+local SPEED = 8 -- tiles per second
+local colors = {[true]={141/255,128/255,22/255}, [false]={71/255,50/255,122/255}}
+
local host = enet.host_create()
local conn = host:connect('localhost:19683')
local players = {}
-local chunks = {}
-local colors = {[true]={141/255,128/255,22/255}, [false]={71/255,50/255,122/255}}
-return chunk and chunk[1+p.x*64+p.y] end
-
-local function draw_player(p, c) G.setColor(c) G.circle('fill',p.x,p.y,0.3) end
-
-local SPEED = 8 -- tiles per second
+local chunks = common.ChunkMap()
local lp = { pos=Pos(0,0), movetimer=0, dir=nil } -- local player
+
local directions = {w=Pos(0,-1),a=Pos(-1,0),s=Pos(0,1),d=Pos(1,0)}
function love.keypressed(k,s,r) if directions[s] then lp.dir=s end end
function love.keyreleased(k,s) if s==lp.dir then lp.dir=nil end end
@@ -25,10 +24,11 @@ function lp.update(dt)
lp.movetimer = lp.movetimer - dt
if lp.movetimer <= 0 and lp.dir then
local d = directions[lp.dir] local newpos = lp.pos+d
- if tile(newpos) == false then
+ if chunks:tile(newpos) == false then
lp.pos = newpos
conn:send(json.encode{type='move',x=newpos.x,y=newpos.y})
lp.movetimer = 1 / SPEED end end end
+local function draw_player(p, c) G.setColor(c) G.circle('fill',p.x,p.y,0.3) end
function lp.draw() draw_player(lp.pos, {0,1,0}) end
local cam = Camera(nil, 64)
@@ -40,7 +40,8 @@ function love.update(dt)
if j.type == 'player' then players[j.name] = {name=j.name,pos=Pos(j.x,j.y)}
elseif j.type == 'unplayer' then players[j.from] = nil
elseif j.type == 'move' then players[j.from].pos = Pos(j.x,j.y)
- elseif j.type == 'chunk' then chunk = j.d
+ elseif j.type == 'chunk' then chunks:add(Pos(j.x,j.y),j.d)
+ elseif j.type == 'unchunk' then chunks:remove(Pos(j.x,j.y))
end
end
ev = host:service()
@@ -49,8 +50,8 @@ end
function love.draw()
G.clear(1,1,1); G.origin()
cam.pos = lp.pos; cam:apply_trans()
- if chunk then for x=0,63 do for y=0,63 do
- G.setColor(colors[chunk[1+x*64+y]]) G.rectangle('fill',x-0.5,y-0.5,1,1) end end end
+ -- if chunk then for x=0,63 do for y=0,63 do
+ -- G.setColor(colors[chunk[1+x*64+y]]) G.rectangle('fill',x-0.5,y-0.5,1,1) end end end
lp.draw()
for _,player in pairs(players) do draw_player(player.pos,{0,1,1}) end
end