/Samples/MacOS/RolePlayingGame/RolePlayingGameData/Characters/CharacterClass.cs

https://github.com/mauriciocirelli/MonoGame-Samples · C# · 279 lines · 157 code · 54 blank · 68 comment · 22 complexity · ca9774a7ac11e9cdfe7a9555ecebf0d7 MD5 · raw file

  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // CharacterClass.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using Microsoft.Xna.Framework.Content;
  13. #endregion
  14. namespace RolePlayingGameData
  15. {
  16. /// <summary>
  17. /// The definition of a type of character.
  18. /// </summary>
  19. public class CharacterClass : ContentObject
  20. {
  21. #region Description
  22. /// <summary>
  23. /// The name of the character class.
  24. /// </summary>
  25. private string name;
  26. /// <summary>
  27. /// The name of the character class.
  28. /// </summary>
  29. public string Name
  30. {
  31. get { return name; }
  32. set { name = value; }
  33. }
  34. #endregion
  35. #region Statistics
  36. /// <summary>
  37. /// The initial statistics of characters that use this class.
  38. /// </summary>
  39. private StatisticsValue initialStatistics = new StatisticsValue();
  40. /// <summary>
  41. /// The initial statistics of characters that use this class.
  42. /// </summary>
  43. public StatisticsValue InitialStatistics
  44. {
  45. get { return initialStatistics; }
  46. set { initialStatistics = value; }
  47. }
  48. #endregion
  49. #region Leveling
  50. /// <summary>
  51. /// Statistics changes for leveling up characters that use this class.
  52. /// </summary>
  53. private CharacterLevelingStatistics levelingStatistics;
  54. /// <summary>
  55. /// Statistics changes for leveling up characters that use this class.
  56. /// </summary>
  57. public CharacterLevelingStatistics LevelingStatistics
  58. {
  59. get { return levelingStatistics; }
  60. set { levelingStatistics = value; }
  61. }
  62. /// <summary>
  63. /// Entries of the requirements and rewards for each level of this class.
  64. /// </summary>
  65. private List<CharacterLevelDescription> levelEntries =
  66. new List<CharacterLevelDescription>();
  67. /// <summary>
  68. /// Entries of the requirements and rewards for each level of this class.
  69. /// </summary>
  70. public List<CharacterLevelDescription> LevelEntries
  71. {
  72. get { return levelEntries; }
  73. set { levelEntries = value; }
  74. }
  75. /// <summary>
  76. /// Calculate the statistics of a character of this class and the given level.
  77. /// </summary>
  78. public StatisticsValue GetStatisticsForLevel(int characterLevel)
  79. {
  80. // check the parameter
  81. if (characterLevel <= 0)
  82. {
  83. throw new ArgumentOutOfRangeException("characterLevel");
  84. }
  85. // start with the initial statistics
  86. StatisticsValue output = initialStatistics;
  87. // add each level of leveling statistics
  88. for (int i = 1; i < characterLevel; i++)
  89. {
  90. if ((levelingStatistics.LevelsPerHealthPointsIncrease > 0) &&
  91. ((i % levelingStatistics.LevelsPerHealthPointsIncrease) == 0))
  92. {
  93. output.HealthPoints += levelingStatistics.HealthPointsIncrease;
  94. }
  95. if ((levelingStatistics.LevelsPerMagicPointsIncrease > 0) &&
  96. ((i % levelingStatistics.LevelsPerMagicPointsIncrease) == 0))
  97. {
  98. output.MagicPoints += levelingStatistics.MagicPointsIncrease;
  99. }
  100. if ((levelingStatistics.LevelsPerPhysicalOffenseIncrease > 0) &&
  101. ((i % levelingStatistics.LevelsPerPhysicalOffenseIncrease) == 0))
  102. {
  103. output.PhysicalOffense += levelingStatistics.PhysicalOffenseIncrease;
  104. }
  105. if ((levelingStatistics.LevelsPerPhysicalDefenseIncrease > 0) &&
  106. ((i % levelingStatistics.LevelsPerPhysicalDefenseIncrease) == 0))
  107. {
  108. output.PhysicalDefense += levelingStatistics.PhysicalDefenseIncrease;
  109. }
  110. if ((levelingStatistics.LevelsPerMagicalOffenseIncrease > 0) &&
  111. ((i % levelingStatistics.LevelsPerMagicalOffenseIncrease) == 0))
  112. {
  113. output.MagicalOffense += levelingStatistics.MagicalOffenseIncrease;
  114. }
  115. if ((levelingStatistics.LevelsPerMagicalDefenseIncrease > 0) &&
  116. ((i % levelingStatistics.LevelsPerMagicalDefenseIncrease) == 0))
  117. {
  118. output.MagicalDefense += levelingStatistics.MagicalDefenseIncrease;
  119. }
  120. }
  121. return output;
  122. }
  123. /// <summary>
  124. /// Build a list of all spells available to a character
  125. /// of this class and the given level.
  126. /// </summary>
  127. public List<Spell> GetAllSpellsForLevel(int characterLevel)
  128. {
  129. // check the parameter
  130. if (characterLevel <= 0)
  131. {
  132. throw new ArgumentOutOfRangeException("characterLevel");
  133. }
  134. // go through each level and add the spells to the output list
  135. List<Spell> spells = new List<Spell>();
  136. for (int i = 0; i < characterLevel; i++)
  137. {
  138. if (i >= levelEntries.Count)
  139. {
  140. break;
  141. }
  142. // add new spells, and level up existing ones
  143. foreach (Spell spell in levelEntries[i].Spells)
  144. {
  145. Spell existingSpell = spells.Find(
  146. delegate(Spell testSpell)
  147. {
  148. return spell.AssetName == testSpell.AssetName;
  149. });
  150. if (existingSpell == null)
  151. {
  152. spells.Add(spell.Clone() as Spell);
  153. }
  154. else
  155. {
  156. existingSpell.Level++;
  157. }
  158. }
  159. }
  160. return spells;
  161. }
  162. #endregion
  163. #region Value Data
  164. /// <summary>
  165. /// The base experience value of Npcs of this character class.
  166. /// </summary>
  167. /// <remarks>Used for calculating combat rewards.</remarks>
  168. private int baseExperienceValue;
  169. /// <summary>
  170. /// The base experience value of Npcs of this character class.
  171. /// </summary>
  172. /// <remarks>Used for calculating combat rewards.</remarks>
  173. public int BaseExperienceValue
  174. {
  175. get { return baseExperienceValue; }
  176. set { baseExperienceValue = value; }
  177. }
  178. /// <summary>
  179. /// The base gold value of Npcs of this character class.
  180. /// </summary>
  181. /// <remarks>Used for calculating combat rewards.</remarks>
  182. private int baseGoldValue;
  183. /// <summary>
  184. /// The base gold value of Npcs of this character class.
  185. /// </summary>
  186. /// <remarks>Used for calculating combat rewards.</remarks>
  187. public int BaseGoldValue
  188. {
  189. get { return baseGoldValue; }
  190. set { baseGoldValue = value; }
  191. }
  192. #endregion
  193. #region Content Type Reader
  194. /// <summary>
  195. /// Reads a CharacterClass object from the content pipeline.
  196. /// </summary>
  197. public class CharacterClassReader : ContentTypeReader<CharacterClass>
  198. {
  199. /// <summary>
  200. /// Reads a CharacterClass object from the content pipeline.
  201. /// </summary>
  202. protected override CharacterClass Read(ContentReader input,
  203. CharacterClass existingInstance)
  204. {
  205. CharacterClass characterClass = existingInstance;
  206. if (characterClass == null)
  207. {
  208. characterClass = new CharacterClass();
  209. }
  210. characterClass.AssetName = input.AssetName;
  211. characterClass.Name = input.ReadString();
  212. characterClass.InitialStatistics =
  213. input.ReadObject<StatisticsValue>();
  214. characterClass.LevelingStatistics =
  215. input.ReadObject<CharacterLevelingStatistics>();
  216. characterClass.LevelEntries.AddRange(
  217. input.ReadObject<List<CharacterLevelDescription>>());
  218. characterClass.BaseExperienceValue = input.ReadInt32();
  219. characterClass.BaseGoldValue = input.ReadInt32();
  220. return characterClass;
  221. }
  222. }
  223. #endregion
  224. }
  225. }