using System;
using LitMotion.Collections;
namespace LitMotion
{
///
/// Manually updatable MotionDispatcher
///
public static class ManualMotionDispatcher
{
static class Cache
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
public static MotionStorage updateStorage;
public static UpdateRunner updateRunner;
public static MotionStorage GetOrCreate()
{
if (updateStorage == null)
{
var storage = new MotionStorage(MotionStorageManager.CurrentStorageId);
MotionStorageManager.AddStorage(storage);
updateStorage = storage;
}
return updateStorage;
}
}
static FastListCore updateRunners;
///
/// ManualMotionDispatcher time. It increases every time Update is called.
///
public static double Time { get; set; }
///
/// Ensures the storage capacity until it reaches at least `capacity`.
///
/// The minimum capacity to ensure.
public static void EnsureStorageCapacity(int capacity)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
Cache.GetOrCreate().EnsureCapacity(capacity);
}
///
/// Update all scheduled motions with MotionScheduler.Manual
///
/// Delta time
public static void Update(double deltaTime)
{
if (deltaTime < 0f) throw new ArgumentException("deltaTime must be 0 or higher.");
Time += deltaTime;
Update();
}
///
/// Update all scheduled motions with MotionScheduler.Manual
///
public static void Update()
{
var span = updateRunners.AsSpan();
for (int i = 0; i < span.Length; i++)
{
span[i].Update(Time, Time, Time);
}
}
///
/// Cancel all motions and reset data.
///
public static void Reset()
{
var span = updateRunners.AsSpan();
for (int i = 0; i < span.Length; i++)
{
span[i].Reset();
}
}
internal static MotionHandle Schedule(in MotionData data, in MotionCallbackData callbackData)
where TValue : unmanaged
where TOptions : unmanaged, IMotionOptions
where TAdapter : unmanaged, IMotionAdapter
{
MotionStorage storage = Cache.GetOrCreate();
if (Cache.updateRunner == null)
{
var runner = new UpdateRunner(storage, Time, Time, Time);
updateRunners.Add(runner);
Cache.updateRunner = runner;
}
var (EntryIndex, Version) = storage.Append(data, callbackData);
return new MotionHandle()
{
StorageId = storage.StorageId,
Index = EntryIndex,
Version = Version
};
}
}
}