Advertisement
Raven6990

SuperAdventure.cs

Nov 23rd, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using Engine;
  12.  
  13. namespace SuperAdventure
  14. {
  15.     public partial class SuperAdventure : Form
  16.     {
  17.         private Player _player;
  18.         private Monster _currentMonster;
  19.  
  20.         public SuperAdventure()
  21.         {
  22.             InitializeComponent();
  23.  
  24.             _player = new Player(10, 10, 20, 0, 1);
  25.             MoveTo(World.LocationByID(World.LOCATION_ID_HOME));
  26.             _player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1));
  27.  
  28.             lblHitPoints.Text = _player.CurrentHitPoints.ToString();
  29.             lblGold.Text = _player.Gold.ToString();
  30.             lblExperience.Text = _player.ExperiencePoints.ToString();
  31.             lblLevel.Text = _player.Level.ToString();
  32.         }
  33.  
  34.         private void btnNorth_Click(object sender, EventArgs e)
  35.         {
  36.             MoveTo(_player.CurrentLocation.LocationToNorth);
  37.         }
  38.  
  39.         private void btnEast_Click(object sender, EventArgs e)
  40.         {
  41.             MoveTo(_player.CurrentLocation.LocationToEast);
  42.         }
  43.  
  44.         private void btnSouth_Click(object sender, EventArgs e)
  45.         {
  46.             MoveTo(_player.CurrentLocation.LocationToSouth);
  47.         }
  48.  
  49.         private void btnWest_Click(object sender, EventArgs e)
  50.         {
  51.             MoveTo(_player.CurrentLocation.LocationToWest);
  52.         }
  53.  
  54.         private void MoveTo(Location newLocation)
  55.         {
  56.             //Does the location have any required items
  57.             if (newLocation.ItemRequiredToEnter != null)
  58.             {
  59.                 // See if the player has the required item in their inventory
  60.                 bool playerHasRequiredItem = false;
  61.  
  62.                 foreach (InventoryItem ii in _player.Inventory)
  63.                 {
  64.                     if (ii.Details.ID == newLocation.ItemRequiredToEnter.ID)
  65.                     {
  66.                         // We found the required item
  67.                         playerHasRequiredItem = true;
  68.                         break; // Exit out of the foreach loop
  69.                     }
  70.                 }
  71.  
  72.                 if (!playerHasRequiredItem)
  73.                 {
  74.                     // We didn't find the required item in their inventory, so display a message and stop trying to move
  75.                     rtbMessages.Text += "You must have a " + newLocation.ItemRequiredToEnter.Name + " to enter this location." + Environment.NewLine;
  76.                     return;
  77.                 }
  78.             }
  79.  
  80.             // Update the player's current location
  81.             _player.CurrentLocation = newLocation;
  82.  
  83.             // Show/hide available movement buttons
  84.             btnNorth.Visible = (newLocation.LocationToNorth != null);
  85.             btnEast.Visible = (newLocation.LocationToEast != null);
  86.             btnSouth.Visible = (newLocation.LocationToSouth != null);
  87.             btnWest.Visible = (newLocation.LocationToWest != null);
  88.  
  89.             // Display current location name and description
  90.             rtbLocation.Text = newLocation.Name + Environment.NewLine;
  91.             rtbLocation.Text += newLocation.Description + Environment.NewLine;
  92.  
  93.             // Completely heal the player
  94.             _player.CurrentHitPoints = _player.MaximumHitPoints;
  95.  
  96.             // Update Hit Points in UI
  97.             lblHitPoints.Text = _player.CurrentHitPoints.ToString();
  98.  
  99.             // Does the location have a quest?
  100.             if (newLocation.QuestAvailableHere != null)
  101.             {
  102.                 // See if the player already has the quest, and if they've completed it
  103.                 bool playerAlreadyHasQuest = false;
  104.                 bool playerAlreadyCompletedQuest = false;
  105.  
  106.                 foreach (PlayerQuest playerQuest in _player.Quests)
  107.                 {
  108.                     if (playerQuest.Details.ID == newLocation.QuestAvailableHere.ID)
  109.                     {
  110.                         playerAlreadyHasQuest = true;
  111.  
  112.                         if (playerQuest.IsCompleted)
  113.                         {
  114.                             playerAlreadyCompletedQuest = true;
  115.                         }
  116.                     }
  117.                 }
  118.  
  119.                 // See if the player already has the quest
  120.                 if (playerAlreadyHasQuest)
  121.                 {
  122.                     // If the player has not completed the quest yet
  123.                     if (!playerAlreadyCompletedQuest)
  124.                     {
  125.                         // See if the player has all the items needed to complete the quest
  126.                         bool playerHasAllItemsToCompleteQuest = true;
  127.  
  128.                         foreach (QuestCompletionItem qci in newLocation.QuestAvailableHere.QuestCompletionItems)
  129.                         {
  130.                             bool foundItemInPlayersInventory = false;
  131.  
  132.                             // Check each item in the player's inventory, to see if they have it, and enough of it
  133.                             foreach (InventoryItem ii in _player.Inventory)
  134.                             {
  135.                                 // The player has this item in their inventory
  136.                                 if (ii.Details.ID == qci.Details.ID)
  137.                                 {
  138.                                     foundItemInPlayersInventory = true;
  139.  
  140.                                     if (ii.Quantity < qci.Quantity)
  141.                                     {
  142.                                         // The player does not have enough of this item to complete the quest
  143.                                         playerHasAllItemsToCompleteQuest = false;
  144.  
  145.                                         // There is no reason to continue checking for the other quest completion items
  146.                                         break;
  147.                                     }
  148.  
  149.                                     // We found the item, so don't check the rest of the player's inventory
  150.                                     break;
  151.                                 }
  152.                             }
  153.  
  154.                             // If we didn't find the required item, set our variable and stop looking for other items
  155.                             if (!foundItemInPlayersInventory)
  156.                             {
  157.                                 // The player does not have this item in their inventory
  158.                                 playerHasAllItemsToCompleteQuest = false;
  159.  
  160.                                 // There is no reason to continue checking for the other quest completion items
  161.                                 break;
  162.                             }
  163.                         }
  164.  
  165.                         // The player has all items required to complete the quest
  166.                         if (playerHasAllItemsToCompleteQuest)
  167.                         {
  168.                             // Display message
  169.                             rtbMessages.Text += Environment.NewLine;
  170.                             rtbMessages.Text += "You complete the '" + newLocation.QuestAvailableHere.Name + "' quest." + Environment.NewLine;
  171.  
  172.                             // Remove quest items from inventory
  173.                             foreach (QuestCompletionItem qci in newLocation.QuestAvailableHere.QuestCompletionItems)
  174.                             {
  175.                                 foreach (InventoryItem ii in _player.Inventory)
  176.                                 {
  177.                                     if (ii.Details.ID == qci.Details.ID)
  178.                                     {
  179.                                         // Subtract the quantity from the player's inventory that was needed to complete the quest
  180.                                         ii.Quantity -= qci.Quantity;
  181.                                         break;
  182.                                     }
  183.                                 }
  184.                             }
  185.  
  186.                             // Give quest rewards
  187.                             rtbMessages.Text += "You receive: " + Environment.NewLine;
  188.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardExperiencePoints.ToString() + " experience points" + Environment.NewLine;
  189.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardGold.ToString() + " gold" + Environment.NewLine;
  190.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardItem.Name + Environment.NewLine;
  191.                             rtbMessages.Text += Environment.NewLine;
  192.  
  193.                             _player.ExperiencePoints += newLocation.QuestAvailableHere.RewardExperiencePoints;
  194.                             _player.Gold += newLocation.QuestAvailableHere.RewardGold;
  195.  
  196.                             // Add the reward item to the player's inventory
  197.                             bool addedItemToPlayerInventory = false;
  198.  
  199.                             foreach (InventoryItem ii in _player.Inventory)
  200.                             {
  201.                                 if (ii.Details.ID == newLocation.QuestAvailableHere.RewardItem.ID)
  202.                                 {
  203.                                     // They have the item in their inventory, so increase the quantity by one
  204.                                     ii.Quantity++;
  205.  
  206.                                     addedItemToPlayerInventory = true;
  207.  
  208.                                     break;
  209.                                 }
  210.                             }
  211.  
  212.                             // They didn't have the item, so add it to their inventory, with a quantity of 1
  213.                             if (!addedItemToPlayerInventory)
  214.                             {
  215.                                 _player.Inventory.Add(new InventoryItem(newLocation.QuestAvailableHere.RewardItem, 1));
  216.                             }
  217.  
  218.                             // Mark the quest as completed
  219.                             // Find the quest in the player's quest list
  220.                             foreach (PlayerQuest pq in _player.Quests)
  221.                             {
  222.                                 if (pq.Details.ID == newLocation.QuestAvailableHere.ID)
  223.                                 {
  224.                                     // Mark it as completed
  225.                                     pq.IsCompleted = true;
  226.  
  227.                                     break;
  228.                                 }
  229.                             }
  230.                         }
  231.                     }
  232.                 }
  233.                 else
  234.                 {
  235.                     // The player does not already have the quest
  236.  
  237.                     // Display the messages
  238.                     rtbMessages.Text += "You receive the " + newLocation.QuestAvailableHere.Name + " quest." + Environment.NewLine;
  239.                     rtbMessages.Text += newLocation.QuestAvailableHere.Description + Environment.NewLine;
  240.                     rtbMessages.Text += "To complete it, return with:" + Environment.NewLine;
  241.                     foreach (QuestCompletionItem qci in newLocation.QuestAvailableHere.QuestCompletionItems)
  242.                     {
  243.                         if (qci.Quantity == 1)
  244.                         {
  245.                             rtbMessages.Text += qci.Quantity.ToString() + " " + qci.Details.Name + Environment.NewLine;
  246.                         }
  247.                         else
  248.                         {
  249.                             rtbMessages.Text += qci.Quantity.ToString() + " " + qci.Details.NamePlural + Environment.NewLine;
  250.                         }
  251.                     }
  252.                     rtbMessages.Text += Environment.NewLine;
  253.  
  254.                     // Add the quest to the player's quest list
  255.                     _player.Quests.Add(new PlayerQuest(newLocation.QuestAvailableHere));
  256.                 }
  257.             }
  258.  
  259.             // Does the location have a monster?
  260.             if (newLocation.MonsterLivingHere != null)
  261.             {
  262.                 rtbMessages.Text += "You see a " + newLocation.MonsterLivingHere.Name + Environment.NewLine;
  263.  
  264.                 // Make a new monster, using the values from the standard monster in the World.Monster list
  265.                 Monster standardMonster = World.MonsterByID(newLocation.MonsterLivingHere.ID);
  266.  
  267.                 _currentMonster = new Monster(standardMonster.ID, standardMonster.Name, standardMonster.MaximumDamage,
  268.                     standardMonster.RewardExperiencePoints, standardMonster.RewardGold, standardMonster.CurrentHitPoints, standardMonster.MaximumHitPoints);
  269.  
  270.                 foreach (LootItem lootItem in standardMonster.LootTable)
  271.                 {
  272.                     _currentMonster.LootTable.Add(lootItem);
  273.                 }
  274.  
  275.                 cboWeapons.Visible = true;
  276.                 cboPotions.Visible = true;
  277.                 btnUseWeapon.Visible = true;
  278.                 btnUsePotion.Visible = true;
  279.             }
  280.             else
  281.             {
  282.                 _currentMonster = null;
  283.  
  284.                 cboWeapons.Visible = false;
  285.                 cboPotions.Visible = false;
  286.                 btnUseWeapon.Visible = false;
  287.                 btnUsePotion.Visible = false;
  288.             }
  289.         }
  290.  
  291.         // Refresh player's inventory list
  292.         private void UpdateInventoryListInUi()
  293.         {
  294.             dgvInventory.RowHeadersVisible = false;
  295.  
  296.             dgvInventory.ColumnCount = 2;
  297.             dgvInventory.Columns[0].Name = "Name";
  298.             dgvInventory.Columns[0].Width = 197;
  299.             dgvInventory.Columns[1].Name = "Quantity";
  300.  
  301.             dgvInventory.Rows.Clear();
  302.  
  303.             foreach (InventoryItem inventoryItem in _player.Inventory)
  304.             {
  305.                 if (inventoryItem.Quantity > 0)
  306.                 {
  307.                     dgvInventory.Rows.Add(new[] { inventoryItem.Details.Name, inventoryItem.Quantity.ToString() });
  308.                 }
  309.             }
  310.         }
  311.  
  312.         // Refresh player's quest list
  313.         dgv.RowHeadersVisible = false;
  314.            
  315.             dgvQuests.ColumnCount = 2;
  316.             dgvQuests.Columns[0].Name = "Name";
  317.             dgvQuests.Columns[0].Width = 197;
  318.             dgvQuests.Columns[1].Name = "Done?";
  319.  
  320.             dgvQuests.Rows.Clear();
  321.  
  322.             foreach (PlayerQuest playerQuest in _player.Quests)
  323.             {
  324.                 dgvQuests.Rows.Add(new[] { playerQuest.Details.Name, playerQuest.IsCompleted.ToString() });
  325.             }
  326.  
  327.             // Refresh player's weapons combobox
  328.             List<Weapon> weapons = new List<Weapon>();
  329.  
  330.             foreach (InventoryItem inventoryItem in _player.Inventory)
  331.             {
  332.                 if (inventoryItem.Details is Weapon)
  333.                 {
  334.                     if (inventoryItem.Quantity > 0)
  335.                     {
  336.                         weapons.Add((Weapon)inventoryItem.Details);
  337.                     }
  338.                 }
  339.             }
  340.  
  341.             if (weapons.Count == 0)
  342.             {
  343.                 // The player doesn't have any weapons, so hide the weapon combobox and "Use" button
  344.                 cboWeapons.Visible = false;
  345.                 btnUseWeapon.Visible = false;
  346.             }
  347.             else
  348.             {
  349.                 cboWeapons.DataSource = weapons;
  350.                 cboWeapons.DisplayMember = "Name";
  351.                 cboWeapons.ValueMember = "ID";
  352.  
  353.                 cboWeapons.SelectedIndex = 0;
  354.             }
  355.  
  356.             // Refresh player's potions combobox
  357.             List<HealingPotion> healingPotions = new List<HealingPotion>();
  358.  
  359.             foreach (InventoryItem inventoryItem in _player.Inventory)
  360.             {
  361.                 if (inventoryItem.Details is HealingPotion)
  362.                 {
  363.                     if (inventoryItem.Quantity > 0)
  364.                     {
  365.                         healingPotions.Add((HealingPotion)inventoryItem.Details);
  366.                     }
  367.                 }
  368.             }
  369.  
  370.             if (healingPotions.Count == 0)
  371.             {
  372.                 // The player doesn't have any potions, so hide the potion combobox and "Use" button
  373.                 cboPotions.Visible = false;
  374.                 btnUsePotion.Visible = false;
  375.             }
  376.             else
  377.             {
  378.                 cboPotions.DataSource = healingPotions;
  379.                 cboPotions.DisplayMember = "Name";
  380.                 cboPotions.ValueMember = "ID";
  381.  
  382.                 cboPotions.SelectedIndex = 0;
  383.             }
  384.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement