-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathGenericNetworkObjectBehaviour.cs
171 lines (148 loc) · 5.63 KB
/
GenericNetworkObjectBehaviour.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
using Unity.Netcode;
using Unity.Netcode.Components;
using UnityEngine;
namespace TestProject.ManualTests
{
/// <summary>
/// A general object that can be used for testing purposes
/// </summary>
public class GenericNetworkObjectBehaviour : NetworkBehaviour
{
/// <summary>
/// Tells us that we are registered with a NetworkPefab pool
/// This is primarily for late joining clients and object synchronization.
/// </summary>
public bool IsRegisteredPoolObject;
/// <summary>
/// This tells us that the NetworkObject has been removed from a pool
/// This is primarily to handle NetworkPrefab pool that was loaded in an additive scene and the
/// additive scene was unloaded but the NetworkObject persisted (i.e. was spawned in a different scene)
/// </summary>
public bool IsRemovedFromPool;
[SerializeField]
[Tooltip("This will make the spawned objects move around randomly. !Caution! You can generate a lot of objects this way!")]
private bool m_MoveRandomly = true;
[SerializeField]
private bool m_Rotate = false;
[SerializeField]
private Vector3 m_RotationAmount = new Vector3(0.0f, 1.0f, 0.0f);
[Header("Manual Testing")]
[SerializeField]
[Tooltip("When enabled this will make connected clients attempt to change the transform locally which should generate a" +
" console log warning message on the client side.")]
private bool m_TestClientSideNotifcation = false;
private Rigidbody m_RigidBody;
private MeshRenderer m_MeshRenderer;
private NetworkTransform m_NetworkTransform;
private Vector3 m_Direction;
private float m_Velocity;
private void Start()
{
m_RigidBody = GetComponent<Rigidbody>();
m_MeshRenderer = GetComponent<MeshRenderer>();
m_NetworkTransform = GetComponent<NetworkTransform>();
}
/// <summary>
/// Makes mesh renderer visible again
/// </summary>
public void Reset()
{
if (m_MeshRenderer == null)
{
m_MeshRenderer = GetComponent<MeshRenderer>();
}
m_MeshRenderer.enabled = true;
}
public void ShouldMoveRandomly(bool shouldMoveRandomly)
{
m_MoveRandomly = shouldMoveRandomly;
}
/// <summary>
/// Sets the object's direction and velocity
/// </summary>
/// <param name="direction">vector3 direction</param>
/// <param name="velocity">float velocity</param>
public void SetDirectionAndVelocity(Vector3 direction, float velocity)
{
m_Direction = direction;
m_Direction.Normalize();
m_Direction.y = 0;
m_Velocity = velocity;
}
/// <summary>
/// Handles moving the object based on its current direction and velocity
/// </summary>
private void FixedUpdate()
{
// Don't do anything until spawned
if (!IsSpawned)
{
return;
}
if (IsServer)
{
m_RigidBody.MovePosition(transform.position + m_Direction * (m_Velocity * Time.fixedDeltaTime));
if (m_Rotate && m_NetworkTransform != null && (m_NetworkTransform.SyncRotAngleX || m_NetworkTransform.SyncRotAngleY || m_NetworkTransform.SyncRotAngleZ))
{
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + m_RotationAmount);
}
if (m_MoveRandomly && Random.Range(0.0f, 1.0f) < 0.01f)
{
var dir = Random.insideUnitCircle;
m_Direction.x = dir.x;
m_Direction.z = dir.y;
}
}
else
{
if (m_TestClientSideNotifcation)
{
// When LogLevel is Developer this should generate a warning message on the client side
m_RigidBody.MovePosition(transform.position + Vector3.one * (2.0f * Time.fixedDeltaTime));
}
if (NetworkObject != null && !NetworkObject.isActiveAndEnabled)
{
Debug.LogWarning($"{nameof(GenericNetworkObjectBehaviour)} id {NetworkObject.NetworkObjectId} is not active and enabled but game object is still active!");
}
}
}
private void Update()
{
if (IsOwner && m_ShouldDespawn && NetworkObject != null)
{
m_ShouldDespawn = false;
if (NetworkObject.NetworkManager != null)
{
NetworkObject.Despawn();
}
}
}
public override void OnNetworkDespawn()
{
if (m_RigidBody != null)
{
m_RigidBody.Sleep();
m_RigidBody.transform.position = Vector3.zero;
}
m_Direction = Vector3.zero;
base.OnNetworkDespawn();
}
[HideInInspector]
public bool HasHandler;
private bool m_ShouldDespawn;
private void OnTriggerEnter(Collider other)
{
if (IsSpawned && IsOwner && !m_ShouldDespawn)
{
if (other.CompareTag("GenericObject") || other.CompareTag("Floor"))
{
return;
}
else
{
m_ShouldDespawn = true;
}
}
}
}
}