SHADE_Y3/Assets/Scripts/UI/SC_TweenManager.cs

186 lines
3.9 KiB
C#
Raw Permalink Normal View History

using SHADE;
using SHADE_Scripting.Audio;
using System;
using System.Collections.Generic;
using System.Threading;
2023-03-08 18:49:36 +08:00
public class TweenThread
{
2023-03-08 18:49:36 +08:00
private float timer = 0.0f;
public float duration = 1.0f;
public EASING_METHOD method;
private float value = 0.0f;
public float startValue = 0.0f;
public float endValue = 1.0f;
public TweenThread(float duration, float startValue, float endValue, EASING_METHOD method)
{
this.duration = duration;
this.method = method;
this.startValue = startValue;
this.endValue = endValue;
}
public void Update(float deltaTime)
{
if (timer >= duration)
return;
timer += deltaTime;
if (timer >= duration)
timer = duration;
value = EasingHelper.EaseHelp(timer / duration, method) * (endValue - startValue) + startValue;
}
public bool IsCompleted()
{
return timer >= duration;
}
public void Reset()
{
timer = 0.0f;
value = startValue;
}
public void Reset(float startValue, float endValue)
{
Reset();
this.startValue = startValue;
this.endValue = endValue;
}
public void ResetInvert()
{
Reset();
float temp = startValue;
startValue = endValue;
endValue = temp;
}
public float GetValue()
{
return value;
}
}
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
public class TweenThreadVec3
{
private float timer = 0.0f;
public float duration = 1.0f;
public EASING_METHOD method;
private Vector3 value = Vector3.Zero;
public Vector3 startValue = Vector3.Zero;
public Vector3 endValue = Vector3.Zero;
public TweenThreadVec3(float duration, Vector3 startValue, Vector3 endValue, EASING_METHOD method)
{
this.duration = duration;
this.method = method;
this.startValue = startValue;
this.endValue = endValue;
}
public void Update(float deltaTime)
{
if (timer >= duration)
return;
timer += deltaTime;
if (timer >= duration)
timer = duration;
value = (endValue - startValue) * EasingHelper.EaseHelp(timer / duration, method) + startValue;
}
public bool IsCompleted()
{
return timer >= duration;
}
public void Reset()
{
timer = 0.0f;
value = startValue;
}
public void Reset(Vector3 startValue, Vector3 endValue)
{
Reset();
this.startValue = startValue;
this.endValue = endValue;
}
public void ResetInvert()
{
Reset();
Vector3 temp = startValue;
startValue = endValue;
endValue = temp;
}
public Vector3 GetValue()
{
return value;
}
}
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
public class TweenManager : Script
{
public static TweenManager Instance { get; private set; }
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
[NonSerialized]
private List<TweenThread> threadList;
2023-03-08 18:49:36 +08:00
[NonSerialized]
private List<TweenThreadVec3> threadVec3List;
2023-03-08 18:49:36 +08:00
protected override void awake()
{
if (Instance != null && Instance != this)
RemoveScript<TweenManager>();
else
Instance = this;
2023-03-08 18:49:36 +08:00
threadList = new List<TweenThread>();
threadVec3List = new List<TweenThreadVec3>();
2023-03-08 18:49:36 +08:00
}
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
protected override void onDestroy()
{
if (Instance == this)
Instance = null;
2023-03-08 18:49:36 +08:00
}
2023-03-08 18:49:36 +08:00
protected override void update()
{
foreach (TweenThread thread in threadList)
{
thread.Update(Time.DeltaTimeF);
}
2023-03-08 18:49:36 +08:00
foreach (TweenThreadVec3 thread in threadVec3List)
{
thread.Update(Time.DeltaTimeF);
}
}
2023-03-08 18:49:36 +08:00
public static TweenThread CreateTweenThread(float duration, float startValue, float endValue, EASING_METHOD method)
{
if (Instance == null)
return null;
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
TweenThread thread = new TweenThread(duration, startValue, endValue, method);
Instance.threadList.Add(thread);
thread.Reset();
return thread;
}
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
public static TweenThreadVec3 CreateTweenThreadVec3(float duration, Vector3 startValue, Vector3 endValue, EASING_METHOD method)
{
if (Instance == null)
return null;
2023-03-02 11:33:21 +08:00
2023-03-08 18:49:36 +08:00
TweenThreadVec3 thread = new TweenThreadVec3(duration, startValue, endValue, method);
Instance.threadVec3List.Add(thread);
thread.Reset();
return thread;
}
2023-03-02 11:33:21 +08:00
}