forked from Serinoxxx/NGOAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayerConnectedEvents.cs
327 lines (275 loc) · 11.1 KB
/
PlayerConnectedEvents.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using MalbersAnimations.Controller;
using MalbersAnimations.Events;
using MalbersAnimations.Scriptables;
using MalbersAnimations.Utilities;
using MalbersAnimations.Weapons;
using System;
using System.Linq;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UIElements;
namespace MalbersAnimations.NetCode
{
public class PlayerConnectedEvents : NetworkBehaviour
{
[SerializeField] UnityEventRaiser cameraEventRaiser;
[SerializeField] TransformHook cameraTransformHook;
private MalbersInput malbersInput;
private MAnimal mAnimal;
[SerializeField] private Stats statsLocal;
[SerializeField] private Stats statsRemote;
private MEventListener eventListener;
private MDamageable damageable;
private MWeaponManager weaponManager;
private MInventory inventory;
[SerializeField] MPickUp pickUpDrop;
private MInteractor interactor;
[SerializeField] GameObject staminaSprintData;
private Aim aim;
[SerializeField] Transform aimTarget;
[SerializeField] StatID healthStatID;
[SerializeField] StatID staminaStatID;
public Transform GetMainCameraTarget()
{
return cameraTransformHook.transform;
}
private void Start()
{
mAnimal.ResetController();
}
public override void OnNetworkSpawn()
{
PlayerRegister.Instance.AddPlayer(this.NetworkObject);
aim = GetComponentInChildren<Aim>();
mAnimal = GetComponentInChildren<MAnimal>();
interactor = GetComponentInChildren<MInteractor>();
malbersInput = GetComponentInChildren<MalbersInput>();
eventListener = GetComponentInChildren<MEventListener>();
weaponManager = GetComponentInChildren<MWeaponManager>();
inventory = GetComponentInChildren<MInventory>();
damageable = GetComponentInChildren<MDamageable>();
// Make sure to find the camera after the player connects
mAnimal.FindCamera();
if (IsServer)
{
//Host/Server manages damage
EnableComponentIfNotNull(damageable);
}
if (IsOwner)
{
Destroy(statsRemote);
damageable.stats = statsLocal;
// Enable Player Specific Components
EnableComponentIfNotNull(cameraEventRaiser);
EnableComponentIfNotNull(cameraTransformHook);
EnableComponentIfNotNull(malbersInput);
EnableComponentIfNotNull(mAnimal);
EnableComponentIfNotNull(eventListener);
EnableComponentIfNotNull(weaponManager);
EnableComponentIfNotNull(inventory);
mAnimal.ResetController();
if (interactor != null)
{
var collider = interactor.GetComponent<Collider>();
collider.enabled = true;
interactor.InteractionArea = collider;
interactor.enabled = true;
}
else
{
Debug.LogWarning("Attempting to enable a null component.");
}
if (staminaSprintData != null)
{
staminaSprintData.SetActive(true);
}
else
{
Debug.LogWarning("Attempting to enable a null component.");
}
// Listen to the equipped weapon event
weaponManager.OnEquipWeapon.AddListener(OnEquippedWeapon);
weaponManager.OnUnequipWeapon.AddListener(OnUnequipWeapon);
// Listen to the onAim event
aim.OnAiming.AddListener(OnAiming);
// Listen to the animal states and modes
mAnimal.OnModeStart.AddListener(OnModeStart);
mAnimal.OnStateChange.AddListener(OnStateChange);
mAnimal.OnStanceChange.AddListener(OnStanceChange);
}
if (!IsOwner)
{
Destroy(statsLocal);
damageable.stats = statsRemote;
//If this is not ours then set the aim target
aim.AimTarget = aimTarget;
//Initialize the animal controller
mAnimal.ResetController();
//Shrink the pickup collider so it doesn't trigger UI events (can't seem to disable it, or other components re-enable it)
var pickupCollider = interactor.GetComponentInChildren<BoxCollider>();
pickupCollider.size = Vector3.zero;
}
}
private void OnStanceChange(int arg0)
{
//throw new NotImplementedException();
}
private void OnStateChange(int stateID)
{
OnStateChangeServerRpc(stateID);
}
[Rpc(SendTo.Server)]
private void OnStateChangeServerRpc(int stateID)
{
OnStateChangeClientRpc(stateID);
}
[Rpc(SendTo.NotOwner)]
private void OnStateChangeClientRpc(int stateID)
{
mAnimal.State_Activate(stateID);
}
private void OnModeStart(int arg0, int arg1)
{
//throw new NotImplementedException();
}
private void EnableComponentIfNotNull(MonoBehaviour component)
{
if (component != null)
{
component.enabled = true;
}
else
{
Debug.LogWarning($"Attempting to enable a null component:{component.name}");
}
}
private void LateUpdate()
{
//The owner should update the position of the aimTarget. The networkTransform component will send the updates to other clients
if (IsOwner)
{
aimTarget.position = aim.RawPoint;
}
}
#region OnAiming
private void OnAiming(bool arg0)
{
OnAimingServerRpc(arg0);
}
[Rpc(SendTo.Server)]
void OnAimingServerRpc(bool arg0)
{
OnAimingClientRpc(arg0);
}
[Rpc(SendTo.NotOwner)]
void OnAimingClientRpc(bool arg0)
{
aim.OnAiming.Invoke(arg0);
}
#endregion
#region WeaponManagerEvents
MWeapon currentWeapon;
MShootable lastShootable;
void OnEquippedWeapon(GameObject weaponGO)
{
// Find the unique Id for the weapon to send to server and request pickup
var weapon = weaponGO.GetComponent<MWeapon>();
//We own this weapon, so call the rpc to replicate a hit when we deal damage
weapon.OnHitPosition.AddListener((x)=>WeaponHitHandler(x));
//if (weapon is MShootable)
//{
// lastShootable = (MShootable)weapon;
// Debug.Log("Adding projectile handler to " + lastShootable.gameObject.name);
// lastShootable.OnFireProjectile.AddListener((x) => HandleProjectile(x)); //Call the event for the weapon
//}
var networkWeapon = weaponGO.GetComponent<NetworkWeapon>();
var uniqueWeaponId = networkWeapon.networkID;
currentWeapon = weapon;
EquipWeaponServerRpc(uniqueWeaponId);
Debug.Log($"Owner: I'm picking up weapon id: {uniqueWeaponId}");
// Tell the UI to update, must be done here so other clients don't updat their UI
NetworkPlayerUIController.Instance.UpdateInventoryUI(weapon.Holster, weapon.WeaponMode);
}
//private void HandleProjectile(GameObject x)
//{
// Debug.Log("Handling projectile: " + x.name);
// if (!IsOwner)
// {
// Debug.Log("Not owner, returning");
// return;
// }
// var projectile = x.GetComponent<MProjectile>();
// projectile.SetEnable(false);
// if (projectile != null)
// {
// var position = x.transform.position;
// var velocity = projectile.Velocity;
// //Destroy the projectile on the client, we only want the server to spawn it
// x.name = "client side fish";
// Destroy(x);
// SpawnProjectileServerRpc(position, velocity);
// }
//}
//[Rpc(SendTo.Server)]
//private void SpawnProjectileServerRpc(Vector3 position, Vector3 velocity)
//{
// var projectileGO = Instantiate(lastShootable.Projectile, position, Quaternion.identity);
// var projectile = projectileGO.GetComponent<MProjectile>();
// projectile.Velocity = velocity;
// projectile.Prepare(gameObject, lastShootable.Gravity, velocity, lastShootable.Layer, lastShootable.TriggerInteraction);
// projectile.Fire(velocity);
// projectile.GetComponent<NetworkObject>().Spawn();
// Debug.Log("Spawning projectile on server for: " + gameObject.name);
//}
void WeaponHitHandler(Vector3 position)
{
WeaponHitRpc(position);
}
[Rpc(SendTo.NotOwner)]
void WeaponHitRpc(Vector3 position)
{
if (currentWeapon == null)
{
return;
}
currentWeapon.OnHit?.Invoke(currentWeapon.transform);
Instantiate(currentWeapon.HitEffect, position, Quaternion.identity);
}
[Rpc(SendTo.Server)]
void EquipWeaponServerRpc(int uniqueWeaponId)
{
//TODO: Verify client is allowed to pick up
//...
Debug.Log($"Server: I'm telling everyone that {gameObject.name} is picking up id: {uniqueWeaponId}");
//Tell other clients to equip this weapon on this character
EquipWeaponClientRpc(uniqueWeaponId);
}
[Rpc(SendTo.NotOwner)]
void EquipWeaponClientRpc(int uniqueWeaponId)
{
Debug.Log($"Client: the server told me that {gameObject.name} is picking up id: {uniqueWeaponId}");
var weapon = FindObjectsByType<NetworkWeapon>(FindObjectsSortMode.None).First(x => x.networkID == uniqueWeaponId);
if (weapon != null)
{
currentWeapon = weapon.GetComponent<MWeapon>();
var pickable = weapon.GetComponent<Pickable>();
pickUpDrop.Item = pickable;
pickUpDrop.PickUpItem();
pickable.enabled = false;
}
else
{
//Weapon should be found but throw error if not
Debug.LogError($"Invalid uniqueWeaponId {uniqueWeaponId}. Not found on client");
}
}
private void OnUnequipWeapon(GameObject weaponGO)
{
currentWeapon = null;
var weapon = weaponGO.GetComponent<MWeapon>();
weapon.OnHitPosition.RemoveListener((x)=> WeaponHitHandler(x));
NetworkPlayerUIController.Instance.UpdateInventoryUI(weapon.Holster, weapon.WeaponMode);
}
#endregion
}
}