forked from Serinoxxx/NGOAC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkShootable.cs
67 lines (53 loc) · 1.94 KB
/
NetworkShootable.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
// In NetworkShootable class
using System;
using MalbersAnimations;
using MalbersAnimations.Weapons;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Events;
namespace MalbersAnimations.NetCode
{
public class NetworkShootable : MonoBehaviour
{
private MShootable mShootable;
private NetworkAnimal networkAnimal;
private void Start()
{
Debug.Log("Start method called.");
// Get the reference to MShootable using GetComponent
mShootable = GetComponent<MShootable>();
mShootable.OnEquiped.AddListener(AddFiredListener);
mShootable.OnUnequiped.AddListener(RemoveFiredListener);
}
private void RemoveFiredListener(Transform arg0)
{
if (networkAnimal == null) return;
mShootable.OnFireProjectile.RemoveListener(networkAnimal.SpawnProjectileOnOtherClients);
}
private void AddFiredListener(Transform arg0)
{
networkAnimal = GetComponentInParent<NetworkAnimal>();
if (networkAnimal == null) return;
if (mShootable != null)
{
Debug.Log("MShootable component found. Subscribing to events.");
// Subscribe to the OnFireProjectile event of MShootable
mShootable.OnFireProjectile.AddListener(networkAnimal.SpawnProjectileOnOtherClients);
}
else
{
Debug.LogError("MShootable component not found on the same GameObject.");
}
}
// Make sure to clean up the event subscription when the object is destroyed
private void OnDestroy()
{
if (networkAnimal == null) return;
Debug.Log("OnDestroy method called.");
if (mShootable != null)
{
mShootable.OnFireProjectile.RemoveListener(networkAnimal.SpawnProjectileOnOtherClients);
}
}
}
}