-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathAnimatedCubeController.cs
More file actions
253 lines (220 loc) · 7.21 KB
/
AnimatedCubeController.cs
File metadata and controls
253 lines (220 loc) · 7.21 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
namespace Tests.Manual.NetworkAnimatorTests
{
[RequireComponent(typeof(Animator))]
public class AnimatedCubeController : NetworkBehaviour
{
private Animator m_Animator;
private bool m_Rotate;
private NetworkAnimator m_NetworkAnimator;
private bool m_IsServerAuthoritative = true;
private void DetermineNetworkAnimatorComponentType()
{
m_NetworkAnimator = GetComponent<NetworkAnimator>();
if (m_NetworkAnimator != null)
{
m_IsServerAuthoritative = m_NetworkAnimator.GetType() != typeof(OwnerNetworkAnimator);
}
else
{
throw new Exception($"{nameof(AnimatedCubeController)} requires that it is paired with either a {nameof(NetworkAnimator)} or {nameof(OwnerNetworkAnimator)}. Neither of the two components were found!");
}
}
public override void OnNetworkSpawn()
{
DetermineNetworkAnimatorComponentType();
m_Animator = GetComponent<Animator>();
m_Rotate = m_Animator.GetBool("Rotate");
}
private bool HasAuthority()
{
if (IsOwnerAuthority() || IsServerAuthority())
{
return true;
}
return false;
}
private bool IsServerAuthority()
{
if (IsServer && m_IsServerAuthoritative)
{
return true;
}
return false;
}
private bool IsOwnerAuthority()
{
if (IsOwner && !m_IsServerAuthoritative)
{
return true;
}
return false;
}
[ServerRpc(RequireOwnership = false)]
private void ToggleRotateAnimationServerRpc(bool rotate)
{
m_Rotate = rotate;
m_Animator.SetBool("Rotate", m_Rotate);
}
internal void ToggleRotateAnimation()
{
m_Rotate = !m_Rotate;
if (m_IsServerAuthoritative)
{
if (!IsServer && IsOwner)
{
ToggleRotateAnimationServerRpc(m_Rotate);
}
else if (IsServer && IsOwner)
{
m_Animator.SetBool("Rotate", m_Rotate);
}
}
else if (IsOwner)
{
m_Animator.SetBool("Rotate", m_Rotate);
}
}
[ServerRpc(RequireOwnership = false)]
private void PlayPulseAnimationServerRpc(bool rotate)
{
m_NetworkAnimator.SetTrigger("Pulse");
}
internal void PlayPulseAnimation()
{
if (m_IsServerAuthoritative)
{
if (!IsServer && IsOwner)
{
m_NetworkAnimator.SetTrigger("Pulse");
}
else if (IsServer && IsOwner)
{
m_NetworkAnimator.SetTrigger("Pulse");
}
}
else if (IsOwner)
{
m_NetworkAnimator.SetTrigger("Pulse");
}
}
private Coroutine m_TestAnimatorRoutine;
internal void TestAnimator(bool useNetworkAnimator = true)
{
if (IsServer)
{
if (m_TestAnimatorRoutine == null)
{
m_TestAnimatorRoutine = StartCoroutine(TestAnimatorRoutine());
}
}
}
private IEnumerator TestAnimatorRoutine()
{
var waitForSeconds = new WaitForSeconds(0.016f);
var counter = 1.0f;
Debug.Log("Linearly increase test:");
while (counter < 100)
{
m_Animator.SetFloat("TestFloat", counter);
m_Animator.SetInteger("TestInt", (int)counter);
counter++;
yield return waitForSeconds;
}
Debug.Log("Random value assignment test:");
counter = 0.0f;
while (counter < 100)
{
m_Animator.SetFloat("TestFloat", UnityEngine.Random.Range(0.0f, 100.0f));
m_Animator.SetInteger("TestInt", UnityEngine.Random.Range(0, 100));
counter++;
yield return waitForSeconds;
}
StopCoroutine(m_TestAnimatorRoutine);
m_TestAnimatorRoutine = null;
}
private int m_TestIntValue;
private float m_TestFloatValue;
private void DisplayTestIntValueIfChanged()
{
var testIntValue = m_Animator.GetInteger("TestInt");
if (m_TestIntValue != testIntValue)
{
m_TestIntValue = testIntValue;
Debug.Log($"[{name}]TestInt value changed to = {m_TestIntValue}");
}
var testFloatValue = m_Animator.GetFloat("TestFloat");
if (m_TestFloatValue != testFloatValue)
{
m_TestFloatValue = testFloatValue;
Debug.Log($"[{name}]TestFloat value changed to = {m_TestIntValue}");
}
}
private void BeginAttack(int weaponType)
{
m_Animator.SetInteger("WeaponType", weaponType);
m_NetworkAnimator.SetTrigger("Attack");
}
private void SetLayerWeight(int layer, float weight)
{
m_Animator.SetLayerWeight(layer, weight);
}
private float GetLayerWeight(int layer)
{
return m_Animator.GetLayerWeight(layer);
}
private void LateUpdate()
{
if (!IsSpawned || !IsOwner)
{
if (!IsOwner && IsSpawned)
{
if (Input.GetKeyDown(KeyCode.Alpha4))
{
Debug.Log($"Layer 1 weight: {GetLayerWeight(1)}");
}
DisplayTestIntValueIfChanged();
return;
}
return;
}
DisplayTestIntValueIfChanged();
// Rotates the cube
if (Input.GetKeyDown(KeyCode.C))
{
ToggleRotateAnimation();
}
// Pulse animation (scale down and up slowly)
if (Input.GetKeyDown(KeyCode.Space))
{
PlayPulseAnimation();
}
// Test changing Animator parameters over time
if (Input.GetKeyDown(KeyCode.T))
{
TestAnimator();
}
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log($"[{name}] TestInt value = {m_TestIntValue}");
Debug.Log($"[{name}] TestInt value = {m_TestIntValue}");
}
if (Input.GetKeyDown(KeyCode.Alpha1))
{
BeginAttack(1);
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
BeginAttack(2);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
SetLayerWeight(1, 0.75f);
}
}
}
}