poppy-client/lua/camera.lua

120 lines
3.2 KiB
Lua

local CAM_SPEED = 10
camera = {}
camera.target = 0
camera.offset = { x = 0, y = 0 }
local fullscreen = false
local altPressed, enterPressed = false, false
function camera.keypressed(key, _)
if key == "return" then enterPressed = true end
if key == "lalt" then altPressed = true end
if key == "f11"
or (altPressed and key == "return")
or (enterPressed and key == "lalt") then
fullscreen = not fullscreen
love.window.setFullscreen(fullscreen, "desktop")
end
end
function camera.keyreleased(key, _)
if key == "u" then error("You pressed the U key!") end
if key == "lalt" then
altPressed = false
end
if key == "return" then
enterPressed = false
end
local change = false
if key == "-" or key == "kp-" or key == "pagedown" then
if scale > 1 then
ui.update_scale(scale - 1)
else
ui.update_scale(scale * 0.5)
end
change = true
end
if key == "+" or key == "kp+" or key == "pageup" then
if scale >= 1 then
ui.update_scale(scale + 1)
else
ui.update_scale(scale * 2)
end
change = true
end
if key == "kp0" then
scale = 1
change = true
end
if scale < 0.125 then ui.update_scale(0.125) change = false end
if scale > 8 then ui.update_scale(8) change = false end
if change then camera.setplayer() end
end
function camera.input(dt)
-- TODO: rewrite for scale, so player always remains in view at max distance
-- direct view control
if love.keyboard.isDown("left") then
camera.offset.x = camera.offset.x + dt * 300
end
if love.keyboard.isDown("right") then
camera.offset.x = camera.offset.x - dt * 300
end
if love.keyboard.isDown("up") then
camera.offset.y = camera.offset.y + dt * 300
end
if love.keyboard.isDown("down") then
camera.offset.y = camera.offset.y - dt * 300
end
camera.offsetclamp()
end
function camera.resize()
camera.setplayer()
end
function camera.offsetclamp()
if camera.offset.x > (window.x/2) / scale then
camera.offset.x = (window.x/2) / scale
end
if camera.offset.x < -(window.x/2) / scale then
camera.offset.x = -(window.x/2) / scale
end
if camera.offset.y > (window.y/2) / scale then
camera.offset.y = (window.y/2) / scale
end
if camera.offset.y < -(window.y/2) / scale then
camera.offset.y = -(window.y/2) / scale
end
end
function camera.setplayer()
if camera.target == 0 then return end
player = participants[camera.target]
camera.offsetclamp()
local perfectx = math.floor(player.x * -1) + ((window.x -300)/2 -((player.avatar:getWidth() /2) / scale)) / scale + camera.offset.x
local perfecty = math.floor(player.y * -1) + (window.y/2 -((player.avatar:getHeight() /2) / scale)) / scale + camera.offset.y
view.x = perfectx
view.y = perfecty
end
function camera.update()
if camera.target == 0 then return end
player = participants[camera.target]
--[[ Update camera to track player ]]--
local perfectx = math.floor(player.x * -1) + ((window.x -300)/2 -(player.avatar:getWidth() /2)) / scale + camera.offset.x
local perfecty = math.floor(player.y * -1) + (window.y/2 -(player.avatar:getHeight() /2)) / scale + camera.offset.y
local deltax = (perfectx - view.x) / scale
local deltay = (perfecty - view.y) / scale
view.x = view.x + (deltax /(CAM_SPEED / (scale * scale)))
view.y = view.y + (deltay /(CAM_SPEED / (scale * scale)))
end