1function levelscreen_load(reason, i)
2	if reason ~= "sublevel" and reason ~= "vine" and testlevel then
3		marioworld = testlevelworld
4		mariolevel = testlevellevel
5		editormode = true
6		testlevel = false
7		startlevel(marioworld .. "-" .. mariolevel)
8		return
9	end
10
11	checkcheckpoint = false
12
13	--check if lives left
14	livesleft = false
15	for i = 1, players do
16		if mariolivecount == false or mariolives[i] > 0 then
17			livesleft = true
18		end
19	end
20
21	if reason == "sublevel" then
22		gamestate = "sublevelscreen"
23		blacktime = sublevelscreentime
24		sublevelscreen_level = i
25	elseif reason == "vine" then
26		gamestate = "sublevelscreen"
27		blacktime = sublevelscreentime
28		sublevelscreen_level = i
29	elseif livesleft then
30		gamestate = "levelscreen"
31		blacktime = levelscreentime
32		if reason == "next" then --next level
33			respawnsublevel = 0
34			checkpointx = nil
35
36			--check if next level doesn't exist
37			if not love.filesystem.exists("mappacks/" .. mappack .. "/" .. marioworld .. "-" .. mariolevel .. ".txt") then
38				gamestate = "mappackfinished"
39				blacktime = gameovertime
40				music:play("princessmusic")
41			end
42		else
43			checkcheckpoint = true
44		end
45	else
46		gamestate = "gameover"
47		blacktime = gameovertime
48		playsound(gameoversound)
49		checkpointx = nil
50	end
51
52	if editormode then
53		blacktime = 0
54	end
55
56	if reason ~= "initial" then
57		updatesizes()
58	end
59
60	if marioworld == 1 or mariolevel == 1 then
61		blacktime = blacktime * 1.5
62	end
63
64	coinframe = 1
65
66	love.graphics.setBackgroundColor(0, 0, 0)
67	levelscreentimer = 0
68
69	--reached worlds
70	local updated = false
71	if not reachedworlds[mappack] then
72		reachedworlds[mappack] = {}
73	end
74
75	if marioworld ~= "M" and not reachedworlds[mappack][marioworld] then
76		reachedworlds[mappack][marioworld] = true
77		updated = true
78	end
79
80	if updated then
81		saveconfig()
82	end
83end
84
85function levelscreen_update(dt)
86	levelscreentimer = levelscreentimer + dt
87	if levelscreentimer > blacktime then
88		if gamestate == "levelscreen" then
89			gamestate = "game"
90			if respawnsublevel ~= 0 then
91				startlevel(marioworld .. "-" .. mariolevel .. "_" .. respawnsublevel)
92			else
93				startlevel(marioworld .. "-" .. mariolevel)
94			end
95		elseif gamestate == "sublevelscreen" then
96			gamestate = "game"
97			startlevel(sublevelscreen_level)
98		else
99			menu_load()
100		end
101
102		return
103	end
104end
105
106function levelscreen_draw()
107	if levelscreentimer < blacktime - blacktimesub and levelscreentimer > blacktimesub then
108		love.graphics.setColor(255, 255, 255, 255)
109
110		if gamestate == "levelscreen" then
111			properprint("world " .. marioworld .. "-" .. mariolevel, (width/2*16)*scale-40*scale, 72*scale - (players-1)*6*scale)
112
113			for i = 1, players do
114				local x = (width/2*16)*scale-29*scale
115				local y = (97 + (i-1)*20 - (players-1)*8)*scale
116
117				for j = 1, 3 do
118					love.graphics.setColor(unpack(mariocolors[i][j]))
119					love.graphics.draw(skinpuppet[j], x, y, 0, scale, scale)
120				end
121
122				--hat
123
124				offsets = hatoffsets["idle"]
125				if #mariohats[i] > 1 or mariohats[i][1] ~= 1 then
126					local yadd = 0
127					for j = 1, #mariohats[i] do
128						love.graphics.setColor(255, 255, 255)
129						love.graphics.draw(hat[mariohats[i][j]].graphic, x-5*scale, y-2*scale, 0, scale, scale, - hat[mariohats[i][j]].x + offsets[1], - hat[mariohats[i][j]].y + offsets[2] + yadd)
130						yadd = yadd + hat[mariohats[i][j]].height
131					end
132				elseif #mariohats[i] == 1 then
133					love.graphics.setColor(mariocolors[i][1])
134					love.graphics.draw(hat[mariohats[i][1]].graphic, x-5*scale, y-2*scale, 0, scale, scale, - hat[mariohats[i][1]].x + offsets[1], - hat[mariohats[i][1]].y + offsets[2])
135				end
136
137				love.graphics.setColor(255, 255, 255, 255)
138
139				love.graphics.draw(skinpuppet[0], x, y, 0, scale, scale)
140
141				if mariolivecount == false then
142					properprint("*  inf", (width/2*16)*scale-8*scale, y+7*scale)
143				else
144					properprint("*  " .. mariolives[i], (width/2*16)*scale-8*scale, y+7*scale)
145				end
146
147				if mappack == "smb" and marioworld == 1 and mariolevel == 1 then
148					local s = "remember that you can run with "
149					for i = 1, #controls[1]["run"] do
150						s = s .. controls[1]["run"][i]
151						if i ~= #controls[1]["run"] then
152							s = s .. "-"
153						end
154					end
155					properprint(s, (width/2*16)*scale-string.len(s)*4*scale, 200*scale)
156				end
157
158				if mappack == "portal" and marioworld == 1 and mariolevel == 1 then
159					local s = "you can remove your portals with "
160					for i = 1, #controls[1]["reload"] do
161						s = s .. controls[1]["reload"][i]
162						if i ~= #controls[1]["reload"] then
163							s = s .. "-"
164						end
165					end
166					properprint(s, (width/2*16)*scale-string.len(s)*4*scale, 190*scale)
167
168					local s = "you can grab cubes and push buttons with "
169					for i = 1, #controls[1]["use"] do
170						s = s .. controls[1]["use"][i]
171						if i ~= #controls[1]["use"] then
172							s = s .. "-"
173						end
174					end
175					properprint(s, (width/2*16)*scale-string.len(s)*4*scale, 200*scale)
176				end
177			end
178
179		elseif gamestate == "mappackfinished" then
180			properprint("congratulations!", (width/2*16)*scale-64*scale, 120*scale)
181			properprint("you have finished this mappack!", (width/2*16)*scale-128*scale, 140*scale)
182		else
183			properprint("game over", (width/2*16)*scale-40*scale, 120*scale)
184		end
185
186		love.graphics.translate(0, -yoffset*scale)
187		if yoffset < 0 then
188			love.graphics.translate(0, yoffset*scale)
189		end
190
191		properprint("mario", uispace*.5 - 24*scale, 8*scale)
192		properprint(addzeros(marioscore, 6), uispace*0.5-24*scale, 16*scale)
193
194		properprint("*", uispace*1.5-8*scale, 16*scale)
195
196		love.graphics.drawq(coinanimationimage, coinanimationquads[2][coinframe], uispace*1.5-16*scale, 16*scale, 0, scale, scale)
197		properprint(addzeros(mariocoincount, 2), uispace*1.5-0*scale, 16*scale)
198
199		properprint("world", uispace*2.5 - 20*scale, 8*scale)
200		properprint(marioworld .. "-" .. mariolevel, uispace*2.5 - 12*scale, 16*scale)
201
202		properprint("time", uispace*3.5 - 16*scale, 8*scale)
203	end
204end
205