summaryrefslogtreecommitdiff
path: root/client/main_1.lua
diff options
context:
space:
mode:
Diffstat (limited to 'client/main_1.lua')
-rw-r--r--client/main_1.lua85
1 files changed, 85 insertions, 0 deletions
diff --git a/client/main_1.lua b/client/main_1.lua
new file mode 100644
index 0000000..f5ee5a9
--- /dev/null
+++ b/client/main_1.lua
@@ -0,0 +1,85 @@
+local G = love.graphics
+local common = require 'common'
+local Camera = require'r.camera'
+local Pos = require'r.pos'
+local class = require'r.class'
+local enet = require'enet'
+local pprint = require'pprint'
+local json = require'dkjson'
+local rle = require'r.rle'
+
+local M = {}
+
+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 = 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 M.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
+ 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
+
+function M.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)
+ 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
+ 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()
+ end
+end
+function M.draw()
+ G.clear(1,1,1); G.origin()
+ cam.pos = lp.pos; cam:apply_trans()
+
+ 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
+
+return M