diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | client/game.lua | 64 | ||||
-rw-r--r-- | client/menu.lua | 1 | ||||
-rw-r--r-- | server/server.lua | 1 |
4 files changed, 55 insertions, 14 deletions
@@ -1,3 +1,6 @@ chunk*.json .env build_tmp/ +*.love +post +todo diff --git a/client/game.lua b/client/game.lua index 38fb1a8..2ad6404 100644 --- a/client/game.lua +++ b/client/game.lua @@ -8,6 +8,9 @@ local enet = require'enet' local json = require'dkjson' local rle = require'r.rle' local print_good = require'r.print_good' +local rmath = require'r.math' +local msgbox = require'r.msgbox' +local utf8 = require'utf8' local M = {} @@ -25,9 +28,28 @@ function M.load(_host,_conn,j) host=_host conn=_conn lp.pos=Pos(j.pos.x,j.pos.y) lp.name=j.name lp.color=j.color end local show={ui=true,debug=false} +local chatmsg = nil +local chatmsg_text = G.newText(G.getFont()) +local function chat_key(k,s,r) + if k == 'escape' then chatmsg = nil + elseif k == 'return' then if #chatmsg>0 then + msgbox.add(lp.name..': '..chatmsg) + conn:send(json.encode{type='chat',msg=chatmsg}) end + chatmsg = nil + elseif k == 'backspace' and #chatmsg>0 then + chatmsg = chatmsg:sub(1,utf8.offset(chatmsg,-1)-1) end end +function M.textinput(str) if chatmsg then chatmsg = chatmsg..str end end +local t = 0 + +local function smoovement(player) if not player.oldpos then return player.pos end + return rmath.clerp(player.oldpos,player.pos,(t-player.at)*16) end +local function moveplayer(player,newpos) + player.oldpos = player.pos + player.pos = newpos + player.at = t end local directions = {w=Pos(0,-1),a=Pos(-1,0),s=Pos(0,1),d=Pos(1,0)} -function M.keypressed(k,s,r) +function M.keypressed(k,s,r) if chatmsg then chat_key(k,s,r) else if directions[s] then if not love.keyboard.isScancodeDown('lshift') then lp.dir = s else local tgt = lp.pos + directions[s] @@ -35,36 +57,42 @@ function M.keypressed(k,s,r) conn:send(json.encode{type='tile',pos=tgt,tile=val}) end elseif k=='space' then cam.zoom = cam.zoom == 10 and 20 or 10 elseif k=='tab' then show.ui = not show.ui - elseif k=='f3' then show.debug = not show.debug end end -function love.keyreleased(k,s) if s==lp.dir then lp.dir=nil end end + elseif k=='return' then chatmsg = '' lp.dir = nil + elseif k=='f3' then show.debug = not show.debug end end end +function M.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 + moveplayer(lp,newpos) lp.movetimer = 1 / SPEED end end end local function draw_player(player,no_label) - G.setColor(player.color) G.circle('fill',player.pos.x,player.pos.y,0.3) + local effpos = smoovement(player) + G.setColor(player.color) G.circle('fill',effpos.x,effpos.y,0.3) if show.ui and not no_label then local f = G.getFont() local txtw,h = f:getWidth(player.name), f:getHeight() - local centre = cam:world_to_screen(player.pos-Pos(0,.4))-Pos(0,h/2) + local centre = cam:world_to_screen(effpos-Pos(0,.4))-Pos(0,h/2) local bb = Rect:from_centre_dims(centre,txtw,h) - G.push() G.origin() G.setColor(0,0,0,0.8) bb:draw'fill' - G.setColor(1,1,1) G.print(player.name,bb.tl:vals()) G.pop() end end + G.push() G.origin() G.setColor(0,0,0,0.4) bb:draw'fill' + G.setColor(1,1,1) G.print(player.name,bb.tl:floor():vals()) G.pop() end end function lp.draw() draw_player(lp,true) end + function M.update(dt) - lp.update(dt) + t = t + dt lp.update(dt) msgbox.update(dt) local ev = host and host:service() while ev do 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,color=j.color} - elseif j.type == 'unplayer' then players[j.from] = nil - elseif j.type == 'move' then players[j.from].pos = pos + if j.type == 'player' then + players[j.name] = {name=j.name,pos=pos,color=j.color,at=t} + msgbox.add(j.name..' joined') + elseif j.type == 'unplayer' then players[j.from] = nil msgbox.add(j.from..' left') + elseif j.type == 'move' then moveplayer(players[j.from],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) + elseif j.type == 'chat' then msgbox.add(j.from..': '..j.msg) end end ev = host and host:service() @@ -72,7 +100,7 @@ function M.update(dt) end function M.draw() G.clear(1,1,1); G.origin() - cam.pos = lp.pos; cam:apply_trans() + cam.pos = smoovement(lp); cam:apply_trans() local tl,br = cam:extents() tl = tl:floor() br = br:ceil() @@ -84,7 +112,15 @@ function M.draw() for _,player in pairs(players) do draw_player(player) table.insert(playernames,player.name) end table.sort(playernames) playernames = table.concat(playernames,'\n') G.origin() - if show.ui then print_good(playernames,0,0) end + if show.ui then msgbox.draw() print_good(playernames,'end',0) end + if chatmsg then + chatmsg_text:setFont(G.getFont()) chatmsg_text:set(lp.name..': '..chatmsg) + local W,H = G.getDimensions() local tw,th = chatmsg_text:getDimensions() + local y = H-th-30 + G.setColor(0,0,0,0.8) G.rectangle('fill',0,y,W,th) + G.setColor(1,1,1) G.draw(chatmsg_text,0,y) + G.setColor(.8,.8,.8) G.line(tw,y,tw,y+th) + end if show.debug then G.setColor(1,0,0) G.print(tostring(lp.pos),100,100) diff --git a/client/menu.lua b/client/menu.lua index 8673217..8d8888c 100644 --- a/client/menu.lua +++ b/client/menu.lua @@ -9,6 +9,7 @@ local qw = require'r.qw' local common = require'common' local HOST = 'ubq323.website' +HOST='localhost' local logo = G.newImage"logo.png" logo:setFilter'nearest' diff --git a/server/server.lua b/server/server.lua index 5a96b11..f2119fe 100644 --- a/server/server.lua +++ b/server/server.lua @@ -100,6 +100,7 @@ while true do else greet(Player(peer,j)) end elseif j.type == 'move' then player.pos=pos send_others(player, j) elseif j.type == 'tile' then chunks:set_tile(pos,j.tile) send_others(player,j) + elseif j.type == 'chat' then send_others(player,j) end end end |