-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
266 lines (240 loc) · 6.26 KB
/
Player.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
using System;
using System.Threading;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using ExtensionMethods;
using System.Xml;
using NLua;
namespace Game
{
public class Player: Being
{
public int X {get; set;}
publ\\ic int Y {get; set;}
public string Message {get; set;}
public string currentDungeon;
public Player (string name, Characteristics ch, Characteristics currentCh, int bagsize, int x, int y) :base(name, ch, currentCh, bagsize)
{
X = x;
Y = y;
symbol = 'P';
}
public void SetCurrentDungeon(string newDungeon)
{
currentDungeon = newDungeon;
}
public void Move(ConsoleKeyInfo c, Map m)
{
bool move = false;
if ((c.Key == ConsoleKey.UpArrow) && (Y>0))
{
move = m.location[X,Y-1].CanMoveTo();
m.location[X,Y-1].AutomaticAction(this);
if (move)
Y--;
}
else if ((c.Key == ConsoleKey.DownArrow) && (Y<(m.Heigth-1)))
{
move = m.location[X,Y+1].CanMoveTo();
m.location[X,Y+1].AutomaticAction(this);
if (move)
Y++;
}
else if ((c.Key == ConsoleKey.LeftArrow) && (X>0))
{
move = m.location[X-1,Y].CanMoveTo();
m.location[X-1,Y].AutomaticAction(this);
if (move)
X--;
}
else if ((c.Key == ConsoleKey.RightArrow) && (X<(m.Width-1)))
{
move = m.location[X+1,Y].CanMoveTo();
m.location[X+1,Y].AutomaticAction(this);
if (move)
X++;
}
}
public int WhoAttacks(Being creature)
{
int init1 = this.CalculateInitiative();
int init2 = creature.CalculateInitiative();
if (init1 > init2)
return 1;
if (init2>init1)
return 2;
return 0;
}
public bool Fight(Being creature)
{
Being b1 = this;
Being b2 = creature;
while(b1.Alive() && b2.Alive())
{
// decide, who will attack first
int attacker = this.WhoAttacks(creature);
if (attacker==2)
{
b2 = this;
b1 = creature;
}
// perform attack
b1.Attack(b2);
if(b2.Alive() || attacker==0) // if enemy 2 is alive, of if the attacks are parallel
{
b2.Attack(b1);
Thread.Sleep(600);
}
b1 = this;
b2 = creature;
Thread.Sleep(600);
}
Console.WriteLine("Press any key to continue");
Console.ReadKey();
return this.Alive();
}
public void Die()
{
this.CurrentCharacteristics.hitpoints = 0;
}
public void ManageInventory()
{
bool runInventory = true;
//Queue commands = new Queue();
LimitedQueue<string> messageBoard = new LimitedQueue<string>(15);
List<string> commands = new List<string>();
while (runInventory)
{
Console.Clear();
Console.Write(this.ToString());
Console.WriteLine();
Console.WriteLine("To strip item, write 'strip #of_equipment'\n" +
"To equip item, wrire 'equip #of_equipment'\n" +
"To drop item from inventory, write 'drop #of_equipment'\n" +
"To use the item, write 'use #of_equipment'\n" +
"To go back to game, press Escape\n");
foreach (string s in messageBoard)
{
Console.WriteLine(s);
}
string response = commands.ListThroughCommands();
commands.Add(response);
messageBoard.Enqueue(response);
string[] words = response.Split(' ');
int n;
switch (words[0])
{
case "close":
{
runInventory = false;
break;
}
case "strip":
{
try
{
n = int.Parse(words[1]);
messageBoard.Enqueue(this.StripItem(this.equiped.bag[n-1]));
}
catch
{
messageBoard.Enqueue("Something wrong with your output");
}
break;
}
case "equip":
{
try
{
n = int.Parse(words[1]);
messageBoard.Enqueue(this.EquipItem(this.bag.bag[n-1], true));
}
catch
{
messageBoard.Enqueue("Something wrong with your output");
}
break;
}
case "drop":
{
try
{
n = int.Parse(words[1]);
Location loc = ThisGame.dungeon.location[this.X,this.Y];
if (loc.CanDropItemOnto())
{
Item i = this.bag.bag[n-1];
messageBoard.Enqueue(this.DropItem(i));
ThisGame.dungeon.location[this.X,this.Y].DropItemOnLocation(i);
ThisGame.dungeon.location[this.X,this.Y].UpdateSymbol();
}
}
catch
{
messageBoard.Enqueue("Something wrong with your output");
}
break;
}
case "use":
{
try
{
n = int.Parse(words[1]);
Item i = this.bag.bag[n-1];
messageBoard.Enqueue(i.Use());
}
catch
{
messageBoard.Enqueue("Something wrong with your output");
}
break;
}
default:
{
messageBoard.Enqueue("Can not recognize the command");
break;
}
}
}
}
public override XmlElement ToXml(XmlDocument doc, string elementName)
{
XmlElement plr = doc.CreateElement(elementName);
plr.SetAttribute("name", Name); // set name attribute
plr.SetAttribute("x", this.X.ToString()); // set coordinates
plr.SetAttribute("y", this.Y.ToString()); // set coordinates
plr.SetAttribute("currentDungeon", this.currentDungeon); // set current dungeon name
// add characteristics
XmlElement ch = this.Characteristics.ToXml(doc, "Characteristics");
plr.AppendChild(ch);
// add Current Characteristics
XmlElement cch = this.CurrentCharacteristics.ToXml(doc, "CurrentCharacteristics");
plr.AppendChild(cch);
// add bag
XmlElement bag = this.bag.ToXml(doc, "Bag");
plr.AppendChild(bag);
// add equiped items
XmlElement equiped = this.equiped.ToXml(doc, "Equiped");
plr.AppendChild(equiped);
// add body
XmlElement body = this.Body.ToXml(doc, "Body");
plr.AppendChild(body);
return plr;
}
public void SaveAsXml()
{
ThisGame.messageLog.Enqueue("Attepmt to save the player.");
string path = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName,"files/players/");
if (!Directory.Exists(path + this.Name.ToLower()))
Directory.CreateDirectory(path + this.Name.ToLower());
XmlDocument doc = new XmlDocument();
XmlDeclaration header = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(header);
XmlElement root = this.ToXml(doc, "Player");
doc.AppendChild(root);
doc.Save( Path.Combine(path, this.Name.ToLower()+"/player.xml") );
ThisGame.messageLog.Enqueue("Save");
}
}
}