summaryrefslogtreecommitdiff
path: root/client/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'client/main.lua')
-rw-r--r--client/main.lua46
1 files changed, 35 insertions, 11 deletions
diff --git a/client/main.lua b/client/main.lua
index 089bc08..2a30aa2 100644
--- a/client/main.lua
+++ b/client/main.lua
@@ -6,6 +6,7 @@ local class = require'r.class'
local enet = require'enet'
local pprint = require'pprint'
local json = require'dkjson'
+local rle = require'r.rle'
local SPEED = 8 -- tiles per second
local colors = {[true]={141/255,128/255,22/255}, [false]={71/255,50/255,122/255}}
@@ -16,32 +17,39 @@ local conn = host:connect('localhost:19683')
local players = {}
local chunks = common.ChunkMap()
local lp = { pos=Pos(0,0), movetimer=0, dir=nil } -- local player
+local cam = Camera(nil, 20)
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.keypressed(k,s,r)
+ if directions[s] then
+ if not love.keyboard.isScancodeDown('lshift') then lp.dir = s
+ else local tgt = lp.pos + directions[s]
+ local val = not chunks:tile(tgt); chunks:set_tile(tgt,val)
+ conn:send(json.encode{type='tile',pos=tgt,tile=val}) end
+ elseif s=='space' then cam.zoom = cam.zoom == 10 and 20 or 10 end end
function love.keyreleased(k,s) if s==lp.dir then lp.dir=nil end end
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 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
+ conn:send(json.encode{type='move',pos=newpos})
+ lp.pos = newpos 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)
function love.update(dt)
lp.update(dt)
local ev = host:service() while ev do
-- pprint(ev)
if ev.type == 'receive' then local j = json.decode(ev.data)
- if j.type == 'player' then players[j.name] = {name=j.name,pos=Pos(j.x,j.y)}
+ local pos if j.pos then pos = Pos(j.pos.x,j.pos.y) end
+ if j.type == 'player' then players[j.name] = {name=j.name,pos=pos}
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 chunks:add(Pos(j.x,j.y),j.d)
- elseif j.type == 'unchunk' then chunks:remove(Pos(j.x,j.y))
+ elseif j.type == 'move' then players[j.from].pos = pos
+ elseif j.type == 'chunk' then chunks:add(pos,{d=rle.decode(j.d)})
+ elseif j.type == 'unchunk' then chunks:remove(pos)
+ elseif j.type == 'tile' then chunks:set_tile(pos,j.tile)
end
end
ev = host:service()
@@ -50,8 +58,24 @@ 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
+
+ local tl,br = cam:extents()
+ tl = tl:floor() br = br:ceil()
+ for x=tl.x,br.x do for y=tl.y,br.y do
+ local t = chunks:tile(Pos(x,y))
+ if t ~= nil then G.setColor(colors[t~=(x<0)]) 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
+ G.origin() G.setColor(1,0,0)
+ G.print(tostring(lp.pos),100,100)
+ G.setColor(0,0,0,0.8)
+ G.rectangle('fill',0,0,100,100)
+ G.setColor(1,1,1)
+ for cx=-7,7 do for cy=-7,7 do local p=Pos(cx,cy) if chunks:get(p) then
+ p = (p + Pos(7,7))*10
+ G.setColor(1,1,1) G.rectangle('fill',p.x,p.y,10,10)
+ G.setColor(0,0,1) G.rectangle('line',p.x,p.y,10,10)
+ end end end
+ local p = (lp.pos/common.SIZE+Pos(7,7))*10
+ G.setColor(1,0,0) G.rectangle('fill',p.x-1,p.y-1,2,2)
end