-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLevel.cs
More file actions
348 lines (309 loc) · 9.65 KB
/
GameLevel.cs
File metadata and controls
348 lines (309 loc) · 9.65 KB
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
using Comora;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProgGame
{
public class GameLevel
{
public Camera camera;
public Tile[,] tiles;
public int Width
{
get { return tiles.GetLength(0); }
}
public int Height
{
get { return tiles.GetLength(1); }
}
public Player player;
private List<Spike> spikes = new();
private List<MovingPlatform> movingPlatforms = new();
private List<Cannon> cannons = new();
private List<MovingSpike> movingSpikes = new();
private List<BackgroundLayer> bgLayers = new();
public Vector2 startPosition;
private Point endPoint;
public bool levelEnded = false;
public ContentManager content;
public GameLevel(IServiceProvider serviceProvider, Stream fileStream, GraphicsDevice device)
{
content = new ContentManager(serviceProvider, "Content");
camera = new Camera(device);
camera.Zoom = 2f;
LoadLevel(fileStream);
bgLayers.Add(new BackgroundLayer(content, "bg/hills", 0.02f));
bgLayers.Add(new BackgroundLayer(content, "bg/clouds1", 0.03f));
bgLayers.Add(new BackgroundLayer(content, "bg/clouds2", 0.04f));
bgLayers.Add(new BackgroundLayer(content, "bg/foreground", 0.1f));
}
public void ProcessCamera(GameTime gameTime)
{
camera.Position = Vector2.Lerp(camera.Position, player.position, 10 * (float)gameTime.ElapsedGameTime.TotalSeconds);
}
public void LoadLevel(Stream fileStream)
{
List<string> lines = new();
int width;
using (StreamReader reader = new StreamReader(fileStream))
{
string line = reader.ReadLine();
width = line.Length;
while (line != null)
{
lines.Add(line);
line = reader.ReadLine();
}
}
tiles = new Tile[width, lines.Count];
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
char tile = lines[y][x];
tiles[x, y] = ParseTileFromChar(tile, x, y);
}
}
SetWallTextures();
}
public Tile ParseTileFromChar(char tile, int x, int y)
{
switch (tile)
{
case '.':
return new Tile(null, CollisionType.Air, TileType.None);
case '#':
return new Tile(content.Load<Texture2D>("tiles/wall"), CollisionType.Solid, TileType.Static);
case 'X':
return new Tile(content.Load<Texture2D>("tiles/wall"), CollisionType.FakeSolid, TileType.Static);
case 'S':
return InstantiateStartTile(x, y);
case '^':
return InstantiateSpike(x, y, Direction.Up);
case '>':
return InstantiateSpike(x, y, Direction.Right);
case '<':
return InstantiateSpike(x, y, Direction.Left);
case 'v':
return InstantiateSpike(x, y, Direction.Down);
case '-':
return InstantiateMovingPlatform(x, y);
case 'E':
return InstantiateEndTile(x, y);
case '{':
return InstantiateCannonTile(x, y, Direction.Left);
case '}':
return InstantiateCannonTile(x, y, Direction.Right);
case '!':
return InstantiateCannonTile(x, y, Direction.Down);
case 'i':
return InstantiateCannonTile(x, y, Direction.Up);
case '(':
return InstantiateMovingSpike(x, y, Direction.Left);
case ')':
return InstantiateMovingSpike(x, y, Direction.Right);
case 'q':
return InstantiateMovingSpike(x, y, Direction.Down);
case '`':
return InstantiateMovingSpike(x, y, Direction.Up);
}
throw new Exception("unknown tile");
}
public void SetWallTextures()
{
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
if (tiles[x, y].texture == null)
continue;
if (!tiles[x, y].texture.Name.Contains("wall"))
continue;
if (y - 1 < 0)
continue;
if (tiles[x, y - 1].texture == null)
{
SetRandomDecoration(x, y - 1);
tiles[x, y].texture = content.Load<Texture2D>("tiles/grasswall");
}
else if (!tiles[x, y - 1].texture.Name.Contains("wall"))
tiles[x, y].texture = content.Load<Texture2D>("tiles/grasswall");
}
}
}
public void SetRandomDecoration(int x, int y)
{
if (tiles[x, y].tileType != TileType.None || tiles[x, y + 1].collisionType == CollisionType.FakeSolid)
return;
Random random = new Random();
var rnd = random.Next(100);
if (rnd <= 45)
{
rnd = random.Next(1, 5);
tiles[x, y].texture = content.Load<Texture2D>($"tiles/flower{rnd}");
}
else if (rnd <= 60)
{
tiles[x, y].texture = content.Load<Texture2D>($"tiles/bush");
}
}
public Tile InstantiateEndTile(int x, int y)
{
endPoint = GetTileBounds(x, y).Center;
return new Tile(content.Load<Texture2D>("tiles/end"), CollisionType.Air, TileType.Static);
}
public Tile InstantiateMovingSpike(int x, int y, Direction direction)
{
MovingSpike spike = new MovingSpike(this, direction, Extentions.GetBottomCenter(GetTileBounds(x, y)), new Vector2(x, y));
movingSpikes.Add(spike);
return new Tile(null, CollisionType.Air, TileType.Entity);
}
public Tile InstantiateMovingPlatform(int x, int y)
{
MovingPlatform movingPlatform = new MovingPlatform(this, Extentions.GetBottomCenter(GetTileBounds(x, y)));
movingPlatforms.Add(movingPlatform);
return new Tile(null, CollisionType.Air, TileType.Entity);
}
public Tile InstantiateSpike(int x, int y, Direction direction)
{
Spike spike = new Spike(this, direction, Extentions.GetBottomCenter(GetTileBounds(x, y)));
spikes.Add(spike);
return new Tile(null, CollisionType.Air, TileType.Entity);
}
public Tile InstantiateStartTile(int x, int y)
{
startPosition = Extentions.GetBottomCenter(GetTileBounds(x, y));
player = new Player(this, startPosition);
camera.Position = startPosition;
return new Tile(null, CollisionType.Air, TileType.Entity);
}
public Tile InstantiateCannonTile(int x, int y, Direction direction)
{
Cannon cannon = new Cannon(this, direction, Extentions.GetBottomCenter(GetTileBounds(x, y)));
cannons.Add(cannon);
return new Tile(null, CollisionType.Air, TileType.Entity);
}
public CollisionType GetCollisionType(int x, int y)
{
if (x < 0 || x >= Width) // игрок пытается выйти за горизонтальные пределы игрового уровня
return CollisionType.Solid;
if (y < 0 || y >= Height) // игрок пытается выйти за вертикальные пределы игрового уровня
return CollisionType.Air; // возвращаем воздух, чтобы игрок смог прыгать/падать за пределы уровня
return tiles[x, y].collisionType;
}
public Rectangle GetTileBounds(int x, int y)
{
return new Rectangle(x * Tile.width, y * Tile.height, Tile.width, Tile.height);
}
public bool CheckForSolidCollision(Rectangle rectangle)
{
Rectangle bounds = rectangle;
int leftTile = (int)Math.Floor((float)bounds.Left / Tile.width);
int rightTile = (int)Math.Ceiling((float)bounds.Right / Tile.width) - 1;
int topTile = (int)Math.Floor((float)bounds.Top / Tile.height);
int bottomTile = (int)Math.Ceiling((float)bounds.Bottom / Tile.height) - 1; ;
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
CollisionType collision = GetCollisionType(x, y);
if (collision == CollisionType.Solid)
{
Rectangle tileBounds = GetTileBounds(x, y);
Vector2 depth = Extentions.GetIntersectionDepth(bounds, tileBounds);
if (depth != Vector2.Zero)
return true;
}
}
}
return false;
}
public void Update(GameTime gameTime)
{
if (levelEnded)
return;
player.Update(gameTime);
if (player.BoundingRectangle.Top >= Height * Tile.height)
player.OnKilled();
foreach (Spike spike in spikes)
{
if (spike.BoundingRectangle.Intersects(player.BoundingRectangle))
{
player.OnKilled();
break;
}
}
foreach (MovingSpike movingSpike in movingSpikes)
{
if (movingSpike.BoundingRectangle.Intersects(player.BoundingRectangle) && movingSpike.isAlive)
{
player.OnKilled();
break;
}
if (movingSpike.TriggerRect.Intersects(player.BoundingRectangle))
movingSpike.TriggerMove();
movingSpike.Update();
}
foreach (MovingPlatform movingPlatform in movingPlatforms)
{
movingPlatform.Update();
if (movingPlatform.Bounds.Intersects(player.BoundingRectangle))
{
movingPlatform.TriggerMove();
player.onPlatform = true;
break;
}
else
{
player.onPlatform = false;
movingPlatform.UnTriggerMove();
}
}
foreach (Cannon cannon in cannons)
{
cannon.Update();
if (cannon.ProjectileBoundingRectangle.Intersects(player.BoundingRectangle))
{
player.OnKilled();
break;
}
}
if (player.BoundingRectangle.Contains(endPoint))
levelEnded = true;
ProcessCamera(gameTime);
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (var bg in bgLayers)
bg.Draw(spriteBatch, camera.Position);
foreach (Spike spike in spikes)
spike.Draw(spriteBatch);
foreach (MovingPlatform movingPlatform in movingPlatforms)
movingPlatform.Draw(spriteBatch);
foreach (Cannon cannon in cannons)
cannon.Draw(spriteBatch);
foreach (MovingSpike movingSpike in movingSpikes)
movingSpike.Draw(spriteBatch);
for (int y = 0; y < Height; ++y)
{
for (int x = 0; x < Width; ++x)
{
Texture2D texture = tiles[x, y].texture;
if (texture != null)
{
Vector2 position = new Vector2(x, y) * Tile.size;
spriteBatch.Draw(texture, position, Color.White);
}
}
}
player.Draw(spriteBatch);
}
}
}