forked from jcmoyer/LD26
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
254 lines (220 loc) · 5.59 KB
/
main.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
local player = require('player')
local world = require('world')
local camera = require('camera')
local mathex = require('mathex')
local message = require('message')
local gamecontext = require('gamecontext')
local switchSnd = love.audio.newSource('data/switch.ogg', 'static')
local portalSnd = love.audio.newSource('data/Portal.ogg', 'static')
local shift05Snd = love.audio.newSource('data/Shifting05.ogg', 'static')
local shift10Snd = love.audio.newSource('data/Shifting10.ogg', 'static')
local shift15Snd = love.audio.newSource('data/Shifting15.ogg', 'static')
local shift20Snd = love.audio.newSource('data/Shifting20.ogg', 'static')
local deathSnd = love.audio.newSource('data/death.ogg', 'static')
local p = player.new()
local w = nil
local c = camera.new(800, 600)
local m = nil
local mdx = 0
local mdy = 0
local lastregion = nil
local context = nil
local gameoverFont = nil
local gameoverSubFont = nil
local gameover = false
local gameoverMessage = nil
function pickShiftSnd(magnitude)
if magnitude < 5 then
return shift05Snd
elseif magnitude < 10 then
return shift10Snd
elseif magnitude < 15 then
return shift15Snd
else
return shift20Snd
end
end
function playDeathSnd()
love.audio.stop(deathSnd)
love.audio.play(deathSnd)
end
function playSwitchSnd()
love.audio.stop(switchSnd)
love.audio.play(switchSnd)
end
function makecontext()
local ctx = gamecontext.new()
function ctx.showMessage(text, duration)
if m then
local oldX = m.x
local oldY = m.y
m = message.new(text, duration)
m.x = oldX
m.y = oldY
else
m = message.new(text, duration)
end
end
function ctx.changeWorld(name, x)
p.x = x or p.x
if name ~= w.name then
changeworld(name)
p.x = mathex.clamp(p.x, w:left(), w:right())
p.y = w:y(p.x)
c:center(p.x, p.y)
end
end
function ctx.addPortal(name, x, d, dx, silent)
if not silent then
love.audio.play(portalSnd)
end
w:addPortal(name, x, d, dx)
end
function ctx.removePortal(name)
w:removePortal(name)
end
function ctx.addRegion(name, x, width)
w:addRegion(name, x, width)
end
function ctx.y(x)
return w:y(x)
end
function ctx.preferredColor()
return w:oppositeColor()
end
function ctx.getSwitchStatus(name)
return w:getSwitchStatus(name)
end
function ctx.setSwitchStatus(name, status)
w:setSwitchStatus(name, status)
end
function ctx.win(text)
gameover = true
gameoverMessage = text
end
function ctx.playSwitchSound()
playSwitchSnd()
end
function ctx.shakeCamera(d, m)
love.audio.play(pickShiftSnd(m))
c:shake(d, m)
end
function ctx.playerX()
return p.x
end
function ctx.left()
return w:left()
end
function ctx.right()
return w:right()
end
function ctx.setBackground(rgb)
w.background = rgb
end
function ctx.setForeground(rgb)
w.foreground = rgb
end
return ctx
end
function drawWinScreen()
local g = love.graphics
local w = g.getWidth()
local h = g.getHeight()
local message = 'Thanks for playing!'
local submessage = 'Made for LD26 :: https://github.com/jcmoyer'
local mh = gameoverFont:getHeight()
local mw = gameoverFont:getWidth(message)
local sw = gameoverSubFont:getWidth(submessage)
local udsw = gameoverSubFont:getWidth(gameoverMessage)
g.setFont(gameoverFont)
g.print(message, w / 2 - mw / 2, h / 2 - mh / 2)
g.setFont(gameoverSubFont)
love.graphics.print(submessage, w / 2 - sw / 2, h / 2 - mh / 2 + mh + 8)
love.graphics.print(gameoverMessage, w / 2 - udsw / 2, h / 3)
end
function changeworld(name)
w = world.new(name, context)
w:onEnter(context)
end
function love.load()
context = makecontext()
love.graphics.setFont(love.graphics.newFont(18))
gameoverFont = love.graphics.newFont(36)
gameoverSubFont = love.graphics.newFont(16)
changeworld('data.start')
end
function love.draw()
if gameover then
drawWinScreen()
return
end
local g = love.graphics
g.setBackgroundColor(w.background)
g.clear()
g.translate(c:calculatedX(), c:calculatedY())
w:draw()
p.color = w:oppositeColor()
p:draw()
if (m and m:visible()) then
m.color = w:oppositeColor()
local f = g.getFont()
local width = f:getWidth(m.text)
mdx = p.x - width / 2
mdy = p.y - p.h - f:getHeight() - 8
m.x = mathex.lerp(m.x, mdx, 0.2)
m.y = mathex.lerp(m.y, mdy, 0.2)
m:draw()
end
w:scriptDraw(context)
end
function love.update(dt)
local k = love.keyboard
if k.isDown('left') then
p.x = p.x - 300 * dt
end
if k.isDown('right') then
p.x = p.x + 300 * dt
end
p.x = mathex.clamp(p.x, w:left(), w:right())
p.y = w:y(p.x)
local r = w:regionAt(p.x)
if r ~= lastregion then
-- if r is nil then we've left a region
if r then
w:onEnterRegion(context, r)
end
lastregion = r
end
-- activate switches at the player's location
if w:activateAt(p.x, context) then
playSwitchSnd()
end
-- check for death conditions
if w:enemyAt(p.x) then
playDeathSnd()
w:onPlayerDeath(context)
end
if m then
m:update(dt)
end
w:update(dt)
w:scriptUpdate(context, dt)
c:update(dt)
c:panCenter(p.x, p.y, dt)
end
function love.keypressed(key, unicode)
if key == 'up' then
local portal = w:portalAt(p.x)
if portal then
if not w:onEnterPortal(context, portal) then return end
p.x = portal.dx
if portal.destination ~= w.name then
changeworld(portal.destination)
-- Instant pan when the world is different
p.x = mathex.clamp(p.x, w:left(), w:right())
p.y = w:y(p.x)
c:center(p.x, p.y)
end
end
end
end