diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | client/camera.lua | 25 | ||||
-rw-r--r-- | client/drawing.lua | 15 | ||||
-rw-r--r-- | client/main.lua | 64 | ||||
-rw-r--r-- | client/profile.lua | 12 | ||||
-rw-r--r-- | server/server.lua | 2 |
6 files changed, 86 insertions, 34 deletions
@@ -1 +1,3 @@ server/enet.so +flamegraph.pl +trace.* diff --git a/client/camera.lua b/client/camera.lua new file mode 100644 index 0000000..bc65044 --- /dev/null +++ b/client/camera.lua @@ -0,0 +1,25 @@ +local coords = require"common.coords" + +-- in screen units +local screen_width, screen_height = love.graphics.getDimensions() + +-- zoom is screen pixels per world unit +local Camera = {} +Camera.__index = Camera +function Camera.make(pos,zoom) + pos = pos or coords.Pos.make(0,0) + zoom = zoom or 30 + return setmetatable({pos=pos,zoom=zoom},Camera) +end +function Camera.apply_trans(self) + love.graphics.origin() + -- centre (0,0) in the middle of the screen + love.graphics.translate(screen_width/2,screen_height/2) + -- apply camera transformations + + love.graphics.scale(self.zoom) + love.graphics.translate(-self.pos.x,-self.pos.y) +end + + +return {Camera=Camera} diff --git a/client/drawing.lua b/client/drawing.lua index e65e716..29069e7 100644 --- a/client/drawing.lua +++ b/client/drawing.lua @@ -13,12 +13,13 @@ for i=0,5 do table.insert(corners,y) end -local function draw_hex(cpos,size) +local function draw_hex(cpos,color,thick) love.graphics.push() - love.graphics.setLineWidth(0.1) + love.graphics.setLineWidth(thick or 0.1) love.graphics.translate(cpos.x,cpos.y) - love.graphics.scale(size) - love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0)) + if color then love.graphics.setColor(color) else + love.graphics.setColor(love.math.colorFromBytes(0xe7,0x9e,0)) + end love.graphics.polygon("fill",corners) love.graphics.setColor(0,0,0) love.graphics.polygon("line",corners) @@ -26,12 +27,12 @@ local function draw_hex(cpos,size) end local function draw_chunk(the_chunk) - for q = 0,chunk.SIZE-1 do - for r = 0,chunk.SIZE-1 do + for q = 0,31 do + for r = 0,31 do local t = the_chunk.tiles[chunk.index(q,r)] if t then local p = coords.Hex.make(q,r):to_pos() - draw_hex(p*10,10) + draw_hex(p) end end end diff --git a/client/main.lua b/client/main.lua index b5d322e..3d7a598 100644 --- a/client/main.lua +++ b/client/main.lua @@ -1,40 +1,42 @@ local enet = require"enet" local words = require"common.words" -local SERVER_HOSTNAME = "ubq323.website" -local PLAYER_SIZE = 30 +-- local SERVER_HOSTNAME = "ubq323.website" +local SERVER_HOSTNAME = "localhost" +local PLAYER_SIZE = 0.7 local local_player = nil +local drawing = require"drawing" +local coords = require"common.coords" +local camera = require"camera".Camera.make() -math.randomseed(os.time()) +-- local pprint=require"common.pprint" +-- pprint.setup{show_all=true} --- { --- pos={100,100}, --- color={love.math.colorFromBytes(0xdf,0x73,0xff)}, --- } -local host,peer +math.randomseed(os.time()) +local host,peer local chunk = require"common.chunk".Chunk.make() -local function draw_player(pl,me) +local function draw_player(pl,islocal) local hplsz = PLAYER_SIZE/2 love.graphics.setColor(pl.color) - love.graphics.rectangle("fill",pl.pos[1]-hplsz,pl.pos[2]-hplsz,PLAYER_SIZE,PLAYER_SIZE) - love.graphics.setColor(0,0,0) - love.graphics.print(tostring(pl.id),pl.pos[1],pl.pos[2]) - if me then - love.graphics.setColor(1,1,1) - love.graphics.rectangle("line",pl.pos[1]-hplsz,pl.pos[2]-hplsz,PLAYER_SIZE,PLAYER_SIZE) + love.graphics.rectangle("fill",pl.pos.x-hplsz,pl.pos.y-hplsz,PLAYER_SIZE,PLAYER_SIZE) + -- love.graphics.print(tostring(pl.id),pl.pos.x,pl.pos.y) + if islocal then + love.graphics.setLineWidth(0.01) + love.graphics.setColor(0.5,0,0) + love.graphics.rectangle("line",pl.pos.x-hplsz,pl.pos.y-hplsz,PLAYER_SIZE,PLAYER_SIZE) end end local remote_players = {} local function update_local_player(pl,dt) - local SPEED = 300 -- pixels/sec + local SPEED = 10 -- pixels/sec local function kd(code) if love.keyboard.isScancodeDown(code) then return 1 else return 0 end end @@ -51,21 +53,25 @@ local function update_local_player(pl,dt) dy = dy / math.sqrt(2) end - pl.pos[1] = pl.pos[1] + SPEED * dt * dx - pl.pos[2] = pl.pos[2] + SPEED * dt * dy + pl.pos.x = pl.pos.x + SPEED * dt * dx + pl.pos.y = pl.pos.y + SPEED * dt * dy pl.pos_dirty = true + + end local function sync_local_player(pl,peer) -- send updated info about local player to server if pl.pos_dirty then - peer:send(words.join("ppos",pl.pos[1],pl.pos[2])) + peer:send(words.join("ppos",pl.pos.x,pl.pos.y)) end end function love.update(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 sync_local_player(local_player,peer) end repeat @@ -82,7 +88,7 @@ function love.update(dt) r=tonumber(r) g=tonumber(g) b=tonumber(b) - remote_players[id] = {pos={x,y},color={r,g,b},id=id} + remote_players[id] = {pos=coords.Pos.make(x,y),color={r,g,b},id=id} elseif op == "leave" then local id = tonumber(w[2]) remote_players[id]=nil @@ -92,8 +98,8 @@ function love.update(dt) x=tonumber(x) y=tonumber(y) assert(remote_players[id],"wheeze") - remote_players[id].pos[1] = x - remote_players[id].pos[2] = y + remote_players[id].pos.x = x + remote_players[id].pos.y = y elseif op == "you" then local id,x,y,r,g,b = unpack(w,2) id=tonumber(id) @@ -102,7 +108,7 @@ function love.update(dt) r=tonumber(r) g=tonumber(g) b=tonumber(b) - local_player = {pos={x,y},color={r,g,b},id=id} + local_player = {pos=coords.Pos.make(x,y),color={r,g,b},id=id} end end until not ev @@ -110,6 +116,14 @@ end function love.draw() love.graphics.clear(1,1,1) + love.graphics.origin() + if local_player then + camera.pos = local_player.pos + end + camera:apply_trans() + + drawing.draw_chunk(chunk) + if local_player then draw_player(local_player,true) end @@ -117,12 +131,10 @@ function love.draw() draw_player(pl) end - -- require"drawing".drawhex({x=200,y=200},40) - require"drawing".draw_chunk(chunk) - end function love.load() + -- require"profile".start(10,io.open("./trace","w")) host = enet.host_create() peer = host:connect(SERVER_HOSTNAME..":8473") end diff --git a/client/profile.lua b/client/profile.lua new file mode 100644 index 0000000..e7eab02 --- /dev/null +++ b/client/profile.lua @@ -0,0 +1,12 @@ +local profile = require"jit.profile" +local function start(period,file) + local function cb(thread,samples,vmstate) + file:write(profile.dumpstack(thread,"pF l;",-100), vmstate, " ", samples, "\n") + end + profile.start("vli"..tonumber(period), cb) +end +local function stop() profile.stop() end + + +return {start=start,stop=stop} + diff --git a/server/server.lua b/server/server.lua index 7f7cc92..93b4e3d 100644 --- a/server/server.lua +++ b/server/server.lua @@ -30,7 +30,7 @@ local function random_color() return {math.random(),math.random(),math.random()} end local function make_player(peer) - local p = {pos={100,100},color=random_color(),peer=peer,id=nextid} + local p = {pos={0,0},color=random_color(),peer=peer,id=nextid} nextid = nextid + 1 return p end |