-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
347 lines (305 loc) · 12.3 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
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
local composer = require("composer")
local widget = require("widget")
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------
--
--Header
--
-- -----------------------------------------------------------------------------------
---- GAMEHEADER ----
--loading spritesheets
local sheetFile = require("sheet")
--loading game functions
local gFunc = require("gameFunctions")
--Loading physics and setting the gravity to 0
local physics = require("physics")
physics.start()
physics.setGravity(0, 0)
--physics.setDrawMode( "hybrid" ) --Uncomment this line to show hitboxes
--hiding the status bar
display.setStatusBar(display.HiddenStatusBar)
--------------------
---- VARIABLES ----
gFunc.gameVarInit()
--music var
local lvl1Track = "Sounds/Lvl 1.ogg"
local lvl2Track = "Sounds/Lvl 2.ogg"
local lvl3Track = "Sounds/Lvl 3.ogg"
--sfx
levelUpSound = audio.loadSound( "Effects/levelup.ogg" )
--game specific vars
level = 1
toNextLevelScore = 25
local gameOverShadow
local gameOver
---------------------------
---- GAME FUNCTIONS ----
local function changeLevelComplete()
rRect:addEventListener("tap", gFunc.tapListener)
lRect:addEventListener("tap", gFunc.tapListener)
mRect:addEventListener("tap", gFunc.tapListener)
-- gFunc.pauseButton:addEventListener( "tap", gFunc.pauseGame )
hudScore.text = "Progress: 0%"
Runtime:addEventListener("touch", gFunc.swipeListener)
if(level == 2) then
map = display.newImage(backGroup, "Sprites/map2.png", centerX, mapMarginY)
mapClosed = display.newImage(backGroup, "Sprites/map2d.png", centerX, mapMarginY)
mapOpened = display.newImage(backGroup, "Sprites/map2do.png", centerX, mapMarginY)
doors = display.newImage(backGroup, "Sprites/doors.png", centerX, mapMarginY)
currentMusic = lvl2Track
else
map = display.newImage(backGroup, "Sprites/map3.png", centerX, mapMarginY)
mapClosed = display.newImage(backGroup, "Sprites/map3d.png", centerX, mapMarginY)
mapOpened = display.newImage(backGroup, "Sprites/map3do.png", centerX, mapMarginY)
doors = display.newImage(backGroup, "Sprites/doors.png", centerX, mapMarginY)
currentMusic = lvl3Track
end
InitialAnimation()
end
local function changeLevelAnimation()
--todo, change playerR position later
playerObj:pause()
playerObj:setSequence("walkingRight")
playerObj:play()
transition.to(playerObj, {time = 3500, x = (display.contentWidth + 20), onComplete = function() playerObj:pause() end})
-- transition.to( playerR, { delay=3250 , alpha = 1})
-- transition.to( playerM, { delay=3500 , alpha = 0})
transition.to(mapOpened, {delay = 3500, time = 2000, alpha = 0})
transition.to(map, {delay = 3500, time = 2000, alpha = 0})
--todo, working now, but needs tweaking in-game
timer.performWithDelay(5500, function() changeLevelComplete() end, 1)
--fadeout score and controlls
end
function changeLevel()
--arrows stop and existing disappear
--erase all arrows #IMPORTANT
for i = #arrowTable, 1, -1 do
display.remove(arrowTable[i])
table.remove(arrowTable, i)
end
--stop physics
physics.pause()
--stop arrow spawn
timer.cancel(gameLoopTimer)
--start music
audio.play(levelUpSound, {channel=2})
--unable event listeners
rRect:removeEventListener("tap", gFunc.tapListener)
lRect:removeEventListener("tap", gFunc.tapListener)
mRect:removeEventListener("tap", gFunc.tapListener)
Runtime:removeEventListener("touch", gFunc.swipeListener)
--map open its doors
transition.to(mapOpened, {time = 400, alpha = 1})
transition.to(mapClosed, {time = 400, alpha = 0})
transition.to(doors, {time = 400, alpha = 0})
--player walks to the middle
transition.to(shieldL, {time = 400, alpha = 0})
transition.to(shieldR, {time = 400, alpha = 0})
transition.to(shieldM, {time = 400, alpha = 0})
--player enters to the right or left and disapears
--todo, make random here
onAnim = true
playerObj:setSequence("walking")
timer.performWithDelay(750, function() playerObj:play() end, 1)
timer.performWithDelay(750, (transition.to(playerObj, {delay = 1000, time = 2000, y = (centerY + 12), onComplete = function() changeLevelAnimation() end})), 1)
--music fades out
audio.fade({channel = 1, time = 500, volume = 0})
--change arrow velocity
--change spawn rate
if(level == 2) then
levelTimeMultiplier = 8
levelStarterTime = 900 -- to 600
levelStarterVelocity = 75 -- to 100
elseif(level == 3) then
levelTimeMultiplier = 6
levelStarterTime = 700 -- to 400
levelStarterVelocity = 100 -- to 150
else
levelTimeMultiplier = 1.1
--todo add no-velocity multiplier
end
end
--gameloop function will only run after the animation
--will not run after player death
--todo, change checks from on colision to here
function gameLoop()
if(onAnim == false and dead == false) then
CreateArrows()
end
if(score >= toNextLevelScore and level == 1) then
--set to lvl 2, clear all arrows, make animations
level = 2
toNextLevelScore = 60
hudScore.text = "Victory!"
changeLevel()
score = 0
elseif(score >= toNextLevelScore and level == 2) then
--to lvl 3
level = 3
toNextLevelScore = 100
hudScore.text = "Victory!"
changeLevel()
score = 0
elseif(score == toNextLevelScore) then
--AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
--end main game
hudScore.text = "Victory! No, really. There is nothing more."
end
end
--button listeners for endGame
local function gotoMenu()
composer.gotoScene("menu", {time = 800, effect = "crossFade"})
end
function endGame()
--setting the game over score and going to the highscores page
audio.stop(1)
backgroundMusic = audio.loadStream("Sounds/Death.ogg")
audio.play(backgroundMusic, {channel = 1, loops = -1})
-- timer.performWithDelay( 1000, composer.setVariable( "finalScore", score ))
transition.to(playerObj, {time = 800, alpha = 0})
-- composer.gotoScene( "highscores", { time=800, effect="crossFade" } )
gameOverShadow = display.newText(uiGroup, "GAME OVER", display.contentCenterX, display.contentCenterY-.5, "Kenney Blocks.ttf", 80)
gameOverShadow:setFillColor(0, 0, 0, 0.9)
gameOverShadow.width = 510
gameOver = display.newText(uiGroup, "GAME OVER", display.contentCenterX, display.contentCenterY, "Kenney Blocks.ttf", 75)
gameOver:setFillColor({type = "gradient", color1 = {0.63, 0.55, 0.36}, color2 = {0.49, 0.43, 0.27}, direction = "up"})
gameOver.height = 65
gameOver.width = 490
--loading ui button
menuButton = widget.newButton(
{
x = display.contentCenterX,
y = display.contentCenterY + 130,
width = 190,
height = 50,
defaultFile = "Sprites/button.png",
overFile = "Sprites/button_pressed.png",
label = "Menu",
font = "Kenney Pixel.ttf",
fontSize = 35,
labelColor = {default = {0.49, 0.43, 0.27}, over = {0.63, 0.55, 0.36}},
labelYOffset = -4,
onEvent = gotoMenu
})
uiGroup:insert(menuButton)
--local menuButton = display.newText( sceneGroup, "Menu", display.contentCenterX, display.contentCenterY + (display.contentCenterY / 2) + 50, scoreFont )
--menuButton:setFillColor( 0.75, 0.78, 1 )
--menuButton:addEventListener( "tap", gotoMenu )
end
--------------------
-- -----------------------------------------------------------------------------------
--
-- Scene event functions
--
-- -----------------------------------------------------------------------------------
-- create()
function scene:create(event)
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
--pause physics for loading
physics.pause()
--setting groups
backGroup = display.newGroup()
sceneGroup:insert(backGroup)
itemGroup = display.newGroup()
sceneGroup:insert(itemGroup)
playerGroup = display.newGroup()
sceneGroup:insert(playerGroup)
uiGroup = display.newGroup()
sceneGroup:insert(uiGroup)
--loading the background map and setting their layers
map = display.newImage(backGroup, "Sprites/map.png", centerX, mapMarginY)
mapClosed = display.newImage(backGroup, "Sprites/mapd.png", centerX, mapMarginY)
mapOpened = display.newImage(backGroup, "Sprites/mapdo.png", centerX, mapMarginY)
doors = display.newImage(backGroup, "Sprites/doors.png", centerX, mapMarginY)
map.alpha = 1
mapClosed.alpha = 0
mapOpened.alpha = 1
doors.alpha = 0
----loading sheets
--player
local playerSheet = graphics.newImageSheet("Sprites/player sheet.png", optionsPlayer)
playerObj = display.newSprite(playerGroup, playerSheet, playerAnimation)
playerObj.x = display.contentCenterX
playerObj.y = playerMarginY
playerObj.alpha = 0
physics.addBody(playerObj, {isSensor = true, shape = playerHitbox})
playerObj.myName = "player"
--shield
local sheetShield = graphics.newImageSheet("Sprites/shield.png", optionsShield)
shieldMMarginY = playerObj.y - playerObj.contentHeight / 2
--creating shield on the right place
shieldM = display.newImage(itemGroup, sheetShield, 1, centerX, shieldMMarginY)
shieldL = display.newImage(itemGroup, sheetShield, 2, centerX - 22, playerObj.y)
shieldR = display.newImage(itemGroup, sheetShield, 2, centerX + 25, playerObj.y)
--adding their bodies
physics.addBody(shieldM, {isSensor = true, shape = shieldMHitbox})
physics.addBody(shieldL, {isSensor = true, shape = shieldLHitbox})
physics.addBody(shieldR, {isSensor = true, shape = shieldRHitbox})
--setting colision names
shieldM.myName = "shield"
shieldL.myName = "shield"
shieldR.myName = "shield"
--making everything disappear to be loaded in the animation
shieldM.alpha = 0
shieldL.alpha = 0
shieldR.alpha = 0
--music set
currentMusic = lvl1Track
createUI()
end
-- show()
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
-- Code here runs when the scene is still off screen (but is about to come on screen)
hudScore.text = "Progress: 0%"
elseif (phase == "did") then
--starting the physics again
physics.start()
--starting the collisions
Runtime:addEventListener("collision", gFunc.onCollision)
--starting the animation
gFunc.InitialAnimation()
end
end
-- hide()
function scene:hide(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
-- Code here runs when the scene is on screen (but is about to go off screen)
if(gameLoopTimer ~= nil) then
timer.cancel(gameLoopTimer)
end
elseif (phase == "did") then
-- Code here runs immediately after the scene goes entirely off screen
--removing the listeners
Runtime:removeEventListener("collision", gFunc.onCollision)
Runtime:removeEventListener("touch", gFunc.swipeListener)
rRect:removeEventListener("tap", gFunc.tapListener)
lRect:removeEventListener("tap", gFunc.tapListener)
mRect:removeEventListener("tap", gFunc.tapListener)
--pausing the physics and the music
physics.pause()
--todo, probably need to dispose of player
--removing the scene
composer.removeScene("game")
audio.dispose( levelUpSound )
end
end
-- destroy()
function scene:destroy(event)
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
-- -----------------------------------------------------------------------------------
return scene