//-----------------------------------------------------------------------------
// CharacterLevelDescription.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Xna.Framework.Content;
namespace RolePlaying.Data
{
///
/// The requirements and rewards for each level for a character class.
///
public class CharacterLevelDescription
{
///
/// The amount of additional experience necessary to achieve this level.
///
private int experiencePoints;
///
/// The amount of additional experience necessary to achieve this level.
///
public int ExperiencePoints
{
get { return experiencePoints; }
set { experiencePoints = value; }
}
///
/// The content names of the spells given to the character
/// when it reaches this level.
///
private List spellContentNames = new List();
///
/// The content names of the spells given to the character
/// when it reaches this level.
///
public List SpellContentNames
{
get { return spellContentNames; }
set { spellContentNames = value; }
}
///
/// Spells given to the character when it reaches this level.
///
private List spells = new List();
///
/// Spells given to the character when it reaches this level.
///
[ContentSerializerIgnore]
public List Spells
{
get { return spells; }
set { spells = value; }
}
///
/// Read a CharacterLevelDescription object from the content pipeline.
///
public class CharacterLevelDescriptionReader :
ContentTypeReader
{
///
/// Read a CharacterLevelDescription object from the content pipeline.
///
protected override CharacterLevelDescription Read(ContentReader input,
CharacterLevelDescription existingInstance)
{
CharacterLevelDescription desc = existingInstance;
if (desc == null)
{
desc = new CharacterLevelDescription();
}
desc.ExperiencePoints = input.ReadInt32();
desc.SpellContentNames.AddRange(input.ReadObject>());
// load all of the spells immediately
foreach (string spellContentName in desc.SpellContentNames)
{
desc.spells.Add(input.ContentManager.Load(Path.Combine("Spells", spellContentName)));
}
return desc;
}
}
internal static CharacterLevelDescription Load(XElement item, ContentManager contentManager)
{
var characterLevelDescription = new CharacterLevelDescription
{
ExperiencePoints = (int?)item.Element("ExperiencePoints") ?? 0,
SpellContentNames = item.Element("SpellContentNames")?
.Elements("Item")
.Select(x =>
{
return (string)x;
})
.ToList() ?? new List(),
};
if (characterLevelDescription.SpellContentNames.Count > 0)
{
// Load the spells for this level
characterLevelDescription.Spells = new List();
foreach (var spellContentName in characterLevelDescription.SpellContentNames)
{
var spell = Spell.Load(Path.Combine("Spells", spellContentName), contentManager);
characterLevelDescription.Spells.Add(spell);
}
}
return characterLevelDescription;
}
}
}