daily pastebin goal
15%
SHARE
TWEET

My superadventure

a guest Aug 5th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         private HealingPotion _healingPotion;
  20.        
  21.         public SuperAdventure()
  22.         {
  23.             InitializeComponent();
  24.  
  25.             _player = new Player(10, 10, 20, 0);
  26.             MoveTo(World.LocationByID(World.LOCATION_ID_HOME));
  27.          
  28.             _player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1));
  29.             _player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_HEALING_POTION), 1));
  30.  
  31.             UpdateInventoryListInUI(); //shows weapon at the beginning, if any
  32.             UpdatePotionListInUI(); //shows potion at the beginning, if any
  33.  
  34.             UpdatePlayerStats(); //shows current stats at the beginning
  35.         }
  36.  
  37.         private void btnNorth_Click(object sender, EventArgs e)
  38.         {
  39.             MoveTo(_player.CurrentLocation.LocationToNorth);
  40.         }
  41.  
  42.         private void btnEast_Click(object sender, EventArgs e)
  43.         {
  44.             MoveTo(_player.CurrentLocation.LocationToEast);
  45.         }
  46.  
  47.         private void btnSouth_Click(object sender, EventArgs e)
  48.         {
  49.             MoveTo(_player.CurrentLocation.LocationToSouth);
  50.         }
  51.  
  52.         private void btnWest_Click(object sender, EventArgs e)
  53.         {
  54.             MoveTo(_player.CurrentLocation.LocationToWest);
  55.         }
  56.  
  57.         public void MoveTo(Location newLocation)
  58.         {
  59.             if (!_player.HasRequiredItemToEnterThisLocation(newLocation))
  60.             {
  61.                 ScrollToBottomOfMessages();
  62.                 rtbMessages.Text += "You must have a/an " + newLocation.ItemRequiredToEnter.Name + " to enter this location." + Environment.NewLine;
  63.                 return;
  64.                
  65.             }
  66.  
  67.             //Update the player's current location
  68.             _player.CurrentLocation = newLocation;
  69.  
  70.             //Show/hide available movement buttons
  71.             btnNorth.Visible = (newLocation.LocationToNorth != null);
  72.             btnEast.Visible = (newLocation.LocationToEast != null);
  73.             btnSouth.Visible = (newLocation.LocationToSouth != null);
  74.             btnWest.Visible = (newLocation.LocationToWest != null);
  75.  
  76.             //Display current location name and description
  77.             rtbLocation.Text = newLocation.Name + Environment.NewLine;
  78.             rtbLocation.Text += newLocation.Description + Environment.NewLine;
  79.  
  80.             //Does the location have a quest?
  81.             if (newLocation.QuestAvailableHere != null)
  82.             {
  83.                 //see if the player already has the quest, and if they've completed it
  84.                 bool playerAlreadyHasQuest = _player.HasThisQuest(newLocation.QuestAvailableHere);
  85.                 bool playerAlreadyHasCompletedQuest = _player.CompletedThisQuest(newLocation.QuestAvailableHere);
  86.  
  87.                 //See if the player already has the quest
  88.                 if (playerAlreadyHasQuest)
  89.                 {
  90.                     //if the player has not completed the quest yet
  91.                     if (!playerAlreadyHasCompletedQuest)
  92.                     {
  93.                         //See if the player has all the items needed to complete the quest
  94.                         bool playerHasAllTheItemsToCompleteQuest = _player.HasAllQuestCompletionItems(newLocation.QuestAvailableHere);
  95.  
  96.                         if (playerHasAllTheItemsToCompleteQuest)
  97.                         {
  98.                             //display message
  99.                             ScrollToBottomOfMessages();
  100.                             rtbMessages.Text += Environment.NewLine;
  101.                             ScrollToBottomOfMessages();
  102.                             rtbMessages.Text += "You complete the " + "«" + newLocation.QuestAvailableHere.Name + "»" + " quest." + Environment.NewLine;
  103.                            
  104.  
  105.                             //remove quest items from inventory
  106.                             _player.RemoveQuestCompletionItems(newLocation.QuestAvailableHere);
  107.  
  108.                             //give quest rewards
  109.                             ScrollToBottomOfMessages();
  110.                             rtbMessages.Text += "You receive: " + Environment.NewLine;
  111.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardExperiencePoints.ToString() + " experience points" + Environment.NewLine;
  112.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardGold.ToString() + " gold" + Environment.NewLine;
  113.                             rtbMessages.Text += newLocation.QuestAvailableHere.RewardItem.Name + Environment.NewLine;
  114.                             rtbMessages.Text += Environment.NewLine;
  115.  
  116.                             _player.ExperiencePoints += newLocation.QuestAvailableHere.RewardExperiencePoints;
  117.                             _player.Gold += newLocation.QuestAvailableHere.RewardGold;
  118.  
  119.                            
  120.  
  121.                             //Add reward item to the player's inventory
  122.                             _player.AddItemToInventory(newLocation.QuestAvailableHere.RewardItem);
  123.  
  124.                             //mark the quest as completed
  125.                             _player.MarkQuestCompleted(newLocation.QuestAvailableHere);
  126.  
  127.                             //Update UI text
  128.                             UpdateInventoryListInUI();
  129.  
  130.                             UpdatePotionListInUI();
  131.  
  132.                             lblGold.Text = _player.Gold.ToString();
  133.                             lblExperience.Text = _player.ExperiencePoints.ToString();
  134.  
  135.                         }
  136.                     }
  137.                 }
  138.                 else
  139.                 {
  140.                     //the player does not already have the quest
  141.  
  142.                     //display messages
  143.                     ScrollToBottomOfMessages();
  144.                     rtbMessages.Text += "You receive the " + "«" + newLocation.QuestAvailableHere.Name + "»" + " quest." + Environment.NewLine;
  145.                     rtbMessages.Text += newLocation.QuestAvailableHere.Description + Environment.NewLine;
  146.                     rtbMessages.Text += "To complete it, return with: " + Environment.NewLine;
  147.                     foreach (QuestCompletionItem qci in newLocation.QuestAvailableHere.QuestCompletionItems)
  148.                     {
  149.                         if (qci.Quantity == 1)
  150.                         {
  151.                             ScrollToBottomOfMessages();
  152.                             rtbMessages.Text += qci.Quantity.ToString() + " " + qci.Details.Name + Environment.NewLine;
  153.                         }
  154.                         else
  155.                         {
  156.                             ScrollToBottomOfMessages();
  157.                             rtbMessages.Text += qci.Quantity.ToString() + " " + qci.Details.NamePlural + Environment.NewLine;
  158.                         }
  159.                     }
  160.                     ScrollToBottomOfMessages();
  161.                     rtbMessages.Text += Environment.NewLine;
  162.  
  163.                     //Add the quest to the player quest's list
  164.                     _player.Quests.Add(new PlayerQuest(newLocation.QuestAvailableHere));
  165.                 }
  166.             }
  167.  
  168.             //Does the location have a monster?
  169.             if (newLocation.MonsterLivingHere != null)
  170.             {
  171.                 ScrollToBottomOfMessages();
  172.                 rtbMessages.Text += "You see a " + newLocation.MonsterLivingHere.Name + "." + Environment.NewLine;
  173.  
  174.                 //make a new monster, using the values from the standard monster in the World.Monster list
  175.                 Monster standardMonster = World.MonsterByID(newLocation.MonsterLivingHere.ID);
  176.  
  177.                 _currentMonster = new Monster(standardMonster.ID, standardMonster.Name, standardMonster.MaximumDamage,
  178.                     standardMonster.RewardExperiencePoints, standardMonster.RewardGold, standardMonster.CurrentHitPoints, standardMonster.MaximumHitPoints);
  179.  
  180.                 foreach (LootItem lootItem in standardMonster.LootTable)
  181.                 {
  182.                     _currentMonster.LootTable.Add(lootItem);
  183.                 }
  184.  
  185.                 cboWeapons.Visible = true;
  186.                 btnUseWeapon.Visible = true;
  187.  
  188.             }
  189.             else
  190.             {
  191.                 _currentMonster = null;
  192.  
  193.                 cboWeapons.Visible = false;
  194.                 btnUseWeapon.Visible = false;
  195.             }
  196.  
  197.             //refresh player's inventory list
  198.             UpdateInventoryListInUI();
  199.  
  200.             //refresh player's quest list
  201.             UpdateQuestListInUI();
  202.  
  203.             //refresh player's weapons combobox
  204.             UpdateWeaponsListInUI();
  205.  
  206.             //refresh player's potion combobox
  207.             UpdatePotionListInUI();
  208.         }
  209.  
  210.         private void UpdateInventoryListInUI()
  211.         {
  212.             dgvInventory.RowHeadersVisible = false;
  213.  
  214.             dgvInventory.ColumnCount = 2;
  215.             dgvInventory.Columns[0].Name = "Name";
  216.             dgvInventory.Columns[0].Width = 209;
  217.             dgvInventory.Columns[1].Name = "Quantity";
  218.  
  219.             dgvInventory.Rows.Clear();
  220.  
  221.             foreach (InventoryItem inventoryItem in _player.Inventory)
  222.             {
  223.                 if (inventoryItem.Quantity > 0)
  224.                 {
  225.                     dgvInventory.Rows.Add(new[] { inventoryItem.Details.Name, inventoryItem.Quantity.ToString() });
  226.                 }
  227.             }
  228.         }
  229.  
  230.         private void UpdateQuestListInUI()
  231.         {
  232.  
  233.             dgvQuests.RowHeadersVisible = false;
  234.  
  235.             dgvQuests.ColumnCount = 2;
  236.             dgvQuests.Columns[0].Name = "Name";
  237.             dgvQuests.Columns[0].Width = 209;
  238.             dgvQuests.Columns[1].Name = "Cleared?";
  239.            
  240.  
  241.             dgvQuests.Rows.Clear();
  242.  
  243.             foreach (PlayerQuest playerQuest in _player.Quests)
  244.             {
  245.                 if (playerQuest.IsCompleted)
  246.                 {
  247.                     dgvQuests.Rows.Add(new[] { playerQuest.Details.Name, "Yes" });
  248.                 }
  249.                 else
  250.                 {
  251.                     dgvQuests.Rows.Add(new[] { playerQuest.Details.Name, "No" });
  252.                 }
  253.             }
  254.         }
  255.  
  256.         private void UpdateWeaponsListInUI()
  257.         {
  258.  
  259.             List<Weapon> weapons = new List<Weapon>();
  260.  
  261.             foreach (InventoryItem inventoryItem in _player.Inventory)
  262.             {
  263.                 if (inventoryItem.Details is Weapon)
  264.                 {
  265.                     if (inventoryItem.Quantity > 0)
  266.                     {
  267.                         weapons.Add((Weapon)inventoryItem.Details);
  268.                     }
  269.                 }
  270.             }
  271.  
  272.             if (weapons.Count == 0)
  273.             {
  274.                 //the player has no weapons, hide the combobox and "use" button
  275.                 cboWeapons.Visible = false;
  276.                 btnUseWeapon.Visible = false;
  277.             }
  278.             else
  279.             {
  280.                 cboWeapons.DataSource = weapons;
  281.                 cboWeapons.DisplayMember = "Name";
  282.                 cboWeapons.ValueMember = "ID";
  283.  
  284.                 cboWeapons.SelectedIndex = 0;
  285.             }
  286.         }
  287.  
  288.         private void UpdatePotionListInUI()
  289.         {
  290.             List<HealingPotion> healingPotions = new List<HealingPotion>();
  291.  
  292.             foreach (InventoryItem inventoryItem in _player.Inventory)
  293.             {
  294.                 if (inventoryItem.Details is HealingPotion)
  295.                 {
  296.                     if (inventoryItem.Quantity > 0)
  297.                     {
  298.                         healingPotions.Add((HealingPotion)inventoryItem.Details);
  299.                     }
  300.                 }
  301.             }
  302.  
  303.             if (healingPotions.Count == 0)
  304.             {
  305.                 //the player does not have any potions, hide the combobox and "use button
  306.                 cboPotions.Visible = false;
  307.                 btnUsePotion.Visible = false;
  308.             }
  309.             else
  310.             {
  311.                 cboPotions.Visible = true;
  312.                 btnUsePotion.Visible = true;
  313.  
  314.                 cboPotions.DataSource = healingPotions;
  315.                 cboPotions.DisplayMember = "Name";
  316.                 cboPotions.ValueMember = "ID";
  317.  
  318.                 cboPotions.SelectedIndex = 0;
  319.             }
  320.         }
  321.  
  322.         private void UpdatePlayerStats()
  323.         {
  324.             // Refresh player information and inventory controls
  325.             lblHitPoints.Text = _player.CurrentHitPoints.ToString();
  326.             lblGold.Text = _player.Gold.ToString();
  327.             lblExperience.Text = _player.ExperiencePoints.ToString();
  328.             lblLevel.Text = _player.Level.ToString();
  329.         }
  330.  
  331.         private void btnUseWeapon_Click(object sender, EventArgs e)
  332.         {
  333.             //get the currently selected weapon from cboWeapons combobox
  334.             Weapon currentWeapon = (Weapon)cboWeapons.SelectedItem;
  335.  
  336.             //Determine the amount of damage to do to the monster
  337.             int damageToMonster = RandomNumberGenerator.NumberBetween(currentWeapon.MinimumDamage, currentWeapon.MaximumDamage);
  338.  
  339.             //apply damage to monster's currenthitpoints
  340.             _currentMonster.CurrentHitPoints -= damageToMonster;
  341.  
  342.             //display message
  343.             ScrollToBottomOfMessages();
  344.             rtbMessages.Text += "You hit the " + _currentMonster.Name + " for " + damageToMonster.ToString() + " points." + Environment.NewLine;
  345.  
  346.             //check if the monster is dead
  347.             if (_currentMonster.CurrentHitPoints <= 0)
  348.             {
  349.                 //Monster is dead
  350.                 ScrollToBottomOfMessages();
  351.                 rtbMessages.Text += Environment.NewLine;
  352.                 rtbMessages.Text += "You defeated the " + _currentMonster.Name + "." + Environment.NewLine;
  353.  
  354.  
  355.                 btnUseWeapon.Visible = false;
  356.                 cboWeapons.Visible = false;
  357.  
  358.                 //Give player experience for killing the monster
  359.                 _player.ExperiencePoints += _currentMonster.RewardExperiencePoints;
  360.                 ScrollToBottomOfMessages();
  361.                 rtbMessages.Text += "You receive " + _currentMonster.RewardExperiencePoints.ToString() + " experience points." + Environment.NewLine;
  362.  
  363.                 //Give player gold for killing the monster
  364.                 _player.Gold += _currentMonster.RewardGold;
  365.                 if (_currentMonster.RewardGold == 1)
  366.                 {
  367.                     ScrollToBottomOfMessages();
  368.                     rtbMessages.Text += "You get " + _currentMonster.RewardGold.ToString() + " piece of gold." + Environment.NewLine;
  369.                 }
  370.                 else
  371.                 {
  372.                     ScrollToBottomOfMessages();
  373.                     rtbMessages.Text += "You get " + _currentMonster.RewardGold.ToString() + " pieces of gold." + Environment.NewLine;
  374.                 }
  375.  
  376.                 //get random loot items from the monster
  377.                 List<InventoryItem> lootedItems = new List<InventoryItem>();
  378.  
  379.                 //Add items to the lootedItems list, comparing a random number to the drop percentage
  380.                 foreach (LootItem lootItem in _currentMonster.LootTable)
  381.                 {
  382.                     if (RandomNumberGenerator.NumberBetween(1, 100) <= lootItem.DropPercentage)
  383.                     {
  384.                         lootedItems.Add(new InventoryItem(lootItem.Details, 1));
  385.                     }
  386.                 }
  387.  
  388.                 //if no items were randomly selected, then add the default loot item(s)
  389.                 if (lootedItems.Count == 0)
  390.                 {
  391.                     foreach (LootItem lootItem in _currentMonster.LootTable)
  392.                     {
  393.                         if (lootItem.IsDefaultItem)
  394.                         {
  395.                             lootedItems.Add(new InventoryItem(lootItem.Details, 1));
  396.                         }
  397.                     }
  398.                 }
  399.  
  400.                 //add the looted items to the player's inventory
  401.                 foreach (InventoryItem inventoryItem in lootedItems)
  402.                 {
  403.                     _player.AddItemToInventory(inventoryItem.Details);
  404.  
  405.                     if (inventoryItem.Quantity == 1)
  406.                     {
  407.                         ScrollToBottomOfMessages();
  408.                         rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + " " + inventoryItem.Details.Name + Environment.NewLine;
  409.                     }
  410.                     else
  411.                     {
  412.                         ScrollToBottomOfMessages();
  413.                         rtbMessages.Text += "You loot " + inventoryItem.Quantity.ToString() + inventoryItem.Details.NamePlural + Environment.NewLine;
  414.                     }
  415.                 }
  416.  
  417.  
  418.  
  419.                 //refresh player information and inventory controls
  420.                 UpdatePlayerStats();
  421.  
  422.  
  423.                 UpdateInventoryListInUI();
  424.                 UpdateWeaponsListInUI();
  425.                 UpdatePotionListInUI();
  426.  
  427.                 _currentMonster = null;
  428.             }
  429.             else
  430.             {
  431.                 //monster is still alive
  432.  
  433.                 //determine the amount of damage the monster does to the player
  434.                 int damageToPlayer = RandomNumberGenerator.NumberBetween(1, _currentMonster.MaximumDamage);
  435.  
  436.                 //display message
  437.                 ScrollToBottomOfMessages();
  438.                 rtbMessages.Text += "The " + _currentMonster.Name + " did " + damageToPlayer.ToString() + " points of damage." + Environment.NewLine;
  439.  
  440.                 //subtract damage from player
  441.                 _player.CurrentHitPoints -= damageToPlayer;
  442.  
  443.                 //refresh player data in UI
  444.                 lblHitPoints.Text = _player.CurrentHitPoints.ToString();
  445.  
  446.                 if (_player.CurrentHitPoints <= 0)
  447.                 {
  448.                     ScrollToBottomOfMessages();
  449.                     rtbMessages.Text += "The " + _currentMonster.Name + " killed you." + Environment.NewLine;
  450.  
  451.                     ResetByDeath();
  452.                 }
  453.             }
  454.         }
  455.  
  456.         private void btnUsePotion_Click(object sender, EventArgs e)
  457.         {
  458.             //get the currently selected potion from the combobox
  459.             HealingPotion potion = (HealingPotion)cboPotions.SelectedItem;
  460.  
  461.             //Add healing amount to the player's current hit points
  462.             _player.CurrentHitPoints += potion.AmountToHeal;
  463.  
  464.             //current hit points cannot exceed player's max hit points
  465.             if (_player.CurrentHitPoints > _player.MaximumHitPoints)
  466.             {
  467.                 _player.CurrentHitPoints = _player.MaximumHitPoints;
  468.             }
  469.  
  470.             //remove the potion from the player's inventory
  471.             foreach (InventoryItem ii in _player.Inventory)
  472.             {
  473.                 if (ii.Details.ID == potion.ID)
  474.                 {
  475.                     ii.Quantity--;
  476.                     break;
  477.                 }
  478.             }
  479.             //display message
  480.             ScrollToBottomOfMessages();
  481.             rtbMessages.Text += "You drink a " + potion.Name + " that replenishes " + potion.AmountToHeal.ToString() + " hit points." + Environment.NewLine;
  482.  
  483.             if (_currentMonster != null)
  484.             {
  485.                 //monster gets their turn to attack
  486.  
  487.                 //determine the amount of damage the monster does to the player
  488.                 int damageToPlayer = RandomNumberGenerator.NumberBetween(0, _currentMonster.MaximumDamage);
  489.  
  490.                 //display message
  491.                 ScrollToBottomOfMessages();
  492.                 rtbMessages.Text += "The " + _currentMonster.Name + " did " + damageToPlayer.ToString() + " points of damage." + Environment.NewLine;
  493.  
  494.                 //subtract damage from player
  495.                 _player.CurrentHitPoints -= damageToPlayer;
  496.  
  497.                 if (_player.CurrentHitPoints <= 0)
  498.                 {
  499.                     ScrollToBottomOfMessages();
  500.                     rtbMessages.Text += "The " + _currentMonster.Name + " killed you." + Environment.NewLine;
  501.  
  502.                     ResetByDeath();
  503.  
  504.                 }
  505.             }
  506.  
  507.             //refresh player data in UI
  508.             lblHitPoints.Text = _player.CurrentHitPoints.ToString();
  509.             UpdateInventoryListInUI();
  510.             UpdatePotionListInUI();
  511.         }
  512.  
  513.         public void ResetByDeath()
  514.            
  515.         {
  516.             List<PlayerQuest> previousQuests = _player.Quests;
  517.             List<InventoryItem> previousInventory = _player.Inventory;
  518.             List<InventoryItem> itemsToEnterPlaces = new List<InventoryItem>();
  519.            
  520.             foreach (InventoryItem ii in previousInventory)
  521.             {
  522.                foreach (Location location in World.Locations)
  523.                 {
  524.                    if(location.ItemRequiredToEnter != null)
  525.                     {
  526.                         if(ii.Details == location.ItemRequiredToEnter)
  527.                         {
  528.                             itemsToEnterPlaces.Add(ii); //stores items that are required to enter locations
  529.                         }
  530.                     }
  531.                 }
  532.             }
  533.  
  534.             _player = new Player(_player.CurrentHitPoints, _player.MaximumHitPoints, 20, _player.ExperiencePoints);
  535.             _player.CurrentHitPoints = _player.MaximumHitPoints;
  536.             _player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_RUSTY_SWORD), 1));
  537.             _player.Inventory.Add(new InventoryItem(World.ItemByID(World.ITEM_ID_HEALING_POTION), 1));
  538.  
  539.  
  540.             foreach(InventoryItem item in itemsToEnterPlaces)
  541.             {
  542.                 _player.Inventory.Add(item); //keeps only items that are required to enter places
  543.             }
  544.  
  545.  
  546.             _player.Quests = previousQuests; //keeps quest list and quests done when you die
  547.  
  548.             UpdateInventoryListInUI();
  549.             UpdatePotionListInUI();
  550.             UpdateQuestListInUI();
  551.  
  552.             MoveTo(World.LocationByID(World.LOCATION_ID_HOME));
  553.  
  554.             UpdatePlayerStats();
  555.  
  556.  
  557.             ScrollToBottomOfMessages();
  558.             rtbMessages.Text += Environment.NewLine + Environment.NewLine + Environment.NewLine;
  559.         }
  560.  
  561.         private void ScrollToBottomOfMessages()
  562.         {
  563.             rtbMessages.SelectionStart = rtbMessages.Text.Length;
  564.             rtbMessages.ScrollToCaret();
  565.         }
  566.     }
  567. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top