local enet = require"enet" local words = require"common.words" -- 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() -- local pprint=require"common.pprint" -- pprint.setup{show_all=true} math.randomseed(os.time()) local host,peer local chunk = require"common.chunk".Chunk.make() local function draw_player(pl,islocal) local hplsz = PLAYER_SIZE/2 love.graphics.setColor(pl.color) 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 = 10 -- pixels/sec local function kd(code) if love.keyboard.isScancodeDown(code) then return 1 else return 0 end end local dx = kd"d"-kd"a" local dy = kd"s"-kd"w" if dx == 0 and dy == 0 then pl.pos_dirty = false return end if dx ~= 0 and dy ~= 0 then dx = dx / math.sqrt(2) dy = dy / math.sqrt(2) end 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.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 local ev = host:service() if ev and ev.type == "receive" then local w = words.split(ev.data) local op = w[1] if op == "join" then local id,x,y,r,g,b = unpack(w,2) -- blegh id=tonumber(id) x=tonumber(x) y=tonumber(y) r=tonumber(r) g=tonumber(g) b=tonumber(b) 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 elseif op == "move" then local id,x,y = unpack(w,2) id=tonumber(id) x=tonumber(x) y=tonumber(y) assert(remote_players[id],"wheeze") 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) x=tonumber(x) y=tonumber(y) r=tonumber(r) g=tonumber(g) b=tonumber(b) local_player = {pos=coords.Pos.make(x,y),color={r,g,b},id=id} end end until not ev 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 for _,pl in pairs(remote_players) do draw_player(pl) end end function love.load() -- require"profile".start(10,io.open("./trace","w")) host = enet.host_create() peer = host:connect(SERVER_HOSTNAME..":8473") end function love.quit() peer:disconnect() host:flush() end