axiosengine/axios/Engine/AxiosTimer.cs

85 lines
2.2 KiB
C#
Raw Normal View History

2012-03-19 23:57:59 +00:00
using System;
using Axios.Engine.Interfaces;
using Microsoft.Xna.Framework;
using GameStateManagement;
2012-03-19 23:57:59 +00:00
namespace Axios.Engine
{
/*
* Modeled after Nicks' implemenentation
* Source: http://www.gamedev.net/topic/473544-how-to-make-a-timer-using-xna/page__view__findpost__p__4107032
*
*/
public class AxiosTimer : AxiosGameObject
2012-03-19 23:57:59 +00:00
{
TimeSpan interval = new TimeSpan(0, 0, 1);
TimeSpan lastTick = new TimeSpan();
private bool _enabled = false;
public event EventHandler Tick;
public TimeSpan Interval
{
get { return interval; }
set { interval = value; }
}
public Boolean Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
public AxiosTimer()
{
}
public override void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
2012-03-19 23:57:59 +00:00
{
if (gameScreen.IsActive) //only "tick" if the window has focus - otherwise the Timer will play catchup
2012-03-19 23:57:59 +00:00
{
if (_enabled)
2012-03-19 23:57:59 +00:00
{
if (gameTime.TotalGameTime - lastTick >= interval)
{
if (Tick != null)
{
//EventArgs e = new EventArgs();
2012-03-19 23:57:59 +00:00
Tick(this, null);
}
lastTick = gameTime.TotalGameTime;
}
}
else
{
2012-03-19 23:57:59 +00:00
lastTick = gameTime.TotalGameTime;
}
}
}
public override void LoadContent(AxiosGameScreen gameScreen)
2012-03-19 23:57:59 +00:00
{
}
public override void HandleInput(AxiosGameScreen gameScreen, InputState input, Microsoft.Xna.Framework.GameTime gameTime)
2012-03-19 23:57:59 +00:00
{
}
public override void HandleCursor(AxiosGameScreen gameScreen, InputState input)
2012-03-19 23:57:59 +00:00
{
}
public override void UnloadContent(AxiosGameScreen gameScreen)
2012-03-19 23:57:59 +00:00
{
}
}
}