summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2023-03-15 01:00:48 +0000
committerubq323 <ubq323@ubq323.website>2023-03-15 01:02:44 +0000
commit0927f9297c06525a453b4aad44fa4c2916c75906 (patch)
tree7eee8c524c3f8889896b49b5b47de32fd117da93 /client
parent805b312daccfa6d21a2169fea7fbd38bad9fcd8b (diff)
refactor game.lua somewhat, use state machinish thing for connecting, instead of `if local_player` guards everywhere
Diffstat (limited to 'client')
-rw-r--r--client/game.lua128
1 files changed, 70 insertions, 58 deletions
diff --git a/client/game.lua b/client/game.lua
index 3445bd7..fd4ade9 100644
--- a/client/game.lua
+++ b/client/game.lua
@@ -163,54 +163,8 @@ function SCENE.wheelmoved(dx,dy)
camera.zoom = math.max(2.5,math.min(50,camera.zoom))
end
-function SCENE.update(dt)
- msgbox.update(dt)
- if ui_mode == "normal" then
-
- -- movement and zoom in/out (keyboard input)
- if local_player then
- update_local_player(local_player,dt)
- sync_local_player(local_player)
- end
-
- -- mouse input
- local msx,msy = love.mouse.getPosition()
- local mh = camera:screen_to_world(Pos:make(msx,msy)):to_hex():round()
- if map:at(mh) == 0 and love.mouse.isDown(1) then
- map:set_at(mh,selected_tile)
- send_settile(mh,selected_tile)
- elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
- map:set_at(mh,0)
- send_settile(mh,0)
- end
- end
-
- -- load and unload chunks
- if local_player then
- local player_cp = local_player.pos:to_hex():containing_chunk()
- -- load chunks near to player (within 3x3 square)
- for _,cp in ipairs(player_cp:neighborhood()) do
- if map:chunk(cp) == nil then
- map:mark_chunk_loading(cp)
- peer:send(json.encode{t="reqchunk",u=cp.u,v=cp.v})
- end
- end
-
- -- unload chunks not near player
- -- todo maybe: instead of immediately unloading chunks when we move away,
- -- have some kind of 'last near' time, so that if player is moving back and forth,
- -- we don't repeatedly unload and reload a given chunk
- local to_remove = {}
- for cp in map:iter_chunks() do
- local d = player_cp:orth_dist(cp)
- if d > 1 then
- map:remove_chunk(cp)
- end
- end
-
- end
-
+local function handle_net()
-- handle network packets
repeat
local ev = host:service()
@@ -249,21 +203,61 @@ function SCENE.update(dt)
until not ev
end
-function SCENE.draw()
+local function update(dt)
+ msgbox.update(dt)
+ if ui_mode == "normal" then
+ -- movement
+ update_local_player(local_player,dt)
+ sync_local_player(local_player)
+
+ -- mouse input (place/mine)
+ local msx,msy = love.mouse.getPosition()
+ local mh = camera:screen_to_world(Pos:make(msx,msy)):to_hex():round()
+ if map:at(mh) == 0 and love.mouse.isDown(1) then
+ map:set_at(mh,selected_tile)
+ send_settile(mh,selected_tile)
+ elseif map:at(mh) ~= 0 and love.mouse.isDown(2) then
+ map:set_at(mh,0)
+ send_settile(mh,0)
+ end
+ end
+
+ -- load and unload chunks
+ local player_cp = local_player.pos:to_hex():containing_chunk()
+ -- load chunks near to player (within 3x3 square)
+ for _,cp in ipairs(player_cp:neighborhood()) do
+ if map:chunk(cp) == nil then
+ map:mark_chunk_loading(cp)
+ peer:send(json.encode{t="reqchunk",u=cp.u,v=cp.v})
+ end
+ end
+ -- unload chunks not near player
+ -- todo maybe: instead of immediately unloading chunks when we move away,
+ -- have some kind of 'last near' time, so that if player is moving back and forth,
+ -- we don't repeatedly unload and reload a given chunk
+ local to_remove = {}
+ for cp in map:iter_chunks() do
+ local d = player_cp:orth_dist(cp)
+ if d > 1 then
+ map:remove_chunk(cp)
+ end
+ end
+
+
+ handle_net()
+end
+
+local function draw()
love.graphics.clear(1,1,1)
love.graphics.origin()
- if local_player then
- camera.pos = local_player.pos
- end
+
+ camera.pos = local_player.pos
camera:apply_trans()
-- drawing.draw_map(camera,map)
drawing2.draw_map(camera,map)
-
- if local_player then
- draw_player(local_player,true)
- end
+ draw_player(local_player,true)
for _,pl in pairs(remote_players) do
draw_player(pl)
end
@@ -335,8 +329,28 @@ function SCENE.draw()
love.graphics.setColor(0.8,0.8,0.8)
love.graphics.line(tw,y,tw,y+th)
end
+end
-
+local connected = false
+function SCENE.update(dt)
+ if connected then
+ return update(dt)
+ else
+ handle_net()
+ if peer:state() == "connected" and local_player then
+ connected = true
+ msgbox.add("connected to "..SERVER_HOSTNAME..":8473")
+ msgbox.add("press F1 for controls help")
+ end
+ end
+end
+function SCENE.draw()
+ if connected then
+ return draw()
+ else
+ love.graphics.clear(1,1,1)
+ love.graphics.print("connecting...",10,10)
+ end
end
function SCENE.load()
@@ -344,8 +358,6 @@ function SCENE.load()
-- require"profile".start(10,io.open("./trace","w"))
host = enet.host_create()
peer = host:connect(SERVER_HOSTNAME..":8473",2)
- msgbox.add("connected to "..SERVER_HOSTNAME..":8473")
- msgbox.add("press F1 for controls help")
end
function SCENE.quit()