1-- GunFu Deadlands
2-- Copyright 2009-2011 Christiaan Janssen, September 2009-October 2011
3--
4-- This file is part of GunFu Deadlands.
5--
6--     GunFu Deadlands is free software: you can redistribute it and/or modify
7--     it under the terms of the GNU General Public License as published by
8--     the Free Software Foundation, either version 3 of the License, or
9--     (at your option) any later version.
10--
11--     GunFu Deadlands is distributed in the hope that it will be useful,
12--     but WITHOUT ANY WARRANTY; without even the implied warranty of
13--     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14--     GNU General Public License for more details.
15--
16--     You should have received a copy of the GNU General Public License
17--     along with GunFu Deadlands.  If not, see <http://www.gnu.org/licenses/>.
18
19
20UserLevel = {}
21
22function UserLevel.load()
23	UserLevel.enabled = false
24	UserLevel.mode = 1
25end
26
27function UserLevel.init()
28	UserLevel.enabled = true
29	Game.gamemode = 3
30	Editor.mode = 3
31	UserLevel.mode = 1
32	Editor.listingtable = Editor.getFileList()
33	Editor.currentfilename = ""
34	Editor.listingoffset = 0
35	UserLevel.block_mouse = false
36end
37
38function UserLevel.update( dt )
39	if UserLevel.mode == 1 then
40		if Editor.fileerror_timer>0 then
41			Editor.fileerror_timer = Editor.fileerror_timer - dt
42			Editor.mode = 3
43		end
44	end
45	if UserLevel.mode == 2 then
46		Game.update(dt)
47	end
48end
49
50function UserLevel.draw()
51	if UserLevel.mode == 1 then
52		Editor.showFileListing()
53	end
54
55	if UserLevel.mode == 2 then
56		Graphics.draw()
57	end
58
59	if UserLevel.mode == 3 then
60		Graphics.draw()
61
62		-- victory panel
63		love.graphics.setColor(Colors.orange)
64		love.graphics.rectangle( "fill" , 180, 182, 275, 85 )
65
66		love.graphics.setColor(Colors.lt_red)
67		love.graphics.setLine(1,"rough")
68		love.graphics.line(179,182,180+275,182)
69		love.graphics.line(180,182,180,182+85)
70		love.graphics.line(180,182+85,180+275,182+85)
71		love.graphics.line(180+275,182,180+275,182+85)
72
73		love.graphics.setColor(Colors.dark_black)
74		Graphics.drawtext("Victory!  You defeated the level\n  ESC to return to load menu", 200, 200)
75	end
76end
77
78function UserLevel.keypressed( key )
79	if key=="f3" and UserLevel.mode==1 then
80		Editor.refreshList()
81		Editor.listingtable = Editor.getFileList()
82	end
83
84	if key == "escape" then
85		if UserLevel.mode == 1 then
86			UserLevel.enabled = false
87			Game.titlescreen()
88		else
89			UserLevel.init()
90		end
91    end
92
93	-- type in filename
94	if UserLevel.mode == 1 then
95		local k = Editor.keyboard_input(key)
96		if k=="back" then
97			if string.len(Editor.currentfilename)>0 then
98				Editor.currentfilename = string.sub(Editor.currentfilename,1,-2)
99			end
100		elseif k=="return" then
101			-- confirm
102			if string.len(Editor.currentfilename)>0 then
103				UserLevel.trytoloadcurrent()
104			end
105		elseif k~="" then
106			Editor.currentfilename = Editor.currentfilename..k
107		end
108	end
109
110	-- game
111	if UserLevel.mode == 2 then
112		Game.keypressed(key)
113	end
114
115end
116
117function UserLevel.keyreleased( key )
118	-- game
119	if UserLevel.mode == 2 then
120		Game.keyreleased(key)
121	end
122end
123
124function UserLevel.mousepressed(x, y, button)
125
126	if UserLevel.mode == 1 then
127		if button == "l" then
128			if y>Editor.listingbox[2] and y<Editor.listingbox[2]+(Editor.listinglength+2)*30+25 then
129				local index = math.floor((y-Editor.listingbox[2])/30)
130
131				if index>0 then Editor.fileerror_timer = 0 end
132				-- up
133				if index==1 and Editor.listingoffset>0 then
134					Editor.listingoffset = Editor.listingoffset-1
135				end
136
137				-- down
138				if index==(Editor.listinglength+2) and Editor.listingoffset+Editor.listinglength < table.getn(Editor.listingtable) then
139					Editor.listingoffset = Editor.listingoffset+1
140				end
141
142				-- select file
143				if index>=2 and index<=Editor.listinglength+1 then
144					local newindex = index - 1 + Editor.listingoffset
145					if newindex<=table.getn(Editor.listingtable) then
146						local candidatefilename = Editor.listingtable[newindex]
147						-- double click
148						if Editor.currentfilename==candidatefilename then
149							-- load level
150							UserLevel.trytoloadcurrent()
151						else
152						-- single click
153							Editor.currentfilename = candidatefilename
154						end
155					end
156				end
157
158				-- confirm
159				if index==0 and Editor.fileerror_timer <= 0 and string.len(Editor.currentfilename)>0 then
160					UserLevel.trytoloadcurrent()
161				end
162			end
163		elseif button == "r" then
164		-- delete
165		elseif button == "wu" and Editor.listingoffset>0 then
166		-- scroll up
167			Editor.listingoffset = Editor.listingoffset-1
168		elseif button == "wd" and Editor.listingoffset+Editor.listinglength < table.getn(Editor.listingtable)then
169		-- scroll down
170			Editor.listingoffset = Editor.listingoffset+1
171		end
172	end
173
174	-- game
175	if UserLevel.mode == 2 and not UserLevel.block_mouse then
176		Game.mousepressed(x, y, button)
177	end
178end
179
180function UserLevel.mousereleased(x, y, button)
181	if UserLevel.block_mouse then
182		UserLevel.block_mouse = false
183	end
184
185	-- game
186	if UserLevel.mode == 2 then
187		Game.mousereleased(x, y, button)
188	end
189end
190
191function UserLevel.trytoloadcurrent()
192	Editor.fileerror_timer = 0
193	Editor.trytoloadcurrent()
194	if Editor.fileerror_timer <= 0 then
195		-- success
196		UserLevel.mode = 2
197		Level.currentlevel = 1
198		-- override shooting enemies option
199		Level.enemiescanshoot = true
200		Level.restart()
201		UserLevel.block_mouse = true
202	else
203		-- fail
204		UserLevel.mode = 1
205		Editor.currentfilename = ""
206	end
207end
208
209
210