StatisticsValueStack.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // StatisticsValueStack.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. #endregion
  13. namespace RolePlayingGameData
  14. {
  15. /// <summary>
  16. /// A collection of statistics that expire over time.
  17. /// </summary>
  18. public class StatisticsValueStack
  19. {
  20. #region Entry List
  21. /// <summary>
  22. /// One entry in the stack.
  23. /// </summary>
  24. private class StatisticsValueStackEntry
  25. {
  26. public StatisticsValue Statistics;
  27. public int RemainingDuration;
  28. }
  29. /// <summary>
  30. /// One entry in the stack.
  31. /// </summary>
  32. private List<StatisticsValueStackEntry> entries =
  33. new List<StatisticsValueStackEntry>();
  34. #endregion
  35. #region Totals
  36. /// <summary>
  37. /// The total of all unexpired statistics in the stack.
  38. /// </summary>
  39. private StatisticsValue totalStatistics = new StatisticsValue();
  40. /// <summary>
  41. /// The total of all unexpired statistics in the stack.
  42. /// </summary>
  43. public StatisticsValue TotalStatistics
  44. {
  45. get { return totalStatistics; }
  46. }
  47. /// <summary>
  48. /// Calculate the total of all unexpired entries.
  49. /// </summary>
  50. private void CalculateTotalStatistics()
  51. {
  52. totalStatistics = new StatisticsValue();
  53. foreach (StatisticsValueStackEntry entry in entries)
  54. {
  55. totalStatistics += entry.Statistics;
  56. }
  57. }
  58. #endregion
  59. /// <summary>
  60. /// Add a new statistics, with a given duration, to the stack.
  61. /// </summary>
  62. /// <remarks>Entries with durations of 0 or less never expire.</remarks>
  63. public void AddStatistics(StatisticsValue statistics, int duration)
  64. {
  65. if (duration < 0)
  66. {
  67. throw new ArgumentOutOfRangeException("duration");
  68. }
  69. StatisticsValueStackEntry entry = new StatisticsValueStackEntry();
  70. entry.Statistics = statistics;
  71. entry.RemainingDuration = duration;
  72. entries.Add(entry);
  73. CalculateTotalStatistics();
  74. }
  75. /// <summary>
  76. /// Advance the stack and remove expired entries.
  77. /// </summary>
  78. public void Advance()
  79. {
  80. // remove the entries at 1 - they are about to go to zero
  81. // -- values that are zero now, never expire
  82. entries.RemoveAll(delegate(StatisticsValueStackEntry entry)
  83. {
  84. return (entry.RemainingDuration == 1);
  85. });
  86. // decrement all of the remaining entries.
  87. foreach (StatisticsValueStackEntry entry in entries)
  88. {
  89. entry.RemainingDuration--;
  90. }
  91. // recalculate the total
  92. CalculateTotalStatistics();
  93. }
  94. }
  95. }