StatisticsValueStack.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //-----------------------------------------------------------------------------
  2. // StatisticsValueStack.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. namespace RolePlaying.Data
  10. {
  11. /// <summary>
  12. /// A collection of statistics that expire over time.
  13. /// </summary>
  14. public class StatisticsValueStack
  15. {
  16. /// <summary>
  17. /// One entry in the stack.
  18. /// </summary>
  19. private class StatisticsValueStackEntry
  20. {
  21. public StatisticsValue Statistics;
  22. public int RemainingDuration;
  23. }
  24. /// <summary>
  25. /// One entry in the stack.
  26. /// </summary>
  27. private List<StatisticsValueStackEntry> entries =
  28. new List<StatisticsValueStackEntry>();
  29. /// <summary>
  30. /// The total of all unexpired statistics in the stack.
  31. /// </summary>
  32. private StatisticsValue totalStatistics = new StatisticsValue();
  33. /// <summary>
  34. /// The total of all unexpired statistics in the stack.
  35. /// </summary>
  36. public StatisticsValue TotalStatistics
  37. {
  38. get { return totalStatistics; }
  39. }
  40. /// <summary>
  41. /// Calculate the total of all unexpired entries.
  42. /// </summary>
  43. private void CalculateTotalStatistics()
  44. {
  45. totalStatistics = new StatisticsValue();
  46. foreach (StatisticsValueStackEntry entry in entries)
  47. {
  48. totalStatistics += entry.Statistics;
  49. }
  50. }
  51. /// <summary>
  52. /// Add a new statistics, with a given duration, to the stack.
  53. /// </summary>
  54. /// <remarks>Entries with durations of 0 or less never expire.</remarks>
  55. public void AddStatistics(StatisticsValue statistics, int duration)
  56. {
  57. if (duration < 0)
  58. {
  59. throw new ArgumentOutOfRangeException("duration");
  60. }
  61. StatisticsValueStackEntry entry = new StatisticsValueStackEntry();
  62. entry.Statistics = statistics;
  63. entry.RemainingDuration = duration;
  64. entries.Add(entry);
  65. CalculateTotalStatistics();
  66. }
  67. /// <summary>
  68. /// Advance the stack and remove expired entries.
  69. /// </summary>
  70. public void Advance()
  71. {
  72. // remove the entries at 1 - they are about to go to zero
  73. // -- values that are zero now, never expire
  74. entries.RemoveAll(delegate(StatisticsValueStackEntry entry)
  75. {
  76. return (entry.RemainingDuration == 1);
  77. });
  78. // decrement all of the remaining entries.
  79. foreach (StatisticsValueStackEntry entry in entries)
  80. {
  81. entry.RemainingDuration--;
  82. }
  83. // recalculate the total
  84. CalculateTotalStatistics();
  85. }
  86. }
  87. }