using System; using System.Collections.Generic; using System.Linq; namespace Chernobyl.Update { /// /// The root of the updateable hierarchy. /// public class RootUpdateable : Updateable, IRootUpdateable { /// /// Constructor. /// /// True if the /// root updateable should ignore the deltaTime passed /// to it and calculate it's own delta time for it /// and it's children to use. public RootUpdateable(bool calculateDeltaTime) : base(new List()) { CalculateDeltaTime = calculateDeltaTime; } /// /// Updates this updateable and it's children. Passing /// in a delta time of 0 will cause this /// /// The amount of time that has /// passed since this update was last called. public override void Update(TimeSpan deltaTime) { if (CalculateDeltaTime) { // make sure previous is initialized if (Previous.Millisecond == 0) Previous = DateTime.Now; // calculate delta time deltaTime = DateTime.Now - Previous; // save the current time for later Previous = DateTime.Now; } // We do a ToArray() here to allow the UpdateableChildren collection // to be modified in the IUpdateable.Update() methods of the children. // If we didn't do this, an exception would be thrown when a new // child was added. foreach (IUpdateable child in UpdateableChildren.ToArray()) child.Update(deltaTime); } /// /// True if this root updateable is calculating delta time and /// ignoring the delta time passed to it on update. /// public bool CalculateDeltaTime { get; set; } /// /// The previous time of last update. Used in delta time /// calculation. /// DateTime Previous { get; set; } } /// /// Used to "mark" an updateable as a root updateable. /// public interface IRootUpdateable : IUpdateable {} }