1
1
using System . Collections . Generic ;
2
- using System . IO ;
3
2
using Unity . Netcode ;
4
3
using UnityEngine ;
5
- using BossRoom . Scripts . Shared . Net . NetworkObjectPool ;
6
4
7
5
namespace Unity . Multiplayer . Samples . BossRoom . Server
8
6
{
9
-
10
7
public class ServerProjectileLogic : NetworkBehaviour
11
8
{
12
- private bool m_Started = false ;
9
+ bool m_Started ;
13
10
14
11
[ SerializeField ]
15
- private NetworkProjectileState m_NetState ;
12
+ NetworkProjectileState m_NetState ;
16
13
17
14
[ SerializeField ]
18
- private SphereCollider m_OurCollider ;
15
+ SphereCollider m_OurCollider ;
19
16
20
17
/// <summary>
21
18
/// The character that created us. Can be 0 to signal that we were created generically by the server.
22
19
/// </summary>
23
- private ulong m_SpawnerId ;
20
+ ulong m_SpawnerId ;
24
21
25
22
/// <summary>
26
23
/// The data for our projectile. Indicates speed, damage, etc.
27
24
/// </summary>
28
- private ActionDescription . ProjectileInfo m_ProjectileInfo ;
25
+ ActionDescription . ProjectileInfo m_ProjectileInfo ;
29
26
30
- private const int k_MaxCollisions = 4 ;
31
- private const float k_WallLingerSec = 2f ; //time in seconds that arrows linger after hitting a target.
32
- private const float k_EnemyLingerSec = 0.2f ; //time after hitting an enemy that we persist.
33
- private Collider [ ] m_CollisionCache = new Collider [ k_MaxCollisions ] ;
27
+ const int k_MaxCollisions = 4 ;
28
+ const float k_WallLingerSec = 2f ; //time in seconds that arrows linger after hitting a target.
29
+ const float k_EnemyLingerSec = 0.2f ; //time after hitting an enemy that we persist.
30
+ Collider [ ] m_CollisionCache = new Collider [ k_MaxCollisions ] ;
34
31
35
32
/// <summary>
36
33
/// Time when we should destroy this arrow, in Time.time seconds.
37
34
/// </summary>
38
- private float m_DestroyAtSec ;
35
+ float m_DestroyAtSec ;
39
36
40
- private int m_CollisionMask ; //mask containing everything we test for while moving
41
- private int m_BlockerMask ; //physics mask for things that block the arrow's flight.
42
- private int m_NPCLayer ;
37
+ int m_CollisionMask ; //mask containing everything we test for while moving
38
+ int m_BlockerMask ; //physics mask for things that block the arrow's flight.
39
+ int m_NPCLayer ;
43
40
44
41
/// <summary>
45
42
/// List of everyone we've hit and dealt damage to.
@@ -49,12 +46,12 @@ public class ServerProjectileLogic : NetworkBehaviour
49
46
/// But that's fine by us! We use <c>m_HitTargets.Count</c> to tell us how many total enemies we've hit,
50
47
/// so those nulls still count as hits.
51
48
/// </remarks>
52
- private List < GameObject > m_HitTargets = new List < GameObject > ( ) ;
49
+ List < GameObject > m_HitTargets = new List < GameObject > ( ) ;
53
50
54
51
/// <summary>
55
52
/// Are we done moving?
56
53
/// </summary>
57
- private bool m_IsDead ;
54
+ bool m_IsDead ;
58
55
59
56
/// <summary>
60
57
/// Set everything up based on provided projectile information.
@@ -80,12 +77,12 @@ public override void OnNetworkSpawn()
80
77
81
78
m_DestroyAtSec = Time . fixedTime + ( m_ProjectileInfo . Range / m_ProjectileInfo . Speed_m_s ) ;
82
79
83
- m_CollisionMask = LayerMask . GetMask ( new [ ] { "NPCs" , "Default" , "Ground " } ) ;
84
- m_BlockerMask = LayerMask . GetMask ( new [ ] { "Default" , "Ground " } ) ;
80
+ m_CollisionMask = LayerMask . GetMask ( new [ ] { "NPCs" , "Default" , "Environment " } ) ;
81
+ m_BlockerMask = LayerMask . GetMask ( new [ ] { "Default" , "Environment " } ) ;
85
82
m_NPCLayer = LayerMask . NameToLayer ( "NPCs" ) ;
86
83
}
87
84
88
- private void FixedUpdate ( )
85
+ void FixedUpdate ( )
89
86
{
90
87
if ( ! m_Started ) { return ; } //don't do anything before OnNetworkSpawn has run.
91
88
@@ -105,13 +102,13 @@ private void FixedUpdate()
105
102
}
106
103
}
107
104
108
- private void DetectCollisions ( )
105
+ void DetectCollisions ( )
109
106
{
110
- Vector3 position = transform . localToWorldMatrix . MultiplyPoint ( m_OurCollider . center ) ;
111
- int numCollisions = Physics . OverlapSphereNonAlloc ( position , m_OurCollider . radius , m_CollisionCache , m_CollisionMask ) ;
107
+ var position = transform . localToWorldMatrix . MultiplyPoint ( m_OurCollider . center ) ;
108
+ var numCollisions = Physics . OverlapSphereNonAlloc ( position , m_OurCollider . radius , m_CollisionCache , m_CollisionMask ) ;
112
109
for ( int i = 0 ; i < numCollisions ; i ++ )
113
110
{
114
- int layerTest = 1 << m_CollisionCache [ i ] . gameObject . layer ;
111
+ var layerTest = 1 << m_CollisionCache [ i ] . gameObject . layer ;
115
112
if ( ( layerTest & m_BlockerMask ) != 0 )
116
113
{
117
114
//hit a wall; leave it for a couple of seconds.
@@ -139,15 +136,16 @@ private void DetectCollisions()
139
136
m_NetState . RecvHitEnemyClientRPC ( targetNetObj . NetworkObjectId ) ;
140
137
141
138
//retrieve the person that created us, if he's still around.
142
- NetworkObject spawnerNet ;
143
- NetworkManager . Singleton . SpawnManager . SpawnedObjects . TryGetValue ( m_SpawnerId , out spawnerNet ) ;
144
- ServerCharacter spawnerObj = spawnerNet != null ? spawnerNet . GetComponent < ServerCharacter > ( ) : null ;
139
+ NetworkManager . Singleton . SpawnManager . SpawnedObjects . TryGetValue ( m_SpawnerId , out var spawnerNet ) ;
140
+ var spawnerObj = spawnerNet != null ? spawnerNet . GetComponent < ServerCharacter > ( ) : null ;
145
141
146
142
m_CollisionCache [ i ] . GetComponent < IDamageable > ( ) . ReceiveHP ( spawnerObj , - m_ProjectileInfo . Damage ) ;
147
143
}
148
144
149
145
if ( m_IsDead )
146
+ {
150
147
return ; // don't keep examining collisions since we can't damage anybody else
148
+ }
151
149
}
152
150
}
153
151
}
0 commit comments