Files

220 lines
6.1 KiB
C#
Raw Permalink Normal View History

2022-10-02 12:58:34 +03:00
using UnityEngine;
2022-10-07 00:59:20 +03:00
using UnityEngine.Events;
2022-10-02 12:58:34 +03:00
public class DemonicsAnimator : MonoBehaviour
{
2022-10-07 00:59:20 +03:00
[SerializeField] protected AnimationSO _animation = default;
2022-10-07 10:29:42 +03:00
[SerializeField] protected SpriteRenderer _spriteRenderer = default;
2022-10-07 00:59:20 +03:00
private int _frame;
protected int _cel;
protected int _group;
protected int _skin;
protected bool _isPaused;
protected bool _frozen;
[HideInInspector] public UnityEvent OnCurrentAnimationFinished;
2022-10-02 12:58:34 +03:00
2022-10-07 00:59:20 +03:00
2022-10-12 23:30:09 +02:00
protected virtual void FixedUpdate()
2022-10-07 00:59:20 +03:00
{
}
2022-10-12 23:30:09 +02:00
public void SetAnimator(AnimationSO animation)
{
_animation = animation;
SetAnimation("Idle");
2022-10-12 23:30:09 +02:00
}
2022-12-15 21:13:43 +02:00
public int GetMaxAnimationFrames(string name = "Idle")
2022-12-14 18:33:44 +02:00
{
int maxFrames = 0;
_group = _animation.GetGroupId(name);
for (int i = 0; i < _animation.GetGroup(_group).animationCel.Count; i++)
{
maxFrames += _animation.GetGroup(_group).animationCel[i].frames;
}
return maxFrames;
}
2022-12-21 19:25:32 +02:00
public static int GetMaxAnimationFrames(AnimationSO animation, string name = "Idle")
{
int maxFrames = 0;
int group = animation.GetGroupId(name);
for (int i = 0; i < animation.GetGroup(group).animationCel.Count; i++)
{
maxFrames += animation.GetGroup(group).animationCel[i].frames;
}
return maxFrames;
}
2022-12-14 18:33:44 +02:00
2022-12-17 22:51:47 +02:00
public virtual void SetAnimation(string name)
2022-10-07 00:59:20 +03:00
{
2022-12-13 01:44:28 +02:00
if (_animation != null && _animation.GetGroup(_group).celName != name)
2022-10-07 10:29:42 +03:00
{
_frame = 0;
_cel = 0;
_group = _animation.GetGroupId(name);
_isPaused = false;
CheckAnimationBoxes();
_spriteRenderer.sprite = _animation.GetSprite(_skin, _group, _cel);
}
2022-10-07 00:59:20 +03:00
}
2023-01-04 17:56:46 +02:00
public bool IsAnimationLoop(string name)
2022-12-14 15:24:10 +02:00
{
2023-01-04 17:56:46 +02:00
_group = _animation.GetGroupId(name);
return _animation.GetGroup(_group).loop;
}
public bool IsAnimationFinished(string name, int frames)
{
_group = _animation.GetGroupId(name);
int totalFrames = 0;
for (int i = 0; i < _animation.GetGroup(_group).animationCel.Count; i++)
{
totalFrames += _animation.GetGroup(_group).animationCel[i].frames;
}
if (frames >= totalFrames)
{
return true;
}
else
{
return false;
}
2022-12-14 15:24:10 +02:00
}
2022-12-28 14:10:45 +02:00
public virtual void SetAnimation(string name, int frame)
2022-12-13 01:44:28 +02:00
{
2022-12-14 15:24:10 +02:00
_group = _animation.GetGroupId(name);
for (int i = 0; i < _animation.GetGroup(_group).animationCel.Count; i++)
2022-12-13 01:44:28 +02:00
{
2022-12-14 15:24:10 +02:00
if (frame > 0)
2022-12-13 01:44:28 +02:00
{
2022-12-14 15:24:10 +02:00
frame -= _animation.GetGroup(_group).animationCel[i].frames;
if (frame < 0)
{
_frame = Mathf.Abs(frame);
_cel = i;
break;
}
2023-01-04 17:56:46 +02:00
else if (frame > 0 && _animation.GetGroup(_group).loop)
2022-12-14 15:24:10 +02:00
{
_frame = 0;
_cel = 0;
}
2023-01-04 17:56:46 +02:00
else
{
_frame = 0;
_cel = _animation.GetGroup(_group).animationCel.Count - 1;
}
2022-12-14 15:24:10 +02:00
}
else
{
_frame = Mathf.Abs(frame);
_cel = i;
break;
2022-12-13 01:44:28 +02:00
}
}
2022-12-14 15:24:10 +02:00
CheckAnimationBoxes();
_spriteRenderer.sprite = _animation.GetSprite(_skin, _group, _cel);
2022-12-13 01:44:28 +02:00
}
2022-10-07 00:59:20 +03:00
2022-12-14 15:24:10 +02:00
public void PlayAnimation()
2022-10-07 00:59:20 +03:00
{
if (!_isPaused && !_frozen)
{
if (_frame == _animation.GetCel(_group, _cel).frames)
{
_cel++;
if (_cel > _animation.GetGroup(_group).animationCel.Count - 1)
{
AnimationEnded();
if (!_animation.GetGroup(_group).loop)
{
return;
}
}
CheckAnimationBoxes();
_frame = 0;
}
2022-11-24 02:19:22 +02:00
CheckGrab();
2022-10-07 00:59:20 +03:00
_spriteRenderer.sprite = _animation.GetSprite(_skin, _group, _cel);
_frame++;
}
}
2022-11-24 02:19:22 +02:00
protected virtual void CheckGrab() { }
2022-10-07 00:59:20 +03:00
protected virtual void CheckEvents() { }
protected virtual void CheckAnimationBoxes() { }
protected virtual void AnimationEnded()
{
if (!_animation.GetGroup(_group).loop)
{
_isPaused = true;
}
_frame = 0;
_cel = 0;
OnCurrentAnimationFinished?.Invoke();
OnCurrentAnimationFinished.RemoveAllListeners();
}
2023-01-04 17:56:46 +02:00
public AnimationBox[] GetHurtboxes(string name, int frame)
2022-10-07 00:59:20 +03:00
{
2023-01-04 17:56:46 +02:00
_group = _animation.GetGroupId(name);
_cel = GetCellByFrame(frame);
2022-10-07 00:59:20 +03:00
return _animation.GetCel(_group, _cel).hurtboxes.ToArray();
}
2023-01-04 17:56:46 +02:00
public AnimationBox[] GetHitboxes(string name, int frame)
2022-10-07 00:59:20 +03:00
{
2023-01-04 17:56:46 +02:00
_group = _animation.GetGroupId(name);
_cel = GetCellByFrame(frame);
2022-10-07 00:59:20 +03:00
return _animation.GetCel(_group, _cel).hitboxes.ToArray();
}
2023-01-23 18:31:58 +02:00
protected AnimationEvent GetEvent(string name, int frame, out int cel)
2022-10-07 00:59:20 +03:00
{
2023-01-09 18:16:00 +02:00
_group = _animation.GetGroupId(name);
_cel = GetCellByFrame(frame);
2023-01-23 18:31:58 +02:00
cel = _cel;
2022-10-07 00:59:20 +03:00
return _animation.GetCel(_group, _cel).animationEvent;
}
2023-01-09 18:16:00 +02:00
protected int GetCellByFrame(int frame)
2023-01-04 17:56:46 +02:00
{
int cel = 0;
for (int i = 0; i < _animation.GetGroup(_group).animationCel.Count; i++)
{
if (frame > 0)
{
frame -= _animation.GetGroup(_group).animationCel[i].frames;
if (frame < 0)
{
cel = i;
break;
}
if (frame > 0 && _animation.GetGroup(_group).loop)
{
cel = 0;
}
}
else
{
cel = i;
break;
}
}
2023-02-15 17:23:52 +02:00
if (frame > 0)
{
cel = _animation.GetGroup(_group).animationCel.Count - 1;
}
2023-01-04 17:56:46 +02:00
return cel;
}
2022-10-07 00:59:20 +03:00
public void Pause()
{
_frozen = true;
}
public void Resume()
{
_frozen = false;
}
2022-10-02 12:58:34 +03:00
}