summaryrefslogtreecommitdiff
path: root/GameCode/CodeAnimation.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-27 11:05:14 +0800
committerchai <215380520@qq.com>2023-10-27 11:05:14 +0800
commit766cdff5ffa72b65d7f106658d1603f47739b2ba (patch)
tree34d7799a94dfa9be182825577583c0fa6dc935f7 /GameCode/CodeAnimation.cs
+ init
Diffstat (limited to 'GameCode/CodeAnimation.cs')
-rw-r--r--GameCode/CodeAnimation.cs205
1 files changed, 205 insertions, 0 deletions
diff --git a/GameCode/CodeAnimation.cs b/GameCode/CodeAnimation.cs
new file mode 100644
index 0000000..ae132b3
--- /dev/null
+++ b/GameCode/CodeAnimation.cs
@@ -0,0 +1,205 @@
+using System;
+using System.Collections;
+using Sirenix.OdinInspector;
+using UnityEngine;
+using UnityEngine.Events;
+
+public class CodeAnimation : MonoBehaviour
+{
+ [HideInInspector]
+ public bool isPlaying;
+
+ public bool loonIn;
+
+ public bool playInOnAwake;
+
+ public bool playInOnEnable;
+
+ public float enablePlayDelay;
+
+ public bool interuptAnimations = true;
+
+ public bool setFirstFrame;
+
+ public bool useTimeScale = true;
+
+ private Vector3 defaultScale;
+
+ private Vector3 defaultLocalPosition;
+
+ private Vector3 defaultRectPosition;
+
+ private Vector3 defaultLocalRotation;
+
+ public CodeAnimationInstance[] animations;
+
+ private RectTransform rectTransform;
+
+ [HideInInspector]
+ public CodeAnimationInstance.AnimationUse currentState;
+
+ [HideInInspector]
+ public float animationValue;
+
+ private Action AnimationChangeAction;
+
+ public void Start()
+ {
+ currentState = CodeAnimationInstance.AnimationUse.None;
+ rectTransform = GetComponent<RectTransform>();
+ for (int i = 0; i < animations.Length; i++)
+ {
+ animations[i].animationSpeed += UnityEngine.Random.Range((0f - animations[i].animationSpeed) * animations[i].randomSpeedAmount, animations[i].animationSpeed * animations[i].randomSpeedAmount);
+ if (animations[i].animationUse == CodeAnimationInstance.AnimationUse.In)
+ {
+ float num = animations[i].curve[animations[i].curve.length - 1].value - animations[i].curve[0].value;
+ if ((bool)rectTransform && animations[i].animationType == CodeAnimationInstance.AnimationType.rectPosition)
+ {
+ rectTransform.anchoredPosition += num * (Vector2)animations[i].direction * (0f - animations[i].multiplier);
+ }
+ }
+ }
+ SetDefaults();
+ if (playInOnAwake)
+ {
+ PlayIn();
+ }
+ if (setFirstFrame)
+ {
+ ApplyValues(animations[0], 0f);
+ }
+ }
+
+ private void OnEnable()
+ {
+ if (playInOnEnable)
+ {
+ StartCoroutine(DelayEnablePlay());
+ }
+ }
+
+ private IEnumerator DelayEnablePlay()
+ {
+ yield return new WaitForSecondsRealtime(enablePlayDelay);
+ PlayIn();
+ }
+
+ private void SetDefaults()
+ {
+ defaultScale = base.transform.localScale;
+ defaultLocalPosition = base.transform.localPosition;
+ defaultLocalRotation = base.transform.localRotation.eulerAngles;
+ if ((bool)rectTransform)
+ {
+ defaultRectPosition = rectTransform.anchoredPosition;
+ }
+ }
+
+ public void Animate(CodeAnimationInstance.AnimationUse use)
+ {
+ currentState = use;
+ if (interuptAnimations)
+ {
+ StopAllCoroutines();
+ }
+ for (int i = 0; i < animations.Length; i++)
+ {
+ if (animations[i].animationUse == use)
+ {
+ StartCoroutine(PlayAnimations(animations[i]));
+ }
+ }
+ }
+
+ [Button]
+ public void PlayIn()
+ {
+ Animate(CodeAnimationInstance.AnimationUse.In);
+ }
+
+ [Button]
+ public void PlayOut()
+ {
+ Animate(CodeAnimationInstance.AnimationUse.Out);
+ }
+
+ [Button]
+ public void PlayBoop()
+ {
+ Animate(CodeAnimationInstance.AnimationUse.Boop);
+ }
+
+ private IEnumerator PlayAnimations(CodeAnimationInstance animation)
+ {
+ isPlaying = true;
+ animation.startEvent.Invoke();
+ StartCoroutine(DelayTimedEvent(animation.eventTiming / animation.animationSpeed, animation.timedEvent));
+ float t = animation.curve.keys[animation.curve.keys.Length - 1].time;
+ float c = 0f;
+ while (c < t)
+ {
+ ApplyValues(animation, c);
+ c += (useTimeScale ? TimeHandler.deltaTime : Time.unscaledDeltaTime) * animation.animationSpeed;
+ yield return null;
+ }
+ ApplyValues(animation, t);
+ isPlaying = false;
+ animation.endEvent.Invoke();
+ if (loonIn)
+ {
+ PlayIn();
+ }
+ }
+
+ private void ApplyValues(CodeAnimationInstance animation, float time)
+ {
+ if (animation.animationType == CodeAnimationInstance.AnimationType.rectPosition && (bool)rectTransform)
+ {
+ rectTransform.anchoredPosition = defaultRectPosition + animation.direction * animation.curve.Evaluate(time) * animation.multiplier;
+ }
+ if (animation.animationType == CodeAnimationInstance.AnimationType.position)
+ {
+ base.transform.localPosition = defaultLocalPosition + animation.direction * animation.curve.Evaluate(time) * animation.multiplier;
+ }
+ if (animation.animationType == CodeAnimationInstance.AnimationType.scale)
+ {
+ Vector3 localScale = defaultScale * animation.curve.Evaluate(time) * animation.multiplier;
+ if (!animation.X)
+ {
+ localScale.x = base.transform.localScale.x;
+ }
+ if (!animation.Y)
+ {
+ localScale.y = base.transform.localScale.y;
+ }
+ if (!animation.Z)
+ {
+ localScale.z = base.transform.localScale.z;
+ }
+ base.transform.localScale = localScale;
+ }
+ if (animation.animationType == CodeAnimationInstance.AnimationType.floatNumber)
+ {
+ animationValue = animation.curve.Evaluate(time) * animation.multiplier;
+ }
+ if (animation.animationType == CodeAnimationInstance.AnimationType.rotation)
+ {
+ base.transform.localRotation = Quaternion.Euler(animation.curve.Evaluate(time) * animation.direction + defaultLocalRotation);
+ }
+ if (AnimationChangeAction != null)
+ {
+ AnimationChangeAction();
+ }
+ }
+
+ public void AddAnimationChangeAction(Action action)
+ {
+ AnimationChangeAction = (Action)Delegate.Combine(AnimationChangeAction, action);
+ }
+
+ private IEnumerator DelayTimedEvent(float time, UnityEvent eventToCall)
+ {
+ yield return new WaitForSeconds(time);
+ eventToCall.Invoke();
+ }
+}