smrendererv3/SMCode/SM.Base/Time/Timer.cs
Michel Fedde beb9c19081 28.10.2020
SM.Core:
+ Particle System
+ scriptable system for scripts

~ Moved Texts- and Particles-namespace to SM.Base.Drawing
~ Changed how you tell the stopwatch to pause. (From method to property)
~ Fixed Randomize.GetFloat(min, max)
~ Now automaticly adds the DrawingBase.Transformation to DrawContext.ModelMatrix. No need to change DrawContext.Instances[0], anymore.

SM.OGL:
+ "one-file-shader"-support

SM2D:
+ DrawParticles (Control for Texture and Color not there yet)

~ Changed coordnate system to upper-right as (1,1)
~ Changed default shader to "one-file-shader"
2020-10-28 18:19:15 +01:00

81 lines
No EOL
2 KiB
C#

#region usings
using System;
using SM.Base.Contexts;
#endregion
namespace SM.Base.Time
{
/// <summary>
/// Timer-System
/// </summary>
public class Timer : Stopwatch
{
/// <summary>
/// Creates a timer with specified seconds.
/// </summary>
/// <param name="seconds"></param>
public Timer(float seconds)
{
Target = seconds;
}
/// <summary>
/// Creates a timer with a time span.
/// </summary>
/// <param name="timeSpan"></param>
public Timer(TimeSpan timeSpan)
{
Target = (float) timeSpan.TotalSeconds;
}
/// <summary>
/// The target time in seconds.
/// </summary>
public float Target { get; private set; }
/// <summary>
/// The already elapsed time but normalized to the target.
/// </summary>
public float ElapsedNormalized { get; private set; }
/// <summary>
/// The event, that is triggered when the timer stops.
/// </summary>
public event Action<Timer, UpdateContext> EndAction;
/// <inheritdoc />
public override void Start()
{
base.Start();
Reset();
}
private protected override void Tick(UpdateContext context)
{
base.Tick(context);
ElapsedNormalized = Elapsed / Target;
if (ElapsedNormalized >= 1) Stopping(context);
}
/// <summary>
/// Occurs, when the timer tries to stop.
/// </summary>
protected virtual void Stopping(UpdateContext context)
{
TriggerEndAction(context);
Stop();
}
/// <summary>
/// This will trigger <see cref="EndAction" />
/// </summary>
/// <param name="context"></param>
protected void TriggerEndAction(UpdateContext context)
{
EndAction?.Invoke(this, context);
}
}
}