diff options
l--------- | client/common.lua | 1 | ||||
-rw-r--r-- | client/main.lua | 23 | ||||
-rw-r--r-- | common.lua | 17 |
3 files changed, 30 insertions, 11 deletions
diff --git a/client/common.lua b/client/common.lua new file mode 120000 index 0000000..1b8b461 --- /dev/null +++ b/client/common.lua @@ -0,0 +1 @@ +common.lua
\ No newline at end of file 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 diff --git a/common.lua b/common.lua new file mode 100644 index 0000000..7e67daf --- /dev/null +++ b/common.lua @@ -0,0 +1,17 @@ +local class = require 'r.class' + +local SIZE = 64 +local ChunkMap = class() +function ChunkMap.make(cls) return setmetatable({d={}},cls) end +local function key(pos) return pos.x .. ':' .. pos.y end +function ChunkMap.add(self,pos,val) self.d[key(pos)] = val end +function ChunkMap.remove(self,pos) self:add(pos,nil) end +function ChunkMap.get(self,pos) return self.d[key(pos)] end +local function idx(pos) return 1+pos.x*SIZE+pos.y end +function ChunkMap.tile(self,pos) + local cp = (pos/SIZE):floor() local ix = idx(pos-cp*SIZE) + return self:get(cp)[ix] end + +return { + ChunkMap = ChunkMap, +} |