-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.cs
More file actions
79 lines (71 loc) · 2.61 KB
/
MainMenu.cs
File metadata and controls
79 lines (71 loc) · 2.61 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading.Tasks;
namespace CYOA
{
public class MainMenu : IMenu
{
private string _prompt;
private List<MenuChoice> _choices;
public MainMenu()
{
var directories = Directory.GetDirectories("GameData").ToList();
_prompt = "Welcome to the CYOA Engine!";
if (directories.Count == 0) _prompt += "\n\nSorry, there are no stories available.";
else
{
_prompt += $" There are {directories.Count} stories on the bookshelf.\n\nWhich story would you like to play?";
_choices = new List<MenuChoice>();
var i = 0;
foreach (var directory in directories)
{
var folderName = directory.Substring(directory.LastIndexOf('\\') + 1);
_choices.Add(new MenuChoice(folderName, folderName));
i++;
}
_choices.Add(new MenuChoice("Quit", "?"));
}
}
public string Display()
{
Settings.Color(FontColor.DEFAULT);
Console.WriteLine(_prompt);
bool isChoiceConfirmed = false;
int currentChoiceIndex = 0;
while (!isChoiceConfirmed)
{
for (var i = 0; i < _choices.Count; i++)
{
if (currentChoiceIndex == i)
{
Settings.Color(FontColor.SELECTION);
Console.Write("> ");
}
else
{
Console.Write(" ");
}
Console.WriteLine(_choices[i].Text);
Settings.Color(FontColor.MENU);
}
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.UpArrow:
currentChoiceIndex -= 1;
if (currentChoiceIndex < 0) currentChoiceIndex += _choices.Count;
break;
case ConsoleKey.DownArrow:
currentChoiceIndex = (currentChoiceIndex + 1) % _choices.Count;
break;
case ConsoleKey.Enter:
isChoiceConfirmed = true;
break;
}
if (!isChoiceConfirmed) Console.CursorTop -= _choices.Count;
}
return _choices[currentChoiceIndex].Link;
}
}
}