-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.lua
50 lines (43 loc) · 897 Bytes
/
camera.lua
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
42
43
44
45
46
47
48
49
50
local camera = {}
function camera.new()
local cam =
setmetatable(
{
x = 0,
y = 0,
tx = 0,
ty = 0,
ts = 0,
s = 0.2
},
{__index = camera}
)
return cam
end
function camera:push()
love.graphics.push()
love.graphics.translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2)
love.graphics.scale(self.s, self.s)
love.graphics.translate(-self.x, self.y)
love.graphics.scale(1, -1)
end
function camera:pop()
love.graphics.pop()
end
function camera:update(dt)
if math.abs(self.tx - self.x) > 300 then
self.x = self.x + (self.tx - self.x) * dt
end
if math.abs(self.ty - self.y) > 200 then
self.y = self.y + (self.ty - self.y) * dt * 5
end
self.s = self.s + (self.ts - self.s) * dt
end
function camera:set(x, y)
self.x, self.tx = x
self.y, self.ty = y
end
function camera:target(x, y, s)
self.tx, self.ty, self.ts = x, y, s
end
return camera