Skip to content

Instantly share code, notes, and snippets.

@ScottLilly ScottLilly/Location.cs Secret
Last active Jan 23, 2016

Embed
What would you like to do?
Lesson 21.2 - Create the vendor class and add it to locations
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public class Location
{
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Item ItemRequiredToEnter { get; set; }
public Quest QuestAvailableHere { get; set; }
public Monster MonsterLivingHere { get; set; }
public Vendor VendorWorkingHere { get; set; }
public Location LocationToNorth { get; set; }
public Location LocationToEast { get; set; }
public Location LocationToSouth { get; set; }
public Location LocationToWest { get; set; }
public Location(int id, string name, string description,
Item itemRequiredToEnter = null, Quest questAvailableHere = null, Monster monsterLivingHere = null)
{
ID = id;
Name = name;
Description = description;
ItemRequiredToEnter = itemRequiredToEnter;
QuestAvailableHere = questAvailableHere;
MonsterLivingHere = monsterLivingHere;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Engine
{
public class Vendor : INotifyPropertyChanged
{
public string Name { get; set; }
public BindingList<InventoryItem> Inventory { get; private set; }
public Vendor(string name)
{
Name = name;
Inventory = new BindingList<InventoryItem>();
}
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;
}
OnPropertyChanged("Inventory");
}
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
OnPropertyChanged("Inventory");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Engine
{
public static class World
{
public static readonly List<Item> Items = new List<Item>();
public static readonly List<Monster> Monsters = new List<Monster>();
public static readonly List<Quest> Quests = new List<Quest>();
public static readonly List<Location> Locations = new List<Location>();
public const int UNSELLABLE_ITEM_PRICE = -1;
public const int ITEM_ID_RUSTY_SWORD = 1;
public const int ITEM_ID_RAT_TAIL = 2;
public const int ITEM_ID_PIECE_OF_FUR = 3;
public const int ITEM_ID_SNAKE_FANG = 4;
public const int ITEM_ID_SNAKESKIN = 5;
public const int ITEM_ID_CLUB = 6;
public const int ITEM_ID_HEALING_POTION = 7;
public const int ITEM_ID_SPIDER_FANG = 8;
public const int ITEM_ID_SPIDER_SILK = 9;
public const int ITEM_ID_ADVENTURER_PASS = 10;
public const int MONSTER_ID_RAT = 1;
public const int MONSTER_ID_SNAKE = 2;
public const int MONSTER_ID_GIANT_SPIDER = 3;
public const int QUEST_ID_CLEAR_ALCHEMIST_GARDEN = 1;
public const int QUEST_ID_CLEAR_FARMERS_FIELD = 2;
public const int LOCATION_ID_HOME = 1;
public const int LOCATION_ID_TOWN_SQUARE = 2;
public const int LOCATION_ID_GUARD_POST = 3;
public const int LOCATION_ID_ALCHEMIST_HUT = 4;
public const int LOCATION_ID_ALCHEMISTS_GARDEN = 5;
public const int LOCATION_ID_FARMHOUSE = 6;
public const int LOCATION_ID_FARM_FIELD = 7;
public const int LOCATION_ID_BRIDGE = 8;
public const int LOCATION_ID_SPIDER_FIELD = 9;
static World()
{
PopulateItems();
PopulateMonsters();
PopulateQuests();
PopulateLocations();
}
private static void PopulateItems()
{
Items.Add(new Weapon(ITEM_ID_RUSTY_SWORD, "Rusty sword", "Rusty swords", 0, 5, 5));
Items.Add(new Item(ITEM_ID_RAT_TAIL, "Rat tail", "Rat tails", 1));
Items.Add(new Item(ITEM_ID_PIECE_OF_FUR, "Piece of fur", "Pieces of fur", 1));
Items.Add(new Item(ITEM_ID_SNAKE_FANG, "Snake fang", "Snake fangs", 1));
Items.Add(new Item(ITEM_ID_SNAKESKIN, "Snakeskin", "Snakeskins", 2));
Items.Add(new Weapon(ITEM_ID_CLUB, "Club", "Clubs", 3, 10, 8));
Items.Add(new HealingPotion(ITEM_ID_HEALING_POTION, "Healing potion", "Healing potions", 5, 3));
Items.Add(new Item(ITEM_ID_SPIDER_FANG, "Spider fang", "Spider fangs", 1));
Items.Add(new Item(ITEM_ID_SPIDER_SILK, "Spider silk", "Spider silks", 1));
Items.Add(new Item(ITEM_ID_ADVENTURER_PASS, "Adventurer pass", "Adventurer passes", UNSELLABLE_ITEM_PRICE));
}
private static void PopulateMonsters()
{
Monster rat = new Monster(MONSTER_ID_RAT, "Rat", 5, 3, 10, 3, 3);
rat.LootTable.Add(new LootItem(ItemByID(ITEM_ID_RAT_TAIL), 75, false));
rat.LootTable.Add(new LootItem(ItemByID(ITEM_ID_PIECE_OF_FUR), 75, true));
Monster snake = new Monster(MONSTER_ID_SNAKE, "Snake", 5, 3, 10, 3, 3);
snake.LootTable.Add(new LootItem(ItemByID(ITEM_ID_SNAKE_FANG), 75, false));
snake.LootTable.Add(new LootItem(ItemByID(ITEM_ID_SNAKESKIN), 75, true));
Monster giantSpider = new Monster(MONSTER_ID_GIANT_SPIDER, "Giant spider", 20, 5, 40, 10, 10);
giantSpider.LootTable.Add(new LootItem(ItemByID(ITEM_ID_SPIDER_FANG), 75, true));
giantSpider.LootTable.Add(new LootItem(ItemByID(ITEM_ID_SPIDER_SILK), 25, false));
Monsters.Add(rat);
Monsters.Add(snake);
Monsters.Add(giantSpider);
}
private static void PopulateQuests()
{
Quest clearAlchemistGarden =
new Quest(
QUEST_ID_CLEAR_ALCHEMIST_GARDEN,
"Clear the alchemist's garden",
"Kill rats in the alchemist's garden and bring back 3 rat tails. You will receive a healing potion and 10 gold pieces.", 20, 10);
clearAlchemistGarden.QuestCompletionItems.Add(new QuestCompletionItem(ItemByID(ITEM_ID_RAT_TAIL), 3));
clearAlchemistGarden.RewardItem = ItemByID(ITEM_ID_HEALING_POTION);
Quest clearFarmersField =
new Quest(
QUEST_ID_CLEAR_FARMERS_FIELD,
"Clear the farmer's field",
"Kill snakes in the farmer's field and bring back 3 snake fangs. You will receive an adventurer's pass and 20 gold pieces.", 20, 20);
clearFarmersField.QuestCompletionItems.Add(new QuestCompletionItem(ItemByID(ITEM_ID_SNAKE_FANG), 3));
clearFarmersField.RewardItem = ItemByID(ITEM_ID_ADVENTURER_PASS);
Quests.Add(clearAlchemistGarden);
Quests.Add(clearFarmersField);
}
private static void PopulateLocations()
{
// Create each location
Location home = new Location(LOCATION_ID_HOME, "Home", "Your house. You really need to clean up the place.");
Location townSquare = new Location(LOCATION_ID_TOWN_SQUARE, "Town square", "You see a fountain.");
Vendor bobTheRatCatcher = new Vendor("Bob the Rat-Catcher");
bobTheRatCatcher.AddItemToInventory(ItemByID(ITEM_ID_PIECE_OF_FUR), 5);
bobTheRatCatcher.AddItemToInventory(ItemByID(ITEM_ID_RAT_TAIL), 3);
townSquare.VendorWorkingHere = bobTheRatCatcher;
Location alchemistHut = new Location(LOCATION_ID_ALCHEMIST_HUT, "Alchemist's hut", "There are many strange plants on the shelves.");
alchemistHut.QuestAvailableHere = QuestByID(QUEST_ID_CLEAR_ALCHEMIST_GARDEN);
Location alchemistsGarden = new Location(LOCATION_ID_ALCHEMISTS_GARDEN, "Alchemist's garden", "Many plants are growing here.");
alchemistsGarden.MonsterLivingHere = MonsterByID(MONSTER_ID_RAT);
Location farmhouse = new Location(LOCATION_ID_FARMHOUSE, "Farmhouse", "There is a small farmhouse, with a farmer in front.");
farmhouse.QuestAvailableHere = QuestByID(QUEST_ID_CLEAR_FARMERS_FIELD);
Location farmersField = new Location(LOCATION_ID_FARM_FIELD, "Farmer's field", "You see rows of vegetables growing here.");
farmersField.MonsterLivingHere = MonsterByID(MONSTER_ID_SNAKE);
Location guardPost = new Location(LOCATION_ID_GUARD_POST, "Guard post", "There is a large, tough-looking guard here.", ItemByID(ITEM_ID_ADVENTURER_PASS));
Location bridge = new Location(LOCATION_ID_BRIDGE, "Bridge", "A stone bridge crosses a wide river.");
Location spiderField = new Location(LOCATION_ID_SPIDER_FIELD, "Forest", "You see spider webs covering covering the trees in this forest.");
spiderField.MonsterLivingHere = MonsterByID(MONSTER_ID_GIANT_SPIDER);
// Link the locations together
home.LocationToNorth = townSquare;
townSquare.LocationToNorth = alchemistHut;
townSquare.LocationToSouth = home;
townSquare.LocationToEast = guardPost;
townSquare.LocationToWest = farmhouse;
farmhouse.LocationToEast = townSquare;
farmhouse.LocationToWest = farmersField;
farmersField.LocationToEast = farmhouse;
alchemistHut.LocationToSouth = townSquare;
alchemistHut.LocationToNorth = alchemistsGarden;
alchemistsGarden.LocationToSouth = alchemistHut;
guardPost.LocationToEast = bridge;
guardPost.LocationToWest = townSquare;
bridge.LocationToWest = guardPost;
bridge.LocationToEast = spiderField;
spiderField.LocationToWest = bridge;
// Add the locations to the static list
Locations.Add(home);
Locations.Add(townSquare);
Locations.Add(guardPost);
Locations.Add(alchemistHut);
Locations.Add(alchemistsGarden);
Locations.Add(farmhouse);
Locations.Add(farmersField);
Locations.Add(bridge);
Locations.Add(spiderField);
}
public static Item ItemByID(int id)
{
foreach(Item item in Items)
{
if(item.ID == id)
{
return item;
}
}
return null;
}
public static Monster MonsterByID(int id)
{
foreach(Monster monster in Monsters)
{
if(monster.ID == id)
{
return monster;
}
}
return null;
}
public static Quest QuestByID(int id)
{
foreach(Quest quest in Quests)
{
if(quest.ID == id)
{
return quest;
}
}
return null;
}
public static Location LocationByID(int id)
{
foreach(Location location in Locations)
{
if(location.ID == id)
{
return location;
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.