forked from Tr1ll10ns/Godot_FPS_Tutorial_CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationPlayerManager.cs
193 lines (157 loc) · 5.73 KB
/
AnimationPlayerManager.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using Godot;
using System;
using System.Collections.Generic;
public class AnimationPlayerManager : AnimationPlayer
{
public enum AnimationState
{
Idle_unarmed,
Pistol_equip,
Pistol_fire,
Pistol_idle,
Pistol_reload,
Pistol_unequip,
Rifle_equip,
Rifle_fire,
Rifle_idle,
Rifle_reload,
Rifle_unequip,
Knife_equip,
Knife_fire,
Knife_idle,
Knife_unequip
}
Dictionary<AnimationState, string[]> states = new Dictionary<AnimationState, string[]>() {
{ AnimationState.Idle_unarmed, new string[] { "Knife_equip", "Pistol_equip", "Rifle_equip", "Idle_unarmed"} },
{ AnimationState.Pistol_equip, new string[] { "Pistol_idle"} },
{ AnimationState.Pistol_fire, new string[] { "Pistol_idle"} },
{ AnimationState.Pistol_idle, new string[] { "Pistol_fire", "Pistol_reload", "Pistol_unequip", "Pistol_idle"} },
{ AnimationState.Pistol_reload, new string[] { "Pistol_idle"} },
{ AnimationState.Pistol_unequip, new string[] { "Idle_unarmed"} },
{ AnimationState.Rifle_equip, new string[] { "Rifle_idle"} },
{ AnimationState.Rifle_fire, new string[] { "Rifle_idle"} },
{ AnimationState.Rifle_idle, new string[] { "Rifle_fire", "Rifle_reload", "Rifle_unequip", "Rifle_idle"} },
{ AnimationState.Rifle_reload, new string[] { "Rifle_idle"} },
{ AnimationState.Rifle_unequip, new string[] { "Idle_unarmed"} },
{ AnimationState.Knife_equip, new string[] { "Knife_idle"} },
{ AnimationState.Knife_fire, new string[] { "Knife_idle"} },
{ AnimationState.Knife_idle, new string[] { "Knife_fire", "Knife_unequip", "Knife_idle"} },
{ AnimationState.Knife_unequip, new string[] { "Idle_unarmed"} }
};
Dictionary<AnimationState, float> animationSpeeds = new Dictionary<AnimationState, float>()
{
{AnimationState.Idle_unarmed, 1f},
{AnimationState.Pistol_equip, 1.4f},
{AnimationState.Pistol_fire, 1.8f},
{AnimationState.Pistol_idle, 1f},
{AnimationState.Pistol_reload, 1f},
{AnimationState.Pistol_unequip, 1.4f},
{AnimationState.Rifle_equip, 2f},
{AnimationState.Rifle_fire, 6f},
{AnimationState.Rifle_idle, 1f},
{AnimationState.Rifle_reload, 1.45f},
{AnimationState.Rifle_unequip, 2f},
{AnimationState.Knife_equip, 1f},
{AnimationState.Knife_fire, 1.35f},
{AnimationState.Knife_idle, 1f},
{AnimationState.Knife_unequip, 1f}
};
public AnimationState currentState = AnimationState.Idle_unarmed;
public Action CallbackFunction = null;
public override void _Ready()
{
// SetAnimation(AnimationState.Idle_unarmed);
Connect("animation_finished", this, "AnimationEnded");
}
public bool SetAnimation(AnimationState animationName)
{
if (animationName == currentState)
{
GD.Print("AnimationPlayer_Manager.cs -- WARNING: animation is already ", animationName);
return true;
}
if (HasAnimation(animationName.ToString()))
{
var possibleAnimations = states[currentState];
if (Array.IndexOf(possibleAnimations, animationName.ToString()) != -1)
{
currentState = animationName;
Play(animationName.ToString(), -1, animationSpeeds[animationName]);
return true;
}
else
{
GD.Print("AnimationPlayer_Manager.cs -- WARNING: Cannot change to ", animationName, " from ", currentState);
return false;
}
}
return false;
}
public void AnimationEnded(AnimationState animationName)
{
// UNARMED transitions
if (currentState == AnimationState.Idle_unarmed)
{
}
// KNIFE transitions
else if (currentState == AnimationState.Knife_equip)
{
SetAnimation(AnimationState.Knife_idle);
}
else if (currentState == AnimationState.Knife_idle)
{
}
else if (currentState == AnimationState.Knife_fire)
{
SetAnimation(AnimationState.Knife_idle);
}
else if (currentState == AnimationState.Knife_unequip)
{
SetAnimation(AnimationState.Idle_unarmed);
}
// PISTOL transitions
else if (currentState == AnimationState.Pistol_equip)
{
SetAnimation(AnimationState.Pistol_idle);
}
else if (currentState == AnimationState.Pistol_idle)
{
}
else if (currentState == AnimationState.Pistol_fire)
{
SetAnimation(AnimationState.Pistol_idle);
}
else if (currentState == AnimationState.Pistol_unequip)
{
SetAnimation(AnimationState.Idle_unarmed);
}
else if (currentState == AnimationState.Pistol_reload)
{
SetAnimation(AnimationState.Pistol_idle);
}
// RIFLE transitions
else if (currentState == AnimationState.Rifle_equip)
{
SetAnimation(AnimationState.Rifle_idle);
}
else if (currentState == AnimationState.Rifle_idle)
{
}
else if (currentState == AnimationState.Rifle_fire)
{
SetAnimation(AnimationState.Rifle_idle);
}
else if (currentState == AnimationState.Rifle_unequip)
{
SetAnimation(AnimationState.Idle_unarmed);
}
else if (currentState == AnimationState.Rifle_reload)
{
SetAnimation(AnimationState.Rifle_idle);
}
}
public void AnimationCallback()
{
CallbackFunction?.Invoke();
}
}