From 04664ee48de5fc8b06a584e20a4b75c41dafa558 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Tue, 31 Jan 2023 23:51:25 +0000 Subject: add sending of tile changes, between players. server remembers all changes made. also add better debug screen --- client/main.lua | 55 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 17 deletions(-) (limited to 'client/main.lua') diff --git a/client/main.lua b/client/main.lua index 819753f..6572621 100644 --- a/client/main.lua +++ b/client/main.lua @@ -12,6 +12,7 @@ local coords = require"common.coords" local Pos = coords.Pos local camera = require"camera".Camera.make() local Chunk = require"common.chunk".Chunk +local util = require"util" -- local pprint=require"common.pprint" -- pprint.setup{show_all=true} @@ -23,6 +24,12 @@ local host,peer local chunk +local f3mode = true + +function love.keypressed(key,scancode,isrepeat) + if scancode == "f3" then f3mode = not f3mode end +end + local function draw_player(pl,islocal) local hplsz = PLAYER_SIZE/2 love.graphics.setColor(pl.color) @@ -69,6 +76,10 @@ local function sync_local_player(pl,peer) end end +local function send_settile(hpos,tile) + peer:send(json.encode{t="settile",q=hpos.q,r=hpos.r,tile=tile}) +end + function love.update(dt) if local_player then update_local_player(local_player,dt) @@ -76,6 +87,19 @@ function love.update(dt) if love.keyboard.isScancodeDown"e" then camera.zoom = camera.zoom/1.05 end sync_local_player(local_player,peer) end + if chunk then + local mh = camera:screen_to_world(Pos.make(love.mouse.getPosition())):to_hex():round() + if false== chunk:at(mh) and love.mouse.isDown(1) then + chunk:set_at(mh,true) + print(mh,true) + send_settile(mh,true) + elseif chunk:at(mh) and love.mouse.isDown(2) then + chunk:set_at(mh,false) + print(mh,false) + send_settile(mh,false) + end + end + repeat local ev = host:service() if ev and ev.type == "receive" then @@ -98,6 +122,9 @@ function love.update(dt) local_player = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id} elseif op == "chunk" then chunk = Chunk.from_packet_data(j) + elseif op == "settile" then + local h = coords.Hex.make(j.q,j.r) + chunk:set_at(h,j.tile) end end until not ev @@ -127,25 +154,19 @@ function love.draw() local sm = Pos.make(love.mouse.getPosition()) local wm = camera:screen_to_world(sm) - local hm = wm:to_hex():round() - - local tl,br = camera:extents() + local hm = wm:to_hex() love.graphics.origin() - love.graphics.scale(2) - love.graphics.setColor(0,0,0) - love.graphics.print(table.concat( - {"sm "..tostring(sm), - "wm "..tostring(wm), - "hm "..tostring(hm), - "", - "tl "..tostring(tl), - "br "..tostring(br)}, - "\n"),10,10) - - - - + if f3mode and local_player then + util.print_good({ + "ms "..tostring(sm), + "mw "..tostring(wm), + "mh "..tostring(hm).." "..tostring(hm:round()), + "-", + "pw "..tostring(local_player.pos), + "ph "..tostring(local_player.pos:to_hex()).." "..tostring(local_player.pos:to_hex():round()) + },10,10) + end end function love.load() -- cgit v1.2.3