Advertisement
Raven6990

SuperAdventure.cs

Nov 30th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.IO;
  6.  
  7. using Engine;
  8.  
  9. namespace SuperAdventure
  10. {
  11. public partial class SuperAdventure : Form
  12. {
  13. private const string PLAYER_DATA_FILE_NAME = "PlayerData.xml";
  14.  
  15. private Player _player;
  16.  
  17. public SuperAdventure()
  18. {
  19. InitializeComponent();
  20.  
  21. if (File.Exists(PLAYER_DATA_FILE_NAME))
  22. {
  23. _player = Player.CreatePlayerFromXmlString(File.ReadAllText(PLAYER_DATA_FILE_NAME));
  24. }
  25. else
  26. {
  27. _player = Player.CreateDefaultPlayer();
  28. }
  29.  
  30. lblHitPoints.DataBindings.Add("Text", _player, "CurrentHitPoints");
  31. lblGold.DataBindings.Add("Text", _player, "Gold");
  32. lblExperience.DataBindings.Add("Text", _player, "ExperiencePoints");
  33. lblLevel.DataBindings.Add("Text", _player, "Level");
  34.  
  35. dgvInventory.RowHeadersVisible = false;
  36. dgvInventory.AutoGenerateColumns = false;
  37.  
  38. dgvInventory.DataSource = _player.Inventory;
  39.  
  40. dgvInventory.Columns.Add(new DataGridViewTextBoxColumn
  41. {
  42. HeaderText = "Name",
  43. Width = 197,
  44. DataPropertyName = "Description"
  45. });
  46.  
  47. dgvInventory.Columns.Add(new DataGridViewTextBoxColumn
  48. {
  49. HeaderText = "Quantity",
  50. DataPropertyName = "Quantity"
  51. });
  52.  
  53. dgvQuests.RowHeadersVisible = false;
  54. dgvQuests.AutoGenerateColumns = false;
  55.  
  56. dgvQuests.DataSource = _player.Quests;
  57.  
  58. dgvQuests.Columns.Add(new DataGridViewTextBoxColumn
  59. {
  60. HeaderText = "Name",
  61. Width = 197,
  62. DataPropertyName = "Name"
  63. });
  64.  
  65. dgvQuests.Columns.Add(new DataGridViewTextBoxColumn
  66. {
  67. HeaderText = "Done?",
  68. DataPropertyName = "IsCompleted"
  69. });
  70.  
  71. cboWeapons.DataSource = _player.Weapons;
  72. cboWeapons.DisplayMember = "Name";
  73. cboWeapons.ValueMember = "Id";
  74.  
  75. if (_player.CurrentWeapon != null)
  76. {
  77. cboWeapons.SelectedItem = _player.CurrentWeapon;
  78. }
  79.  
  80. cboWeapons.SelectedIndexChanged += cboWeapons_SelectedIndexChanged;
  81.  
  82. cboPotions.DataSource = _player.Potions;
  83. cboPotions.DisplayMember = "Name";
  84. cboPotions.ValueMember = "Id";
  85.  
  86. _player.PropertyChanged += PlayerOnPropertyChanged;
  87. _player.OnMessage += DisplayMessage;
  88.  
  89. _player.MoveTo(_player.CurrentLocation);
  90. }
  91.  
  92. private void DisplayMessage(object sender, MessageEventArgs messageEventArgs)
  93. {
  94. rtbMessages.Text += messageEventArgs.Message + Environment.NewLine;
  95.  
  96. if (messageEventArgs.AddExtraNewLine)
  97. {
  98. rtbMessages.Text += Environment.NewLine;
  99. }
  100.  
  101. rtbMessages.SelectionStart = rtbMessages.Text.Length;
  102. rtbMessages.ScrollToCaret();
  103. }
  104.  
  105. private void PlayerOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
  106. {
  107. if (propertyChangedEventArgs.PropertyName == "Weapons")
  108. {
  109. cboWeapons.DataSource = _player.Weapons;
  110.  
  111. if (!_player.Weapons.Any())
  112. {
  113. cboWeapons.Visible = false;
  114. btnUseWeapon.Visible = false;
  115. }
  116. }
  117.  
  118. if (propertyChangedEventArgs.PropertyName == "Potions")
  119. {
  120. cboPotions.DataSource = _player.Potions;
  121.  
  122. if (!_player.Potions.Any())
  123. {
  124. cboPotions.Visible = false;
  125. btnUsePotion.Visible = false;
  126. }
  127. }
  128.  
  129. if (propertyChangedEventArgs.PropertyName == "CurrentLocation")
  130. {
  131. // Show/hide available movement buttons
  132. btnNorth.Visible = (_player.CurrentLocation.LocationToNorth != null);
  133. btnEast.Visible = (_player.CurrentLocation.LocationToEast != null);
  134. btnSouth.Visible = (_player.CurrentLocation.LocationToSouth != null);
  135. btnWest.Visible = (_player.CurrentLocation.LocationToWest != null);
  136. btnTrade.Visible = (_player.CurrentLocation.VendorWorkingHere != null);
  137.  
  138. // Display current location name and description
  139. rtbLocation.Text = _player.CurrentLocation.Name + Environment.NewLine;
  140. rtbLocation.Text += _player.CurrentLocation.Description + Environment.NewLine;
  141.  
  142. if (_player.CurrentLocation.MonsterLivingHere == null)
  143. {
  144. cboWeapons.Visible = false;
  145. cboPotions.Visible = false;
  146. btnUseWeapon.Visible = false;
  147. btnUsePotion.Visible = false;
  148. }
  149. else
  150. {
  151. cboWeapons.Visible = _player.Weapons.Any();
  152. cboPotions.Visible = _player.Potions.Any();
  153. btnUseWeapon.Visible = _player.Weapons.Any();
  154. btnUsePotion.Visible = _player.Potions.Any();
  155. }
  156. }
  157. }
  158.  
  159. private void btnNorth_Click(object sender, EventArgs e)
  160. {
  161. _player.MoveNorth();
  162. }
  163.  
  164. private void btnEast_Click(object sender, EventArgs e)
  165. {
  166. _player.MoveEast();
  167. }
  168.  
  169. private void btnSouth_Click(object sender, EventArgs e)
  170. {
  171. _player.MoveSouth();
  172. }
  173.  
  174. private void btnWest_Click(object sender, EventArgs e)
  175. {
  176. _player.MoveWest();
  177. }
  178.  
  179. private void btnUseWeapon_Click(object sender, EventArgs e)
  180. {
  181. // Get the currently selected weapon from the cboWeapons ComboBox
  182. Weapon currentWeapon = (Weapon)cboWeapons.SelectedItem;
  183.  
  184. _player.UseWeapon(currentWeapon);
  185. }
  186.  
  187. private void btnUsePotion_Click(object sender, EventArgs e)
  188. {
  189. // Get the currently selected potion from the combobox
  190. HealingPotion potion = (HealingPotion)cboPotions.SelectedItem;
  191.  
  192. _player.UsePotion(potion);
  193. }
  194.  
  195. private void SuperAdventure_FormClosing(object sender, FormClosingEventArgs e)
  196. {
  197. File.WriteAllText(PLAYER_DATA_FILE_NAME, _player.ToXmlString());
  198. }
  199.  
  200. private void cboWeapons_SelectedIndexChanged(object sender, EventArgs e)
  201. {
  202. _player.CurrentWeapon = (Weapon)cboWeapons.SelectedItem;
  203. }
  204.  
  205. private void btnTrade_Click(object sender, EventArgs e)
  206. {
  207. TradingScreen tradingScreen = new TradingScreen(_player);
  208. tradingScreen.StartPosition = FormStartPosition.CenterParent;
  209. tradingScreen.ShowDialog(this);
  210. }
  211. }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement