2023-07-28 05:17:00 +00:00
|
|
|
local tf = love.math.newTransform()
|
2023-07-28 23:23:08 +00:00
|
|
|
local tfTerritory = love.math.newTransform()
|
2023-08-26 21:05:48 +00:00
|
|
|
local tfNodes = love.math.newTransform()
|
2023-07-28 05:17:00 +00:00
|
|
|
local lg = assert( love.graphics )
|
2023-08-26 21:05:48 +00:00
|
|
|
local Camera = {
|
2024-07-18 20:00:51 +00:00
|
|
|
x = 0, y = 70,
|
2023-08-26 21:05:48 +00:00
|
|
|
w = 360, h = 200,
|
|
|
|
zoom = 1, tf = tf,
|
|
|
|
tfTerritory = tfTerritory, tfNodes = tfNodes }
|
2023-07-28 05:17:00 +00:00
|
|
|
|
|
|
|
function Camera.GetWorldCoordinate( x, y )
|
|
|
|
return tf:inverseTransformPoint( x, y )
|
|
|
|
end
|
|
|
|
|
2023-07-28 23:23:08 +00:00
|
|
|
function Camera.GetBitmapCoordinate( x, y )
|
|
|
|
return tfTerritory:inverseTransformPoint( x, y )
|
|
|
|
end
|
|
|
|
|
2023-08-26 21:05:48 +00:00
|
|
|
function Camera.GetNodeCoordinate( x, y )
|
|
|
|
return tfNodes:inverseTransformPoint( x, y )
|
|
|
|
end
|
|
|
|
|
2024-04-25 01:06:41 +00:00
|
|
|
function Camera.Zoom( delta )
|
|
|
|
if Camera.zoom < 25.0 and delta > 0 or --zooming in
|
|
|
|
Camera.zoom > 0.5 and delta < 0 then --zooming out
|
2024-05-02 00:32:07 +00:00
|
|
|
|
|
|
|
delta = delta * Camera.zoom
|
|
|
|
local cx, cy = Camera.x, Camera.y
|
|
|
|
return Camera.Set(
|
|
|
|
cx,
|
|
|
|
cy,
|
|
|
|
Camera.w + delta,
|
|
|
|
Camera.h * (Camera.w + delta) / Camera.w )
|
2024-04-25 01:06:41 +00:00
|
|
|
end
|
2023-07-28 05:17:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Camera.Translate( x, y )
|
|
|
|
x = x or 0
|
|
|
|
y = y or 0
|
2024-07-18 20:00:51 +00:00
|
|
|
return Camera.Set(
|
|
|
|
math.max(-360, math.min(360, Camera.x + x)),
|
|
|
|
math.max(-140, math.min(140, Camera.y + y)), Camera.w, Camera.h)
|
2023-07-28 05:17:00 +00:00
|
|
|
end
|
|
|
|
|
2024-05-02 00:32:07 +00:00
|
|
|
--In world coordinates: top left corner at x, y, extent of 1/w, 1/h.
|
2023-07-28 05:17:00 +00:00
|
|
|
function Camera.Set( x, y, w, h )
|
|
|
|
Camera.x, Camera.y, Camera.w, Camera.h = x, y, w, h
|
2024-04-25 01:06:41 +00:00
|
|
|
Camera.zoom = w / 800
|
2023-07-28 05:17:00 +00:00
|
|
|
tf:reset()
|
|
|
|
tf:scale( w / 360, -h / 200 )
|
|
|
|
tf:translate( 180 - x, -y - 100 )
|
2023-07-28 23:23:08 +00:00
|
|
|
|
2024-07-18 20:00:51 +00:00
|
|
|
|
2023-07-28 23:23:08 +00:00
|
|
|
tfTerritory:reset()
|
|
|
|
tfTerritory:scale( w / 512, h / 285 )
|
|
|
|
tfTerritory:translate( -x * 512 / 360, y * 512 / 360 )
|
2023-08-26 21:05:48 +00:00
|
|
|
|
2024-07-18 20:00:51 +00:00
|
|
|
|
2023-08-26 21:05:48 +00:00
|
|
|
tfNodes:reset()
|
2024-04-28 01:38:23 +00:00
|
|
|
tfNodes:scale( w / 360, -h / 200 )
|
|
|
|
tfNodes:translate( 180 - x , -y - 100 )
|
2023-08-26 21:05:48 +00:00
|
|
|
--tfNodes:translate( -x * 800 / 360, y * 400 / 200 )
|
2024-07-18 20:00:51 +00:00
|
|
|
|
2023-07-28 05:17:00 +00:00
|
|
|
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
|