diff options
author | ubq323 <ubq323@ubq323.website> | 2023-01-31 23:51:25 +0000 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2023-01-31 23:51:25 +0000 |
commit | 04664ee48de5fc8b06a584e20a4b75c41dafa558 (patch) | |
tree | 49192241d64897fb34f75a92dbab1e11d706fa22 | |
parent | 82c4e04fa6b27a1e9cb5469d7847aef80e5a22bd (diff) |
add sending of tile changes, between players. server remembers all changes made. also add better debug screen
-rw-r--r-- | client/main.lua | 55 | ||||
-rw-r--r-- | client/util.lua | 18 | ||||
-rw-r--r-- | common/chunk.lua | 4 | ||||
-rw-r--r-- | net.txt | 5 | ||||
-rw-r--r-- | server/server.lua | 15 |
5 files changed, 79 insertions, 18 deletions
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() diff --git a/client/util.lua b/client/util.lua new file mode 100644 index 0000000..1d9b753 --- /dev/null +++ b/client/util.lua @@ -0,0 +1,18 @@ +local font = love.graphics.getFont() +local text = love.graphics.newText(font) + +local function print_good(lines,x,y) + for _,line in ipairs(lines) do + text:set(line) + local w,h = text:getDimensions() + love.graphics.setColor(0,0,0,0.6) + love.graphics.rectangle("fill",x,y,w,h) + love.graphics.setColor(1,1,1) + love.graphics.draw(text,x,y) + y = y + h + end +end + +return { + print_good=print_good +} diff --git a/common/chunk.lua b/common/chunk.lua index e210387..a9a8bc5 100644 --- a/common/chunk.lua +++ b/common/chunk.lua @@ -35,6 +35,10 @@ function Chunk.at(self,hoffs) if not index_ok(hoffs.q,hoffs.r) then return nil end return self.tiles[index(hoffs.q,hoffs.r)] end +function Chunk.set_at(self,hoffs,tile) + if not index_ok(hoffs.q,hoffs.r) then return end + self.tiles[index(hoffs.q,hoffs.r)] = tile +end function Chunk.data_packet(self) return json.encode{t="chunk",tiles=self.tiles} @@ -2,6 +2,9 @@ c2s: ppos {x,y} this player has moved to position x,y +settile {q,r,tile} + set tile at H(q,r) to tile (currently boolean) + s2c: playerinfopart:: {id,x,y,color:[r,g,b]} @@ -20,4 +23,6 @@ move {id,x,y} chunk {tiles=[array of 128*128 booleans]} chunk has data d. one day changes will exist and also multiple chunks will exist and also more than 2 tile types will exist +settile {q,r,tile} + tile at H(q,r) was set to tile (currently boolean) diff --git a/server/server.lua b/server/server.lua index 42ed41d..ff3c3eb 100644 --- a/server/server.lua +++ b/server/server.lua @@ -2,6 +2,7 @@ local enet = require"enet" local json = require"common.dkjson" local Chunk = require"common.chunk".Chunk local unpack = unpack or table.unpack +local coords = require"common.coords" math.randomseed(os.time()) @@ -94,12 +95,24 @@ while true do local x,y = j.x,j.y player.pos[1] = x player.pos[2] = y - print(player.id,"-->",player.pos[1],player.pos[2]) + -- print(player.id,"-->",player.pos[1],player.pos[2]) for i,otherplayer in ipairs(playerlist) do if otherplayer ~= player then otherplayer.peer:send(player_move_packet(player,x,y)) end end + elseif op == "settile" then + local h = coords.Hex.make(j.q,j.r) + the_chunk:set_at(h,j.tile) + print(player.id,"settile",h,j.tile) + for i,otherplayer in ipairs(playerlist) do + if otherplayer ~= player then + -- same packet structure s2c as c2s + -- when multiple chunks exist and players only get info + -- about stuff near to them, that won't be the case any more + otherplayer.peer:send(ev.data) + end + end end end |