#region File Description
//-----------------------------------------------------------------------------
// Gear.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.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
#endregion
namespace RolePlayingGameData
{
///
/// An inventory element - items, equipment, etc.
///
#if !XBOX
[DebuggerDisplay("Name = {name}")]
#endif
public abstract class Gear : ContentObject
{
#region Description Data
///
/// The name of this gear.
///
private string name;
///
/// The name of this gear.
///
public string Name
{
get { return name; }
set { name = value; }
}
///
/// The long description of this gear.
///
private string description;
///
/// The long description of this gear.
///
public string Description
{
get { return description; }
set { description = value; }
}
///
/// Builds and returns a string describing the power of this gear.
///
public virtual string GetPowerText()
{
return String.Empty;
}
#endregion
#region Value Data
///
/// The value of this gear.
///
/// If the value is less than zero, it cannot be sold.
private int goldValue;
///
/// The value of this gear.
///
/// If the value is less than zero, it cannot be sold.
public int GoldValue
{
get { return goldValue; }
set { goldValue = value; }
}
///
/// If true, the gear can be dropped. If false, it cannot ever be dropped.
///
private bool isDroppable;
///
/// If true, the gear can be dropped. If false, it cannot ever be dropped.
///
public bool IsDroppable
{
get { return isDroppable; }
set { isDroppable = value; }
}
#endregion
#region Restrictions
///
/// The minimum character level required to equip or use this gear.
///
private int minimumCharacterLevel;
///
/// The minimum character level required to equip or use this gear.
///
public int MinimumCharacterLevel
{
get { return minimumCharacterLevel; }
set { minimumCharacterLevel = value; }
}
///
/// The list of the names of all supported classes.
///
/// Class names are compared case-insensitive.
private List supportedClasses = new List();
///
/// The list of the names of all supported classes.
///
/// Class names are compared case-insensitive.
public List SupportedClasses
{
get { return supportedClasses; }
}
///
/// Check the restrictions on this object against the provided character.
///
/// True if the gear could be used, false otherwise.
public virtual bool CheckRestrictions(FightingCharacter fightingCharacter)
{
if (fightingCharacter == null)
{
throw new ArgumentNullException("fightingCharacter");
}
return ((fightingCharacter.CharacterLevel >= MinimumCharacterLevel) &&
((SupportedClasses.Count <= 0) ||
SupportedClasses.Contains(fightingCharacter.CharacterClass.Name)));
}
///
/// Builds a string describing the restrictions on this piece of gear.
///
public virtual string GetRestrictionsText()
{
StringBuilder sb = new StringBuilder();
// add the minimum character level, if any
if (MinimumCharacterLevel > 0)
{
sb.Append("Level - ");
sb.Append(MinimumCharacterLevel.ToString());
sb.Append("; ");
}
// add the classes
if (SupportedClasses.Count > 0)
{
sb.Append("Class - ");
bool firstClass = true;
foreach (string className in SupportedClasses)
{
if (firstClass)
{
firstClass = false;
}
else
{
sb.Append(",");
}
sb.Append(className);
}
}
return sb.ToString();
}
#endregion
#region Graphics Data
///
/// The content path and name of the icon for this gear.
///
private string iconTextureName;
///
/// The content path and name of the icon for this gear.
///
public string IconTextureName
{
get { return iconTextureName; }
set { iconTextureName = value; }
}
///
/// The icon texture for this gear.
///
private Texture2D iconTexture;
///
/// The icon texture for this gear.
///
[ContentSerializerIgnore]
public Texture2D IconTexture
{
get { return iconTexture; }
}
#endregion
#region Drawing Methods
///
/// Draw the icon for this gear.
///
/// The SpriteBatch object to use when drawing.
/// The position of the icon on the screen.
public virtual void DrawIcon(SpriteBatch spriteBatch, Vector2 position)
{
// check the parameters
if (spriteBatch == null)
{
throw new ArgumentNullException("spriteBatch");
}
// draw the icon, if we there is a texture for it
if (iconTexture != null)
{
spriteBatch.Draw(iconTexture, position, Color.White);
}
}
///
/// Draw the description for this gear in the space provided.
///
/// The SpriteBatch object to use when drawing.
/// The font that the text is drawn with.
/// The color of the text.
/// The position of the text on the screen.
///
/// The maximum length of a single line of text.
///
/// The maximum number of lines to draw.
public virtual void DrawDescription(SpriteBatch spriteBatch,
SpriteFont spriteFont, Color color, Vector2 position,
int maximumCharactersPerLine, int maximumLines)
{
// check the parameters
if (spriteBatch == null)
{
throw new ArgumentNullException("spriteBatch");
}
if (spriteFont == null)
{
throw new ArgumentNullException("spriteFont");
}
if (maximumLines <= 0)
{
throw new ArgumentOutOfRangeException("maximumLines");
}
if (maximumCharactersPerLine <= 0)
{
throw new ArgumentOutOfRangeException("maximumCharactersPerLine");
}
// if the string is trivial, then this is really easy
if (String.IsNullOrEmpty(description))
{
return;
}
// if the text is short enough to fit on one line, then this is still easy
if (description.Length < maximumCharactersPerLine)
{
spriteBatch.DrawString(spriteFont, description, position, color);
return;
}
// construct a new string with carriage returns
StringBuilder stringBuilder = new StringBuilder(description);
int currentLine = 0;
int newLineIndex = 0;
while (((description.Length - newLineIndex) > maximumCharactersPerLine) &&
(currentLine < maximumLines))
{
description.IndexOf(' ', 0);
int nextIndex = newLineIndex;
while (nextIndex < maximumCharactersPerLine)
{
newLineIndex = nextIndex;
nextIndex = description.IndexOf(' ', newLineIndex + 1);
}
stringBuilder.Replace(' ', '\n', newLineIndex, 1);
currentLine++;
}
// draw the string
spriteBatch.DrawString(spriteFont, stringBuilder.ToString(),
position, color);
}
#endregion
#region Content Type Reader
///
/// Reads a Gear object from the content pipeline.
///
public class GearReader : ContentTypeReader
{
///
/// Reads a Gear object from the content pipeline.
///
protected override Gear Read(ContentReader input, Gear existingInstance)
{
Gear gear = existingInstance;
if (gear == null)
{
throw new ArgumentException("Unable to create new Gear objects.");
}
gear.AssetName = input.AssetName;
// read gear settings
gear.Name = input.ReadString();
gear.Description = input.ReadString();
gear.GoldValue = input.ReadInt32();
gear.IsDroppable = input.ReadBoolean();
gear.MinimumCharacterLevel = input.ReadInt32();
gear.SupportedClasses.AddRange(input.ReadObject>());
gear.IconTextureName = input.ReadString();
gear.iconTexture = input.ContentManager.Load(
System.IO.Path.Combine(@"Textures\Gear", gear.IconTextureName));
return gear;
}
}
#endregion
}
}