This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Level.cs
483 lines (445 loc) · 15.3 KB
/
Level.cs
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
Copyright (C) 2010 Tom Postma
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace Frogger
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml;
public class Level
{
private const int lineDistance = 100;
private List<int> roads;
private List<int> rivirs;
private int displayWidth, displayHeight;
private bool error = false;
private string naam;
/// <summary>
/// Creating an new instance of the level class.
/// </summary>
/// <param name="lvlnaam">The name of the level (same as filename without extension)</param>
/// <param name="width">The width of the screen/level width to draw.</param>
/// <param name="height">The height of the screen/level height to draw.</param>
public Level(string lvlnaam, int width, int height)
{
this.displayWidth = width;
this.displayHeight = height;
roads = new List<int>();
rivirs = new List<int>();
this.naam = lvlnaam;
if (!error)
{
LoadDesign();
}
}
/// <summary>
/// Creating an new instance of the level class.
/// </summary>
/// <param name="width">The width of the screen/level width to draw.</param>
/// <param name="height">The height of the screen/level height to draw.</param>
public Level(int width, int height)
{
this.displayWidth = width;
this.displayHeight = height;
this.roads = new List<int>();
this.rivirs = new List<int>();
}
public string Naam
{
get
{
return this.naam;
}
set
{
this.naam = value;
}
}
public bool HasError
{
get
{
return this.error;
}
}
public int NumRoads
{
get
{
return this.roads.Count;
}
}
public int NumRivirs
{
get
{
return this.rivirs.Count;
}
}
public int RoadlineHeight
{
get
{
return 4;
}
}
/// <summary>
/// Loads the xml file with the level design.
/// </summary>
private void LoadDesign()
{
string file = Path.Combine(Program.GetLevelFolder(), this.naam + ".lvl");
if (File.Exists(file) && !String.IsNullOrEmpty(file))
{
XmlReader reader = new XmlTextReader(file);
roads.Clear();
rivirs.Clear();
while (reader.Read())
{
if (reader.Name == "road")
{
int pos = reader.ReadElementContentAsInt();
roads.Add(this.displayHeight - GetHeightRoad() * (pos + 1));
}
else if (reader.Name == "rivir")
{
int pos = reader.ReadElementContentAsInt();
rivirs.Add(this.displayHeight - GetHeightRivir(1) * (pos + 1));
}
}
}
else if (!error)
{
error = true;
// MessageBox.Show("Level " + file + " not found."); //removed can still cause a lot of messageboxes.
}
}
/// <summary>
/// Save the level design to a xml file with a the lvl extension.
/// </summary>
/// <param name="levelname">the new name</param>
/// <returns>true if succeded</returns>
public bool SaveDesign(string levelname)
{
char[] forbiddenchars = "?<>:*|\\/".ToCharArray();
for (int pos = 0; (pos < levelname.Length) && (pos <= 100); pos++)
{
for (int fc = 0; fc < forbiddenchars.Length; fc++)
{
if (levelname[pos] == forbiddenchars[fc])
{
return false; //error has forbidden character
}
}
}
string lvlsdir = Program.GetLevelFolder();
if (Directory.Exists(lvlsdir))
{
string filenamepath = Path.Combine(lvlsdir, levelname + ".lvl");
XmlWriter xmlwr = null;
try
{
xmlwr = new XmlTextWriter(filenamepath, System.Text.Encoding.UTF8);
xmlwr.WriteStartDocument();
xmlwr.WriteStartElement("level");
xmlwr.WriteString("\r\n");
int placeheight = this.displayHeight / 10;
foreach (int locy in this.roads)
{
try
{
int posroad = (10 - (locy / placeheight)) - 1;
xmlwr.WriteStartElement("road");
xmlwr.WriteValue(posroad);
xmlwr.WriteEndElement();
xmlwr.WriteString("\r\n");
}
catch (Exception)
{
return false;//error placeheight prob.
}
}
foreach (int locy in this.rivirs)
{
try
{
int posrivir = (10 - (locy / placeheight)) - 1;
xmlwr.WriteStartElement("rivir");
xmlwr.WriteValue(posrivir);
xmlwr.WriteEndElement();
xmlwr.WriteString("\r\n");
}
catch (Exception)
{
return false;//error placeheight prob.
}
}
xmlwr.WriteEndElement();
xmlwr.WriteEndDocument();
}
catch (IOException)
{
return false;//error: no write access/permission prob.
}
finally
{
xmlwr.Flush();
xmlwr.Close();
}
return true;
}
else
{
return false;//error level folder gona.
}
}
/// <summary>
/// Sets the level width and height.
/// </summary>
/// <param name="width">The new level width.</param>
/// <param name="height">The new level height.</param>
public void SetLevelSize(int width, int height, bool reloadlvlfile)
{
this.displayWidth = width;
this.displayHeight = height;
if (!this.error)
{
if (reloadlvlfile)
{
LoadDesign();
}
else
{
for (int i = 0; i < this.roads.Count; i++)
{
int rownr = this.CalcNumRow(this.roads[i]);
//this.roads[i] = rownr * (displayHeight / 10);
int newy = rownr * (displayHeight / 10);
if (!CheckIfAdded(this.roads, newy))
{
this.roads[i] = newy;
}
}
for (int i = 0; i < this.rivirs.Count; i++)
{
int rownr = this.CalcNumRow(this.rivirs[i]);
int newy = rownr * (displayHeight / 10);
if (!CheckIfAdded(this.rivirs, newy))
{
this.rivirs[i] = newy;
}
}
}
}
}
public int GetPosRivirs(int nr)
{
return this.rivirs[nr];
}
public int GetPosRoad(int nr)
{
return this.roads[nr];
}
/// <summary>
/// Adds a road to the road list
/// if the road at pos. is not already added.
/// and there is no rivir at this position, if so
/// remove the rivir first.
/// </summary>
/// <param name="y">y position to add road</param>
public void AddRoad(int y)
{
if ((y >= 0) && (y <= displayHeight))
{
if (CheckIfAdded(this.rivirs, y))
{
this.rivirs.Remove(y);
}
if (!CheckIfAdded(this.roads, y))
{
this.roads.Add(y);
}
}
}
/// <summary>
/// Adds a rivir to the rivir list
/// if the rivir at pos. is not aleady added.
/// And there is no road at the same y position, if so
/// remove the road first.
/// </summary>
/// <param name="y">y position to add rivir</param>
public void AddRivir(int y)
{
if ((y >= 0) && (y <= displayHeight))
{
if (CheckIfAdded(this.roads, y))
{
this.roads.Remove(y);
}
if (!CheckIfAdded(this.rivirs, y))
{
this.rivirs.Add(y);
}
}
}
/// <summary>
/// Remove a object(road or rivir etc)
/// </summary>
/// <param name="atposy">the top y-position the object is.</param>
/// <returns>true if succeeded</returns>
public bool RemoveObj(int atposy)
{
if (CheckIfAdded(this.roads, atposy))
{
this.roads.Remove(atposy);
return true;
}
else if (CheckIfAdded(this.rivirs, atposy))
{
this.rivirs.Remove(atposy);
return true;
}
else
{
return false;
}
}
/// <summary>
/// Calculate the number of row based on the y coordinate.
/// </summary>
/// <param name="y"></param>
/// <returns></returns>
private int CalcNumRow(int y)
{
int npos = 0;
while (y >0)
{
y = y - this.GetHeightRoad();
npos++;
if (npos >= 8) { return npos; }
}
return npos;
}
/// <summary>
/// Calculate the height of the rivir.
/// </summary>
/// <returns>the height in number of pixels</returns>
public int GetHeightRivir(int baans)
{
int hrivir = (displayHeight / 10) * baans;
return hrivir;
}
/// <summary>
/// Check if item at locy is in list
/// </summary>
/// <param name="itemlist">the list to check</param>
/// <param name="locy">to location y pos.</param>
/// <returns>true if it is in the list</returns>
private bool CheckIfAdded(List<int> itemlist, int locy)
{
foreach (int cury in itemlist)
{
if (locy == cury)
{
return true;
}
}
return false;
}
/// <summary>
/// Calculate the height of the road to draw
/// </summary>
/// <returns></returns>
public int GetHeightRoad()
{
int hroad = displayHeight / 10;
return hroad;
}
/// <summary>
/// Draw the level.
/// </summary>
/// <param name="g"></param>
public void Draw(Graphics g)
{
if (!error)
{
foreach (int rivirlocY in this.rivirs)
{
DrawRiver(g, rivirlocY, 1);
}
/* optimizing stuff, work in process
for (int i = 0; i < this.rivirs.Count; i++)
{
if (i >= 1)
{
if (rivirs[i] - (this.displayHeight / 10) == rivirs[i - 1])
{
DrawRiver(g, rivirs[i-1], 2);
i++;
}
}
else
{
DrawRiver(g, rivirs[i], 1);
}
}
*/
foreach (int roadlocY in this.roads)
{
DrawRoad(g, roadlocY);
}
}
}
/// <summary>
/// Draws a river.
/// </summary>
/// <param name="g">The graphics component that should be used</param>
/// <param name="locy">The y-coördinate the river is created at</param>
public void DrawRiver(Graphics g, int locy, int numcourses)
{
SolidBrush brushRiver = new SolidBrush(Color.Blue);
if (this.naam == "maris") { brushRiver = new SolidBrush(Color.Purple); }//easter egg
if ((numcourses < 9) && (numcourses > 0))
{
Rectangle rectRiver = new Rectangle(0, locy, displayWidth, GetHeightRivir(numcourses));
g.FillRectangle(brushRiver, rectRiver);
}
else
{
throw new Exception("number of courses not valid.");
}
}
/// <summary>
/// Draws a road.
/// </summary>
/// <param name="g">The graphics component that should be used</param>
/// <param name="locy">The y-coördinate the road is created at</param>
public void DrawRoad(Graphics g, int locy)
{
SolidBrush brushRoad = new SolidBrush(Color.Black); // the color of the road
SolidBrush brushRoadLine = new SolidBrush(Color.White); // the color of the lines on the road
Rectangle rectWeg;
rectWeg = new Rectangle(0, locy, displayWidth, GetHeightRoad());
g.FillRectangle(brushRoad, rectWeg);
int lineloc = locy + (GetHeightRoad() / 2);
for (int xpos = 0; xpos < displayWidth; xpos += lineDistance)
{
Rectangle rectRoadLine = new Rectangle(xpos, lineloc, 20, RoadlineHeight);
g.FillRectangle(brushRoadLine, rectRoadLine);
}
}
}
}