Fix for weapon and potion images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using Engine; | |
using System.IO; | |
namespace MyLittleRPG | |
{ | |
public partial class MyLittleRPG : Form | |
{ | |
private Player _player; | |
private const string PLAYER_DATA_FILE_NAME = "PlayerData.xml"; | |
public MyLittleRPG() | |
{ | |
InitializeComponent(); | |
//Create new player + giving base stats | |
if (File.Exists(PLAYER_DATA_FILE_NAME)) | |
{ | |
_player = Player.CreatePlayerFromXmlString(File.ReadAllText(PLAYER_DATA_FILE_NAME)); | |
} | |
else | |
{ | |
_player = Player.CreateDefaultPlayer(); | |
} | |
lblHitPoints.DataBindings.Add("Text", _player, "CurrentHitPoints"); | |
lblGold.DataBindings.Add("Text", _player, "Gold"); | |
lblExperience.DataBindings.Add("Text", _player, "ExperiencePoints"); | |
lblLevel.DataBindings.Add("Text", _player, "Level"); | |
dgvInventory.RowHeadersVisible = false; | |
dgvInventory.AutoGenerateColumns = false; | |
dgvInventory.DataSource = _player.Inventory; | |
dgvInventory.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Name", | |
Width = 185, | |
DataPropertyName = "Description" | |
}); | |
dgvInventory.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Quantity", | |
DataPropertyName = "Quantity" | |
}); | |
dgvQuests.RowHeadersVisible = false; | |
dgvQuests.AutoGenerateColumns = false; | |
dgvQuests.DataSource = _player.Quests; | |
dgvQuests.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Name", | |
Width = 185, | |
DataPropertyName = "Name" | |
}); | |
dgvQuests.Columns.Add(new DataGridViewTextBoxColumn | |
{ | |
HeaderText = "Done?", | |
DataPropertyName="IsCompleted" | |
}); | |
// Add the event handler BEFORE we have code | |
// that might set cboWeapons.SelectedItem. | |
// This includes the default selection of the first item, | |
// when cboWeapons.DataSource is first set. | |
cboWeapons.SelectedIndexChanged += cboWeapons_SelectedIndexChanged; | |
cboWeapons.DataSource = _player.Weapons; | |
cboWeapons.DisplayMember = "Name"; | |
cboWeapons.ValueMember = "Id"; | |
// If the player does not have a weapon, | |
// and there are weapons available, | |
// select the first weapon as the player's current weapon | |
if (_player.CurrentWeapon == null && | |
_player.Weapons.Count > 0) | |
{ | |
_player.CurrentWeapon = _player.Weapons[0]; | |
} | |
if (_player.CurrentWeapon != null) | |
{ | |
cboWeapons.SelectedItem = _player.CurrentWeapon; | |
} | |
// Add an event handler to watch if the player selects a potion. | |
// This includes the default selection of the first item, | |
// when cboPotions.DataSource is first set. | |
cboPotions.SelectedIndexChanged += cboPotions_SelectedIndexChanged; | |
cboPotions.DataSource = _player.Potions; | |
cboPotions.DisplayMember = "Name"; | |
cboPotions.ValueMember = "Id"; | |
_player.PropertyChanged += PlayerOnPropertyChanged; | |
_player.OnMessage += DisplayMessage; | |
pctPlayer.Image = Properties.Resources.HumanWithSword; | |
} | |
private void DisplayMessage(object sender, MessageEventArgs messageEventArgs) | |
{ | |
rtbMessages.Text += messageEventArgs.Message + Environment.NewLine; | |
if (messageEventArgs.AddExtraNewLine) | |
{ | |
rtbMessages.Text += Environment.NewLine; | |
} | |
rtbMessages.SelectionStart = rtbMessages.Text.Length; | |
rtbMessages.ScrollToCaret(); | |
} | |
private void PlayerOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) | |
{ | |
if (propertyChangedEventArgs.PropertyName=="Weapons") | |
{ | |
cboWeapons.DataSource = _player.Weapons; | |
if (!_player.Weapons.Any()) | |
{ | |
cboWeapons.Visible = false; | |
btnUseWeapon.Visible = false; | |
pctWeapon.Image = null; | |
} | |
} | |
if (propertyChangedEventArgs.PropertyName=="Potions") | |
{ | |
cboPotions.DataSource = _player.Potions; | |
if (!_player.Potions.Any()) | |
{ | |
cboPotions.Visible = false; | |
btnUsePotion.Visible = false; | |
pctHealing.Image = null; | |
} | |
} | |
if (propertyChangedEventArgs.PropertyName == "CurrentLocation") | |
{ | |
btnTrade.Visible = (_player.CurrentLocation.VendorWorkingHere != null); | |
//Show/hide available movement | |
btnNorth.Enabled = (_player.CurrentLocation.LocationToNorth != null); | |
btnEast.Enabled = (_player.CurrentLocation.LocationToEast != null); | |
btnSouth.Enabled = (_player.CurrentLocation.LocationToSouth != null); | |
btnWest.Enabled = (_player.CurrentLocation.LocationToWest != null); | |
//display current location name and description | |
rtbLocation.Text = _player.CurrentLocation.Name + Environment.NewLine; | |
rtbLocation.Text += _player.CurrentLocation.Description + Environment.NewLine; | |
if (_player.CurrentLocation.MonsterLivingHere==null) | |
{ | |
cboWeapons.Visible = false; | |
cboPotions.Visible = false; | |
btnUseWeapon.Visible = false; | |
btnUsePotion.Visible = false; | |
} | |
else | |
{ | |
cboWeapons.Visible = _player.Weapons.Any(); | |
cboPotions.Visible = _player.Weapons.Any(); | |
btnUseWeapon.Visible = _player.Weapons.Any(); | |
btnUsePotion.Visible = _player.Weapons.Any(); | |
} | |
} | |
} | |
private void btnNorth_Click(object sender, EventArgs e) | |
{ | |
_player.MoveNorth(); | |
ScrollToBottomMessages(); | |
} | |
private void btnEast_Click(object sender, EventArgs e) | |
{ | |
_player.MoveEast(); | |
ScrollToBottomMessages(); | |
} | |
private void btnSouth_Click(object sender, EventArgs e) | |
{ | |
_player.MoveSouth(); | |
ScrollToBottomMessages(); | |
} | |
private void btnWest_Click(object sender, EventArgs e) | |
{ | |
_player.MoveWest(); | |
ScrollToBottomMessages(); | |
} | |
private void btnUseWeapon_Click(object sender, EventArgs e) | |
{ | |
//Get the current selected weapon from cbobox | |
Weapon currentWeapon = (Weapon)cboWeapons.SelectedItem; | |
_player.UseWeapon(currentWeapon); | |
} | |
private void btnUsePotion_Click(object sender, EventArgs e) | |
{ | |
//get the current selected potion from cbobox | |
HealingPotion potion = (HealingPotion)cboPotions.SelectedItem; | |
_player.UsePotion(potion); | |
} | |
private void ScrollToBottomMessages() | |
{ | |
rtbMessages.SelectionStart = rtbMessages.Text.Length; | |
rtbMessages.ScrollToCaret(); | |
} | |
private void MyLittleRPG_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
File.WriteAllText(PLAYER_DATA_FILE_NAME, _player.ToXmlString()); | |
} | |
private void cboWeapons_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
_player.CurrentWeapon = (Weapon)cboWeapons.SelectedItem; | |
pctWeapon.Image = _player.CurrentWeapon?.Picture; | |
} | |
private void cboPotions_SelectedIndexChanged(object sender, EventArgs e) | |
{ | |
pctHealing.Image = (cboPotions.SelectedItem as HealingPotion)?.Picture; | |
} | |
private void btnTrade_Click(object sender, EventArgs e) | |
{ | |
TradingScreen tradingScreen = new TradingScreen(_player); | |
tradingScreen.StartPosition = FormStartPosition.CenterParent; | |
tradingScreen.ShowDialog(this); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment