-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.lua
More file actions
149 lines (127 loc) · 4.6 KB
/
main.lua
File metadata and controls
149 lines (127 loc) · 4.6 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
local Camera = require 'camera'
local offset = 32
local W, H = love.graphics.getDimensions()
local function resizeCamera( self, w, h )
local scaleW, scaleH = w / self.w, h / self.h
local scale = math.min( scaleW, scaleH )
self.w, self.h = scale * self.w, scale * self.h
self.aspectRatio = self.w / w
self.offsetX, self.offsetY = self.w / 2, self.h / 2
offset = offset * scale
end
local function drawCameraBounds( cam, mode )
love.graphics.rectangle( mode, cam.x, cam.y, cam.w, cam.h )
end
local mobileCam = Camera( W / 2 - 2 * offset, H - 2 * offset, { x = offset, y = offset, resizable = true, maintainAspectRatio = true,
resizingFunction = function( self, w, h )
resizeCamera( self, w, h )
local W, H = love.graphics.getDimensions()
self.x = offset
self.y = offset
end,
getContainerDimensions = function()
local W, H = love.graphics.getDimensions()
return W / 2 - 2 * offset, H - 2 * offset
end
} )
-- Moves at the same speed as the main layer
close = mobileCam:addLayer( 'close', 2, { relativeScale = .5 } )
far = mobileCam:addLayer( 'far', .5 )
local overviewCam = Camera( W / 2 - 2 * offset, H - 2 * offset, { x = W / 2 + offset, y = offset, resizable = true, maintainAspectRatio = true,
resizingFunction = function( self, w, h )
resizeCamera( self, w, h )
local W, H = love.graphics.getDimensions()
self.x = W / 2 + offset
self.y = offset
end,
getContainerDimensions = function()
local W, H = love.graphics.getDimensions()
return W / 2 - 2 * offset, H - 2 * offset
end
} )
local squares = {}
local function newSquare( x, y, w, h )
table.insert( squares, {
x = x - w / 2, y = y - h / 2,
w = w, h = h,
draw = function( self )
love.graphics.rectangle( 'fill', self.x, self.y, self.w, self.h )
end,
} )
end
local function drawSquares()
for _, square in ipairs( squares ) do square:draw() end
end
function love.load()
end
function love.update( dt )
local moveSpeed = 300 / mobileCam.scale
-- Proves that getWorldCoordinates and getScreenCoordinates are accurate
-- Floating points may cause this to be _almost_ the same
-- local x, y = love.mouse.getPosition()
-- local _x, _y = mobileCam:getScreenCoordinates( mobileCam:getWorldCoordinates( x, y ) )
-- assert( x == _x and y == _y, x .. '~=' .. _x .. ', ' .. y .. '~=' .. _y )
if love.keyboard.isDown( 'q' ) then mobileCam:rotate( math.pi * dt ) end
if love.keyboard.isDown( 'e' ) then mobileCam:rotate( -math.pi * dt ) end
if love.keyboard.isDown( 'w' ) then mobileCam:translate( 0, -moveSpeed * dt ) end
if love.keyboard.isDown( 's' ) then mobileCam:translate( 0, moveSpeed * dt ) end
if love.keyboard.isDown( 'a' ) then mobileCam:translate( -moveSpeed * dt, 0 ) end
if love.keyboard.isDown( 'd' ) then mobileCam:translate( moveSpeed * dt, 0 ) end
if love.mouse.isDown( 1 ) then
local x, y = love.mouse.getPosition()
if x > mobileCam.x and x < mobileCam.x + mobileCam.w
and y > mobileCam.y and y < mobileCam.y + mobileCam.h then
local newX, newY = mobileCam:getWorldCoordinates( x, y )
newSquare( newX, newY, offset / mobileCam.scale, offset / mobileCam.scale )
end
if x > overviewCam.x and x < overviewCam.x + overviewCam.w
and y > overviewCam.y and y < overviewCam.y + overviewCam.h then
local newX, newY = overviewCam:getWorldCoordinates( x, y )
newSquare( newX, newY, offset / mobileCam.scale, offset / mobileCam.scale )
end
end
mobileCam:update()
overviewCam:update()
end
function love.draw()
drawCameraBounds( mobileCam, 'line' )
drawCameraBounds( overviewCam, 'line' )
-- Keep squares from bleeding
love.graphics.stencil( function() drawCameraBounds( mobileCam, 'fill' ) end, 'replace', 1 )
love.graphics.setStencilTest( 'greater', 0 )
mobileCam:push()
far:push()
love.graphics.setColor( 255, 0, 0, 25 )
drawSquares()
far:pop()
love.graphics.setColor( 0, 255, 0, 255 )
drawSquares()
-- Either method is acceptable
mobileCam:push( 'close' )
love.graphics.setColor( 0, 0, 255, 25 )
drawSquares()
mobileCam:pop( 'close' )
mobileCam:pop()
love.graphics.setColor( 255, 255, 255 )
love.graphics.stencil( function() drawCameraBounds( overviewCam, 'fill' ) end, 'replace', 1 )
love.graphics.setStencilTest( 'greater', 0 )
overviewCam:push()
drawSquares()
overviewCam:pop()
love.graphics.setStencilTest()
end
function love.keyreleased( key )
if key == 'escape' then love.event.quit()
elseif key == 'c' then squares = {}
elseif key == 'o' then
mobileCam:setTranslation( 0, 0 )
mobileCam:setRotation( 0, 0 )
mobileCam:setScale( 1 )
elseif key == 'r' then
love.keyreleased( 'o' )
love.keyreleased( 'c' )
end
end
function love.wheelmoved( dx, dy )
mobileCam:scaleToPoint( 1 + dy / 10 )
end