-
-
Save ScottLilly/c7eab205b98bf50681214edb0d9db644 to your computer and use it in GitHub Desktop.
ArcticFlare fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Xml; | |
namespace Engine | |
{ | |
public class Player : LivingCreature | |
{ | |
public int Gold { get; set; } | |
public int ExperiencePoints { get; set; } | |
public int Level | |
{ | |
get { return ((ExperiencePoints / 100) + 1); } | |
} | |
public List<InventoryItem> Inventory { get; set; } | |
public List<PlayerQuest> Quests { get; set; } | |
public Location CurrentLocation { get; set; } | |
private Player(int currentHitPoints, int maximumHitPoints, int gold, int experiencePoitns) : | |
base (currentHitPoints, maximumHitPoints) | |
{ | |
Gold = gold; | |
ExperiencePoints = experiencePoitns; | |
Inventory = new List<InventoryItem>(); | |
Quests = new List<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 experiencePoints = Convert.ToInt32(playerData.SelectSingleNode("/player/Stats/ExperiencePoints").InnerText); | |
int gold = Convert.ToInt32(playerData.SelectSingleNode("/player/Stats/Gold").InnerText); | |
Player player = new Player(currentHitPoints, maximumHitPoints, gold, experiencePoints); | |
int currentLocationID = Convert.ToInt32(playerData.SelectSingleNode("Player/Stats/CurrentLocation").InnerText); | |
player.CurrentLocation = World.LocationByID(currentLocationID); | |
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 HasRequiredItemToEnterLocation(Location location) | |
{ | |
if (location.ItemRequiredToEnter == null) | |
{ | |
return true; | |
} | |
return Inventory.Exists(ii => ii.Details.ID == location.ItemRequiredToEnter.ID); | |
} | |
public bool HasThisQuest(Quest quest) | |
{ | |
return Quests.Exists(pq => pq.Details.ID == quest.ID); | |
} | |
public bool CompletedQuest(Quest quest) | |
{ | |
foreach (PlayerQuest pq in Quests) | |
{ | |
if (pq.Details.ID == quest.ID) | |
{ | |
return pq.IsCompleted; | |
} | |
} | |
return false; | |
} | |
public bool HasCompletionItems(Quest quest) | |
{ | |
foreach (QuestCompletionItem qci in quest.QuestCompletionItems) | |
{ | |
if (!Inventory.Exists(ii => ii.Details.ID == qci.Details.ID && ii.Quantity >= qci.Quantity)) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public void RemoveQuestItems(Quest quest) | |
{ | |
foreach (QuestCompletionItem qci in quest.QuestCompletionItems) | |
{ | |
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == qci.Details.ID); | |
if(item != null) | |
{ | |
item.Quantity -= qci.Quantity; | |
} | |
} | |
} | |
public void AddItemToInventory(Item itemToAdd) | |
{ | |
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == itemToAdd.ID); | |
if (item == null) | |
{ | |
Inventory.Add(new InventoryItem(itemToAdd, 1)); | |
} | |
else | |
{ | |
item.Quantity++; | |
} | |
} | |
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(); | |
//Create top level XML node | |
XmlNode player = playerData.CreateElement("Player"); | |
playerData.AppendChild(player); | |
//Create the 'Stats' child node to hold the other player stat nodes | |
XmlNode Stats = playerData.CreateElement("Stats"); | |
player.AppendChild(Stats); | |
//Create child nodes for 'stats' node | |
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); | |
//Create an InventoryItems node to hold inventory items | |
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); | |
} | |
//Create the "PlayerQuests" node to hold my quests | |
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; // The XML document, as a string, so we can save the data. | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Player> | |
<Stats> | |
<CurrentHitPoints>10</CurrentHitPoints> | |
<MaximumHitPoints>10</MaximumHitPoints> | |
<Gold>20</Gold> | |
<ExperiencePoints>0</ExperiencePoints> | |
<CurrentLocation>4</CurrentLocation> | |
</Stats> | |
<InventoryItems> | |
<InventoryItem ID="1" Quantity="1" /> | |
</InventoryItems> | |
<PlayerQuests> | |
<PlayerQuest ID="1" IsCompleted="False" /> | |
</PlayerQuests> | |
</Player> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Player> | |
<Stats> | |
<CurrentHitPoints>10</CurrentHitPoints> | |
<MaximumHitPoints>10</MaximumHitPoints> | |
<Gold>20</Gold> | |
<ExperiencePoints>0</ExperiencePoints> | |
<CurrentLocation>Engine.Location</CurrentLocation> | |
</Stats> | |
<InventoryItems> | |
<InventoryItem Quantity="1" ID="1"/> | |
</InventoryItems> | |
<PlayerQuests> | |
<PlayerQuest ID="1" IsCompleted="False"/> | |
</PlayerQuests> | |
</Player> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment