mirror of
https://gitlab.com/MaddoScientisto/cirnogodot.git
synced 2026-07-01 22:21:17 +00:00
Added GTWeen
This commit is contained in:
parent
457998788e
commit
2036e4e748
152 changed files with 5889 additions and 7 deletions
114
GTweensGodot/GTweens/Source/Contexts/GTweensContext.cs
Normal file
114
GTweensGodot/GTweens/Source/Contexts/GTweensContext.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using GTweens.Tweens;
|
||||
|
||||
namespace GTweens.Contexts
|
||||
{
|
||||
/// <summary>
|
||||
/// Manages and updates a collection of GTweens.
|
||||
/// </summary>
|
||||
public sealed class GTweensContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the time scale at which the tweens should play.
|
||||
/// </summary>
|
||||
public float TimeScale { get; set; } = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the duration of the last tweens tick in milliseconds.
|
||||
/// </summary>
|
||||
public float TickDurationMs { get; private set; }
|
||||
|
||||
readonly List<GTween> _aliveTweens = new();
|
||||
readonly List<GTween> _tweensToAdd = new();
|
||||
readonly List<GTween> _tweensToRemove = new();
|
||||
|
||||
readonly Stopwatch _updateStopwatch = new();
|
||||
|
||||
/// <summary>
|
||||
/// Plays a GTween within the context.
|
||||
/// </summary>
|
||||
/// <param name="gTween">The GTween to play.</param>
|
||||
public void Play(GTween gTween)
|
||||
{
|
||||
if(gTween.IsNested)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if(gTween.IsAlive)
|
||||
{
|
||||
TryStartTween(gTween);
|
||||
return;
|
||||
}
|
||||
|
||||
gTween.IsAlive = true;
|
||||
|
||||
_tweensToAdd.Add(gTween);
|
||||
|
||||
TryStartTween(gTween);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the context and all managed tweens.
|
||||
/// </summary>
|
||||
/// <param name="deltaTime">The elapsed time since the last update in seconds.</param>
|
||||
public void Tick(float deltaTime)
|
||||
{
|
||||
float scaledDeltaTime = deltaTime * TimeScale;
|
||||
|
||||
_updateStopwatch.Restart();
|
||||
|
||||
foreach(GTween tween in _tweensToAdd)
|
||||
{
|
||||
_aliveTweens.Add(tween);
|
||||
}
|
||||
|
||||
_tweensToAdd.Clear();
|
||||
|
||||
foreach (GTween tween in _aliveTweens)
|
||||
{
|
||||
if(tween.IsPlaying)
|
||||
{
|
||||
tween.Tick(scaledDeltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
_tweensToRemove.Add(tween);
|
||||
}
|
||||
}
|
||||
|
||||
foreach(GTween tween in _tweensToRemove)
|
||||
{
|
||||
tween.IsAlive = false;
|
||||
|
||||
_aliveTweens.Remove(tween);
|
||||
_tweensToAdd.Remove(tween);
|
||||
}
|
||||
|
||||
_tweensToRemove.Clear();
|
||||
|
||||
_updateStopwatch.Stop();
|
||||
|
||||
TickDurationMs = _updateStopwatch.ElapsedMilliseconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears all tweens from the context.
|
||||
/// </summary>
|
||||
public void Clear()
|
||||
{
|
||||
_aliveTweens.Clear();
|
||||
_tweensToAdd.Clear();
|
||||
_tweensToRemove.Clear();
|
||||
}
|
||||
|
||||
void TryStartTween(GTween gTween)
|
||||
{
|
||||
if (!gTween.IsPlaying)
|
||||
{
|
||||
gTween.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
uid://cc0xhrqaeyj2a
|
||||
Loading…
Add table
Add a link
Reference in a new issue