-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcamera.lua
More file actions
40 lines (31 loc) · 774 Bytes
/
camera.lua
File metadata and controls
40 lines (31 loc) · 774 Bytes
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
local Camera =
{
x = 0,
y = 0
}
function Camera:set()
-- push our transformation onto the stack for drawing
love.graphics.push()
love.graphics.translate(-self.x, -self.y)
end
function Camera:unset()
-- take off the stack for updating
love.graphics.pop()
end
function Camera:move(dx, dy)
self.x = self.x + (dx or 0)
self.y = self.y + (dy or 0)
end
function Camera:moveTo(x, y)
self.x = x or self.x
self.y = y or self.y
end
-- necessary because shifing everything makes mouse position invalid
function Camera:getMousePosition()
return self.x + love.mouse.getX(), self.y + love.mouse.getY()
end
-- converts an absolute position to a camera-transformed one
function Camera:resolvePosition(x, y)
return x - self.x, y - self.y
end
return Camera