-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathGenericPooledObjectBehaviour.cs
More file actions
186 lines (167 loc) · 5.68 KB
/
GenericPooledObjectBehaviour.cs
File metadata and controls
186 lines (167 loc) · 5.68 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
using Unity.Netcode;
using UnityEngine;
/// <summary>
/// A general object used for pooling purposes
/// </summary>
public class GenericPooledObjectBehaviour : NetworkBehaviour
{
[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;
[HideInInspector]
public bool HasHandler;
[HideInInspector]
public int SyncrhonizedObjectTypeIndex;
private Rigidbody m_RigidBody;
private MeshRenderer m_MeshRenderer;
private Vector3 m_Direction;
private float m_Velocity;
private float m_VisibilitySpawn;
private bool m_ShouldDespawn;
private void Start()
{
m_RigidBody = GetComponent<Rigidbody>();
m_MeshRenderer = GetComponent<MeshRenderer>();
}
/// <summary>
/// Handles disabling the MeshRenderer when the client despawns a NetworkObject
/// </summary>
public override void OnNetworkDespawn()
{
if (!IsServer)
{
if (m_MeshRenderer == null)
{
m_MeshRenderer = GetComponent<MeshRenderer>();
}
if (m_MeshRenderer != null)
{
m_MeshRenderer.enabled = false;
}
var objectLabel = GetComponentInChildren<NetworkObjectLabel>();
if (objectLabel != null)
{
objectLabel.enabled = false;
}
}
base.OnNetworkDespawn();
}
/// <summary>
/// Handles setting a delay before the newly spawned object is visible
/// </summary>
public override void OnNetworkSpawn()
{
if (!IsServer)
{
if (m_MeshRenderer == null)
{
m_MeshRenderer = GetComponent<MeshRenderer>();
}
m_MeshRenderer.enabled = false;
var objectLabel = GetComponentInChildren<NetworkObjectLabel>();
if (objectLabel != null)
{
objectLabel.enabled = false;
}
m_VisibilitySpawn = Time.realtimeSinceStartup + 0.12f;
}
base.OnNetworkSpawn();
}
/// <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()
{
if (NetworkManager != null && NetworkManager.IsListening)
{
if (IsOwner)
{
m_RigidBody.MovePosition(transform.position + m_Direction * (m_Velocity * Time.fixedDeltaTime));
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
{
// All of the below is strictly for debugging purposes. None of these conditions should ever be true.
if (NetworkObject != null && !NetworkObject.isActiveAndEnabled)
{
Debug.LogWarning($"{nameof(GenericPooledObjectBehaviour)} id {NetworkObject.NetworkObjectId} is not active and enabled but game object is still active!");
}
if (NetworkObject != null && !NetworkObject.IsSpawned)
{
Debug.LogWarning($"{nameof(GenericPooledObjectBehaviour)} id {NetworkObject.NetworkObjectId} is not spawned but still active and enabled (forcing set active false)");
gameObject.SetActive(false);
}
}
}
}
/// <summary>
/// This update handles server side despawning and enables client side visibility
/// </summary>
private void Update()
{
if (IsOwner && m_ShouldDespawn && NetworkObject != null)
{
m_ShouldDespawn = false;
NetworkObject.Despawn(HasHandler);
if (!HasHandler)
{
NetworkObject.gameObject.SetActive(false);
NetworkObject.gameObject.transform.position = Vector3.zero;
}
}
else if (!IsServer)
{
// This is here to handle any short term latency between the time
// an object becomes spawned to the time it takes to update its first
// position.
if (m_MeshRenderer != null && !m_MeshRenderer.enabled)
{
if (m_VisibilitySpawn < Time.realtimeSinceStartup)
{
m_MeshRenderer.enabled = true;
var objectLabel = GetComponentInChildren<NetworkObjectLabel>();
if (objectLabel != null)
{
objectLabel.enabled = true;
}
}
}
}
}
/// <summary>
/// Host and Server Only:
/// When triggered this will start the despawn process
/// </summary>
/// <param name="other"></param>
private void OnTriggerEnter(Collider other)
{
if (IsOwner && gameObject.activeInHierarchy)
{
if (other.CompareTag("GenericObject") || other.CompareTag("Floor"))
{
return;
}
else
{
m_ShouldDespawn = true;
}
}
}
}