dcearth/camera.lua

40 lines
1.1 KiB
Lua

local tf = love.math.newTransform()
local lg = assert( love.graphics )
local Camera = { x = 0, y = 0, w = 360, h = 200, zoom = 1, tf = tf }
function Camera.GetWorldCoordinate( x, y )
return tf:inverseTransformPoint( x, y )
end
function Camera.Zoom( out )
local scale = out and 1.1 or 0.9
tf:scale( scale, scale )
local x = Camera.x
local y = Camera.y
return Camera.Set( x, y, Camera.w * scale, Camera.h * scale )
end
function Camera.Translate( x, y )
x = x or 0
y = y or 0
return Camera.Set( math.max(-180, math.min(360, Camera.x + x)), math.min(100, Camera.y + y), Camera.w, Camera.h)
end
--In world coordinates: top left corner at x, y, extent of w, h.
function Camera.Set( x, y, w, h )
print( ("CAMERA: %3.2f %3.2f %3.2f %3.2f"):format(x, y, w, h) )
Camera.x, Camera.y, Camera.w, Camera.h = x, y, w, h
Camera.zoom = w / 360
tf:reset()
tf:scale( w / 360, -h / 200 )
tf:translate( 180 - x, -y - 100 )
end
function Camera.Resize( w, h )
w, h = math.min( w, h * 360 / 200 ), math.min( h, w * 200 / 360 )
return Camera.Set( Camera.x, Camera.y, w, h )
end
Camera.Resize( lg.getDimensions() )
return Camera