#region File Description //----------------------------------------------------------------------------- // StatisticsValueStack.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; #endregion namespace RolePlayingGameData { /// /// A collection of statistics that expire over time. /// public class StatisticsValueStack { #region Entry List /// /// One entry in the stack. /// private class StatisticsValueStackEntry { public StatisticsValue Statistics; public int RemainingDuration; } /// /// One entry in the stack. /// private List entries = new List(); #endregion #region Totals /// /// The total of all unexpired statistics in the stack. /// private StatisticsValue totalStatistics = new StatisticsValue(); /// /// The total of all unexpired statistics in the stack. /// public StatisticsValue TotalStatistics { get { return totalStatistics; } } /// /// Calculate the total of all unexpired entries. /// private void CalculateTotalStatistics() { totalStatistics = new StatisticsValue(); foreach (StatisticsValueStackEntry entry in entries) { totalStatistics += entry.Statistics; } } #endregion /// /// Add a new statistics, with a given duration, to the stack. /// /// Entries with durations of 0 or less never expire. public void AddStatistics(StatisticsValue statistics, int duration) { if (duration < 0) { throw new ArgumentOutOfRangeException("duration"); } StatisticsValueStackEntry entry = new StatisticsValueStackEntry(); entry.Statistics = statistics; entry.RemainingDuration = duration; entries.Add(entry); CalculateTotalStatistics(); } /// /// Advance the stack and remove expired entries. /// public void Advance() { // remove the entries at 1 - they are about to go to zero // -- values that are zero now, never expire entries.RemoveAll(delegate(StatisticsValueStackEntry entry) { return (entry.RemainingDuration == 1); }); // decrement all of the remaining entries. foreach (StatisticsValueStackEntry entry in entries) { entry.RemainingDuration--; } // recalculate the total CalculateTotalStatistics(); } } }