-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
273 lines (228 loc) · 9.32 KB
/
game.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
require('global')
local Class = require('modules/middleclass/middleclass')
local Stateful = require('modules/stateful/stateful')
local Timer = require('modules/hump/timer')
local Anim8 = require('modules/anim8/anim8')
local Vector = require('modules/hump/vector')
local Boost = require('boost')
local Player = require('player')
local Background = require('background')
local Foreground = require('foreground')
local Camera = require('Camera')
Particles = require('particles')
Song = {
melody = love.audio.newSource('res/sound/music_melody.mp3'),
backing = love.audio.newSource('res/sound/music_backing.mp3'),
space = love.audio.newSource('res/sound/music_space.mp3'),
title = love.audio.newSource('res/sound/music_title.mp3'),
ending = love.audio.newSource('res/sound/music_end.mp3')
}
SFX = {
sweep = love.audio.newSource('res/sound/sweep.mp3'),
bat = love.audio.newSource('res/sound/bat.wav'),
crashLanding = love.audio.newSource('res/sound/crash_landing.wav'),
flap = love.audio.newSource('res/sound/flap.wav')
}
local sprites = {
endingMoon = love.graphics.newImage('res/images/ending_moon.png'),
endingFly = love.graphics.newImage('res/images/ending_fly.png'),
endingBird = love.graphics.newImage('res/images/ending_bird.png'),
endingDebris = love.graphics.newImage('res/images/ending_debris.png'),
endingBubble = love.graphics.newImage('res/images/ending_bubble.png'),
endingSpace = love.graphics.newImage('res/images/ending_space.png')
}
--============================================================================== GAME
local Game = Class('Game')
Game:include(Stateful)
Game.Title = Game:addState('Title')
Game.Play = Game:addState('Play')
Game.End = Game:addState('End')
Game.Cutscene = Game:addState('Cutscene')
function Game:startup()
self.player = Player:new(0, 0)
self.camera = Camera.new(0, 0, Screen.targetW, Screen.targetH)
self.camTarget = Vector(0, 0)
local scale = 1.0
for i = 1, 2 do
scale = scale - 0.1
self.camera:addLayer(i .. '', scale)
end
self.foreground = Foreground:new(self.player, self.camera)
self.background = Background:new(self.player, self.foreground, self.camera)
end
function Game:initialize()
Particles.initialize()
self:startup()
self:gotoState('Title')
end
function Game:update(dt)
self.background:update(dt)
self.player:update(dt)
self.camera:zoomTo(math.max(self.player.pos.y / WORLD.spaceHeight + 1, 0.52118472042539))
self.camera:update(dt)
self.foreground:update(dt)
local d = self.camTarget - Vector(self.camera.x, self.camera.y)
self.camera:move(d.x / 4, d.y / 4)
end
function Game:draw()
self.camera:push()
self.background:draw()
self.player:draw()
self.foreground:draw()
self.camera:pop()
end
--============================================================================== GAME.TITLE
local title, titleToPitcher, pitching, pitchToBatter, pitcherToPlay = 0, 1, 2, 3
function Game.Title:enteredState()
local grid = Anim8.newGrid(Player.SIZE, Player.SIZE, Player.SIZE * 6, Player.SIZE)
self.gameLogo = love.graphics.newImage("res/images/logo.png")
local gameTitleScreenOffset = Screen.targetH * 1.5
self.gameLogoHeight = -gameTitleScreenOffset + 40
self.camTarget = Vector(0, -gameTitleScreenOffset + Screen.targetH / 2)
self.camera:moveTo(self.camTarget.x, self.camTarget.y)
self.titleTimer = 0
self.cameraTimer = nil
self.cameraMoveState = title
Song.title:setLooping(true)
Song.title:play()
Song.melody:stop()
Song.backing:stop()
Song.space:stop()
Song.ending:stop()
end
function Game.Title:update(dt)
Game.update(self, dt)
self.titleTimer = self.titleTimer + dt
if self.cameraMoveState == title and Input.pressed('return') then
Song.title:stop()
self.cameraMoveState = titleToPitcher
self.cameraTimer = Timer.new()
self.camTarget = Vector(0, 0)
self.cameraTimer.after(1, function(func)
self.foreground.ground:startPitcher()
self.cameraTimer.after(0.5, function()
self.cameraMoveState = pitching
end)
end)
elseif self.cameraMoveState == pitching then
self.cameraMoveState = pitchToBatter
self.player.vel = Vector(-20, 0)
self.player.intro = false
self.cameraTimer.after(0.3, function()
self.foreground.ground:startBatter()
end)
self.cameraTimer.after(0.5, function()
self.player.vel = Vector(40, -40)
end)
elseif self.cameraMoveState == pitchToBatter then
self.camTarget = Vector(self.player.pos.x, self.player.pos.y)
local _, y = self.camera:toScreenCoordinates(0, INTRO.groundHeight)
if (y >= Screen.targetH) then
self.cameraMoveState = pitcherToPlay
end
elseif self.cameraMoveState == pitcherToPlay then
SFX.bat:play()
self:gotoState('Play')
end
if self.cameraTimer then
self.cameraTimer.update(dt)
end
end
function Game.Title:draw()
Game.draw(self)
self.camera:push()
if (self.player.intro) then
love.graphics.draw(self.gameLogo, -self.gameLogo:getWidth() / 2, self.gameLogoHeight + 4 * math.sin(self.titleTimer * 4))
love.graphics.setFont(FONT.redalert)
love.graphics.setColor(63, 63, 116)
love.graphics.printf("As a bird, press UP to flag your wings", -Screen.targetW / 2, self.gameLogoHeight + self.gameLogo:getHeight() + 30, Screen.targetW, 'center')
love.graphics.printf("Collect feathers to transform!", -Screen.targetW / 2, self.gameLogoHeight + self.gameLogo:getHeight() + 43, Screen.targetW, 'center')
love.graphics.printf("As a ball, use UP and DOWN to control your angle", -Screen.targetW / 2, self.gameLogoHeight + self.gameLogo:getHeight() + 56, Screen.targetW, 'center')
love.graphics.printf("Press ENTER to START!", -Screen.targetW / 2, self.gameLogoHeight + self.gameLogo:getHeight() + 92, Screen.targetW, 'center')
love.graphics.setColor(255, 255, 255)
end
self.camera:pop()
end
--============================================================================== GAME.PLAY
function Game.Play:enteredState()
Debug('GAME.PLAY', 'Play enteredState.')
self.player.userCanTurn = true
self.player.vel = Vector(20,-20)
self.player:boost()
Song.melody:setLooping(true)
Song.melody:play()
Song.backing:setLooping(true)
Song.backing:play()
Song.backing:setVolume(0)
Song.space:setLooping(true)
Song.space:play()
Song.space:setVolume(0)
end
function Game.Play:update(dt)
Game.update(self, dt)
self.camTarget = Vector(self.player.pos.x, self.player.pos.y)
if (self.player.state == Player.STATE.DEAD) then
self:gotoState('End')
end
end
function Game.Play:draw()
Game.draw(self)
end
--============================================================================== GAME.END
function Game.End:enteredState()
Debug('GAME.END', 'End enteredState.')
self.endTimer = Timer.new()
self.endTimer.after(4, function()
self:gotoState('Cutscene')
end)
end
function Game.End:update(dt)
Game.update(self, dt)
self.endTimer.update(dt)
end
function Game.End:draw()
Game.draw(self)
end
--============================================================================== GAME.CUTSCENE
function Game.Cutscene:enteredState()
Debug('GAME.CUTSCENE', 'Cutscene enteredState.')
self.cutsceneTimer = 0
local grid = Anim8.newGrid(420, 280, 420 * 4, 280)
self.flyAnimation = Anim8.newAnimation(grid:getFrames('1-4', 1), 0.3)
end
function Game.Cutscene:update(dt)
self.cutsceneTimer = self.cutsceneTimer + dt
self.flyAnimation:update(dt)
end
function Game.Cutscene:draw()
if self.cutsceneTimer > 12 then
love.graphics.draw(sprites.endingSpace)
self.flyAnimation:draw(sprites.endingFly)
love.graphics.setFont(FONT.redalert)
love.graphics.setColor(63, 63, 116)
love.graphics.print("Made by @dvdfu, Hamdan Javeed and Seikun Kambashi", 80, Screen.targetH / 2 + 60)
love.graphics.setColor(255, 255, 255)
love.graphics.print("Thank you for playing!", 80, Screen.targetH / 2 + 73)
elseif self.cutsceneTimer > 8 then
local a = self.cutsceneTimer - 8
love.graphics.draw(sprites.endingBird)
love.graphics.draw(sprites.endingDebris, Screen.targetW / 2, Screen.targetH / 2, 0, 1 + a / 32, 1 + a / 32, Screen.targetW / 2, Screen.targetH / 2)
elseif self.cutsceneTimer > 4 then
local a = (self.cutsceneTimer - 4) / 4
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle('fill', 0, 0, Screen.targetW, Screen.targetH)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.draw(sprites.endingMoon, a * 6 * (math.random() - 0.5), a * 6 * (math.random() - 0.5))
love.graphics.setBlendMode('add')
love.graphics.setColor(200 * a, 230 * a, 255 * a, 255)
love.graphics.rectangle('fill', 0, 0, Screen.targetW, Screen.targetH)
love.graphics.setColor(255, 255, 255, 255)
love.graphics.setBlendMode('alpha')
else
love.graphics.draw(sprites.endingMoon)
if math.floor(self.cutsceneTimer * 4) % 4 > 0 then
love.graphics.draw(sprites.endingBubble)
end
end
end
return Game