Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace notContra
- {
- /// <summary>
- /// A class to manage Timers. Puts timers into a list. Ticks every timer in the list every game update.
- /// When the timer reaches it's time limit it gets added to the toRemove list and then removed from timers.
- /// The toRemove list is cleared after.
- /// </summary>
- class TimerManager
- {
- List<Timer> timers = new List<Timer>();
- List<Timer> toRemove = new List<Timer>();
- int nrToRemove = 0;
- int nrOfTimers = 0;
- public int NrOfTimers
- {
- get
- {
- return nrOfTimers;
- }
- }
- public TimerManager() { }
- public void Wait(int timeToWait)
- {
- Add(new Timer(timeToWait));
- }
- public void Wait(float timeToWait)
- {
- Add(new Timer(timeToWait));
- }
- void Add(Timer timer)
- {
- timers.Add(timer);
- nrOfTimers++;
- }
- public void Update(GameTime gameTime)
- {
- foreach(Timer element in timers)
- {
- element.Tick(gameTime);
- if(element.HasFinished)
- {
- toRemove.Add(element);
- nrToRemove++;
- }
- }
- if(nrToRemove != 0)
- {
- foreach(Timer element in toRemove)
- {
- timers.Remove(element);
- nrToRemove--;
- nrOfTimers--;
- }
- toRemove.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement