IndieGame/client/Assets/Ether/Scripts/Module/Animation/AnimationSet.cs

88 lines
2.8 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00

using Sirenix.OdinInspector;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Ether
{
public enum AnimationSetType
{
[InspectorName("独立")]
Single,
[InspectorName("上")]
Up,
[InspectorName("下")]
Down,
[InspectorName("左")]
Left,
[InspectorName("右")]
Right,
[InspectorName("左上")]
UpLeft,
[InspectorName("右上")]
UpRight,
[InspectorName("左下")]
DownLeft,
[InspectorName("右下")]
DownRight
}
[CreateAssetMenu(fileName = "New Animation Set", menuName = "动画/创建动画资源", order = 1)]
public class AnimationSet : SerializedScriptableObject
{
[LabelText("动画列表")]
[ShowInInspector]
[DictionaryDrawerSettings(KeyLabel = "方向", ValueLabel = "动画clip")]
public Dictionary<AnimationSetType, AnimationClip> animationClips = new Dictionary<AnimationSetType, AnimationClip>();
public AnimationClip GetClip(AnimationSetType type = AnimationSetType.Single)
{
animationClips.TryGetValue(type, out AnimationClip clip);
return clip;
}
public AnimationClip GetClip(Vector2 direction)
{
var angle = Mathf.Atan2(direction.y, direction.x);
var octant = Mathf.RoundToInt(8 * angle / (2 * Mathf.PI) + 8) % 8;
AnimationClip clip = null;
switch (octant)
{
case 0:
clip = GetClip(AnimationSetType.Right);
break;
case 1:
clip = GetClip(AnimationSetType.UpRight);
clip = clip == null ? GetClip(AnimationSetType.Right) : clip;
break;
case 2:
clip = GetClip(AnimationSetType.Up);
break;
case 3:
clip = GetClip(AnimationSetType.UpLeft);
clip = clip == null ? GetClip(AnimationSetType.Left) : clip;
break;
case 4:
clip = GetClip(AnimationSetType.Left);
break;
case 5:
clip = GetClip(AnimationSetType.DownLeft);
clip = clip == null ? GetClip(AnimationSetType.Left) : clip;
break;
case 6:
clip = GetClip(AnimationSetType.Down);
break;
case 7:
clip = GetClip(AnimationSetType.DownRight);
clip = clip == null ? GetClip(AnimationSetType.Right) : clip;
break;
}
return clip;
}
}
}