2012-03-19 23:57:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
using Axios.Engine.Interfaces;
|
|
|
|
|
using Microsoft.Xna.Framework;
|
2012-04-18 03:07:51 +00:00
|
|
|
|
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
|
|
|
|
|
*
|
|
|
|
|
*/
|
2012-04-07 18:15:15 +00:00
|
|
|
|
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;
|
2012-12-28 22:57:08 +00:00
|
|
|
|
public TimeSpan? offset = null;
|
2012-03-19 23:57:59 +00:00
|
|
|
|
|
|
|
|
|
public event EventHandler Tick;
|
|
|
|
|
|
|
|
|
|
public TimeSpan Interval
|
|
|
|
|
{
|
|
|
|
|
get { return interval; }
|
|
|
|
|
set { interval = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Boolean Enabled
|
|
|
|
|
{
|
|
|
|
|
get { return _enabled; }
|
|
|
|
|
set { _enabled = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AxiosTimer()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-07 18:15:15 +00:00
|
|
|
|
public override void Update(AxiosGameScreen gameScreen, GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-12-28 22:57:08 +00:00
|
|
|
|
// Issue here: if you add a timer later on and use the algorithm of
|
|
|
|
|
// gameTime.TotalGameTime - lastTick >= interval
|
|
|
|
|
// The timer will always run
|
|
|
|
|
// What we should do is have an offset of the time it was added like this:
|
|
|
|
|
// ((gameTime.TotalGameTime - offset) - lastTick) >= interval
|
2012-04-13 03:19:41 +00:00
|
|
|
|
if (gameScreen.ScreenManager.Game.IsActive) //only "tick" if the window has focus - otherwise the Timer will play catchup
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-12-28 22:57:08 +00:00
|
|
|
|
if (offset == null)
|
|
|
|
|
{
|
|
|
|
|
offset = gameTime.TotalGameTime;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2012-04-08 19:40:58 +00:00
|
|
|
|
if (_enabled)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
2012-12-28 22:57:08 +00:00
|
|
|
|
if (((gameTime.TotalGameTime - offset) - lastTick) >= interval)
|
2012-04-07 18:15:15 +00:00
|
|
|
|
{
|
2012-04-08 19:40:58 +00:00
|
|
|
|
if (Tick != null)
|
|
|
|
|
{
|
|
|
|
|
//EventArgs e = new EventArgs();
|
2012-04-13 03:19:41 +00:00
|
|
|
|
//System.Diagnostics.Debugger.Break();
|
2012-04-08 19:40:58 +00:00
|
|
|
|
Tick(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastTick = gameTime.TotalGameTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2012-03-19 23:57:59 +00:00
|
|
|
|
lastTick = gameTime.TotalGameTime;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-07 18:15:15 +00:00
|
|
|
|
public override void LoadContent(AxiosGameScreen gameScreen)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 03:07:51 +00:00
|
|
|
|
public override void HandleInput(AxiosGameScreen gameScreen, InputState input, Microsoft.Xna.Framework.GameTime gameTime)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-18 03:07:51 +00:00
|
|
|
|
public override void HandleCursor(AxiosGameScreen gameScreen, InputState input)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-07 18:15:15 +00:00
|
|
|
|
public override void UnloadContent(AxiosGameScreen gameScreen)
|
2012-03-19 23:57:59 +00:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|