//-----------------------------------------------------------------------------
// CharacterClass.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
namespace RolePlaying.Data
{
///
/// The definition of a type of character.
///
public class CharacterClass : ContentObject
{
///
/// The name of the character class.
///
private string name;
///
/// The name of the character class.
///
public string Name
{
get { return name; }
set { name = value; }
}
///
/// The initial statistics of characters that use this class.
///
private StatisticsValue initialStatistics = new StatisticsValue();
///
/// The initial statistics of characters that use this class.
///
public StatisticsValue InitialStatistics
{
get { return initialStatistics; }
set { initialStatistics = value; }
}
///
/// Statistics changes for leveling up characters that use this class.
///
private CharacterLevelingStatistics levelingStatistics;
///
/// Statistics changes for leveling up characters that use this class.
///
public CharacterLevelingStatistics LevelingStatistics
{
get { return levelingStatistics; }
set { levelingStatistics = value; }
}
///
/// Entries of the requirements and rewards for each level of this class.
///
private List levelEntries =
new List();
///
/// Entries of the requirements and rewards for each level of this class.
///
public List LevelEntries
{
get { return levelEntries; }
set { levelEntries = value; }
}
///
/// Calculate the statistics of a character of this class and the given level.
///
public StatisticsValue GetStatisticsForLevel(int characterLevel)
{
// check the parameter
if (characterLevel <= 0)
{
throw new ArgumentOutOfRangeException("characterLevel");
}
// start with the initial statistics
StatisticsValue output = initialStatistics;
// add each level of leveling statistics
for (int i = 1; i < characterLevel; i++)
{
if ((levelingStatistics.LevelsPerHealthPointsIncrease > 0) &&
((i % levelingStatistics.LevelsPerHealthPointsIncrease) == 0))
{
output.HealthPoints += levelingStatistics.HealthPointsIncrease;
}
if ((levelingStatistics.LevelsPerMagicPointsIncrease > 0) &&
((i % levelingStatistics.LevelsPerMagicPointsIncrease) == 0))
{
output.MagicPoints += levelingStatistics.MagicPointsIncrease;
}
if ((levelingStatistics.LevelsPerPhysicalOffenseIncrease > 0) &&
((i % levelingStatistics.LevelsPerPhysicalOffenseIncrease) == 0))
{
output.PhysicalOffense += levelingStatistics.PhysicalOffenseIncrease;
}
if ((levelingStatistics.LevelsPerPhysicalDefenseIncrease > 0) &&
((i % levelingStatistics.LevelsPerPhysicalDefenseIncrease) == 0))
{
output.PhysicalDefense += levelingStatistics.PhysicalDefenseIncrease;
}
if ((levelingStatistics.LevelsPerMagicalOffenseIncrease > 0) &&
((i % levelingStatistics.LevelsPerMagicalOffenseIncrease) == 0))
{
output.MagicalOffense += levelingStatistics.MagicalOffenseIncrease;
}
if ((levelingStatistics.LevelsPerMagicalDefenseIncrease > 0) &&
((i % levelingStatistics.LevelsPerMagicalDefenseIncrease) == 0))
{
output.MagicalDefense += levelingStatistics.MagicalDefenseIncrease;
}
}
return output;
}
///
/// Build a list of all spells available to a character
/// of this class and the given level.
///
public List GetAllSpellsForLevel(int characterLevel)
{
// check the parameter
if (characterLevel <= 0)
{
throw new ArgumentOutOfRangeException("characterLevel");
}
// go through each level and add the spells to the output list
List spells = new List();
for (int i = 0; i < characterLevel; i++)
{
if (i >= levelEntries.Count)
{
break;
}
// add new spells, and level up existing ones
foreach (Spell spell in levelEntries[i].Spells)
{
Spell existingSpell = spells.Find(
delegate (Spell testSpell)
{
return spell.AssetName == testSpell.AssetName;
});
if (existingSpell == null)
{
spells.Add(spell.Clone() as Spell);
}
else
{
existingSpell.Level++;
}
}
}
return spells;
}
internal static CharacterClass Load(string path)
{
var asset = XmlHelper.GetAssetElementFromXML(path);
var characterClass = new CharacterClass
{
Name = (string)asset.Element("Name"),
InitialStatistics = new StatisticsValue
{
HealthPoints = (int?)asset.Element("InitialStatistics").Element("HealthPoints") ?? 0,
MagicPoints = (int?)asset.Element("InitialStatistics").Element("MagicPoints") ?? 0,
PhysicalOffense = (int?)asset.Element("InitialStatistics").Element("PhysicalOffense") ?? 0,
PhysicalDefense = (int?)asset.Element("InitialStatistics").Element("PhysicalDefense") ?? 0,
MagicalOffense = (int?)asset.Element("InitialStatistics").Element("MagicalOffense") ?? 0,
MagicalDefense = (int?)asset.Element("InitialStatistics").Element("MagicalDefense") ?? 0,
},
LevelingStatistics = new CharacterLevelingStatistics
{
HealthPointsIncrease = (int?)asset.Element("LevelingStatistics").Element("HealthPointsIncrease") ?? 0,
LevelsPerHealthPointsIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerHealthPointsIncrease") ?? 0,
MagicPointsIncrease = (int?)asset.Element("LevelingStatistics").Element("MagicPointsIncrease") ?? 0,
LevelsPerMagicPointsIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerMagicPointsIncrease") ?? 0,
PhysicalOffenseIncrease = (int?)asset.Element("LevelingStatistics").Element("PhysicalOffenseIncrease") ?? 0,
LevelsPerPhysicalOffenseIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerPhysicalOffenseIncrease") ?? 0,
PhysicalDefenseIncrease = (int?)asset.Element("LevelingStatistics").Element("PhysicalDefenseIncrease") ?? 0,
LevelsPerPhysicalDefenseIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerPhysicalDefenseIncrease") ?? 0,
MagicalOffenseIncrease = (int?)asset.Element("LevelingStatistics").Element("MagicalOffenseIncrease") ?? 0,
LevelsPerMagicalOffenseIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerMagicalOffenseIncrease") ?? 0,
MagicalDefenseIncrease = (int?)asset.Element("LevelingStatistics").Element("MagicalDefenseIncrease") ?? 0,
LevelsPerMagicalDefenseIncrease = (int?)asset.Element("LevelingStatistics").Element("LevelsPerMagicalDefenseIncrease") ?? 0,
},
LevelEntries = asset.Element("LevelEntries")
.Elements("Item")
.Select(item => new CharacterLevelDescription
{
ExperiencePoints = (int?)item.Element("ExperiencePoints") ?? 0,
SpellContentNames = item.Element("SpellContentNames")?
.Elements("Item")
.Select(x => (string)x)
.ToList() ?? new List()
})
.ToList(),
BaseExperienceValue = (int?)asset.Element("BaseExperienceValue") ?? 0,
BaseGoldValue = (int?)asset.Element("BaseGoldValue") ?? 0
};
return characterClass;
}
///
/// The base experience value of Npcs of this character class.
///
/// Used for calculating combat rewards.
private int baseExperienceValue;
///
/// The base experience value of Npcs of this character class.
///
/// Used for calculating combat rewards.
public int BaseExperienceValue
{
get { return baseExperienceValue; }
set { baseExperienceValue = value; }
}
///
/// The base gold value of Npcs of this character class.
///
/// Used for calculating combat rewards.
private int baseGoldValue;
///
/// The base gold value of Npcs of this character class.
///
/// Used for calculating combat rewards.
public int BaseGoldValue
{
get { return baseGoldValue; }
set { baseGoldValue = value; }
}
///
/// Reads a CharacterClass object from the content pipeline.
///
public class CharacterClassReader : ContentTypeReader
{
///
/// Reads a CharacterClass object from the content pipeline.
///
protected override CharacterClass Read(ContentReader input,
CharacterClass existingInstance)
{
CharacterClass characterClass = existingInstance;
if (characterClass == null)
{
characterClass = new CharacterClass();
}
characterClass.AssetName = input.AssetName;
characterClass.Name = input.ReadString();
characterClass.InitialStatistics =
input.ReadObject();
characterClass.LevelingStatistics =
input.ReadObject();
characterClass.LevelEntries.AddRange(
input.ReadObject>());
characterClass.BaseExperienceValue = input.ReadInt32();
characterClass.BaseGoldValue = input.ReadInt32();
return characterClass;
}
}
}
}