-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBehaviour.cs
564 lines (506 loc) · 25.4 KB
/
GameBehaviour.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using System;
namespace EscapeGame
{
public class GameBehaviour
{
/// <summary>
/// Creates the standard state of all game objects at the beginning of the game.
/// </summary>
public static void CreateNewGame()
{
GameObject.playerPosition = GameObject.RoomLocation.Entrance;
//intro text
Console.WriteLine("You wake up... seems like you have overslept through most of the morning and into noon,\n" +
"judging from the sun outside your window. Flashes of last night's party come to your mind\n" +
"as you suddenly realize how dry your mouth is and how much your head hurts from hangover.\n" +
"You get up, thinking of heading to the kitchen to grab a bite to calm your angry stomach.\n\n" +
"However, as you try to pull your room's door open you realize that is locked!\n" +
"As your numb brain tries to make sense of the situation a sudden memory of last night rushes\n" +
"to your mind... You played a rather borderline prank on your friend Marvin and as retribution\n" +
"he must have for sure locked the door. You slam it and yell for a while, but there is no answer from outside...\n\n" +
"You sigh to yourself, leaning your head agains the door. If there are spare copies of the keys,\n" +
"they must be for sure in the basement... and mentalize yourself of having to stay there until\n" +
"Marvin decides that you have suffered enough.\n\n" +
"After what it feels like an eternity leaning at the door a distant childhood memory strikes you\n" +
"seemingly from nowhere: you loved to create riddles and puzzles with your father and hide\n" +
"stuff throughout the house to play treasure hunt with him. Sometimes also spare keys.\n" +
"Maybe you hid a spare key somewhere in your room? You are sure of it, because he jokingly complained\n" +
"about it for years before passing away. However, it was so long ago that you barely remember the details.\n\n" +
"You turn around and look at your L-shaped Room: the Entrance part,the Center part and the Back part\n" +
"and decide to look for clues... After all you are going to be here for a while anyway,\n" +
"so it's better if you keep yourself occupied with something instead of idly looking at the ceiling.\n" +
"Maybe you might find something in the wardrobe or the desk? You wonder to yourself...\n");
Console.WriteLine("You find yourself in the {0} part of the Room.", GameObject.playerPosition);
}
/// <summary>
/// Saves the current state of the game
/// </summary>
public static void SaveAndExit()
{
System.IO.StreamWriter gameStateSaver = new System.IO.StreamWriter("Save_File_Escape_The_Room.txt");
try
{
gameStateSaver.WriteLine(GameObject.playerPosition.ToString());
gameStateSaver.WriteLine(GameObject.playerInventory.Count);
foreach(string item in GameObject.playerInventory)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.isOpenable.Length);
foreach (GameObject.ItemClosedOrOpen item in GameObject.isOpenable)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.isSwitchable.Length);
foreach (GameObject.ItemOnOrOff item in GameObject.isSwitchable)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.isCombinable.Length);
foreach (GameObject.ItemCombined item in GameObject.isCombinable)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.objectsInCenter.Length);
foreach (string item in GameObject.objectsInCenter)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.objectsInBack.Length);
foreach (string item in GameObject.objectsInBack)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.objectsInEntrance.Length);
foreach (string item in GameObject.objectsInEntrance)
{
gameStateSaver.WriteLine(item);
}
gameStateSaver.WriteLine(GameObject.objectsInRoof.Length);
foreach (string item in GameObject.objectsInRoof)
{
gameStateSaver.WriteLine(item);
}
}
catch
{
Console.WriteLine("Save failed!");
}
finally
{
gameStateSaver.Close();
}
}
/// <summary>
/// Loads the game to the previous state.
/// </summary>
public static void LoadGame()
{
System.IO.StreamReader gameStateLoader = new System.IO.StreamReader("Save_File_Escape_The_Room.txt");
try
{
string playerPositionString = gameStateLoader.ReadLine();
GameObject.playerPosition = Enum.Parse<GameObject.RoomLocation>(playerPositionString);
string itemsInInventoryString = gameStateLoader.ReadLine();
int itemsInInventory = int.Parse(itemsInInventoryString);
for(int i = 0; i < itemsInInventory; i++)
{
GameObject.playerInventory.Add(gameStateLoader.ReadLine());
}
string itemsInIsOpenableString = gameStateLoader.ReadLine();
int itemsInIsOpenable = int.Parse(itemsInIsOpenableString);
for (int i = 0; i < itemsInIsOpenable; i++)
{
GameObject.isOpenable[i] = Enum.Parse
<GameObject.ItemClosedOrOpen>(gameStateLoader.ReadLine());
}
string itemsInIsSwitchableString = gameStateLoader.ReadLine();
int itemsInIsSwitchable = int.Parse(itemsInIsSwitchableString);
for (int i = 0; i < itemsInIsSwitchable; i++)
{
GameObject.isSwitchable[i] = Enum.Parse
<GameObject.ItemOnOrOff>(gameStateLoader.ReadLine());
}
string itemsInIsCombinableString = gameStateLoader.ReadLine();
int itemsInIsCombinable = int.Parse(itemsInIsCombinableString);
for (int i = 0; i < itemsInIsCombinable; i++)
{
GameObject.isCombinable[i] = Enum.Parse
<GameObject.ItemCombined>(gameStateLoader.ReadLine());
}
string itemsInObjectsInCenterString = gameStateLoader.ReadLine();
int itemsInObjectsInCenter = int.Parse(itemsInObjectsInCenterString);
for (int i = 0; i < itemsInObjectsInCenter; i++)
{
GameObject.objectsInCenter[i] = gameStateLoader.ReadLine();
}
string itemsInObjectsInBackString = gameStateLoader.ReadLine();
int itemsInObjectsInBack = int.Parse(itemsInObjectsInBackString);
for (int i = 0; i < itemsInObjectsInBack; i++)
{
GameObject.objectsInBack[i] = gameStateLoader.ReadLine();
}
string itemsInObjectsInEntranceString = gameStateLoader.ReadLine();
int itemsInObjectsInEntrance = int.Parse(itemsInObjectsInEntranceString);
for (int i = 0; i < itemsInObjectsInEntrance; i++)
{
GameObject.objectsInEntrance[i] = gameStateLoader.ReadLine();
}
string itemsInObjectsInRoofString = gameStateLoader.ReadLine();
int itemsInObjectsInRoof = int.Parse(itemsInObjectsInRoofString);
for (int i = 0; i < itemsInObjectsInRoof; i++)
{
GameObject.objectsInRoof[i] = gameStateLoader.ReadLine();
}
}
catch
{
Console.WriteLine("Loading failed!");
}
finally
{
gameStateLoader.Close();
}
}
/// <summary>
/// Shows the game's instructions to the player. Accessed by typing "h" or "help".
/// </summary>
public static void HelpMenu()
{
Console.WriteLine("Escape the Room is a text-based adventure game where the objective is to get out of your room.\n" +
"For that you will need to examine and interact with your environment.\n" +
"This is done through different commands you need to type in.\n" +
"The different commands are listed in the next page.\n" +
"You will also have to type objects or locations. For simplicity you will never have to type more\n" +
"than one word at a time. No compound commands are used in this game.\n" +
"As a hint, some objects that you can interact with may start with an uppercase in descriptions.\n" +
"However, as a rule of thumb, if there is a noun in a description of something,\n" +
"chances are that you can examine/interact with it in some way.\n\n" +
"The key here is to experiment and have fun!\n");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
Console.WriteLine("These are all possible commands you can type. Shortcuts to them are marked in uppercase.");
Console.WriteLine("Type \"Help\" to call this menu back at any given time during the game.");
Console.WriteLine("Type \"Clear\" to clear the screen.");
Console.WriteLine("Type \"eXit\" to save and exit the game");
Console.WriteLine("Type \"Move\" to move to the different parts of the room.");
Console.WriteLine("Type \"Examine\" to have a closer look at a given item.");
Console.WriteLine("Type \"Use\" to use an item.");
Console.WriteLine("Type \"Pick\" to pick up an item.");
Console.WriteLine("Type \"Inventory\" to see your inventory.\n");
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
}
/// <summary>
/// This moves the player around the different parts of the room.
/// </summary>
public static void MoveChar()
{
do
{
Console.WriteLine("\nWhere do you want to move?");
string charDestinationString = Console.ReadLine().Trim().ToLower();
GameObject.RoomLocation charDestination;
try
{
charDestination = Enum.Parse<GameObject.RoomLocation>(charDestinationString, true);
}
catch
{
Console.WriteLine("There is not such a place you can move to in your Room.");
continue;
}
if (charDestination == GameObject.playerPosition)
{
Console.WriteLine("You find yourself already there.");
continue;
}
else
{
switch (charDestination)
{
case GameObject.RoomLocation.Center:
if (GameObject.playerPosition == GameObject.RoomLocation.Roof)
{
Console.WriteLine("You carefully go down the ladder, grab the rope and swiftly jump into your room.\n" +
"You then move across your Room towards the center.");
GameObject.playerPosition = GameObject.RoomLocation.Center;
}
else
{
Console.WriteLine("You move across your Room towards the center.");
GameObject.playerPosition = GameObject.RoomLocation.Center;
}
break;
case GameObject.RoomLocation.Back:
if (GameObject.playerPosition == GameObject.RoomLocation.Roof)
{
Console.WriteLine("You carefully go down the ladder, grab the rope and swiftly jump into your room.\n" +
"You then move across your Room towards the Back.");
GameObject.playerPosition = GameObject.RoomLocation.Back;
}
else
{
Console.WriteLine("You move across your Room towards the Back.");
GameObject.playerPosition = GameObject.RoomLocation.Back;
}
break;
case GameObject.RoomLocation.Entrance:
if (GameObject.playerPosition == GameObject.RoomLocation.Roof)
{
Console.WriteLine("You carefully go down the ladder, grab the rope and swiftly jump into your room.\n" +
"You then move across your Room towards the Entrance.");
GameObject.playerPosition = GameObject.RoomLocation.Entrance;
}
else
{
Console.WriteLine("You move across your Room towards the Entrance.");
GameObject.playerPosition = GameObject.RoomLocation.Entrance;
}
break;
case GameObject.RoomLocation.Roof:
if((GameObject.isCombinable[12] == GameObject.ItemCombined.isCombined) &&
(GameObject.playerInventory.Contains("gear")))
{
GameObject.playerPosition = GameObject.RoomLocation.Roof;
Console.WriteLine("You move across your room and lean out the window next to your bed.\n" +
"You put on your gear and hiking shoes and attach yourself to the rope.\n" +
"You grab the rope very carefully. With a quick, decisive move you get a hold of the ladder\n" +
"and before you realize it you find yourself in the roof.");
}
else
{
Console.WriteLine("You can't go there, it's too risky!");
}
break;
}
}
break;
} while (true);
}
/// <summary>
/// It looks if an item is in the part of the room where the player is and returns its description.
/// </summary>
/// <param name="location">The part of the room the player is in.</param>
/// <param name="item">The item to examine.</param>
public static string FindItemToExamine(GameObject.RoomLocation location, string item)
{
if (GameObject.playerInventory.Contains(item))
{
return GameObject.ItemDescriptionInventory(item);
}
if (location == GameObject.RoomLocation.Center)
{
for (int i = 0; i < GameObject.objectsInCenter.Length; i++)
{
if (GameObject.objectsInCenter[i] == item)
{
return GameObject.ItemDescriptionCenter(item);
}
}
}
if (location == GameObject.RoomLocation.Back)
{
for (int i = 0; i < GameObject.objectsInBack.Length; i++)
{
if (GameObject.objectsInBack[i] == item)
{
return GameObject.ItemDescriptionBack(item);
}
}
}
if (location == GameObject.RoomLocation.Entrance)
{
for (int i = 0; i < GameObject.objectsInEntrance.Length; i++)
{
if (GameObject.objectsInEntrance[i] == item)
{
return GameObject.ItemDescriptionEntrance(item);
}
}
}
if (location == GameObject.RoomLocation.Roof)
{
for (int i = 0; i < GameObject.objectsInRoof.Length; i++)
{
if (GameObject.objectsInRoof[i] == item)
{
return GameObject.ItemDescriptionRoof(item);
}
}
}
return "There is no such item here.";
}
/// <summary>
/// It allows the player to examine the different objects and room parts.
/// </summary>
public static void ExamineItem()
{
Console.WriteLine("\nWhat do you want to examine?");
string itemToExamine = Console.ReadLine().Trim().ToLower();
Console.WriteLine(FindItemToExamine(GameObject.playerPosition, itemToExamine));
}
/// <summary>
/// It looks if an item is in the part of the room where the player is and returns the specific
/// behaviour for that item as a string.
/// </summary>
/// <param name="location">The part of the room where the player is.</param>
/// <param name="item">The specific item to use.</param>
/// <returns></returns>
public static string FindItemToUse(GameObject.RoomLocation location, string item)
{
if (GameObject.playerInventory.Contains(item))
{
return GameObject.UseItemInventory(item);
}
if (location == GameObject.RoomLocation.Center)
{
for (int i = 0; i < GameObject.objectsInCenter.Length; i++)
{
if (GameObject.objectsInCenter[i] == item)
{
return GameObject.UseItemCenter(item);
}
}
}
if (location == GameObject.RoomLocation.Back)
{
for (int i = 0; i < GameObject.objectsInBack.Length; i++)
{
if (GameObject.objectsInBack[i] == item)
{
return GameObject.UseItemBack(item);
}
}
}
if (location == GameObject.RoomLocation.Entrance)
{
for (int i = 0; i < GameObject.objectsInEntrance.Length; i++)
{
if (GameObject.objectsInEntrance[i] == item)
{
return GameObject.UseItemEntrance(item);
}
}
}
if (location == GameObject.RoomLocation.Roof)
{
for (int i = 0; i < GameObject.objectsInRoof.Length; i++)
{
if (GameObject.objectsInRoof[i] == item)
{
return GameObject.UseItemRoof(item);
}
}
}
return "There is no such item here."; ;
}
/// <summary>
/// Allows the player to interact with the sorroundings, using items for certain purposes.
/// </summary>
public static void UseItem()
{
Console.WriteLine("\nWhat do you want to use?");
string itemToUse = Console.ReadLine().Trim().ToLower();
Console.WriteLine(FindItemToUse(GameObject.playerPosition, itemToUse));
}
/// <summary>
/// It looks if an item is in the part of the room where the player is and returns if
/// the player has picked it or not.
/// </summary>
/// <param name="location">The part of the room where the player is.</param>
/// <param name="item">The specific item to pick.</param>
/// <returns></returns>
public static string FindItemToPick(GameObject.RoomLocation location, string item)
{
foreach(string s in GameObject.playerInventory)
{
if(s == item)
{
return "You already picked that item.";
}
}
if (location == GameObject.RoomLocation.Center)
{
for (int i = 0; i < GameObject.objectsInCenter.Length; i++)
{
if (GameObject.objectsInCenter[i] == item)
{
return GameObject.PickItemCenter(item);
}
}
}
if (location == GameObject.RoomLocation.Back)
{
for (int i = 0; i < GameObject.objectsInBack.Length; i++)
{
if (GameObject.objectsInBack[i] == item)
{
return GameObject.PickItemBack(item);
}
}
}
if (location == GameObject.RoomLocation.Entrance)
{
for (int i = 0; i < GameObject.objectsInEntrance.Length; i++)
{
if (GameObject.objectsInEntrance[i] == item)
{
return GameObject.PickItemEntrance(item);
}
}
}
if (location == GameObject.RoomLocation.Roof)
{
for (int i = 0; i < GameObject.objectsInRoof.Length; i++)
{
if (GameObject.objectsInRoof[i] == item)
{
return GameObject.PickItemRoof(item);
}
}
}
return "There is no such item here."; ;
}
/// <summary>
/// Allows the player to pick up items from the surroundings
/// </summary>
public static void PickItem()
{
Console.WriteLine("\nWhat do you want to pick up?");
string itemToPick = Console.ReadLine().Trim().ToLower();
Console.WriteLine(FindItemToPick(GameObject.playerPosition, itemToPick));
}
/// <summary>
/// Shows the current items in the player's inventory.
/// </summary>
public static void ShowInventory()
{
if(GameObject.playerInventory.Count == 0)
{
Console.WriteLine("Your inventory is empty.");
}
else
{
Console.WriteLine("\nYour inventory contains:");
foreach (string item in GameObject.playerInventory)
{
Console.WriteLine(item);
}
}
}
/// <summary>
/// Ending credits or the game
/// </summary>
/// <returns>The ending credits after the player beats the game</returns>
public static string ThanksAndGoodbye()
{
return "You have managed to get out of your room. You have beated the game!\n" +
"I hope you enjoyed playing this as much as I enjoyed creating it.\n" +
"If you feel like giving feedback, don't hesitate to write me a PM on either Reddit or GitHub (name OuterGazer)\n\n" +
"Designed, coded and written by OuterGazer on December 2020\n\n" +
"Thank you for playing and goodbye!\n" +
"Press Enter to continue...";
}
}
}