-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.lua
More file actions
157 lines (133 loc) · 4.04 KB
/
Copy pathcamera.lua
File metadata and controls
157 lines (133 loc) · 4.04 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
--
-- Camera
--
require 'slidable_utils'
function cameraInit(x, y, sx, sy, r)
local camera = {}
camera.x = x
camera.y = y
camera.scaleX = sx
camera.scaleY = sy
camera.rotation = r
camera.cx = 0;
camera.cy = 0;
-- For interpolating
camera.slidable = {}
camera.slidable.x = 0;
camera.slidable.y = 0;
camera.slidable.sourceX = 0;
camera.slidable.sourceY = 0;
camera.slidable.targetX = 0;
camera.slidable.targetY = 0;
camera.slidable.t = 0;
camera.slidable.animationTimeMs = 500
-- For camera controls
camera.lastMouseX = 0
camera.lastMouseY = 0
camera.mouseX = 0
camera.mouseY = 0
return camera
end
function cameraSet(camera, isCenter)
love.graphics.push()
love.graphics.scale(1 / camera.scaleX, 1 / camera.scaleY)
love.graphics.rotate(-camera.rotation)
if isCenter then
love.graphics.translate(-camera.cx + (camera.scaleX * 160)/2, -camera.cy + (camera.scaleY *144)/2)
else
love.graphics.translate(-camera.x, -camera.y)
end
end
function cameraCenter(camera, cx, cy)
camera.cx = cx;
camera.cy = cy;
end
function cameraSetTarget(camera, tx, ty)
setSlidableTarget(camera.slidable, tx, ty)
end
function cameraSetTargetCenter(camera, tx, ty)
cameraSetTarget(camera, tx - (camera.scaleX * 160)/2, ty - (camera.scaleY * 144)/2)
end
function cameraUnset(camera)
love.graphics.pop()
end
function cameraMove(camera, dx, dy)
camera.x = camera.x + (dx or 0)
camera.y = camera.y + (dy or 0)
end
function cameraAnimate(camera, dt, easingFunc)
updateSlidable(camera.slidable, dt, easingFunc)
camera.x = camera.slidable.x
camera.y = camera.slidable.y
end
function cameraLerp(camera, dt)
cameraAnimate(camera, dt, lerp)
end
function cameraRotate(dr)
camera.rotation = camera.rotation + dr
end
function cameraScale(camera, sx, sy)
sx = sx or 1
camera.scaleX = camera.scaleX * sx
camera.scaleY = camera.scaleY * (sy or sx)
end
function cameraSetPosition(camera, x, y)
camera.x = x or camera.x
camera.y = y or camera.y
end
function cameraSetPositionCenter(camera, x, y)
camera.x = x or camera.x
camera.x = camera.x - 144/2
camera.y = y or camera.y
camera.y = camera.y - 160/2
end
function cameraSetScale(camera, sx, sy)
camera.scaleX = sx or camera.scaleX
camera.scaleY = sy or camera.scaleY
end
function cameraGetMousePosition(camera)
return love.mouse.getX() * camera.scaleX + camera.x, love.mouse.getY() * camera.scaleY + camera.y
end
function rectIntersectsCamera(camera, x, y, w, h)
return AABB2Intersection(camera.x, camera.y, camera.scaleX * 160, camera.scaleY * 144, x, y, w, h)
end
function rectInsideCamera(camera, x, y, w, h)
return AABB2Overlaps(camera.x, camera.y, camera.scaleX * 160, camera.scaleY * 144, x, y, w, h)
end
-- TODO(ray): Should maybe be in a geometry/math/utils file
function AABB2Intersection(x1, y1, w1, h1, x2, y2, w2, h2)
if (x1 + w1 < x2) or (x2 + w2 < x1) then
return false
end
if (y1 + h1 < y2) or (y2 + h2 < y1) then
return false
end
return true
end
-- Does Rect1 contain all of Rect2
function AABB2Overlaps(x1, y1, w1, h1, x2, y2, w2, h2)
if x2 >= x1 and x1+w1 >= x2+w2 and y2 >= y1 and y1+h1 >= y2+h2 then return true end
return false
end
function basicCameraControl(camera, dt)
if love.keyboard.isDown("left") then
cameraMove(camera, -500 * dt, 0)
end
if love.keyboard.isDown("right") then
cameraMove(camera, 500 * dt, 0)
end
if love.keyboard.isDown("up") then
cameraMove(camera, 0, -500 * dt)
end
if love.keyboard.isDown("down") then
cameraMove(camera, 0, 500 * dt)
end
camera.lastMouseX = camera.mouseX
camera.lastMouseY = camera.mouseY
camera.mouseX, camera.mouseY = love.mouse.getPosition()
if love.mouse.isDown(3) then
local dx = camera.mouseX - camera.lastMouseX
local dy = camera.mouseY - camera.lastMouseY
cameraMove(camera, -200 * camera.scaleX * dx * dt, -200 * camera.scaleY * dy * dt)
end
end