-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnimationHandler.cs
98 lines (80 loc) · 2.4 KB
/
AnimationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
namespace Nitemare3D
{
public sealed class AnimationFrame
{
public int index;
public Action action = null;
}
public sealed class Animation
{
static int animCount = 0;
public int id;
public int duration; //duration in ms
public List<AnimationFrame> frames = new List<AnimationFrame>();
public bool loop;
public Action onEnd;
public Animation(int start, int count, int duration, bool loop = true, Action onEnd = null)
{
for (int i = start; i < start + count; i++)
{
AnimationFrame anim = new AnimationFrame();
anim.index = i;
frames.Add(anim);
}
id = animCount;
animCount++;
this.onEnd = onEnd;
this.duration = duration;
this.loop = loop;
}
}
public sealed class AnimationHandler
{
public int index;
int frameIndex = 0;
Animation current;
float timer;
int currentID = -1;
//returns false if asked to play the same animation or if the current animation is not completed
public bool LoadAnimation(Animation anim)
{
if(currentID == anim.id){return false;}
if(currentID > -1)
{
//return if current anim is not done
if(frameIndex < current.frames.Count-1){return false;}
}
current = anim;
currentID = anim.id;
index = current.frames[0].index;
timer = 0;
frameIndex = 0;
return true;
}
public void Update()
{
timer += Time.dt * 1000;
if (timer > current.duration)
{
timer = 0;
frameIndex++;
if (frameIndex >= current.frames.Count)
{
if (current.loop)
{
frameIndex = 0;
}
else
{
frameIndex = current.frames.Count-1;
}
current.onEnd?.Invoke();
timer = 0;
}
}
index = current.frames[frameIndex].index;
}
}
}