From 0dc1276df57aa16b4f0eaecf54fb5cd8f00115c6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 4 Feb 2023 23:03:19 +0000 Subject: many many optimizations and refactorings; introduction of Map to support multiple chunks, modify worldgen and client drawing to support multiple chunks --- client/main.lua | 53 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 19 deletions(-) (limited to 'client/main.lua') diff --git a/client/main.lua b/client/main.lua index 7af1bd4..da1b91d 100644 --- a/client/main.lua +++ b/client/main.lua @@ -13,6 +13,7 @@ local Pos = coords.Pos local camera = require"camera".Camera:make() local Chunk = require"common.chunk".Chunk local util = require"util" +local Map = require"common.map".Map -- local pprint=require"common.pprint" -- pprint.setup{show_all=true} @@ -20,9 +21,9 @@ local util = require"util" math.randomseed(os.time()) -local host,peer +local map = Map:make() -local chunk +local host,peer _G.debugmode = false @@ -70,7 +71,7 @@ local function update_local_player(pl,dt) end -local function sync_local_player(pl,peer) +local function sync_local_player(pl) -- send updated info about local player to server if pl.pos_dirty then peer:send(json.encode{t="ppos",x=pl.pos.x,y=pl.pos.y}) @@ -81,26 +82,39 @@ local function send_settile(hpos,tile) peer:send(json.encode{t="settile",q=hpos.q,r=hpos.r,tile=tile}) end + +-- hack +local time_since_last_chunkreq = 100 function love.update(dt) + time_since_last_chunkreq = time_since_last_chunkreq + dt if local_player then update_local_player(local_player,dt) if love.keyboard.isScancodeDown"q" then camera.zoom = camera.zoom*1.05 end if love.keyboard.isScancodeDown"e" then camera.zoom = camera.zoom/1.05 end camera.zoom = math.max(2.25,math.min(50,camera.zoom)) - sync_local_player(local_player,peer) + sync_local_player(local_player) + end + + local mh = camera:screen_to_world(Pos:make(love.mouse.getPosition())):to_hex():round() + if false== map:at(mh) and love.mouse.isDown(1) then + print("place at",mh) + map:set_at(mh,true) + -- print(mh,true) + send_settile(mh,true) + elseif map:at(mh) and love.mouse.isDown(2) then + map:set_at(mh,false) + -- print(mh,false) + send_settile(mh,false) 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) + + if local_player then + local player_cp = local_player.pos:to_hex():containing_chunk() + if map:chunk(player_cp) == nil and time_since_last_chunkreq > 1 then + time_since_last_chunkreq = 0 + peer:send(json.encode{t="reqchunk",u=player_cp.u,v=player_cp.v}) end end + repeat local ev = host:service() @@ -123,10 +137,11 @@ function love.update(dt) local pl = j.pl 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) + local ch = Chunk.from_packet_data(j) + map:add_chunk(ch.cp,ch) elseif op == "settile" then local h = coords.Hex:make(j.q,j.r) - chunk:set_at(h,j.tile) + map:set_at(h,j.tile) end end until not ev @@ -140,9 +155,7 @@ function love.draw() end camera:apply_trans() - if chunk then - drawing.draw_chunk(camera,chunk) - end + drawing.draw_map(camera,map) if local_player then draw_player(local_player,true) @@ -169,6 +182,8 @@ function love.draw() "ph "..tostring(local_player.pos:to_hex()).." "..tostring(local_player.pos:to_hex():round()), "-", "voob "..tostring(camera.zoom), + "-", + "fps "..tostring(love.timer.getFPS()), },10,10) end end -- cgit v1.2.3