-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
399 lines (332 loc) · 13.3 KB
/
main.lua
File metadata and controls
399 lines (332 loc) · 13.3 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
-- main.lua
-- (c) 2018 by Milan Gruner
class = require "libs.30log"
inspect = require "libs.inspect"
local bump = require "libs.bump"
local anim8 = require "libs.anim8"
local util = require "util.util"
local ProgressBar = require "ui.ProgressBar"
local Background = require "fx.Background"
local SoundManager = require "util.SoundManager"
local Settings = require "data.Settings"
require "data.Entities"
local mobile = false
local noise
if love.system.getOS() == "iOS" or love.system.getOS() == "Android" then
mobile = true
Settings.scale = Settings.mobileScale
Settings.uiScale = Settings.mobileUiScale
else
noise = require "libs.noise"
end
local isRunning = true
local staminaBar, background, soundManager, noiseShader, noiseTimer
local function getScreenSize(unscaled)
local scaleX, scaleY = Settings.scale, Settings.scale
if unscaled then
scaleX, scaleY = Settings.uiScale, Settings.uiScale
end
return love.graphics.getWidth() / scaleX, love.graphics.getHeight() / scaleY
end
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest", 1)
local font = love.graphics.newFont("fonts/permanent_marker.ttf", 40)
love.graphics.setFont(font)
if not mobile then
noise.init()
noiseShader = noise.build_shader("libs/noise.frag", Settings.noiseSeed)
noiseTimer = 0
end
love.resize(love.graphics.getDimensions())
soundManager = SoundManager()
Player.sprite = love.graphics.newImage("sprites/ananas.png")
local spriteWidth, spriteHeight = Player.sprite:getWidth(), Player.sprite:getHeight()
local grid = anim8.newGrid(Player.width, Player.height, spriteWidth, spriteHeight)
Player.animation = anim8.newAnimation(grid('1-7', 1), Settings.playerAnimationSpeed) -- , "pauseAtEnd"
Player.healthAnimation = anim8.newAnimation(grid('1-4', 1, '4-1', 1), Settings.playerAnimationSpeed * 2) -- , "pauseAtEnd"
Player.frontalGlassesAnimation = anim8.newAnimation(grid('1-4', 2, '4-1', 2), Settings.playerAnimationSpeed * 2) -- , "pauseAtEnd"
Player.glassesAnimation = anim8.newAnimation(grid('1-7', 3), Settings.playerAnimationSpeed) -- , "pauseAtEnd"
Banana.sprite = love.graphics.newImage("sprites/banana.png")
spriteWidth, spriteHeight = Banana.sprite:getWidth(), Banana.sprite:getHeight()
grid = anim8.newGrid(Banana.width, Banana.height, spriteWidth, spriteHeight)
Banana.animation = anim8.newAnimation(grid('1-5', 1, '4-2', 1), Settings.playerAnimationSpeed)
bumpWorld = bump.newWorld()
bumpWorld:add(Player, Player.x, Player.y, Player.width, Player.height)
bumpWorld:add(Ground, Ground.x, Ground.y, Ground.height, Ground.width)
Obstacles.sprite = love.graphics.newImage("sprites/obstacles.png")
local tileWidth, tileHeight = 16, 16
for i = 1, Obstacles.count do
local quad = love.graphics.newQuad((i - 1) * tileWidth, 0, tileWidth, tileHeight, Obstacles.sprite:getWidth(), Obstacles.sprite:getHeight())
local width, height = Obstacles.defaultWidth, Obstacles.defaultHeight
local x, y = love.graphics.getWidth() / Settings.scale + i * Obstacles.spacing, Ground.y - height
local obstacle = {
x = x, y = y, width = width, height = height, quad = quad, isObstacle = true, index = #Obstacles + 1
}
bumpWorld:add(obstacle, x, y, width, height)
table.insert(Obstacles, obstacle)
end
local plantSprite = love.graphics.newImage("sprites/plant.png")
local width, height = plantSprite:getWidth(), plantSprite:getHeight()
for i = 1, Obstacles.pickupCount do
local x = love.graphics.getWidth() / Settings.scale + Obstacles.count * Obstacles.spacing + i * Obstacles.spacing
local y = Ground.y - height
local plantObstacle = {
x = x, y = y, width = width, height = height, sprite = plantSprite, isPickup = true, index = #Obstacles + 1
}
bumpWorld:add(plantObstacle, x, y, width, height)
table.insert(Obstacles, plantObstacle)
end
bumpWorld:add(Banana, Banana.x, Banana.y, Banana.width, Banana.height)
Banana.index = #Obstacles + 1
table.insert(Obstacles, Banana)
staminaBar = ProgressBar("staminabar", 70, 35, 1, 0, "right", 24, 7, false)
local width, height = getScreenSize()
background = Background(width, height * (1 - Settings.groundPercentage))
soundManager:playMusic(1)
end
local function jump()
if Player.canJump then
soundManager:play("jump", "random")
Player.isJumping = true
Player.canJump = false
Player.hasLanded = false
Player.velocityY = -Player.jumpVelocity
end
end
local function fallFast()
if Player.isJumping then
Player.isJumping = false
Player.velocityY = Player.velocityY + Player.jumpVelocity
end
end
local function resetWorld()
for i = 1, #Obstacles do
Obstacles[i].x = love.graphics.getWidth() / Settings.scale + i * Obstacles.spacing
end
end
local function removeObstacle(obstacle)
bumpWorld:remove(obstacle)
if Obstacles[obstacle.index] == obstacle then
Obstacles[obstacle.index] = nil
end
--util.clearAllEqualValues(Obstacles, obstacle)
--util.removeValue(Obstacles, obstacle)
end
local function resetGame()
isRunning = true
resetWorld()
Player.score = 0
Player.stamina = 0
end
local function gameOver()
soundManager:play("explosion", "random")
isRunning = false
Player.health = Settings.maxPlayerHealth
print("Game over! Score: " .. math.floor(Player.score))
end
local function takeHit(obstacle)
soundManager:play("hit", "random")
Player.health = Player.health - 1
-- TODO slow down time
removeObstacle(obstacle)
if Player.health <= 0 then
gameOver()
end
end
local function collectPickup(pickup)
soundManager:play("pickup", "random")
Player.stamina = Player.stamina + Settings.pickupStamina
-- TODO add combo multiplier
-- bumpWorld:remove(pickup)
-- util.removeValue(Obstacles, pickup)
pickup.x = love.graphics.getWidth() / Settings.scale + pickup.sprite:getWidth()
bumpWorld:update(pickup, pickup.x, pickup.y)
end
function love.update(dt)
if not isRunning then return end
for i = 1, #Obstacles do
local obstacle = Obstacles[i]
if obstacle then
obstacle.x = obstacle.x + Obstacles.velocityX * dt
if obstacle.x < -obstacle.width then
obstacle.x = love.graphics.getWidth() / Settings.scale + obstacle.width
bumpWorld:update(obstacle, obstacle.x, obstacle.y)
end
local actualX, actualY, collisions = bumpWorld:move(obstacle, obstacle.x, obstacle.y)
obstacle.x, obstacle.y = actualX, actualY
for i = 1, #collisions do
if collisions[i].other.isPlayer then
if obstacle.isObstacle then
takeHit(obstacle)
elseif obstacle.isPickup then
collectPickup(obstacle)
end
end
end
end
end
if Player.stamina ~= staminaBar.targetValue then
staminaBar:animateToValue(Player.stamina)
end
Player.animation:update(dt)
Player.healthAnimation:update(dt)
Player.frontalGlassesAnimation:update(dt)
Player.glassesAnimation:update(dt)
Banana.animation:update(dt)
if Player.isJumping then
Player.animation:pauseAtStart()
Player.glassesAnimation:pauseAtStart()
else
Player.animation:resume()
Player.glassesAnimation:resume()
end
Player.velocityY = Player.velocityY + Player.gravity * dt
if Player.velocityY > Player.maxFallVelocity then
Player.velocityY = Player.gravity
end
local targetX = Player.x + Player.velocityX * dt
local targetY = Player.y + Player.velocityY * dt
local actualX, actualY, collisions = bumpWorld:move(Player, targetX, targetY)
Player.x, Player.y = actualX, actualY
for i = 1, #collisions do
local collision = collisions[i]
if collision.other.isGround then
if not Player.hasLanded then
Player.hasLanded = true
soundManager:play("wobble", "random")
end
Player.canJump = true
Player.isJumping = false
Player.velocityY = 0
elseif collision.other.isObstacle then
takeHit(collision.other)
elseif collision.other.isPickup then
collectPickup(collision.other)
end
end
Player.score = Player.score + dt * Settings.scoreMultiplier * math.floor(Player.stamina + 1)
staminaBar:update(dt)
background:update(dt, Settings.backgroundSpeed)
if not mobile then
noiseTimer = noiseTimer + dt
end
end
function love.draw()
love.graphics.setBackgroundColor(Settings.backgroundColor)
love.graphics.setColor(255, 255, 255)
local width, height = getScreenSize(true)
-- background noise effects
if not mobile then
local noiseAlpha = util.fract(staminaBar.value) * Settings.maxNoiseAlpha
local greenNoiseScale = Player.stamina + 1
love.graphics.setColor(50, 200, 70, noiseAlpha)
noise.sample(noiseShader, noise.types.simplex3d, width, height, 0, 0, greenNoiseScale, greenNoiseScale, noiseTimer)
if Player.stamina > 1 then
local redNoiseScale = 5 / (Player.stamina + 1)
love.graphics.setColor(255, 0, 50, noiseAlpha / 5)
noise.sample(noiseShader, noise.types.simplex3d, width, height, 0, 0, redNoiseScale, redNoiseScale, noiseTimer * 2 + 10)
end
if Player.stamina > 2 then
love.graphics.setColor(157, 59, 75, noiseAlpha + 20)
noise.sample(noiseShader, noise.types.simplex3d, width, height, 5, 5, 7, 7, noiseTimer / 2 + 3.14)
end
end
love.graphics.setColor(255, 255, 255)
background:draw(Settings.scale)
love.graphics.push()
love.graphics.scale(Settings.scale)
for i = 1, #Obstacles do
local obstacle = Obstacles[i]
if obstacle then
if obstacle.animation then
obstacle.animation:draw(obstacle.sprite, obstacle.x, obstacle.y)
elseif obstacle.quad then
love.graphics.draw(Obstacles.sprite, obstacle.quad, obstacle.x, obstacle.y)
elseif obstacle.sprite then
love.graphics.draw(obstacle.sprite, obstacle.x, obstacle.y)
end
if Settings.debugDraw then
love.graphics.rectangle("line", obstacle.x, obstacle.y, obstacle.width, obstacle.height)
end
end
end
if Settings.debugDraw then
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("line", Player.x, Player.y, Player.width, Player.height)
end
love.graphics.setColor(Ground.color)
love.graphics.rectangle("fill", Ground.x, Ground.y, Ground.width, Ground.height)
love.graphics.setColor(255, 255, 255)
Player.animation:draw(Player.sprite, Player.x, Player.y)
Player.glassesAnimation:draw(Player.sprite, Player.x, Player.y)
-- UI
love.graphics.pop()
love.graphics.push()
love.graphics.scale(Settings.uiScale, Settings.uiScale)
staminaBar:draw()
for i = 0, Player.health - 1 do
local x, y = i * (Player.width + 10) + 10, 10
Player.healthAnimation:draw(Player.sprite, x, y)
Player.frontalGlassesAnimation:draw(Player.sprite, x, y)
end
love.graphics.print(math.floor(Player.stamina) .. "x", love.graphics.getWidth() - 60, 20)
love.graphics.print(math.floor(Player.score), 20, height - 70)
love.graphics.print(love.timer.getFPS() .. "FPS", love.graphics.getWidth() - 140, height - 70)
love.graphics.pop()
-- grey overlay when paused
if not isRunning then
love.graphics.setColor(128, 128, 128, 128)
love.graphics.rectangle("fill", 0, 0, width, height)
love.graphics.setColor(0, 0, 0)
love.graphics.printf("Game Over! Score: " .. math.floor(Player.score), 0, height / 2, width, "center")
end
end
function love.resize(width, height)
local previousHeight = Ground.height
Ground.width = width / Settings.scale
Ground.height = height * Settings.groundPercentage / Settings.scale
local distanceY = Ground.height - (previousHeight or Ground.height)
Ground.y = height / Settings.scale - Ground.height
if distanceY == 0 or not bumpWorld then
return
end
--Player.y = Player.y + distanceY
for index, obstacle in ipairs(Obstacles) do
obstacle.y = obstacle.y + distanceY
bumpWorld:update(obstacle, obstacle.x, obstacle.y)
end
bumpWorld:update(Player, Player.x, Player.y)
bumpWorld:remove(Ground)
bumpWorld:add(Ground, Ground.x, Ground.y, Ground.width, Ground.height)
background:regenerate(width, height * (1 - Settings.groundPercentage) / Settings.scale, true)
end
function love.keypressed(key)
if key == "space" then
if not isRunning then
resetGame()
return
end
jump()
end
if key == "escape" then
love.event.quit()
end
if key == "f" then
love.window.setFullscreen(not love.window.getFullscreen())
end
end
function love.keyreleased(key)
if key == "space" then
fallFast()
end
end
function love.mousepressed(x, y, button)
if not isRunning then
resetGame()
return
end
jump()
end
function love.mousereleased(x, y, button)
fallFast()
end