-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.lua
More file actions
executable file
·123 lines (93 loc) · 2.32 KB
/
camera.lua
File metadata and controls
executable file
·123 lines (93 loc) · 2.32 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'charactercontroller'
camera = {}
camera._x = 0
camera._y = 0
camera.scaleX = 1
camera.scaleY = 1
camera.rotation = 0
camera.layers = {}
--local windowSize = {}
--windowSize.x, windowSize.y = love.window.getHeight(),love.window.getWidth()
--function windowSize:scale(x)
-- windowSize.x = windowSize.x * x
-- windowSize.y = windowSize.y * x
--end
function camera:set()
love.graphics.push()
love.graphics.rotate(-self.rotation)
love.graphics.scale(1 / self.scaleX, 1 / self.scaleY)
love.graphics.translate(-self._x+windowSize.x/2, -self._y+windowSize.y/2)
end
function camera:newLayer(scale, func)
table.insert(self.layers, {draw = func, scale = scale})
table.sort(self.layers, function(a,b) return a.scale < b.scale end)
end
function camera:draw()
local bx,by = self._x, self._y
for _,v in ipairs(self.layers) do
self.x = bx * v.scale
self.y = by * v.scale
camera:set()
v.draw()
camera:unset()
end
end
function camera:unset()
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:rotate(dr)
self.rotation = self.rotation + dr
end
function camera:scale(sx, sy)
sx = sx or 1
self.scaleX = self.scaleX * sx
self.scaleY = self.scaleY * (sy or sx)
end
function camera:setX(value)
if self._bounds then
self._x = math.clamp(value, self._bounds.x1, self._bounds.x2)
else
self._x = value
end
end
function camera:setY(value)
if self._bounds then
self._y = math.clamp(value, self._bounds.y1, self._bounds.y2)
else
self._y = value
end
end
function camera:setPosition(x, y)
if x then self:setX(x) end
if y then self:setY(y) end
end
function camera:setScale(sx, sy)
self.scaleX = sx or self.scaleX
self.scaleY = sy or self.scaleY
end
function camera:getBounds()
return unpack(self._bounds)
end
function camera:setBounds(x1, y1, x2, y2)
self._bounds = { x1 = x1, y1 = y1, x2 = x2, y2 = y2 }
end
function camera:load(args)
--camera:setPosition(player.x-windowSize.x/2,player.y-windowSize.y/2)
--camera:setScale(.75,.75)
end
function camera:update(dt)
--camera:setPosition(player.x-windowSize.x/2,player.y-windowSize.y/2)
end
function math.clamp(x, min, max)
if x < min then
return min
elseif x > max then
return max
else
return x
end
end