-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathAnimatedCubeController.cs
More file actions
159 lines (142 loc) · 4.37 KB
/
AnimatedCubeController.cs
File metadata and controls
159 lines (142 loc) · 4.37 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
using System.Collections;
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
namespace Tests.Manual.NetworkAnimatorTests
{
[RequireComponent(typeof(Animator))]
public class AnimatedCubeController : NetworkBehaviour
{
public int TestIterations = 20;
private Animator m_Animator;
private bool m_Rotate;
private NetworkAnimator m_NetworkAnimator;
private bool m_IsServerAuthoritative = true;
private void Awake()
{
m_Animator = GetComponent<Animator>();
m_NetworkAnimator = GetComponent<NetworkAnimator>();
if (m_NetworkAnimator == null)
{
m_NetworkAnimator = GetComponent<OwnerNetworkAnimator>();
if (m_NetworkAnimator != null)
{
m_IsServerAuthoritative = false;
}
else
{
throw new System.Exception($"{nameof(AnimatedCubeController)} requires that it is paired with either a {nameof(NetworkAnimator)} or {nameof(OwnerNetworkAnimator)}. Neither of the two components were found!");
}
}
m_Rotate = m_Animator.GetBool("Rotate");
}
public override void OnNetworkSpawn()
{
if (HasAuthority())
{
enabled = false;
}
}
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)
{
PlayPulseAnimationServerRpc(m_Rotate);
}
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 interations = 0;
while (interations < TestIterations)
{
var counter = 1.0f;
m_NetworkAnimator.SetTrigger("TestTrigger");
while (counter < 100)
{
m_Animator.SetFloat("TestFloat", counter);
m_Animator.SetInteger("TestInt", (int)counter);
counter++;
yield return null;
}
interations++;
}
yield return null;
}
}
}