-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.lua
More file actions
63 lines (53 loc) · 1.36 KB
/
Copy pathcamera.lua
File metadata and controls
63 lines (53 loc) · 1.36 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
MAP_WIDTH = 1920
local camera = {}
local instance -- Camera is a Singleton(how would there be more than one?)
setmetatable(camera,camera)
function camera:__call()
if not instance then
instance = {x = 640,offset = 0}
setmetatable(instance,{__index = camera})
end
return instance
end
function camera:init(c1,c2)
if not instance then camera() end
instance.c1 = c1
instance.c2 = c2
end
function camera:reset()
self.x = 640
self.offset = 0
end
local function calculateMiddle(self)
local distance
if self.c1.lookingRight then
distance =self.c2.x-(self.c1.x+75)
return (self.c1.x + 75)+distance/2,distance
else
distance = self.c1.x-(self.c2.x+75)
return (self.c2.x + 75)+distance/2,distance
end
end
function camera:update()
local mid,distance = calculateMiddle(self)-- the point central between the characters
self.offset = (self.x+320) - mid
self.x = mid -320
if self.x < 0 then self.x= 0
elseif self.x > MAP_WIDTH-640 then self.x = MAP_WIDTH-640 end
--[[ if distance >= 150 then
self.x = mid-320
elseif self.offset >= 150 then
self.x = self.x + (self.offset-150)
elseif self.offset <= -150 then
self.x = self.x - (self.offset-150)
end]]
self.offset = (self.x+320) -mid
end
function camera:applyTransformations(scaleX,scaleY)
local xTrans = self.x
if scaleX then
xTrans = xTrans*scaleX
end
love.graphics.translate(-xTrans,0)
end
return camera