-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGameEngine.cs
489 lines (447 loc) · 17.8 KB
/
GameEngine.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
484
485
486
487
488
489
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using CaveGenerator;
namespace Roguelike
{
class GameEngine
{
private Hero CurrentHero;
private MapInspector Inspector;
private bool GameOver = false;
public bool GameStarted { get; private set; }
private Cave Map;
public Rectangle HeroInfoBorder { get; set; }
public Rectangle MapBorder { get; set; }
public InfoBorder InfoBorder { get; set; }
public Random GameRandom = new Random();
private int ConsoleHeight = 0;
private int ConsoleWidth = 0;
private Monster TmpMonster; //make this as list
public void Init()
{
Map = new Cave();
Map.Build();
Map.ConnectCaves();
Map.BuildTileMap();
Map.Offset = new Point(0, 0);
CurrentHero = new Hero(new Point(12, 10), 15, 0, 8, 10, "Chiks-Chiriks");
TmpMonster = new Monster(new Point(15, 15), 10, 1000, 10, "Snake", 'S', new DijkstraAI());
Inspector = new MapInspector("Inspector", new Point(CurrentHero.Coords.X, CurrentHero.Coords.Y));
GameStarted = true;
ConsoleHeight = Console.WindowHeight;
ConsoleWidth = Console.WindowWidth;
}
private void Input()
{
var key = Console.ReadKey(true).Key;
CurrentHero.CurrentMoveAction = (BaseCharacter.MoveAction)key;
CurrentHero.CurrentGameAction = (Hero.GameAction)key;
}
private void Input(MapInspector inspector)
{
var key = Console.ReadKey(true).Key;
if (key == ConsoleKey.M) inspector.IsInspect = false;
inspector.CurrentMoveAction = (BaseCharacter.MoveAction)key;
}
private void Logic()
{
CurrentHero.Move();
if (!CurrentHero.IsActionDone)
{
//"if" is not correct, we mustn't go there if we hit a wall, for example
CurrentHero.DoGameAction();
if(!CurrentHero.IsActionDone) return;
}
TmpMonster.MoveTo(CurrentHero);
TmpMonster.DoGameAction();
MoveMap(CurrentHero);
}
public void StartMenu()
{
StartingMenu startingMenu = new StartingMenu();
startingMenu.Process();
}
public void InspectMap()
{
Inspector.IsInspect = true;
Inspector.Coords.SetValue(CurrentHero.Coords);
char symbol = CurrentHero.Symbol;
TileFlyweight tile;
ConsoleColor color;
string description;
while (Inspector.IsInspect)
{
tile = GetTile(Inspector.Coords.X, Inspector.Coords.Y);
if (tile.Object != null) //TODO: draw this with right color
{
description = tile.Object.Name; // must be description
symbol = tile.Object.Symbol;
}
else
{
description = tile.Description;
symbol = tile.Symbol;
}
color = tile.Color;
InfoBorder.ClearLineAndWrite($"{description}: {symbol}", color, tile.Description.Length + 2, 1);
RedrawInspector(symbol, color);
Input(Inspector);
HandleConsoleResize();
if (!Inspector.IsInspect)
{
SetMapOffset();
DrawAfterMapMoving();
Draw();
Program.GameEngine.InfoBorder.Clear();
break;
}
Inspector.Move();
if (!Map.WorldTile[Inspector.Coords.Y, Inspector.Coords.X].Visible) Inspector.RestoreCoords();
MoveMap(Inspector);
}
}
enum MapMoveDirection
{
Top,
Bot,
Left,
Right
}
private void MoveMap(BaseCharacter baseCharacter)
{
int distToTop = baseCharacter.Coords.Y - Map.Offset.Y + 1;
int distToLeft = baseCharacter.Coords.X - Map.Offset.X + 1;
int distToRight = MapBorder.Width - baseCharacter.Coords.X + Map.Offset.X - 2;
int distToBot = MapBorder.Height - 2 - baseCharacter.Coords.Y + Map.Offset.Y;
int vision = CurrentHero.RangeOfVision;
if (distToTop <= vision)
MoveMapToDir(MapMoveDirection.Bot);
else if (distToBot <= vision)
MoveMapToDir(MapMoveDirection.Top);
else if (distToRight <= vision)
MoveMapToDir(MapMoveDirection.Left);
else if (distToLeft <= vision)
MoveMapToDir(MapMoveDirection.Right);
}
private void MoveMapToDir(MapMoveDirection direction)
{
int offset, length;
switch (direction)
{
case MapMoveDirection.Top:
offset = Map.Offset.Y;
length = Map.Map.GetLength(0);
Map.Offset.Y = offset + 1 < length ? offset + 1 : offset;
break;
case MapMoveDirection.Bot:
offset = Map.Offset.Y;
Map.Offset.Y = offset - 1 >= 0 ? offset - 1 : offset;
break;
case MapMoveDirection.Left:
offset = Map.Offset.X;
length = Map.Map.GetLength(1);
Map.Offset.X = offset + 1 < length ? offset + 1 : offset;
break;
case MapMoveDirection.Right:
offset = Map.Offset.X;
Map.Offset.X = offset - 1 >= 0 ? offset - 1 : offset;
break;
}
DrawAfterMapMoving();
}
private void SetMapOffset()
{
Map.Offset.X = CurrentHero.Coords.X - MapBorder.Width / 2 >= 0 ?
CurrentHero.Coords.X - MapBorder.Width / 2 : 0;
Map.Offset.Y = CurrentHero.Coords.Y - MapBorder.Height / 2 >= 0 ?
CurrentHero.Coords.Y - MapBorder.Height / 2 : 0;
}
#region drawstuff
private void RedrawInspector(char symbol, ConsoleColor color)
{
int left = Inspector.Coords.X;
int top = Inspector.Coords.Y;
HandleConsoleResize();
Console.BackgroundColor = ConsoleColor.DarkYellow;
while (!Console.KeyAvailable && Inspector.IsInspect)
{
HandleConsoleResize();
if (!Inspector.IsInspect) break;
Console.SetCursorPosition(left - Map.Offset.X + MapBorder.Offset.X, top - Map.Offset. Y + MapBorder.Offset.Y);
Console.Write(' ');
Thread.Sleep(100);
HandleConsoleResize();
if (!Inspector.IsInspect) break;
Console.SetCursorPosition(left - Map.Offset.X + MapBorder.Offset.X, top - Map.Offset.Y + MapBorder.Offset.Y);
Console.ForegroundColor = color;
Console.Write(symbol);
Console.ForegroundColor = ConsoleColor.White;
Thread.Sleep(100);
}
Console.ResetColor();
HandleConsoleResize();
if (!Inspector.IsInspect) return;
Console.SetCursorPosition(left - Map.Offset.X + MapBorder.Offset.X, top - Map.Offset.Y + MapBorder.Offset.Y);
Console.Write(symbol);
}
private void DrawCharacter(Character character)
{
int left = character.Coords.X - Map.Offset.X + MapBorder.Offset.X;
int top = character.Coords.Y - Map.Offset.Y + MapBorder.Offset.Y;
if (left >= MapBorder.Offset.X && left <= MapBorder.Offset.X + MapBorder.Width - 3 &&
top >= MapBorder.Offset.Y && top <= MapBorder.Offset.Y + MapBorder.Height - 3 &&
Map.WorldTile[character.Coords.Y, character.Coords.X].Visible)
{
Console.SetCursorPosition(left, top);
Console.Write(character.Symbol);
}
}
private void RedrawCharacter(Character character)
{
//must not redraw when character coords is out of current console size
DrawCharacter(character);
int left = character.PrevCoords.X - Map.Offset.X + MapBorder.Offset.X;
int top = character.PrevCoords.Y - Map.Offset.Y + MapBorder.Offset.Y;
if (left >= MapBorder.Offset.X && left <= MapBorder.Offset.X + MapBorder.Width - 3 &&
top >= MapBorder.Offset.Y && top <= MapBorder.Offset.Y + MapBorder.Height - 3 &&
Map.WorldTile[character.PrevCoords.Y, character.PrevCoords.X].Visible)
{
Console.SetCursorPosition(left, top);
Console.Write(GetTile(character.PrevCoords.X, character.PrevCoords.Y).Symbol);
}
DrawCharacter(character);
}
private void Redraw()
{
Draw();
//RedrawCharacter(CurrentHero);
//loop over list of monsters
RedrawCharacter(TmpMonster);
}
private void Draw()
{
FOV();
TileFlyweight tileFlyweight;
for (int i = Map.Offset.Y, y = 0; y < MapBorder.Height - 2 && i < Map.WorldTile.GetLength(0); i++, y++)
{
for (int j = Map.Offset.X, x = 0; x < MapBorder.Width - 2 && j < Map.WorldTile.GetLength(1); j++, x++)
{
if (Map.WorldTile[i, j].Visible)
{
Console.SetCursorPosition(MapBorder.Offset.X + x, MapBorder.Offset.Y + y);
tileFlyweight = GetTile(j, i);
Console.ForegroundColor = tileFlyweight.Color;
Console.Write(tileFlyweight.Symbol);
Console.ResetColor();
}
}
}
DrawCharacter(CurrentHero);
//loop over list of monsters
DrawCharacter(TmpMonster);
}
private void DrawAfterMapMoving()
{
FOV();
for (int i = Map.Offset.Y, y = 0; y < MapBorder.Height - 2 && i < Map.WorldTile.GetLength(0); i++, y++)
{
Console.SetCursorPosition(MapBorder.Offset.X, y + 1);
Console.Write(new string(' ', MapBorder.Width - 2));
for (int j = Map.Offset.X, x = 0; x < MapBorder.Width - 2 && j < Map.WorldTile.GetLength(1); j++, x++)
{
if (Map.WorldTile[i, j].Visible)
{
Console.SetCursorPosition(MapBorder.Offset.X + x, MapBorder.Offset.Y + y);
Console.Write(GetTile(j, i).Symbol);
}
}
}
DrawCharacter(CurrentHero);
DrawCharacter(TmpMonster);
}
private void DrawAllBorders()
{
MapBorder = new Rectangle
{
Width = Console.WindowWidth * 3 / 4,
Height = Console.WindowHeight * 4 / 5
};
MapBorder.Location = new Point(Console.WindowWidth - MapBorder.Width, 0);
MapBorder.Offset = new Point(MapBorder.Location.X + 1, MapBorder.Location.Y + 1);
DrawBorder(MapBorder);
InfoBorder = new InfoBorder
{
Height = Console.WindowHeight - MapBorder.Height,
Width = Console.WindowWidth,
Location = new Point(0, MapBorder.Height),
Offset = new Point(1, MapBorder.Height + 1)
};
DrawBorder(InfoBorder);
HeroInfoBorder = new Rectangle
{
Width = Console.WindowWidth - MapBorder.Width,
Height = MapBorder.Height,
Location = new Point(0, 0),
Offset = new Point(1, 1)
};
DrawBorder(HeroInfoBorder);
}
private void DrawBorder(Rectangle border)
{
int width = border.Width;
int height = border.Height;
Point location = border.Location;
Console.SetCursorPosition(location.X, location.Y);
Console.Write("╔");
Console.SetCursorPosition(location.X + 1, location.Y);
Console.Write(new string('═', width - 2));
Console.SetCursorPosition(location.X + width - 1, location.Y);
Console.WriteLine("╗");
for (int i = 1; i < height - 1; i++)
{
Console.SetCursorPosition(location.X, location.Y + i);
Console.Write("║");
Console.SetCursorPosition(location.X + 1, location.Y + i);
Console.Write(new string(' ', width - 2));
Console.SetCursorPosition(location.X + width - 1, location.Y + i);
Console.WriteLine("║");
}
Console.SetCursorPosition(location.X, location.Y + height - 1);
Console.Write("╚");
Console.SetCursorPosition(location.X + 1, location.Y + height - 1);
Console.Write(new string('═', width - 2));
Console.SetCursorPosition(location.X + width - 1, location.Y + height - 1);
Console.Write("╝");
}
#endregion
private void FOV()
{
double x, y;
/*for (int i = Map.Offset.Y; i < Map.Offset.Y + MapBorder.Height - 2 && i < Map.WorldTile.GetLength(0); i++)
for (int j = Map.Offset.X; j < Map.Offset.X + MapBorder.Width - 2 && j < Map.WorldTile.GetLength(1); j++)
Map.WorldTile[i, j].Visible = false;*/
for (int i = 0; i < 360; i++)
{
x = Math.Cos(i * 0.01745);
y = Math.Sin(i * 0.01745);
ComputeFOV(x, y);
}
}
private void ComputeFOV(double x, double y)
{
double ox, oy;
ox = CurrentHero.Coords.X + 0.5;
oy = CurrentHero.Coords.Y + 0.5;
for (int i = 0; i < CurrentHero.RangeOfVision; i++)
{
Map.WorldTile[(int)oy, (int)ox].Visible = true;
if (Map.WorldTile[(int)oy, (int)ox].Type == TileFlyweight.Type.Wall)
return;
ox += x;
oy += y;
}
}
public int GetMapHeight()
{
return Map.MapSize.Height;
}
public int GetMapWidth()
{
return Map.MapSize.Width;
}
public Tile[,] GetMapRegion(Point a, Point b, int offset)
{
//в get tile передавать координаты героя, т.к там они уже меняются
if (a.X > b.X) Point.SwapPoints(ref a, ref b);
if (a.Y > b.Y)
{
int tmp;
tmp = a.Y;
a.Y = b.Y;
b.Y = tmp;
}
a.X = Math.Max(a.X - offset, 0);
b.X = Math.Min(b.X + offset, Map.WorldTile.GetLength(0) - 1); //map height
a.Y = Math.Max(a.Y - offset, 0);
b.Y = Math.Min(b.Y + offset, Map.WorldTile.GetLength(1) - 1); //map width
Tile[,] region = new Tile[b.X - a.X + 1, b.Y - a.Y + 1];
for (int i = a.X; i <= b.X; i++)
{
for (int j = a.Y; j <= b.Y; j++)
{
region[i - a.X, j - a.Y] = Map.WorldTile[i, j];
}
}
return region;
}
/// <summary>
/// Gets the tile.
/// </summary>
/// <returns>The tile.</returns>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
public TileFlyweight GetTile(int x, int y)
{
Tile tile = Map.WorldTile[y, x];
TileFactory factory = new TileFactory();
return factory.GetTile(tile);
}
/// <summary>
/// Sets the object to a tile.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
/// <param name="obj">Object.</param>
public void SetObject(int x, int y, BaseEntity obj)
{
Map.WorldTile[y, x].Object = obj;
}
/// <summary>
/// Removes the object from tile.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
public void RemoveObject(int x, int y)
{
Map.WorldTile[y, x].Object = null;
}
public char GetEntitySymbol(Point point)
{
//list over all entities
char tmpsymbol = 'S';
return tmpsymbol;
}
private void HandleConsoleResize()
{
if (ConsoleWidth != Console.WindowWidth || ConsoleHeight != Console.WindowHeight)
{
Inspector.IsInspect = false;
Console.CursorVisible = false;
Console.Clear();
DrawAllBorders();
SetMapOffset();
Draw();
}
ConsoleWidth = Console.WindowWidth;
ConsoleHeight = Console.WindowHeight;
}
public void PlayGame()
{
Console.Clear();
DrawAllBorders();
Draw();
while (!GameOver)
{
Input();
HandleConsoleResize();
Logic();
if (CurrentHero.IsActionDone)
Redraw();
//need to Redraw() not only when CurrentHero doesn't move
//For example, if we just killed a monster
}
}
}
}