aboutsummaryrefslogtreecommitdiff
path: root/src/camera.lua
blob: a32809971f3ef34189e00f5e0d781b7fc619d96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
local DEFAULT_CAMERA_SMOOTHING = 30

Cam = {}
Cam.__index = Cam

function Cam.new(x, y)
  local self = setmetatable({}, Cam)

  self.x = x or 0
  self.y = y or 0
  self.realx = self.x
  self.realy = self.y
  self.zoom = 2

  self.smoothing = DEFAULT_CAMERA_SMOOTHING

  return self
end

function Cam:update(dt)
  self.realx = dlerp(self.realx, self.x, self.smoothing * dt)
  self.realy = dlerp(self.realy, self.y, self.smoothing * dt)
end

local active_cam = Cam.new()

function get_active_camera()
  return active_cam
end

function set_active_camera(new_cam)
  active_cam = new_cam
end

function use_camera_transform()
  local w, h = SCR_WIDTH / 2, SCR_HEIGHT / 2
  lg.scale(active_cam.zoom)
  lg.translate(
    -active_cam.realx + w / active_cam.zoom,
    -active_cam.realy + h / active_cam.zoom)
end