123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- #region File Description
- //-----------------------------------------------------------------------------
- // StoreBuyScreen.cs
- //
- // Microsoft XNA Community Game Platform
- // Copyright (C) Microsoft Corporation. All rights reserved.
- //-----------------------------------------------------------------------------
- #endregion
- #region Using Statements
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Content;
- using RolePlayingGameData;
- #endregion
- namespace RolePlaying
- {
- /// <summary>
- /// Displays the gear from a particular store and allows the user to purchase them.
- /// </summary>
- class StoreBuyScreen : ListScreen<Gear>
- {
- #region Graphics Data
- /// <summary>
- /// The left-facing quantity arrow.
- /// </summary>
- private Texture2D leftQuantityArrow;
- /// <summary>
- /// The right-facing quantity arrow.
- /// </summary>
- private Texture2D rightQuantityArrow;
- #endregion
- #region Columns
- private string nameColumnText = "Name";
- private const int nameColumnInterval = 80;
- private string powerColumnText = "Power (min, max)";
- private const int powerColumnInterval = 270;
- private string quantityColumnText = "Qty";
- private const int quantityColumnInterval = 340;
- private string priceColumnText = "Price";
- private const int priceColumnInterval = 120;
- #endregion
- #region Data Access
- /// <summary>
- /// The store whose goods are being displayed.
- /// </summary>
- private Store store;
- /// <summary>
- /// The index of the current StoreCategory.
- /// </summary>
- private int currentCategoryIndex = 0;
- /// <summary>
- /// Get the list that this screen displays.
- /// </summary>
- public override ReadOnlyCollection<Gear> GetDataList()
- {
- return
- store.StoreCategories[currentCategoryIndex].AvailableGear.AsReadOnly();
- }
- /// <summary>
- /// The selected quantity of the current entry.
- /// </summary>
- private int selectedQuantity = 0;
- /// <summary>
- /// The maximum quantity of the current entry.
- /// </summary>
- private int maximumQuantity = 0;
- /// <summary>
- /// Resets the selected quantity to the maximum value for the selected entry.
- /// </summary>
- private void ResetQuantities()
- {
- // check the indices before recalculating
- if ((currentCategoryIndex < 0) ||
- (currentCategoryIndex > store.StoreCategories.Count) ||
- (SelectedIndex < 0) || (SelectedIndex >=
- store.StoreCategories[currentCategoryIndex].AvailableGear.Count))
- {
- return;
- }
- // get the value of the selected gear
- Gear gear =
- store.StoreCategories[currentCategoryIndex].AvailableGear[SelectedIndex];
- if ((gear == null) || (gear.GoldValue <= 0))
- {
- selectedQuantity = maximumQuantity = 0;
- }
- selectedQuantity = 1;
- maximumQuantity = (int)Math.Floor(Session.Party.PartyGold /
- (gear.GoldValue * store.BuyMultiplier));
- }
-
- #endregion
- #region List Navigation
- /// <summary>
- /// Move the current selection up one entry.
- /// </summary>
- protected override void MoveCursorUp()
- {
- int oldIndex = SelectedIndex;
- base.MoveCursorUp();
- if (SelectedIndex != oldIndex)
- {
- ResetQuantities();
- }
- }
- /// <summary>
- /// Move the current selection down one entry.
- /// </summary>
- protected override void MoveCursorDown()
- {
- int oldIndex = SelectedIndex;
- base.MoveCursorDown();
- if (SelectedIndex != oldIndex)
- {
- ResetQuantities();
- }
- }
- /// <summary>
- /// Decrease the selected quantity by one.
- /// </summary>
- protected override void MoveCursorLeft()
- {
- if (maximumQuantity > 0)
- {
- // decrement the quantity, looping around if necessary
- selectedQuantity = (selectedQuantity > 1) ?
- selectedQuantity - 1 : maximumQuantity;
- }
- }
- /// <summary>
- /// Increase the selected quantity by one.
- /// </summary>
- protected override void MoveCursorRight()
- {
- if (maximumQuantity > 0)
- {
- // loop to one if the selected quantity is already at maximum.
- selectedQuantity = selectedQuantity < maximumQuantity ?
- selectedQuantity + 1 : 1;
- }
- }
- #endregion
- #region Initialization
- /// <summary>
- /// Creates a new StoreBuyScreen object for the given store.
- /// </summary>
- public StoreBuyScreen(Store store)
- : base()
- {
- // check the parameter
- if ((store == null) || (store.StoreCategories.Count <= 0))
- {
- throw new ArgumentNullException("store");
- }
- this.store = store;
- // configure the menu text
- selectButtonText = "Purchase";
- backButtonText = "Back";
- xButtonText = String.Empty;
- yButtonText = String.Empty;
- ResetMenu();
- ResetQuantities();
- }
-
- /// <summary>
- /// Load the graphics content from the content manager.
- /// </summary>
- public override void LoadContent()
- {
- base.LoadContent();
- ContentManager content = ScreenManager.Game.Content;
- leftQuantityArrow =
- content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowLeft");
- rightQuantityArrow =
- content.Load<Texture2D>(@"Textures\Buttons\QuantityArrowRight");
- }
- #endregion
- #region Input Handling
- /// <summary>
- /// Respond to the triggering of the Select action.
- /// </summary>
- protected override void SelectTriggered(Gear entry)
- {
- if (entry == null)
- {
- return;
- }
- // purchase the items if possible
- int totalPrice = (int)Math.Floor(entry.GoldValue * selectedQuantity *
- store.BuyMultiplier);
- if (totalPrice <= Session.Party.PartyGold)
- {
- Session.Party.PartyGold -= totalPrice;
- Session.Party.AddToInventory(entry, selectedQuantity);
- }
- // reset the quantities - either gold has gone down or the total was bad
- ResetQuantities();
- }
- /// <summary>
- /// Switch to the previous store category.
- /// </summary>
- protected override void PageScreenLeft()
- {
- currentCategoryIndex--;
- if (currentCategoryIndex < 0)
- {
- currentCategoryIndex = store.StoreCategories.Count - 1;
- }
- ResetMenu();
- ResetQuantities();
- }
- /// <summary>
- /// Switch to the next store category.
- /// </summary>
- protected override void PageScreenRight()
- {
- currentCategoryIndex++;
- if (currentCategoryIndex >= store.StoreCategories.Count)
- {
- currentCategoryIndex = 0;
- }
- ResetMenu();
- ResetQuantities();
- }
- /// <summary>
- /// Reset the menu title and trigger button text for the current store category.
- /// </summary>
- private void ResetMenu()
- {
- // update the title the title
- titleText = store.StoreCategories[currentCategoryIndex].Name;
- // get the left trigger text
- int index = currentCategoryIndex - 1;
- if (index < 0)
- {
- index = store.StoreCategories.Count - 1;
- }
- leftTriggerText = store.StoreCategories[index].Name;
- // get the right trigger text
- index = currentCategoryIndex + 1;
- if (index >= store.StoreCategories.Count)
- {
- index = 0;
- }
- rightTriggerText = store.StoreCategories[index].Name;
- }
- #endregion
- #region Drawing
- /// <summary>
- /// Draw the entry at the given position in the list.
- /// </summary>
- /// <param name="contentEntry">The entry to draw.</param>
- /// <param name="position">The position to draw the entry at.</param>
- /// <param name="isSelected">If true, this item is selected.</param>
- protected override void DrawEntry(Gear entry, Vector2 position,
- bool isSelected)
- {
- // check the parameter
- if (entry == null)
- {
- throw new ArgumentNullException("entry");
- }
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- Vector2 drawPosition = position;
- // draw the icon
- spriteBatch.Draw(entry.IconTexture, drawPosition + iconOffset, Color.White);
- // draw the name
- Color color = isSelected ? Fonts.HighlightColor : Fonts.DisplayColor;
- drawPosition.Y += listLineSpacing / 4;
- drawPosition.X += nameColumnInterval;
- spriteBatch.DrawString(Fonts.GearInfoFont, entry.Name, drawPosition, color);
- // draw the power
- drawPosition.X += powerColumnInterval;
- string powerText = entry.GetPowerText();
- Vector2 powerTextSize = Fonts.GearInfoFont.MeasureString(powerText);
- Vector2 powerPosition = drawPosition;
- powerPosition.Y -= (float)Math.Ceiling((powerTextSize.Y - 30f) / 2);
- spriteBatch.DrawString(Fonts.GearInfoFont, powerText,
- powerPosition, color);
- // draw the quantity
- drawPosition.X += quantityColumnInterval;
- int priceSingle = (int)Math.Floor(entry.GoldValue * store.BuyMultiplier);
- if (isSelected)
- {
- if (priceSingle <= Session.Party.PartyGold)
- {
- Vector2 quantityPosition = drawPosition;
- // draw the left selection arrow
- quantityPosition.X -= leftQuantityArrow.Width;
- spriteBatch.Draw(leftQuantityArrow,
- new Vector2(quantityPosition.X, quantityPosition.Y - 4),
- Color.White);
- quantityPosition.X += leftQuantityArrow.Width;
- // draw the selected quantity ratio
- string quantityText = selectedQuantity.ToString() + "/" +
- maximumQuantity.ToString();
- spriteBatch.DrawString(Fonts.GearInfoFont, quantityText,
- quantityPosition, color);
- quantityPosition.X +=
- Fonts.GearInfoFont.MeasureString(quantityText).X;
- // draw the right selection arrow
- spriteBatch.Draw(rightQuantityArrow,
- new Vector2(quantityPosition.X, quantityPosition.Y - 4),
- Color.White);
- quantityPosition.X += rightQuantityArrow.Width;
- // draw the purchase button
- selectButtonText = "Purchase";
- }
- else
- {
- // turn off the purchase button
- selectButtonText = String.Empty;
- }
- }
- // draw the price
- drawPosition.X += priceColumnInterval;
- string priceText = String.Empty;
- if (isSelected)
- {
- int totalPrice = (int)Math.Floor(entry.GoldValue * store.BuyMultiplier) *
- selectedQuantity;
- priceText = totalPrice.ToString();
- }
- else
- {
- priceText = ((int)Math.Floor(entry.GoldValue *
- store.BuyMultiplier)).ToString();
- }
- spriteBatch.DrawString(Fonts.GearInfoFont, priceText,
- drawPosition, color);
- }
- /// <summary>
- /// Draw the description of the selected item.
- /// </summary>
- protected override void DrawSelectedDescription(Gear entry)
- {
- // check the parameter
- if (entry == null)
- {
- throw new ArgumentNullException("entry");
- }
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- Vector2 position = descriptionTextPosition;
- // draw the description
- // -- it's up to the content owner to fit the description
- string text = entry.Description;
- if (!String.IsNullOrEmpty(text))
- {
- spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
- Fonts.DescriptionColor);
- position.Y += Fonts.DescriptionFont.LineSpacing;
- }
- // draw the modifiers
- Equipment equipment = entry as Equipment;
- if (equipment != null)
- {
- text = equipment.OwnerBuffStatistics.GetModifierString();
- if (!String.IsNullOrEmpty(text))
- {
- spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
- Fonts.DescriptionColor);
- position.Y += Fonts.DescriptionFont.LineSpacing;
- }
- }
- // draw the restrictions
- text = entry.GetRestrictionsText();
- if (!String.IsNullOrEmpty(text))
- {
- spriteBatch.DrawString(Fonts.DescriptionFont, text, position,
- Fonts.DescriptionColor);
- position.Y += Fonts.DescriptionFont.LineSpacing;
- }
- }
- /// <summary>
- /// Draw the column headers above the list.
- /// </summary>
- protected override void DrawColumnHeaders()
- {
- SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
- Vector2 position = listEntryStartPosition;
- position.X += nameColumnInterval;
- if (!String.IsNullOrEmpty(nameColumnText))
- {
- spriteBatch.DrawString(Fonts.CaptionFont, nameColumnText, position,
- Fonts.CaptionColor);
- }
- position.X += powerColumnInterval;
- if (!String.IsNullOrEmpty(powerColumnText))
- {
- spriteBatch.DrawString(Fonts.CaptionFont, powerColumnText, position,
- Fonts.CaptionColor);
- }
- position.X += quantityColumnInterval;
- if (!String.IsNullOrEmpty(quantityColumnText))
- {
- spriteBatch.DrawString(Fonts.CaptionFont, quantityColumnText, position,
- Fonts.CaptionColor);
- }
- position.X += priceColumnInterval;
- if (!String.IsNullOrEmpty(priceColumnText))
- {
- spriteBatch.DrawString(Fonts.CaptionFont, priceColumnText, position,
- Fonts.CaptionColor);
- }
- }
- #endregion
- }
- }
|