01.10.2020

+ Time controls (Stopwatch, Timers, Intervals)
+ Added smmeries to everything in SM.Base

~ Renamed Vectors to CVectors.
This commit is contained in:
Michel Fedde 2020-10-01 15:39:03 +02:00
parent 7acdba92f8
commit 97e638d9d9
44 changed files with 1092 additions and 289 deletions

View file

@ -0,0 +1,48 @@
using System;
using SM.Base.Contexts;
namespace SM.Base.Time
{
/// <summary>
/// Performs intervals.
/// </summary>
public class Interval : Timer
{
private bool _stop;
/// <inheritdoc />
public Interval(float seconds) : base(seconds)
{
}
/// <inheritdoc />
public Interval(TimeSpan timeSpan) : base(timeSpan)
{
}
/// <inheritdoc />
protected override void Stopping(UpdateContext context)
{
TriggerEndAction(context);
if (_stop){base.Stop();}
else Reset();
}
/// <summary>
/// This will tell the interval to stop after the next iteration.
/// <para>To stop immediately use <see cref="Cancel"/></para>
/// </summary>
public override void Stop()
{
_stop = true;
}
/// <summary>
/// This will stop the interval immediately.
/// </summary>
public void Cancel()
{
base.Stop();
}
}
}

View file

@ -0,0 +1,59 @@
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Time
{
/// <summary>
/// Represents a stopwatch.
/// </summary>
public class Stopwatch
{
private static List<Stopwatch> _activeStopwatches = new List<Stopwatch>();
/// <summary>
/// Contains how much time already has passed. (in seconds)
/// </summary>
public float Elapsed { get; private set; }
/// <summary>
/// Starts the stopwatch.
/// </summary>
public virtual void Start()
{
_activeStopwatches.Add(this);
}
/// <summary>
/// Performs a tick.
/// </summary>
/// <param name="context"></param>
private protected virtual void Tick(UpdateContext context)
{
Elapsed += context.Deltatime;
}
/// <summary>
/// Stops the stopwatch.
/// </summary>
public virtual void Stop()
{
_activeStopwatches.Remove(this);
}
/// <summary>
/// Resets the stopwatch.
/// </summary>
public void Reset()
{
Elapsed = 0;
}
internal static void PerformTicks(UpdateContext context)
{
for (var i = 0; i < _activeStopwatches.Count; i++)
{
_activeStopwatches[i].Tick(context);
}
}
}
}

View file

@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using SM.Base.Contexts;
namespace SM.Base.Time
{
/// <summary>
/// Timer-System
/// </summary>
public class Timer : Stopwatch
{
/// <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;
/// <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;
}
/// <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)
{
EndAction?.Invoke(this, context);
Stop();
}
/// <summary>
/// This will trigger <see cref="EndAction"/>
/// </summary>
/// <param name="context"></param>
protected void TriggerEndAction(UpdateContext context)
{
EndAction?.Invoke(this, context);
}
}
}