-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.lua
88 lines (78 loc) · 2.93 KB
/
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
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
local class = require 'middleclass'
camera = class('camera')
function camera:initialize(mapWidth, mapHeight, rotation, scale)
self._width = love.graphics.getWidth()
self._height = love.graphics.getHeight()
self._mapWidth = mapWidth
self._mapHeight = mapHeight
self._rotation = rotation
self._scale = scale
self._layers = {}
self._transformationX = nil
self._transformationY = nil
end
function camera:centerOn(x, y)
if self._width < self._mapWidth*self._scale then
self._transformationX = math.floor((-x * self._scale) + (self._width /2))/self._scale
if self._transformationX > 0 then
self._transformationX = 0
elseif self._transformationX < ((self._mapWidth) - (self._width)) then
self._transformationX = (self._mapWidth - self._width)
end
else
self._transformationX = ((self._width / self._scale) - self._mapWidth)/2
end
if self._height < self._mapHeight * self._scale then
self._transformationY = (-y /2)/self._scale
if self._transformationY > 0 then
self._transformationY = 0
elseif self._transformationY < -((self._mapHeight * self._scale) - (self._height))/self._scale then
self._transformationY = (self._height / self._scale) - self._mapHeight
end
else
self._transformationY = ((self._height / self._scale) - self._mapHeight)/2
end
--if self._height < self._mapHeight*self._scale then
-- self._transformationY = math.floor(-y/self._scale + ((self._height/self._scale) /2))
-- if self._transformationY > 0 then
-- self._transformationY = 0
-- elseif self._transformationY < -((self._mapHeight/self._scale) - (self._height/self._scale)) then
-- self._transformationY = -((self._mapHeight/self._scale) - (self._height/self._scale))
-- end
--else
-- self._transformationY = ((self._height/self._scale) - (self._mapHeight/self._scale))/2
--end
end
function camera:newLayer(order, scale, func)
local newLayer = {draw = func, scale = scale, order = order}
table.insert(self._layers, newLayer)
table.sort(self._layers, function(a,b) return a.order < b.order end)
return newLayer
end
function camera:set()
love.graphics.push()
love.graphics.rotate(-self._rotation)
love.graphics.scale(self._scale, self._scale)
love.graphics.translate(self._transformationX, self._transformationY)
end
function camera:unset()
love.graphics.pop()
end
function camera:draw()
local bx, by = self._transformationX, self._transformationY
for i, v in ipairs(self._layers) do
self._transformationX = bx * v.scale
--self._transformationY = by * v.scale
self:set()
v.draw()
self:unset()
end
self._transformationX, self._transformationY = bx, by
-- draw lives remaining
love.graphics.setColor(206, 210, 161)
love.graphics.print('Lives: '..myPlayer:getLives(), love.graphics.getWidth()-(50*self._scale), 10, 0, self._scale, self._scale)
love.graphics.setColor(256, 256, 256)
end
function camera:getScale()
return self._scale
end