-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
415 lines (342 loc) · 18.9 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
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
------------------------------------------------------------------------------
--
-- This file is part of OpenBilgi, a roguelike trivia game repository
--
-- For overview and more information on licensing please refer to README.md
--
-- Home page: https://github.com/sekodev/OpenBilgi
--
-- Contact: [email protected]
--
------------------------------------------------------------------------------
-- Set properties to hide OS related UI elements
display.setStatusBar( display.HiddenStatusBar )
native.setProperty( "androidSystemUiVisibility", "immersiveSticky" )
native.setProperty( "prefersHomeIndicatorAutoHidden", true )
composer = require ( "libs.composer_alt" ) -- Modified version is used for tossLeft effect
preference = require ( "libs.preference" ) -- Used to save player-related variables
themeSettings = require ( "libs.themeSettings" ) -- Contains theme data
utils = require ( "libs.utils" ) -- Contains universally usable code
commonMethods = require ( "libs.commonMethods" ) -- Contains project specific functions used in different occassions
contentWidthSafe = display.safeActualContentWidth
contentHeightSafe = display.safeActualContentHeight
contentWidth = display.contentWidth
contentHeight = display.contentHeight
math.randomseed( os.time() )
local function assignVariables()
-- userX variables are inactive in current builds.
-- Those variables were used for online functionalities that existed before.
-- Stays in code to keep preference/save file intact.
composer.setVariable( "userID" , "" )
composer.setVariable( "userName" , "" )
composer.setVariable( "userToken", "" )
composer.setVariable( "emailSupport", "[email protected]" ) -- Used to show player a way to get in contact
composer.setVariable( "currentVersion" , "OpenBilgi, v0.9.3 (77)" ) -- Visible in Logo screen
composer.setVariable( "idAppStore" , "123456789" ) -- Required to show rating pop-ups
composer.setVariable( "urlLandingPage" , "https://sekodev.github.io/bilgiWeb/" ) -- Required for sharing landing page on social media
composer.setVariable( "pathIconFile" , "assets/socialMedia/iconQuiz.png" ) -- Required for share UI
composer.setVariable( "currentTheme" , "dark") -- dark/light
composer.setVariable( "currentLanguage" , "tr") -- Default: Turkish
composer.setVariable( "languageSelected" , "") -- Default: ""
composer.setVariable( "fullScreen" , true) -- Full screen support - Default: true
composer.setVariable( "currentAppScene" , "menuScreen") -- Required for back button on Android
composer.setVariable( "isMotionReduced", false )
-- Activate "reduce motion" based on device settings on iOS, tvOS and macOS
if (system.getInfo( "platform" ) == "ios" or
system.getInfo( "platform" ) == "tvos" or
system.getInfo( "platform" ) == "macos") then
if (system.getInfo( "reduceMotion" )) then
composer.setVariable( "isMotionReduced", true )
end
end
composer.setVariable( "sceneTransitionTime", 250 ) -- Used in every scene change
composer.setVariable( "sceneTransitionEffect", "tossLeft" ) -- Used in every scene change
composer.setVariable( "sceneTransitionEffectDefault", "tossLeft" ) -- Not saved but used as variable in settings
composer.setVariable( "sceneTransitionEffectReduceMotion", "crossFade" ) -- Not saved but used as variable in settings
composer.setVariable( "fontIngame", "Arial" )
composer.setVariable( "fontLogo", "Russo_One.ttf" )
composer.setVariable( "askedRateGame", false ) -- Used to show rate game popup
composer.setVariable( "askedConsent", false ) -- Inactive
composer.setVariable( "analyticsConsent", false ) -- Inactive
composer.setVariable( "soundLevel", 0.5 )
composer.setVariable( "musicLevel", 0.5 )
composer.setVariable( "timeSuspend", 0 ) -- Used to track plsyer away time to prevent searching for answer
composer.setVariable( "lastRandomSeed", 0 )
composer.setVariable( "lastQuestionSet", 1 ) -- fail-safe value is 1
-- Save and quit variables
composer.setVariable( "savedRandomSeed", 0 )
composer.setVariable( "savedQuestionSet", 1 )
composer.setVariable( "savedQuestionsAsked", { } )
composer.setVariable( "savedQuestionCurrent", 1 )
composer.setVariable( "savedPlayerScore", 0 )
composer.setVariable( "savedIsRevived", false )
-- Number of questions sets available
-- New sets should be added to lockedQuestionSets
composer.setVariable( "lockedQuestionSets", { 1, 2, 3, 4 } )
composer.setVariable( "availableQuestionSets", { 1 } )
composer.setVariable( "completedQuestionSets", { } )
-- Starting pack for player
composer.setVariable( "locksAvailable", 3 )
composer.setVariable( "coinsAvailable", 250 )
composer.setVariable( "coinsCompletedSet", 250 )
composer.setVariable( "priceLockCoins", 1000 )
-- Amount of questions can be changed here
-- Single set is number of questions between two campfires
composer.setVariable( "amountQuestionSingleGame", 20 )
composer.setVariable( "amountQuestionSingleSet", 5 )
composer.setVariable( "lockInfoAvailable", true ) -- "Do not show again" flag for lock usage information
composer.setVariable( "percentageRevival", 2 ) -- default - %2
composer.setVariable( "sharedGame", false ) -- Used to increase percentageRevival
composer.setVariable( "isQuestionResetNeeded", true ) -- Used to fix a consistency between versions
composer.setVariable( "isTermsPrivacyAccepted", false ) -- Privacy policy & terms of use information flag
composer.setVariable( "isLanguageOptionShown", false ) -- Language option notification flag
-- Statistics variables
composer.setVariable( "scoreHigh", 0 )
composer.setVariable( "gamesPlayed", 0 )
composer.setVariable( "questionsAnsweredTotal", 0 )
composer.setVariable( "runsCompleted", 0 )
composer.setVariable( "locksUsed", 0 )
composer.setVariable( "coinsTotal", 0 )
end
-- Loads previously saved settings and variables
local function loadPreferences()
if ( preference.getValue("settings") ) then
composer.setVariable( "userToken", preference.getValue("settings")[1] )
composer.setVariable( "askedConsent", preference.getValue("settings")[2] )
composer.setVariable( "analyticsConsent", preference.getValue("settings")[3] )
composer.setVariable( "soundLevel", preference.getValue("settings")[4] )
composer.setVariable( "musicLevel", preference.getValue("settings")[5] )
composer.setVariable( "timeSuspend", preference.getValue("settings")[6] )
composer.setVariable( "scoreHigh", preference.getValue("settings")[7] )
composer.setVariable( "gamesPlayed", preference.getValue("settings")[8] )
composer.setVariable( "questionsAnsweredTotal", preference.getValue("settings")[9] )
composer.setVariable( "askedRateGame", preference.getValue("settings")[10] )
composer.setVariable( "currentTheme", preference.getValue("settings")[11] )
composer.setVariable( "percentageRevival", preference.getValue("settings")[12] )
composer.setVariable( "sharedGame", preference.getValue("settings")[13] )
composer.setVariable( "userID", preference.getValue("settings")[14] )
composer.setVariable( "userName", preference.getValue("settings")[15] )
composer.setVariable( "runsCompleted", preference.getValue("settings")[16] )
composer.setVariable( "lastRandomSeed", preference.getValue("settings")[17] )
composer.setVariable( "availableQuestionSets", preference.getValue("settings")[18] )
composer.setVariable( "lockedQuestionSets", preference.getValue("settings")[19] )
composer.setVariable( "locksAvailable", preference.getValue("settings")[20] )
composer.setVariable( "lockInfoAvailable", preference.getValue("settings")[21] )
composer.setVariable( "coinsAvailable", preference.getValue("settings")[22] )
composer.setVariable( "isQuestionResetNeeded", preference.getValue("settings")[23] )
composer.setVariable( "lastQuestionSet", preference.getValue("settings")[24] )
composer.setVariable( "completedQuestionSets", preference.getValue("settings")[25] )
composer.setVariable( "locksUsed", preference.getValue("settings")[26] )
composer.setVariable( "coinsTotal", preference.getValue("settings")[27] )
composer.setVariable( "savedRandomSeed", preference.getValue("settings")[28] )
composer.setVariable( "savedQuestionSet", preference.getValue("settings")[29] )
composer.setVariable( "savedQuestionsAsked", preference.getValue("settings")[30] )
composer.setVariable( "savedQuestionCurrent", preference.getValue("settings")[31] )
composer.setVariable( "savedPlayerScore", preference.getValue("settings")[32] )
composer.setVariable( "savedIsRevived", preference.getValue("settings")[33] )
composer.setVariable( "isTermsPrivacyAccepted", preference.getValue("settings")[34] )
composer.setVariable( "fullScreen", preference.getValue("settings")[35] )
composer.setVariable( "isLanguageOptionShown", preference.getValue("settings")[36] )
composer.setVariable( "languageSelected", preference.getValue("settings")[37] )
composer.setVariable( "isMotionReduced", preference.getValue("settings")[38] )
end
end
-- Saves settings and variables
function savePreferences()
preference.save{settings = {
composer.getVariable( "userToken" ),
composer.getVariable( "askedConsent" ),
composer.getVariable( "analyticsConsent" ),
composer.getVariable( "soundLevel" ),
composer.getVariable( "musicLevel" ),
composer.getVariable( "timeSuspend" ),
composer.getVariable( "scoreHigh" ),
composer.getVariable( "gamesPlayed" ),
composer.getVariable( "questionsAnsweredTotal" ),
composer.getVariable( "askedRateGame" ),
composer.getVariable( "currentTheme" ),
composer.getVariable( "percentageRevival" ),
composer.getVariable( "sharedGame" ),
composer.getVariable( "userID" ),
composer.getVariable( "userName" ),
composer.getVariable( "runsCompleted" ),
composer.getVariable( "lastRandomSeed" ),
composer.getVariable( "availableQuestionSets" ),
composer.getVariable( "lockedQuestionSets" ),
composer.getVariable( "locksAvailable" ),
composer.getVariable( "lockInfoAvailable" ),
composer.getVariable( "coinsAvailable" ),
composer.getVariable( "isQuestionResetNeeded" ),
composer.getVariable( "lastQuestionSet" ),
composer.getVariable( "completedQuestionSets" ),
composer.getVariable( "locksUsed" ),
composer.getVariable( "coinsTotal" ),
composer.getVariable( "savedRandomSeed" ),
composer.getVariable( "savedQuestionSet" ),
composer.getVariable( "savedQuestionsAsked" ),
composer.getVariable( "savedQuestionCurrent" ),
composer.getVariable( "savedPlayerScore" ),
composer.getVariable( "savedIsRevived" ),
composer.getVariable( "isTermsPrivacyAccepted" ),
composer.getVariable( "fullScreen" ),
composer.getVariable( "isLanguageOptionShown" ),
composer.getVariable( "languageSelected" ),
composer.getVariable( "isMotionReduced" ), } }
end
-- Reset preferences file
-- Used in previous versions but not used in later versions to avoid unintended user errors
local function resetPreferences()
assignVariables()
savePreferences()
end
-- Resets question sets
-- Used in settings screen
local function resetQuestionSets()
local lockedQuestionSets = { 1, 2, 3, 4 }
local availableQuestionSets = { 1 }
local completedQuestionSets = { }
table.remove( lockedQuestionSets, 1 )
composer.setVariable( "availableQuestionSets", availableQuestionSets )
composer.setVariable( "completedQuestionSets", completedQuestionSets )
composer.setVariable( "lockedQuestionSets", lockedQuestionSets )
composer.setVariable( "lastQuestionSet", 1 )
end
-- Picks the starting set
-- Starting set was random in earlier versions but thought it would be better to fix it to 1 for better UX
-- Fine tuning the first question set after making it a fixed value is important
local function pickStartingQuestionSet()
local lockedQuestionSets = composer.getVariable("lockedQuestionSets")
local availableQuestionSets = composer.getVariable("availableQuestionSets")
local isQuestionResetNeeded = composer.getVariable("isQuestionResetNeeded")
-- trigger reset for previous build versions
if (isQuestionResetNeeded) then
composer.setVariable( "isQuestionResetNeeded", false )
resetQuestionSets()
else
if (lockedQuestionSets ~= nil) then
-- defensive. reset questions if available question sets and locked question sets is somehow not filled
if (#lockedQuestionSets <= 0 and availableQuestionSets) then
if (#availableQuestionSets <= 0) then
resetQuestionSets()
end
end
else
-- reset in case the locked question sets are somehow not created
resetQuestionSets()
end
end
end
-- Safety net for bugs introduced in different versions and game reset
local function resetFaultyVariables()
-- reset fault revival percentage
if (composer.getVariable( "percentageRevival" ) > 10) then
composer.setVariable( "percentageRevival", 2 ) -- default - %2
end
-- reset random seed on cold start
composer.setVariable( "lastRandomSeed", 0 )
-- set starting question set to 1
local amountAvailableQuestionSets = #composer.getVariable( "availableQuestionSets" )
if ( amountAvailableQuestionSets <= 1 ) then
pickStartingQuestionSet()
end
savePreferences()
end
-- Resets question sets, locks previously unlocked sets
-- Triggered from settings screen
function resetQuestions()
-- preferences not reset to preserve earned currency and statistics
--resetPreferences()
resetQuestionSets()
resetFaultyVariables()
end
-- Adjust screen dimensions depending on full screen option
function adjustScreenDimensions(fullScreen)
if (fullScreen == false) then
contentWidth = contentWidthSafe
contentHeight = contentHeightSafe
else
contentWidth = display.contentWidth
contentHeight = display.contentHeight
end
end
-- Set transition effect based on "Reduce motion" option
local function setSceneTransitionEffect()
local isMotionReduced = composer.getVariable( "isMotionReduced" )
local sceneTransitionEffect = composer.getVariable( "sceneTransitionEffect" )
if (isMotionReduced) then
sceneTransitionEffect = composer.getVariable( "sceneTransitionEffectReduceMotion" )
end
composer.setVariable( "sceneTransitionEffect", sceneTransitionEffect )
return sceneTransitionEffect
end
assignVariables()
-- Load preferences file and initialize variables
loadPreferences()
-- Adjust screen dimensions depending on full screen option
adjustScreenDimensions(composer.getVariable( "fullScreen" ))
local sceneTransitionTime = composer.getVariable( "sceneTransitionTime" )
local sceneTransitionEffect = setSceneTransitionEffect()
-- Import sozluk library for localization
-- https://github.com/sekodev/sozluk
sozluk = require ( "libs.sozluk" )
sozluk.setTranslationsPath( "libs.translations" )
sozluk.setTranslationFolder( "translations" )
sozluk.init()
-- Code block used for language selection
local languageSelected = composer.getVariable( "languageSelected" )
if (languageSelected == "") then
-- If language is not picked by the player before, get device language
local deviceLanguage
if (system.getInfo("targetAppStore") == "google" or system.getInfo("targetAppStore") == "amazon" or system.getInfo("targetAppStore") == "none") then
deviceLanguage = system.getPreference( "locale", "language" )
elseif (system.getInfo("targetAppStore") == "apple") then
deviceLanguage = string.sub( system.getPreference( "ui", "language" ), 1, 2 )
end
if ( deviceLanguage == "tr" or deviceLanguage == "TR" or "Türkçe" == deviceLanguage or deviceLanguage == "Turkish" or "Turkce" == deviceLanguage ) then
composer.setVariable( "currentLanguage", "tr" )
else
composer.setVariable( "currentLanguage", "en" )
end
else
composer.setVariable( "currentLanguage", languageSelected )
end
sozluk.setSelectedTranslation( composer.getVariable("currentLanguage") )
-- Pick starting set and handle bugs caused by previous versions
pickStartingQuestionSet()
resetFaultyVariables()
-- Assign selected theme data
local currentTheme = composer.getVariable( "currentTheme" )
themeData = themeSettings.getData(currentTheme)
-- Start playing theme music
audio.setVolume( 1 )
streamMusicBackground = audio.loadStream("assets/music/menuTheme.mp3")
channelMusicBackground = audio.play(streamMusicBackground, { channel = 1, loops = -1})
audio.setVolume( composer.getVariable("musicLevel"), {channel = channelMusicBackground} )
for i = 2, audio.totalChannels do
audio.setVolume( composer.getVariable("soundLevel"), {channel = i} )
end
-- Back button behavior for Android
local function onKeyEvent( event )
if ( event.keyName == "back" and event.phase == "up" ) then
local currentAppScene = composer.getVariable( "currentAppScene" )
if ( currentAppScene == "menuScreen" ) then
native.requestExit()
elseif ( currentAppScene == "gameScreen" ) then
composer.setVariable( "currentAppScene", "menuScreen" )
audio.fade( {channel = channelMusicBackground, time = sceneTransitionTime, volume = 0} )
local optionsChangeScene = {effect = sceneTransitionEffect, time = sceneTransitionTime, params = {callSource = currentAppScene}}
composer.gotoScene( "screens.menuScreen", optionsChangeScene )
else
composer.setVariable( "currentAppScene", "menuScreen" )
--audio.fade( {channel = channelMusicBackground, time = sceneTransitionTime, volume = 0} )
local optionsChangeScene = {effect = sceneTransitionEffect, time = sceneTransitionTime, params = {callSource = currentAppScene}}
composer.gotoScene( "screens.menuScreen", optionsChangeScene )
end
return true
end
end
Runtime:addEventListener( "key", onKeyEvent )
-- After everything is done, switch to logo screen to start the game
local optionsChangeScene = {effect = sceneTransitionEffect, time = sceneTransitionTime,
params = {callSource = "main"}}
composer.gotoScene( "screens.logoScreen", optionsChangeScene )