forked from sh-akira/VirtualMotionCapture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationController.cs
More file actions
143 lines (124 loc) · 5.13 KB
/
Copy pathAnimationController.cs
File metadata and controls
143 lines (124 loc) · 5.13 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System.Collections.Generic;
using UnityEngine;
namespace VMC
{
public class AnimationController
{
public class AnimationItem
{
public float Time { get; set; } //アニメーションにかける時間
public float StartValue { get; set; }
public float EndValue { get; set; }
public System.Action<float> SetAction { get; set; }
public System.Func<float> TimeInitializer { get; set; }
public void RunAction(float value)
{
SetAction?.Invoke(value);
}
public void Initialize()
{
if (TimeInitializer != null)
{
Time = TimeInitializer();
}
}
}
private bool isStart = false;
private float startTime = 0.0f;
public System.Action ResetAction { get; set; }
public List<AnimationItem> AnimationItems = new List<AnimationItem>();
public Dictionary<float, AnimationItem> CurrentAnimationItems = new Dictionary<float, AnimationItem>(); //Key:開始時間
private AnimationItem EndLastItem = null;
private AnimationItem CurrentItem = null;
private void InitializeAnimation()
{
CurrentAnimationItems.Clear();
EndLastItem = null;
CurrentItem = null;
var starttime = 0.0f;
foreach (var item in AnimationItems)
{
item.Initialize();
CurrentAnimationItems.Add(starttime, item);
starttime += item.Time == 0.0f ? 0.0001f : item.Time;
}
}
public void AddResetAction(System.Action resetAction)
{
ResetAction = resetAction;
}
public void AddWait(float? time, System.Func<float> timeInitializer = null)
{
AddAnimation(time, 0.0f, 0.0f, null, timeInitializer);
}
public void AddAnimation(float? time, float startValue, float endValue, System.Action<float> setAction, System.Func<float> timeInitializer = null)
{
AnimationItems.Add(new AnimationItem { Time = time ?? 0.0f, StartValue = startValue, EndValue = endValue, SetAction = setAction, TimeInitializer = timeInitializer });
}
public void Reset()
{
StopAnimations();
ResetAction?.Invoke();
}
public void ClearAnimations()
{
AnimationItems.Clear();
}
public void StopAnimations()
{
isStart = false;
lastitem = null;
}
private AnimationItem lastitem = null;
public bool Next()
{
if (isStart == false)
{
isStart = true;
startTime = Time.time;
InitializeAnimation();
}
var elapsedTime = Time.time - startTime;
var addTime = 0.0f;
foreach (var item in CurrentAnimationItems)
{
addTime = item.Key + item.Value.Time; //すべてのアニメーションの時間+今のアニメーション時間
if (addTime >= elapsedTime)
{//経過時間がまだアニメーションの終了時間に届いていない間(アニメーション中)
//Debug.Log($"AnimationTime:{elapsedTime}");
if (lastitem != null && EndLastItem != lastitem)
{//前回のアニメーションが終わりまで行ってない場合があるので100%で実行
lastitem.RunAction(lastitem.EndValue);
EndLastItem = lastitem;
}
if (CurrentItem != item.Value)
{
if (lastitem != null)
{//前回のアニメーションが終わりまで行ってない場合があるので100%で実行
lastitem.RunAction(lastitem.EndValue);
EndLastItem = lastitem;
}
//新しいアニメーションになったときには時間にかかわらずきちんと最初の値を使う
item.Value.RunAction(item.Value.StartValue);
CurrentItem = item.Value;
}
else
{
var currentTime = item.Value.Time + (elapsedTime - addTime);
var setvalue = item.Value.StartValue + (item.Value.EndValue - item.Value.StartValue) * (currentTime / item.Value.Time);
item.Value.RunAction(setvalue);
}
lastitem = item.Value;
return true;
}
}
//最後までアニメーションしたとき
if (lastitem != null)
{//最後のアニメーションが終わりまで行ってない場合があるので100%で実行
lastitem.RunAction(lastitem.EndValue);
}
isStart = false;
return false;
}
}
}