-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cs
361 lines (295 loc) · 11.6 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.ComponentModel;
namespace Engine
{
public class Player : LivingCreature
{
private int _gold;
private int _experiencePoints;
public int Gold
{
get
{
return _gold;
}
set
{
_gold = value;
OnPropertyChanged("Gold");
}
}
public int ExperiencePoints
{
get
{
return _experiencePoints;
}
private set
{
_experiencePoints = value;
OnPropertyChanged("ExperiencePoints");
OnPropertyChanged("Level");
}
}
public int Level
{
get
{
return ((ExperiencePoints / 100) + 1);
}
}
public BindingList<InventoryItem> Inventory
{
get;
set;
}
public BindingList<PlayerQuest> Quests
{
get;
set;
}
public List<Weapon> Weapons
{
get
{
return Inventory.Where(x => x.Details is Weapon).Select(x => x.Details as Weapon).ToList();
}
}
public List<HealingPotion> Potions
{
get
{
return Inventory.Where(x => x.Details is HealingPotion).Select(x => x.Details as HealingPotion).ToList();
}
}
public Location CurrentLocation
{
get;
set;
}
public Weapon CurrentWeapon
{
get;
set;
}
private Player(int CurrentHitPoints, int MaximumHitPoints,int Gold, int ExperiencePoints):base(CurrentHitPoints, MaximumHitPoints)
{
this.Gold = Gold;
this.ExperiencePoints = ExperiencePoints;
Inventory = new BindingList<InventoryItem>();
Quests = new BindingList<PlayerQuest>();
}
public static Player CreateDefaultPlayer()
{
Player player = new Player(10,10,20,0);
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD),1));
player.CurrentLocation = World.LocationByID(World.LOCATION_ID_HOME);
return player;
}
public static Player CreatePlayerFromXmlString(string xmlPlayerData)
{
try
{
XmlDocument playerData = new XmlDocument();
playerData.LoadXml(xmlPlayerData);
int currentHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHitPoints").InnerText);
int maximumHitPoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaximumHitPoints").InnerText);
int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText);
int experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/ExperiencePoints").InnerText);
Player player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints);
int currentLocationID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentLocation").InnerText);
player.CurrentLocation = World.LocationByID(currentLocationID);
if(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon")!= null)
{
int currentWeaponID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentWeapon").InnerText);
player.CurrentWeapon = (Weapon)World.ItemByID(currentWeaponID);
}
foreach (XmlNode node in playerData.SelectNodes("/Player/InventoryItems/InventoryItem"))
{
int id = Convert.ToInt32(node.Attributes["ID"].Value);
int quantity = Convert.ToInt32(node.Attributes["Quantity"].Value);
for(int i = 0;i< quantity;i++)
{
player.AddItemToInventory(World.ItemByID(id));
}
}
foreach(XmlNode node in playerData.SelectNodes("/Player/PlayerQuests/PlayerQuest"))
{
int id = Convert.ToInt32(node.Attributes["ID"].Value);
bool isCompleted = Convert.ToBoolean(node.Attributes["IsCompleted"].Value);
PlayerQuest playerQuest = new PlayerQuest(World.QuestByID(id));
playerQuest.IsCompleted = isCompleted;
player.Quests.Add(playerQuest);
}
return player;
}
catch
{
return Player.CreateDefaultPlayer();
}
}
public bool HasRequiredItemToEnterThisLocation(Location location)
{
if(location.ItemRequiredToEnter== null)
{
return true;
}
return Inventory.Any(ii => ii.Details.ID == location.ItemRequiredToEnter.ID);
}
public bool HasThisQuest(Quest quest)
{
return Quests.Any(pq => pq.Details.ID == quest.ID);
}
public bool CompletedThisQuest(Quest quest)
{
foreach(PlayerQuest playerQuest in Quests)
{
if(playerQuest.Details.ID==quest.ID)
{
return playerQuest.IsCompleted;
}
}
return false;
}
public bool HasAllQuestCompletionItems(Quest quest)
{
foreach(QuestCompletionItem qci in quest.QuestCompletionItems)
{
if(!Inventory.Any(ii => ii.Details.ID == qci.Details.ID && ii.Quantity >= qci.Quantity))
{
return false;
}
}
return true;
}
public void RemoveQuestCompletionItems(Quest quest)
{
foreach(QuestCompletionItem qci in quest.QuestCompletionItems)
{
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == qci.Details.ID);
if(item != null)
{
RemoveItemFromInventory(item.Details,qci.Quantity);
}
}
}
public void AddItemToInventory(Item itemToAdd, int quantity = 1)
{
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == itemToAdd.ID);
if(item== null)
{
Inventory.Add(new InventoryItem(itemToAdd, quantity));
}
else
{
item.Quantity+= quantity;
}
RaiseInventoryChangedEvent(itemToAdd);
}
public void MarkQuestCompleted(Quest quest)
{
PlayerQuest playerQuest = Quests.SingleOrDefault(pq => pq.Details.ID == quest.ID);
if(playerQuest != null)
{
playerQuest.IsCompleted = true;
}
}
public String ToXmlString()
{
XmlDocument playerData = new XmlDocument();
XmlNode player = playerData.CreateElement("Player");
playerData.AppendChild(player);
XmlNode stats = playerData.CreateElement("Stats");
player.AppendChild(stats);
XmlNode currentHitPoints = playerData.CreateElement("CurrentHitPoints");
currentHitPoints.AppendChild(playerData.CreateTextNode(this.CurrentHitPoints.ToString()));
stats.AppendChild(currentHitPoints);
XmlNode maximumHitPoints = playerData.CreateElement("MaximumHitPoints");
maximumHitPoints.AppendChild(playerData.CreateTextNode(this.MaximumHitPoints.ToString()));
stats.AppendChild(maximumHitPoints);
XmlNode gold = playerData.CreateElement("Gold");
gold.AppendChild(playerData.CreateTextNode(this.Gold.ToString()));
stats.AppendChild(gold);
XmlNode experiencePoints = playerData.CreateElement("ExperiencePoints");
experiencePoints.AppendChild(playerData.CreateTextNode(this.ExperiencePoints.ToString()));
stats.AppendChild(experiencePoints);
XmlNode currentLocation = playerData.CreateElement("CurrentLocation");
currentLocation.AppendChild(playerData.CreateTextNode(this.CurrentLocation.ID.ToString()));
stats.AppendChild(currentLocation);
if(CurrentWeapon!= null)
{
XmlNode currentWeapon = playerData.CreateElement("CurrentWeapon");
currentWeapon.AppendChild(playerData.CreateTextNode(this.CurrentWeapon.ID.ToString()));
stats.AppendChild(currentWeapon);
}
XmlNode inventoryItems = playerData.CreateElement("InventoryItems");
player.AppendChild(inventoryItems);
foreach(InventoryItem item in this.Inventory)
{
XmlNode inventoryItem = playerData.CreateElement("InventoryItem");
XmlAttribute idAttribute = playerData.CreateAttribute("ID");
idAttribute.Value = item.Details.ID.ToString();
inventoryItem.Attributes.Append(idAttribute);
XmlAttribute quantityAttribute = playerData.CreateAttribute("Quantity");
quantityAttribute.Value = item.Quantity.ToString();
inventoryItem.Attributes.Append(quantityAttribute);
inventoryItems.AppendChild(inventoryItem);
}
XmlNode playerQuests = playerData.CreateElement("PlayerQuests");
player.AppendChild(playerQuests);
foreach(PlayerQuest quest in this.Quests)
{
XmlNode playerQuest = playerData.CreateElement("PlayerQuest");
XmlAttribute idAttribute = playerData.CreateAttribute("ID");
idAttribute.Value = quest.Details.ID.ToString();
playerQuest.Attributes.Append(idAttribute);
XmlAttribute isCompletedAttribute = playerData.CreateAttribute("IsCompleted");
isCompletedAttribute.Value = quest.IsCompleted.ToString();
playerQuest.Attributes.Append(isCompletedAttribute);
playerQuests.AppendChild(playerQuest);
}
return playerData.InnerXml;
}
public void AddExperiencePoints(int experiencePointsToAdd)
{
ExperiencePoints += experiencePointsToAdd;
MaximumHitPoints = Level * 10;
}
public void RaiseInventoryChangedEvent(Item item)
{
if(item is Weapon)
{
OnPropertyChanged("Weapons");
}
if(item is HealingPotion)
{
OnPropertyChanged("Potions");
}
}
public void RemoveItemFromInventory(Item itemToRemove, int quantity = 1)
{
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == itemToRemove.ID);
if(item == null)
{
}
else
{
item.Quantity -= quantity;
if(item.Quantity<0)
{
item.Quantity = 0;
}
if(item.Quantity ==0)
{
Inventory.Remove(item);
}
RaiseInventoryChangedEvent(itemToRemove);
}
}
}
}