diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/camera.lua | 16 | ||||
-rw-r--r-- | client/drawing.lua | 19 | ||||
-rw-r--r-- | client/main.lua | 22 |
3 files changed, 36 insertions, 21 deletions
diff --git a/client/camera.lua b/client/camera.lua index 6309e38..528b45b 100644 --- a/client/camera.lua +++ b/client/camera.lua @@ -1,15 +1,15 @@ local coords = require"common.coords" +local class = require"common.class" -- 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) +local Camera = class() +function Camera.make(cls,pos,zoom) + pos = pos or coords.Pos:make(0,0) zoom = zoom or 30 - return setmetatable({pos=pos,zoom=zoom},Camera) + return setmetatable({pos=pos,zoom=zoom},cls) end function Camera.apply_trans(self) love.graphics.origin() @@ -21,7 +21,7 @@ function Camera.apply_trans(self) love.graphics.translate(-self.pos.x,-self.pos.y) end -local screen_offset = coords.Pos.make(screen_width/2,screen_height/2) +local screen_offset = coords.Pos:make(screen_width/2,screen_height/2) function Camera.screen_to_world(self,pos) return (pos-screen_offset)/self.zoom + self.pos end @@ -31,8 +31,8 @@ end function Camera.extents(self) -- returns top left and bottom right pos's in world coords - return self:screen_to_world(coords.Pos.make(0,0)), - self:screen_to_world(coords.Pos.make(screen_width,screen_height)) + return self:screen_to_world(coords.Pos:make(0,0)), + self:screen_to_world(coords.Pos:make(screen_width,screen_height)) end diff --git a/client/drawing.lua b/client/drawing.lua index 111b2e2..bbcf03a 100644 --- a/client/drawing.lua +++ b/client/drawing.lua @@ -33,7 +33,6 @@ local colors = { c(255,128,0), -- orange c(192,192,64), -- yellow c(0,192,0), -- green - c(0,192,192), -- teal c(64,64,255), -- blue c(192,0,192), -- purple @@ -67,14 +66,14 @@ end local function draw_chunk(camera,the_chunk) local tl,br = camera:extents() local tlh,brh = tl:to_hex():round(), br:to_hex():round() - local trh = coords.Pos.make(br.x,tl.y):to_hex():round() + local trh = coords.Pos:make(br.x,tl.y):to_hex():round() for r = tlh.r-1,brh.r+1 do local rowidx = r-tlh.r local minq = tlh.q - math.floor((rowidx+1)/2) local maxq = minq+(trh.q-tlh.q)+1 for q = minq,maxq do - local h = coords.Hex.make(q,r) + local h = coords.Hex:make(q,r) local t = the_chunk:at(h) if type(t) == "number" then draw_hex(h:to_pos(),colors[t],camera.zoom) @@ -84,6 +83,20 @@ local function draw_chunk(camera,the_chunk) end end + if _G.debugmode then + love.graphics.setColor(0,1,0) + + local function p(q,r) return coords.Hex:make(q,r):to_pos() end + local h = chunk.SIZE-0.5 + local c00 = p(-0.5,-0.5) + local c01 = p(-0.5,h) + local c10 = p(h,-0.5) + local c11 = p(h,h) + + love.graphics.polygon("line", + c00.x,c00.y, c01.x,c01.y, c11.x,c11.y, c10.x, c10.y) + end + end diff --git a/client/main.lua b/client/main.lua index 2fad49e..7af1bd4 100644 --- a/client/main.lua +++ b/client/main.lua @@ -10,7 +10,7 @@ local local_player = nil local drawing = require"drawing" local coords = require"common.coords" local Pos = coords.Pos -local camera = require"camera".Camera.make() +local camera = require"camera".Camera:make() local Chunk = require"common.chunk".Chunk local util = require"util" @@ -24,10 +24,10 @@ local host,peer local chunk -local f3mode = false +_G.debugmode = false function love.keypressed(key,scancode,isrepeat) - if scancode == "f3" then f3mode = not f3mode end + if scancode == "f3" then _G.debugmode = not _G.debugmode end end local function draw_player(pl,islocal) @@ -45,7 +45,8 @@ end local remote_players = {} local function update_local_player(pl,dt) - local SPEED = 10 -- pixels/sec + local SPEED = 10 -- units/sec + if love.keyboard.isScancodeDown("lshift") then SPEED = 100 end local function kd(code) if love.keyboard.isScancodeDown(code) then return 1 else return 0 end end @@ -85,10 +86,11 @@ function love.update(dt) 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) end if chunk then - local mh = camera:screen_to_world(Pos.make(love.mouse.getPosition())):to_hex():round() + 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) @@ -108,7 +110,7 @@ function love.update(dt) local op = j.t if op == "join" then local pl = j.pl - remote_players[pl.id] = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id} + remote_players[pl.id] = {pos=coords.Pos:make(pl.x,pl.y),color=pl.color,id=pl.id} elseif op == "leave" then local id = j.id remote_players[id]=nil @@ -119,11 +121,11 @@ function love.update(dt) remote_players[id].pos.y = y elseif op == "you" then local pl = j.pl - local_player = {pos=coords.Pos.make(pl.x,pl.y),color=pl.color,id=pl.id} + 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) + local h = coords.Hex:make(j.q,j.r) chunk:set_at(h,j.tile) end end @@ -152,12 +154,12 @@ function love.draw() love.graphics.setColor(1,0,0) love.graphics.rectangle("fill",0,0,1,1) - local sm = Pos.make(love.mouse.getPosition()) + local sm = Pos:make(love.mouse.getPosition()) local wm = camera:screen_to_world(sm) local hm = wm:to_hex() love.graphics.origin() - if f3mode and local_player then + if _G.debugmode and local_player then util.print_good({ "ms "..tostring(sm), "mw "..tostring(wm), |