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
| using System;
using GXPEngine;
using System.Drawing;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Linq;
namespace GXPEngine
{
public class Level : GameObject
{
bool playerHasKey1 = false;
bool playerHasKey2 = false;
Shadow shadow = new Shadow ();
MyGame _game;
Player _player;
const int WIDTH = 35;
const int HEIGHT = 10;
const int TILESIZE = 64;
StreamReader streamReader;
int[,] _data = new int[HEIGHT, WIDTH];
Sprite _sky;
Player player;
Shining _shining;
Coin _coin;
Wall _wall;
Box _box;
Door1 _door1;
Door2 _door2;
Key1 _key1;
Key2 _key2;
Ground _grass;
Fog _fog;
List<Wall> wallList;
Scoreboard _scoreboard;
Healthdisplay _healthdisplay;
public Level (MyGame myGame) : base ()
{
//get content of a textfile
if (myGame._state == "level1") {
streamReader = new StreamReader ("level.txt");
} else if (myGame._state == "level2") {
streamReader = new StreamReader ("level2.txt");
}
string fileData = streamReader.ReadToEnd ();
streamReader.Close ();
//solit data at 'newline' character = divide in lines
string[] lines = fileData.Split ('\n');
for (int j = 0; j < HEIGHT; j++) {
string[] cols = lines [j].Split (',');
for (int k = 0; k < WIDTH; k++) {
string col = cols [k];
_data [j, k] = int.Parse (col);
}
}
for (int j = 0; j < HEIGHT; j++) {
for (int k = 0; k < WIDTH; k++) {
int tile = _data [j, k];
if (tile != 0)
addGameObject (k * TILESIZE, j * TILESIZE, tile);
}
}
player = new Player (shadow);
AddChild (player);
player.SetXY (370, 498);
_player = player;
_scoreboard = new Scoreboard ();
AddChild (_scoreboard);
_healthdisplay = new Healthdisplay ();
AddChild (_healthdisplay);
_fog = new Fog ();
AddChild (_fog);
//store handle to myGame
_game = myGame;
//add level to game
game.Add (this);
}
void addGameObject (int x, int y, int tile)
{
switch (tile) {
case 1:
_shining = new Shining (shadow);
AddChild (_shining);
_shining.SetXY (x, y);
break;
case 2:
_wall = new Wall ();
AddChild (_wall);
_wall.SetXY (x, y);
break;
case 3:
_box = new Box ();
AddChild (_box);
_box.SetXY (x, y);
break;
case 4:
_door1 = new Door1 ();
AddChild (_door1);
_door1.SetXY (x, y);
break;
case 5:
_door2 = new Door2 ();
AddChild (_door2);
_door2.SetXY (x, y);
break;
case 6:
_key1 = new Key1 ();
AddChild (_key1);
_key1.SetXY (x, y);
break;
case 7:
_key2 = new Key2 ();
AddChild (_key2);
_key2.SetXY (x, y);
break;
case 8:
_coin = new Coin ();
AddChild (_coin);
_coin.SetXY (x, y);
break;
case 9:
_grass = new Ground ();
AddChild (_grass);
_grass.SetXY (x, y);
break;
case 10:
_sky = new Sprite ("nightsky.png");
AddChild (_sky);
_sky.SetXY (x, y);
break;
}
game.Remove (this);
game.Add (this);
}
void Update ()
{
if (player.HitTest (_shining)) {
_player.health = 100;
Thread.Sleep (1000);
_player.Respawn ();
}
if (_coin != null) {
if (_player.HitTest (_coin)) {
_player.GetPoint ();
_coin.GetPickedup ();
_coin = null;
}
}
if (_player.HitTest (_box)) {
_player.MoveBack (_box);
}
if (_player.HitTest (_grass)) {
_player.MoveBack (_grass);
}
if (_key1 != null) {
if (_player.HitTest (_key1)) {
_key1.GetPickedup ();
playerHasKey1 = true;
_key1 = null;
}
}
if (_key2 != null) {
if (_player.HitTest (_key2)) {
_key2.GetPickedup ();
playerHasKey2 = true;
_key2 = null;
}
}
if ((playerHasKey1 == true) && _player.HitTest (_door1)) {
_door1.Open ();
} else {
if (_player.HitTest (_door1)) {
_player.MoveBack (_door1);
}
}
if ((playerHasKey2 == true) && _player.HitTest (_door2)) {
_door2.Open ();
} else {
if (_player.HitTest (_door2)) {
_player.MoveBack (_door2);
}
}
foreach (Wall _wall in wallList) {
if (_player.HitTest (_wall)) {
_player.MoveBack (_wall);
}
}
if (_game._state == "level1" && _player.x >= 1825) {
_game.SetState ("level2");
}
_scoreboard.DrawScore (player.score);
_healthdisplay.DrawHealth (player.health);
if (_player != null) {
if (_player.x + x > 400)
x = 400 - _player.x;
_scoreboard.x = 0 - x;
_healthdisplay.x = 0 - x;
if (_player.x + x < 100)
x = 100 - _player.x;
_scoreboard.x = 0 - x;
_healthdisplay.x = 0 - x;
}
}
}
} |