-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperspective.lua
295 lines (245 loc) · 11.5 KB
/
perspective.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
--[[
Perspective v2.1.0
A library for easily and smoothly integrating a virtual camera into your game.
Based on modified version of the Dusk camera system.
v2.1.0 allows you to prepend layers in front, starting with index 0, and fixes
offset behavior.
By GymbylCoding
https://gist.github.com/GymbylCoding/8675733
--]]
local lib_perspective = {}
--------------------------------------------------------------------------------
-- Localize
--------------------------------------------------------------------------------
local display_newGroup = display.newGroup
local display_remove = display.remove
local type = type
local table_insert = table.insert
local math_huge = math.huge
local math_nhuge = -math.huge
local clamp = function(v, l, h) return (v < l and l) or (v > h and h) or v end
--------------------------------------------------------------------------------
-- Create View
--------------------------------------------------------------------------------
lib_perspective.createView = function(layerCount)
------------------------------------------------------------------------------
-- Create view, internal object, and layers
------------------------------------------------------------------------------
local view = display_newGroup()
view.damping = 1
view.snapWhenFocused = true -- Do we instantly snap to the object when :setFocus() is called?
local isTracking
local prependedLayers = 0
local internal -- So we can access it from inside the declaration
internal = {
trackingLevel = 1,
damping = 1,
scaleBoundsToScreen = true,
xScale = 1,
yScale = 1,
xOffset = 0,
yOffset = 0,
addX = display.contentCenterX,
addY = display.contentCenterY,
bounds = {
xMin = math_nhuge,
xMax = math_huge,
yMin = math_nhuge,
yMax = math_huge
},
scaledBounds = {
xMin = math_nhuge,
xMax = math_huge,
yMin = math_nhuge,
yMax = math_huge
},
trackFocus = true,
focus = nil,
viewX = 0,
viewY = 0,
getViewXY = function() if internal.focus then return internal.focus.x, internal.focus.y else return internal.viewX, internal.viewY end end,
layer = {},
updateAddXY = function() internal.addX = display.contentCenterX / view.xScale internal.addY = display.contentCenterY / view.yScale end
}
local layers = {}
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Internal Methods
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Scale Bounds
------------------------------------------------------------------------------
internal.scaleBounds = function(doX, doY)
if internal.scaleBoundsToScreen then
local xMin = internal.bounds.xMin
local xMax = internal.bounds.xMax
local yMin = internal.bounds.yMin
local yMax = internal.bounds.yMax
local doX = doX and not ((xMin == math_nhuge) or (xMax == math_huge))
local doY = doY and not ((yMin == math_nhuge) or (yMax == math_huge))
if doX then
local scaled_xMin = xMin / view.xScale
local scaled_xMax = xMax - (scaled_xMin - xMin)
if scaled_xMax < scaled_xMin then
local hopDist = scaled_xMin - scaled_xMax
local halfDist = hopDist * 0.5
scaled_xMax = scaled_xMax + halfDist
scaled_xMin = scaled_xMin - halfDist
end
internal.scaledBounds.xMin = scaled_xMin
internal.scaledBounds.xMax = scaled_xMax
end
if doY then
local scaled_yMin = yMin / view.yScale
local scaled_yMax = yMax - (scaled_yMin - yMin)
if scaled_yMax < scaled_yMin then
local hopDist = scaled_yMin - scaled_yMax
local halfDist = hopDist * 0.5
scaled_yMax = scaled_yMax + halfDist
scaled_yMin = scaled_yMin - halfDist
end
internal.scaledBounds.yMin = scaled_yMin
internal.scaledBounds.yMax = scaled_yMax
end
else
camera.scaledBounds.xMin, camera.scaledBounds.xMax, camera.scaledBounds.yMin, camera.scaledBounds.yMax = camera.bounds.xMin, camera.bounds.xMax, camera.bounds.yMin, camera.bounds.yMax
end
end
------------------------------------------------------------------------------
-- Process Viewpoint
------------------------------------------------------------------------------
internal.processViewpoint = function()
if internal.damping ~= view.damping then internal.trackingLevel = 1 / view.damping internal.damping = view.damping end
if internal.trackFocus then
local x, y = internal.getViewXY()
if view.xScale ~= internal.xScale or view.yScale ~= internal.yScale then internal.updateAddXY() end
if view.xScale ~= internal.xScale then internal.xScale = view.xScale internal.scaleBounds(true, false) end
if view.yScale ~= internal.yScale then internal.yScale = view.yScale internal.scaleBounds(false, true) end
x = clamp(x, internal.scaledBounds.xMin, internal.scaledBounds.xMax)
y = clamp(y, internal.scaledBounds.yMin, internal.scaledBounds.yMax)
internal.viewX, internal.viewY = x, y
end
end
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Public Methods
------------------------------------------------------------------------------
------------------------------------------------------------------------------
------------------------------------------------------------------------------
-- Append Layer
------------------------------------------------------------------------------
view.appendLayer = function()
local layer = display_newGroup()
layer.xParallax, layer.yParallax = 1, 1
view:insert(layer)
layer:toBack()
table_insert(layers, layer)
layer._perspectiveIndex = #layers
internal.layer[#layers] = {
x = 0,
y = 0,
xOffset = 0,
yOffset = 0
}
function layer:setCameraOffset(x, y) internal.layer[layer._perspectiveIndex].xOffset, internal.layer[layer._perspectiveIndex].yOffset = x, y end
end
view.prependLayer = function()
view.appendLayer()
layers[#layers]:toFront()
layers[-prependedLayers] = layers[#layers]
internal.layer[-prependedLayers] = internal.layer[#internal.layer]
table.remove(layers, #layers)
table.remove(internal.layer, #internal.layer)
layers[-prependedLayers]._perspectiveIndex = -prependedLayers
prependedLayers = prependedLayers + 1
end
------------------------------------------------------------------------------
-- Add an Object to the Camera
------------------------------------------------------------------------------
function view:add(obj, l, isFocus)
local l = l or 4
layers[l]:insert(obj)
obj._perspectiveLayer = l
if isFocus then view:setFocus(obj) end
-- Move an object to a layer
function obj:toLayer(newLayer) if layer[newLayer] then layer[newLayer]:insert(obj) obj._perspectiveLayer = newLayer end end
--Move an object back a layer
function obj:back() if layer[obj._perspectiveLayer + 1] then layer[obj._perspectiveLayer + 1]:insert(obj) obj._perspectiveLayer = obj.layer + 1 end end
--Moves an object forwards a layer
function obj:forward() if layer[obj._perspectiveLayer - 1] then layer[obj._perspectiveLayer - 1]:insert(obj) obj._perspectiveLayer = obj.layer - 1 end end
--Moves an object to the very front of the camera
function obj:toCameraFront() layer[1]:insert(obj) obj._perspectiveLayer = 1 obj:toFront() end
--Moves an object to the very back of the camera
function obj:toCameraBack() layer[#layers]:insert(obj) obj._perspectiveLayer = #layers obj:toBack() end
end
------------------------------------------------------------------------------
-- Main Tracking Function
------------------------------------------------------------------------------
function view:trackFocus()
internal.processViewpoint()
local viewX, viewY = internal.viewX, internal.viewY
layers[1].xParallax, layers[1].yParallax = 1, 1
for i = -prependedLayers + 1, #layers do
local addX, addY = internal.addX, internal.addY
local layerX, layerY = internal.layer[i].x, internal.layer[i].y
local diffX = (-viewX - layerX)
local diffY = (-viewY - layerY)
local incrX = diffX
local incrY = diffY
internal.layer[i].x = layerX + incrX + internal.layer[i].xOffset + internal.xOffset
internal.layer[i].y = layerY + incrY + internal.layer[i].yOffset + internal.yOffset
layers[i].x = (layers[i].x - (layers[i].x - (internal.layer[i].x + addX) * layers[i].xParallax) * internal.trackingLevel)
layers[i].y = (layers[i].y - (layers[i].y - (internal.layer[i].y + addY) * layers[i].yParallax) * internal.trackingLevel)
end
view.scrollX, view.scrollY = layers[1].x, layers[1].y
end
------------------------------------------------------------------------------
-- Set the Camera Bounds
------------------------------------------------------------------------------
function view:setBounds(x1, x2, y1, y2)
local xMin, xMax, yMin, yMax
if x1 ~= nil then if not x1 then xMin = math_nhuge else xMin = x1 end end
if x2 ~= nil then if not x2 then xMax = math_huge else xMax = x2 end end
if y1 ~= nil then if not y1 then yMin = math_nhuge else yMin = y1 end end
if y2 ~= nil then if not y2 then yMax = math_huge else yMax = y2 end end
internal.bounds.xMin = xMin
internal.bounds.xMax = xMax
internal.bounds.yMin = yMin
internal.bounds.yMax = yMax
internal.scaleBounds(true, true)
end
------------------------------------------------------------------------------
-- Miscellaneous Functions
------------------------------------------------------------------------------
-- Begin auto-tracking
function view:track() if not isTracking then Runtime:addEventListener("enterFrame", view.trackFocus) isTracking = true end end
-- Stop auto-tracking
function view:cancel() if isTracking then Runtime:removeEventListener("enterFrame", view.trackFocus) isTracking = false end end
-- Remove an object from the view
function view:remove(obj) if obj and obj._perspectiveLayer then layers[obj._perspectiveLayer]:remove(obj) end end
-- Set the view's focus
function view:setFocus(obj) if obj then internal.focus = obj end if view.snapWhenFocused then view.snap() end end
-- Snap the view to the focus point
function view:snap() local t = internal.trackingLevel local d = internal.damping internal.trackingLevel = 1 internal.damping = view.damping view:trackFocus() internal.trackingLevel = t internal.damping = d end
-- Move the view to a point
function view:toPoint(x, y) view:cancel() local newFocus = {x = x, y = y} view:setFocus(newFocus) view:track() return newFocus end
-- Get a layer of the view
function view:layer(n) return layers[n] end
-- Get the layer container of the view
function view:layers() return layers end
-- Destroy the view
function view:destroy() view:cancel() for i = 1, #layers do display_remove(layers[i]) end display_remove(view) view = nil return true end
-- Set layer parallax
function view:setParallax(...) for i = 1, #arg do if type(arg[i]) == "table" then layers[i].xParallax, layers[i].yParallax = arg[i][1], arg[i][2] else layers[i].xParallax, layers[i].yParallax = arg[i], arg[i] end end end
-- Get number of layers
function view:layerCount() return #layers end
-- Set the view's master offset
function view:setMasterOffset(x, y) internal.xOffset, internal.yOffset = x, y end
------------------------------------------------------------------------------
-- Build Layers
------------------------------------------------------------------------------
for i = layerCount or 8, 1, -1 do view.appendLayer() end
return view
end
return lib_perspective