-
Notifications
You must be signed in to change notification settings - Fork 539
/
Copy pathPhysicsWrapper.cs
60 lines (48 loc) · 1.74 KB
/
PhysicsWrapper.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
using System;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
namespace Unity.BossRoom.Gameplay.GameplayObjects.Character
{
/// <summary>
/// Wrapper class for direct references to components relevant to physics.
/// Each instance of a PhysicsWrapper is registered to a static dictionary, indexed by the NetworkObject's ID.
/// </summary>
/// <remarks>
/// The root GameObject of PCs & NPCs is not the object which will move through the world, so other classes will
/// need a quick reference to a PC's/NPC's in-game position.
/// </remarks>
public class PhysicsWrapper : NetworkBehaviour
{
static Dictionary<ulong, PhysicsWrapper> m_PhysicsWrappers = new Dictionary<ulong, PhysicsWrapper>();
[SerializeField]
Transform m_Transform;
public Transform Transform => m_Transform;
[SerializeField]
Collider m_DamageCollider;
public Collider DamageCollider => m_DamageCollider;
ulong m_NetworkObjectID;
public override void OnNetworkSpawn()
{
m_PhysicsWrappers.Add(NetworkObjectId, this);
m_NetworkObjectID = NetworkObjectId;
}
public override void OnNetworkDespawn()
{
RemovePhysicsWrapper();
}
public override void OnDestroy()
{
base.OnDestroy();
RemovePhysicsWrapper();
}
void RemovePhysicsWrapper()
{
m_PhysicsWrappers.Remove(m_NetworkObjectID);
}
public static bool TryGetPhysicsWrapper(ulong networkObjectID, out PhysicsWrapper physicsWrapper)
{
return m_PhysicsWrappers.TryGetValue(networkObjectID, out physicsWrapper);
}
}
}