Advertisement
errur

TimerManager.cs

Nov 13th, 2015
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6.  
  7. namespace notContra
  8. {
  9.     /// <summary>
  10.     /// A class to manage Timers. Puts timers into a list. Ticks every timer in the list every game update.
  11.     /// When the timer reaches it's time limit it gets added to the toRemove list and then removed from timers.
  12.     /// The toRemove list is cleared after.
  13.     /// </summary>
  14.     class TimerManager
  15.     {
  16.         List<Timer> timers = new List<Timer>();
  17.         List<Timer> toRemove = new List<Timer>();
  18.         int nrToRemove = 0;
  19.         int nrOfTimers = 0;
  20.  
  21.         public int NrOfTimers
  22.         {
  23.             get
  24.             {
  25.                 return nrOfTimers;
  26.             }
  27.         }
  28.  
  29.         public TimerManager() { }
  30.  
  31.         public void Wait(int timeToWait)
  32.         {
  33.             Add(new Timer(timeToWait));
  34.         }
  35.         public void Wait(float timeToWait)
  36.         {
  37.             Add(new Timer(timeToWait));
  38.         }
  39.  
  40.         void Add(Timer timer)
  41.         {
  42.             timers.Add(timer);
  43.             nrOfTimers++;
  44.         }
  45.  
  46.         public void Update(GameTime gameTime)
  47.         {
  48.             foreach(Timer element in timers)
  49.             {
  50.                 element.Tick(gameTime);
  51.                 if(element.HasFinished)
  52.                 {
  53.                     toRemove.Add(element);
  54.                     nrToRemove++;
  55.                 }
  56.             }
  57.             if(nrToRemove != 0)
  58.             {
  59.                 foreach(Timer element in toRemove)
  60.                 {
  61.                     timers.Remove(element);
  62.                     nrToRemove--;
  63.                     nrOfTimers--;
  64.                 }
  65.                 toRemove.Clear();
  66.             }
  67.         }        
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement