-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
218 lines (184 loc) · 6.76 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
--
-- Created by IntelliJ IDEA.
-- User: arne
-- Date: 22.02.17
-- Time: 18:35
-- To change this template use File | Settings | File Templates.
--
gamestate = require("hump.gamestate")
signal = require("hump.signal")
log = require("log")
require("persistent_storage")
require("common")
require("scaling")
require("background_music")
require("spacy3_love_extensions")
local MAXIMUM_HIGHSCORE_COUNT = 5
local function highscore_dialog_finished(result, entered_text, score)
local highscores = persistent_storage.get("highscores", {})
-- pop the "killed" music
background_music.pop()
-- insert new table with name and score in highscores table
table.insert(highscores, { entered_text, score })
table.sort(highscores,
function(hs_a, hs_b)
return hs_a[2] > hs_b[2]
end)
while (#highscores > MAXIMUM_HIGHSCORE_COUNT) do
highscores[#highscores] = nil
end
-- store highscore data
local success = persistent_storage.set("highscores", highscores) and
persistent_storage.set("lowest_highscore", highscores[#highscores])
if not success then
print("could not store data")
end
-- pop highscore entering dialog
gamestate.pop()
end
function player_wants_to_quit(_)
local quit_confirmation = dofile("menu.lua")
gamestate.push(quit_confirmation)
quit_confirmation:add_button("really quit")
quit_confirmation:add_button("abort quitting")
quit_confirmation:set_title("confirm quitting")
quit_confirmation.on_button_clicked = function(button_txt)
if button_txt == "really quit" then
love.event.push("quit")
elseif button_txt == "abort quitting" then
gamestate.pop()
end
end
quit_confirmation.on_escape_pressed = function()
gamestate.pop()
end
end
function player_wants_back_to_main(score)
local quit_confirmation = dofile("menu.lua")
gamestate.push(quit_confirmation)
quit_confirmation:add_button("really back to main")
quit_confirmation:add_button("abort going to main")
quit_confirmation:set_title("confirm back to main menu")
quit_confirmation.on_button_clicked = function(button_txt)
if button_txt == "really back to main" then
gamestate.pop() -- pop warning
gamestate.pop() -- pop game
elseif button_txt == "abort going to main" then
gamestate.pop()
end
end
quit_confirmation.on_escape_pressed = function()
gamestate.pop()
end
end
function player_died(score)
local quit_confirmation = dofile("menu.lua")
--pop game now, will remove music from stack
gamestate.pop()
gamestate.push(quit_confirmation)
quit_confirmation:add_button("back to main")
-- push fallback music for returning to main menu
background_music.push("main_menu")
-- play u dead music
background_music.push("killscreen")
local hs_entry_ok = #persistent_storage.get("highscores", {}) < MAXIMUM_HIGHSCORE_COUNT or score > persistent_storage.get("lowest_highscore", { "", 0 })[2]
if hs_entry_ok then
quit_confirmation:add_button("enter highscore")
end
quit_confirmation:add_button("quit")
quit_confirmation:set_title("you made " .. score .. " points.\nyou also horrifically failed your colony.")
quit_confirmation.on_button_clicked = function(button_txt)
if button_txt == "back to main" then
gamestate.pop() -- pop warning
background_music.pop() -- pop u dead music
elseif button_txt == "enter highscore" then
gamestate.pop() -- pop warning
-- show highscore entry thingy
local highscore_entry = dofile("highscore_entry.lua")
gamestate.push(highscore_entry)
highscore_entry:set_title("enter your score of " .. score .. " points")
highscore_entry:set_score(score)
highscore_entry:set_callback(highscore_dialog_finished)
elseif button_txt == "quit" then
love.event.push("quit")
end
end
quit_confirmation.on_escape_pressed = function()
gamestate.pop() -- pop warning
background_music.pop()
end
end
local function push_game_mode_menu()
--- push main menu onto stack
local mode_menu = dofile("menu.lua")
gamestate.push(mode_menu)
--- configure main menu
mode_menu:set_title("select game mode")
mode_menu:add_button("endless game")
--mode_menu:add_button("asteroid rush")
mode_menu:add_button("back")
--- set main menu callback
mode_menu.on_button_clicked = function(button_text)
if (button_text == "back") then
gamestate.pop()
else
gamestate.pop() -- pop mode menu
scaling.gamemode = button_text
-- push game on gamestate stack with desired gamemode==button text
gamestate.push(dofile("game_gamestate.lua"), button_text)
end
end
end
local function push_main_menu()
--- push main menu onto stack
local main_menu = dofile("menu.lua")
gamestate.push(main_menu)
--- configure main menu
main_menu:add_button("new game")
main_menu:add_button("view highscores")
main_menu:add_button("settings")
main_menu:add_button("quit")
main_menu:set_title("spacy3")
--- set main menu callback
main_menu.on_button_clicked = function(button_text)
if (button_text == "new game") then
push_game_mode_menu()
elseif (button_text == "view highscores") then
gamestate.push(dofile("highscore_view.lua"))
elseif button_text == "settings" then
gamestate.push(dofile("settings.lua"))
elseif button_text == "quit" then
love.event.quit()
end
end
end
function love.load(arg)
--- enable zerobrane ide debugging
if arg[#arg] == "-debug" then require("mobdebug").start() end
for _, argument in ipairs(arg) do
if argument == "-debug" then
require("mobdebug").start()
elseif argument == "--disable-log-colors" then
log.usecolor = false
end
end
--- unshittify (technical term) random numbers
math.randomseed(os.time())
--- load gamepad mapping and hope it is useful
love.joystick.loadGamepadMappings("custom_gamecontroller_db.txt")
--- apply settings (or default)
local settings = require("settings")
settings:init()
settings:graphics_mode_changed()
settings:audio_mode_changed()
-- hump.gamestate will call this even after registerEvents
love.update = background_music.update
--- register event callbacks
gamestate.registerEvents()
-- create main menu and push onto stack
push_main_menu()
-- make background music
background_music.push("main_menu")
local splash_screen = dofile("splashscreen.lua")
gamestate.push(splash_screen)
end