-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
camera.lua
196 lines (156 loc) · 3.59 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
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
--=========== Copyright © 2019, Planimeter, All rights reserved. ===========--
--
-- Purpose: Camera interface
--
--==========================================================================--
local accessor = accessor
local class = class
local concommand = concommand
local ipairs = ipairs
local love = love
local math = math
local point = point
local table = table
local tween = tween
local vector = vector
local _G = _G
module( "camera" )
_contexts = _contexts or {}
class( "context" )
local context = _G.context
function context:context( worldIndex, x, y, func )
self.worldIndex = worldIndex
self.position = vector( x, y )
self._drawFunc = func
end
function context:draw()
self._drawFunc()
self:remove()
end
function context:getDrawPosition()
local position = self.position
return position.x, position.y
end
accessor( context, "position" )
accessor( context, "worldIndex" )
function context:remove()
for i, v in ipairs( _contexts ) do
if ( v == self ) then
table.remove( _contexts, i )
end
end
end
function drawToWorld( worldIndex, x, y, func )
local context = context( worldIndex, x, y, func )
table.insert( _contexts, context )
end
function getParentEntity()
return _entity
end
_position = _position or vector()
function getPosition()
local entity = getParentEntity()
if ( entity ) then
return entity:getPosition()
end
return _position
end
function getTranslation()
local pos = getPosition()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local scale = getZoom()
if ( pos == nil ) then
pos = vector.origin
end
return -pos.x + ( width / 2 ) / scale,
-pos.y + ( height / 2 ) / scale
end
function getWorldContexts()
return _contexts
end
_worldIndex = _worldIndex or 1
function getWorldIndex()
local entity = getParentEntity()
if ( entity ) then
return entity:getWorldIndex()
end
return _worldIndex
end
_minZoom = _minZoom or 1
function getMinZoom()
return _minZoom
end
_maxZoom = _maxZoom or 4
function getMaxZoom()
return _maxZoom
end
_zoom = _zoom or 1
function getZoom()
return _zoom
end
local function resetTween()
_tween = nil
end
function resetZoom()
if ( _tween == nil ) then
_tween = tween( _M, nil, {
_zoom = 2,
onComplete = resetTween
} )
end
_tween.pos = 0
end
function screenToWorld( x, y )
local pos = getPosition()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local scale = getZoom()
if ( pos == nil ) then
pos = vector.origin
end
return pos.x - ( width / 2 ) / scale + x / scale,
pos.y - ( height / 2 ) / scale + y / scale
end
function worldToScreen( x, y )
local pos = getPosition()
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
local scale = getZoom()
if ( pos == nil ) then
pos = vector.origin
end
return pos.x * -scale + ( width / 2 ) + x * scale,
pos.y * -scale + ( height / 2 ) + y * scale
end
function setParentEntity( entity )
_entity = entity
end
function setPosition( position )
_position = position
end
function setMinZoom( minZoom )
_minZoom = minZoom
end
function setMaxZoom( maxZoom )
_maxZoom = maxZoom
end
function setZoom( zoom )
if ( _tween ) then
return
end
_zoom = math.clamp( zoom, getMinZoom(), getMaxZoom() )
end
concommand( "zoomin", "Zooms the camera in", function()
local scale = getZoom()
setZoom( scale + 1 )
end )
concommand( "zoomout", "Zooms the camera out", function()
local scale = getZoom()
setZoom( scale - 1 )
end )
function update( dt )
if ( _tween ) then
_tween:update( dt )
end
end