Skip to content

Instantly share code, notes, and snippets.

@ScottLilly
Created February 26, 2017 19:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ScottLilly/a8f1bbb691b35e905b8764e74d91921c to your computer and use it in GitHub Desktop.
Trading screen fixes
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public class InventoryItem : INotifyPropertyChanged
{
private Item _details;
private int _quantity;
public Item Details
{
get { return _details; }
set
{
_details = value;
OnPropertyChanged("Details");
}
}
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
OnPropertyChanged("Quantity");
OnPropertyChanged("Description");
}
}
public InventoryItem(Item details, int quantity)
{
Details = details;
Quantity = quantity;
}
public int ItemID
{
get { return Details.ID; }
}
public int Price
{
get { return Details.Price; }
}
public string Description
{
get { return Quantity > 1 ? Details.NamePlural : Details.Name; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Engine
{
public class Player : Creature
{
//Fields
private int _gold;
private int _exp;
public int Gold
{
get { return _gold; }
set
{
_gold = value;
OnPropertyChanged("Gold");
}
}
public int EXP
{
get { return _exp; }
set
{
_exp = value;
OnPropertyChanged("Experience Points");
OnPropertyChanged("Level");
}
}
public int Level
{
get { return ((EXP / 100) + 1); }
}
public Location CurrentLocation { get; set; }
public Weapon CurrentWeapon { get; set; }
public Armor CurrentArmor { get; set; }
public BindingList<InventoryItem> Inventory { get; set; }
public BindingList<PlayerQuest> Quests { get; set; }
//Constructor
public Player(int currentHealth, int maxHealth, int currentMagic, int maxMagic, int gold, int exp) : base(currentHealth,
maxHealth, currentMagic, maxMagic)
{
Gold = gold;
EXP = exp;
Inventory = new BindingList<InventoryItem>();
Quests = new BindingList<PlayerQuest>();
}
public static Player CreateDefaultPlayer()
{
Player player = new Player(100, 100, 50, 50, 20, 0);
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1));
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_HOLY_WATER), 2)); //This item is for survival purposes in testing; remove later.
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_HEALING_POTION_2), 1));
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_SPELL_HEALING), 1));
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_SPELL_FIREBALL), 1));
player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_CLOTH_ARMOR), 1));
player.CurrentLocation = World.LocationByID(World.LOCATION_ID_START);
return player;
}
public void AddExperiencePoints(int experiencePointsToAdd)
{
EXP += experiencePointsToAdd;
MaxHealth = (Level * 10);
}
public static Player CreatePlayerFromXmlString(string xmlPlayerData)
{
try
{
XmlDocument playerData = new XmlDocument();
playerData.LoadXml(xmlPlayerData);
int currentHealth = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentHealth").InnerText);
int maxHealth = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaxHealth").InnerText);
int currentMagic = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrenMagic").InnerText);
int maxMagic = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/MaxMagic").InnerText);
int gold = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/Gold").InnerText);
int exp = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/EXP").InnerText);
Player player = new Player(currentHealth, maxHealth, currentMagic, maxMagic, gold, exp);
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);
}
if (playerData.SelectSingleNode("/Player/Stats/CurrentArmor") != null)
{
int currentArmorID = Convert.ToInt32(playerData.SelectSingleNode("/Player/Stats/CurrentArmor").InnerText);
player.CurrentArmor = (Armor)World.ItemByID(currentArmorID);
}
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
{
// If there was an error with the XML data, return a default player object
return Player.CreateDefaultPlayer();
}
}
public bool HasRequiredItemToEnterThisLocation(Location location)
{
if (location.ItemRequiredToEnter == null)
{
// There is no required item for this location, so return "true"
return true;
}
// See if the player has the required item in their inventory
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)
{
// See if the player has all the items needed to complete the quest here
foreach (QuestCompletionItem qci in quest.QuestCompletionItems)
{
// Check each item in the player's inventory, to see if they have it, and enough of it
if (!Inventory.Any(ii => ii.Details.ID == qci.Details.ID && ii.Quantity >= qci.Quantity))
{
return false;
}
}
// If we got here, then the player must have all the required items, and enough of them, to complete the quest.
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)
{
// Subtract the quantity from the player's inventory that was needed to complete the quest
item.Quantity -= qci.Quantity;
}
}
}
public void AddItemToInventory(Item itemToAdd, int quantity = 1)
{
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == itemToAdd.ID);
if (item == null)
{
// They didn't have the item, so add it to their inventory
Inventory.Add(new InventoryItem(itemToAdd, quantity));
}
else
{
// They have the item in their inventory, so increase the quantity
item.Quantity += quantity;
}
RaiseInventoryChangedEvent(itemToAdd);
}
public void RemoveItemFromInventory(Item itemToRemove, int quantity = 1)
{
InventoryItem item = Inventory.SingleOrDefault(ii => ii.Details.ID == itemToRemove.ID);
if (item == null)
{
// The item is not in the player's inventory, so ignore it.
// We might want to raise an error for this situation
}
else
{
// They have the item in their inventory, so decrease the quantity
item.Quantity -= quantity;
// Don't allow negative quantities.
// We might want to raise an error for this situation
if (item.Quantity < 0)
{
item.Quantity = 0;
}
// If the quantity is zero, remove the item from the list
if (item.Quantity == 0)
{
Inventory.Remove(item);
}
// Notify the UI that the inventory has changed
RaiseInventoryChangedEvent(itemToRemove);
}
}
private void RaiseInventoryChangedEvent(Item item)
{
if (item is Weapon)
{
OnPropertyChanged("Weapons");
}
if (item is HealingPotions)
{
OnPropertyChanged("Potions");
}
}
public void MarkQuestCompleted(Quest quest)
{
// Find the quest in the player's quest list
PlayerQuest playerQuest = Quests.SingleOrDefault(pq => pq.Details.ID == quest.ID);
if (playerQuest != null)
{
playerQuest.IsCompleted = true;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public class PlayerQuest : INotifyPropertyChanged
{
private Quest _details;
private bool _isCompleted;
public Quest Details
{
get { return _details; }
set
{
_details = value;
OnPropertyChanged("Details");
}
}
public bool IsCompleted
{
get { return _isCompleted; }
set
{
_isCompleted = value;
OnPropertyChanged("IsCompleted");
OnPropertyChanged("Name");
}
}
public string Name
{
get { return Details.Name; }
}
public PlayerQuest(Quest details)
{
Details = details;
IsCompleted = false;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment