-
Notifications
You must be signed in to change notification settings - Fork 0
/
Story.cs
375 lines (340 loc) · 13.2 KB
/
Story.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using InteractiveStory.Commands;
using InteractiveStory.Weaponry;
using InteractiveStory.Items;
using InteractiveStory.Enemies;
namespace InteractiveStory
{
public class Story
{
public Dictionary<string, CommandBase> commandList = new Dictionary<string, CommandBase>();
public WeaponMaker weaponMaker;
public Room[,] map = new Room[5,5];
public int points = 0;
public int floor = 0;
string exitPos = "";
public Random rand = new Random();
public Player player;
public string name;
public bool quit = false;
bool loaded = false;
public Story(string name)
{
this.name = name;
InitCommands();
weaponMaker = new WeaponMaker(this);
this.newFloor();
this.player = new Player(this, new int[] { 100, 1, 1 });
}
public void Start()
{
//int[] stats = ChooseStats();
Console.Clear();
if (!loaded)
{
Console.WriteLine(" --- Backstory --- ");
Console.WriteLine();
Console.Write("Walking through the night in the light of the full moon, you hear a loud explosion sound echo throught the air. ");
Console.Write("You follow the source of the sound to investigate further, upon reaching a graveyard you notice a burial crypt has been smashed open. ");
Console.WriteLine("Reluctantly you take a look inside, queitly creeping down the marble stairs, to see if any potential grave robbers are still around.");
Console.Write("As if a supernatural force acted upon the entrance, it collapses sealing you in. ");
Console.Write("Any attempt you make to dig your way out only causes more rubble to fill the space. ");
Console.WriteLine("Accepting defeat you turn and continue onward to find another way out...");
Console.WriteLine();
Console.WriteLine("Press [ENTER] to continue...");
Console.ReadLine();
this.SaveGame();
}
Console.Clear();
Console.WriteLine("Type 'help' to get a list of available actions...");
Console.WriteLine();
string[] command;
commandList["look"].DoCommand(this, new string[0]);
while (player.health > 0 && !quit)
{
player.canBlock = true;
command = Console.ReadLine().Split(Char.Parse(" "));
string msg = doCommand(command);
if (msg != "")
{
Console.WriteLine(msg);
}
doAttack();
}
if (!quit)
{
Console.WriteLine();
Console.WriteLine("You died :(");
Console.WriteLine("Score: " + points);
Console.ReadLine();
}
else
{
Console.Clear();
}
}
public void InitCommands()
{
AddCommand(new CommandAttack(), new string[] { "attack", "hit", "kill", "a" });
AddCommand(new CommandDrop(), new string[] { "drop", "discard", "d" });
AddCommand(new CommandEquip(), new string[] { "equip", "hold", "e" });
AddCommand(new CommandInvo(), new string[] { "inventory", "invo", "items", "i" });
AddCommand(new CommandLook(), new string[] { "look", "search", "l" });
AddCommand(new CommandMove(), new string[] { "move", "go", "walk", "run", "w" });
AddCommand(new CommandPickup(), new string[] { "pickup", "get", "grab", "p" });
AddCommand(new CommandSkills(), new string[] { "skills", "x" });
AddCommand(new CommandStats(), new string[] { "stats", "statistics", "s" });
AddCommand(new CommandUse(), new string[] { "use", "u" });
AddCommand(new CommandMap(), new string[] { "map", "m" });
AddCommand(new CommandSave(), new string[] { "save", "savegame" });
AddCommand(new CommandQuit(), new string[] { "quit" });
}
public void AddCommand(CommandBase command, string[] aliases)
{
for (int i = 0; i < aliases.Length; i++)
{
commandList.Add(aliases[i].Trim().ToLower(), command);
}
}
public Room getRoom(int x, int y)
{
try
{
if (map[x, y] == null)
{
map[x, y] = makeRoom(x,y);
}
return map[x, y];
}
catch (IndexOutOfRangeException e)
{
return null;
}
}
public Room makeRoom(int x, int y)
{
Room room = new Room(this, x, y);
for (int i = this.rand.Next(4) + floor; i >= 0; i--)
{
if ((x + "," + y) == exitPos)
{
exitPos = "";
room.contents.Add(new Objects.ExitObject(this, "Exit"));
continue;
}
int num = rand.Next(3 + floor);
switch (num)
{
case 0:
{
if (rand.Next(2) == 0)
{
room.contents.Add(new Items.Coin(this, "Coin"));
}
else
{
room.contents.Add(new Objects.Chest(this, "Chest"));
}
break;
}
case 1:
{
room.contents.Add(weaponMaker.MakeWeapon());
break;
}
case 2:
{
room.contents.Add(new Items.Heart(this, "Heart"));
break;
}
default:
{
if (num - 2 >= 5)
{
room.contents.Add(new Spider(this, "Spider"));
}
else if (num - 2 >= 3)
{
room.contents.Add(new Skeleton(this, "Skeleton"));
}
else
{
room.contents.Add(new Zombie(this, "Zombie"));
}
break;
}
}
}
return room;
}
public void newFloor()
{
floor += 1;
map = new Room[5, 5];
exitPos = rand.Next(map.GetLength(0)) + "," + rand.Next(map.GetLength(1));
if (floor > 1 && player != null)
{
this.SaveGame();
}
}
public bool SaveGame()
{
Dictionary<string, int> saveData = new Dictionary<string, int>();
saveData.Add("floor", this.floor);
saveData.Add("points", this.points);
this.player.SavePlayer(saveData);
try
{
BinaryWriter writer = new BinaryWriter(File.Open(@"saves\" + this.name + ".crypt", FileMode.OpenOrCreate));
writer.Write(saveData.Count);
for (int i = 0; i < saveData.Count; i++)
{
writer.Write(saveData.Keys.ElementAt(i));
writer.Write(saveData.Values.ElementAt(i));
}
writer.Close();
return true;
} catch(Exception e)
{
Console.Clear();
Console.WriteLine("An error occured while trying to save the game!");
Console.WriteLine();
Console.WriteLine("Error Report:");
Console.WriteLine(e);
Console.WriteLine();
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
return false;
}
}
public void LoadGame(Dictionary<string, int> loadData)
{
this.loaded = true;
this.floor = loadData["floor"];
this.points = loadData["points"];
this.player.LoadPlayer(loadData);
}
public int[] ChooseStats()
{
int[] stats = new int[]{100, 1, 5};
/*while (true)
{
Console.Clear();
Console.WriteLine("What class are you?");
Console.WriteLine("1: Knight (HP: 125, Att: 5, Inv: 3)");
Console.WriteLine("2: Nimble (HP: 100, Att: 3, Inv: 5)");
Console.WriteLine("3: Scavanger (HP: 100, Att: 1, Inv: 7)");
Console.WriteLine("4: Tank (HP: 150, Att: 7, Inv: 1)");
Console.WriteLine("5: Average (HP: 100, Att: 1, Inv: 5)");
ConsoleKeyInfo keyInfo = Console.ReadKey();
if (Char.IsNumber(keyInfo.KeyChar))
{
int num = Convert.ToInt32(Char.ToString(keyInfo.KeyChar));
if (num == 1)
{
stats[0] = 125;
stats[1] = 5;
stats[2] = 3;
break;
}
else if(num == 2)
{
stats[0] = 100;
stats[1] = 3;
stats[2] = 5;
break;
}
else if (num == 3)
{
stats[0] = 100;
stats[1] = 1;
stats[2] = 7;
break;
}
else if (num == 4)
{
stats[0] = 150;
stats[1] = 7;
stats[2] = 1;
break;
}
else if (num == 5)
{
break;
}
}
}*/
return stats;
}
public void doAttack()
{
Room room = this.getRoom(this.player.posX, this.player.posY);
if (room.contents.Count <= 0)
{
return;
}
int index = rand.Next(room.contents.Count);
if (rand.Next(2) == 0 && room.contents.ElementAt(index) is Enemies.EnemyBase)
{
Enemies.EnemyBase enemy = (Enemies.EnemyBase)room.contents.ElementAt(index);
Console.WriteLine("You were attacked by a " + enemy.name);
if (player.equipped != null && player.equipped.block > 0 && rand.Next(100) < player.equipped.block && player.canBlock)
{
Console.WriteLine("You blocked the attack with your " + player.equipped.name);
player.equipped.durability--;
if (player.equipped.durability <= 0)
{
Console.WriteLine("Your " + player.equipped.name + " broke!");
player.equipped = null;
}
}
else
{
enemy.attackPlayer(this.player);
if (player.health > 0)
{
Console.WriteLine("Your health is now at " + player.health);
}
}
}
}
public string doCommand(string[] args)
{
if(args.Length <= 0 || args[0] == "")
{
return "...";
} else if (commandList.ContainsKey(args[0]))
{
commandList[args[0]].DoCommand(this, args);
return "";
} else if (args[0].ToLower() == "help")
{
Console.WriteLine("Available Commands:");
Console.WriteLine("w/walk/move/go <direction>");
Console.WriteLine("l/look/search");
Console.WriteLine("p/pickup/get/grab <item>");
Console.WriteLine("e/equip <item>");
Console.WriteLine("d/drop <item>");
Console.WriteLine("u/use <object>");
Console.WriteLine("a/attack/kill <enemy>");
Console.WriteLine("i/invo/inventory");
Console.WriteLine("s/stats/statistics");
Console.WriteLine("x/skills");
Console.WriteLine("m/map");
Console.WriteLine("save/savegame");
Console.WriteLine("q/quit");
return "";
}
else
{
return "Unknown action '" + args[0].ToLower() + "'";
}
}
}
}