@@ -7,73 +7,78 @@ namespace MLAPI.NetworkingManagerComponents
77{
88 public static class NetworkPoolManager
99 {
10- internal static Dictionary < string , NetworkPool > Pools ;
11- //We want to keep the pool indexes incrementing, this is to prevent new pools getting old names and the wrong objects being spawned.
10+ internal static Dictionary < ushort , NetworkPool > Pools ;
1211 private static ushort PoolIndex = 0 ;
12+ internal static Dictionary < string , ushort > PoolNamesToIndexes ;
1313
14- internal static Dictionary < ushort , string > PoolIndexToPoolName = new Dictionary < ushort , string > ( ) ;
15- internal static Dictionary < string , ushort > PoolNamesToIndexes = new Dictionary < string , ushort > ( ) ;
16-
17- public static void CreatePool ( string poolName , GameObject poolPrefab , uint size = 16 )
14+ //Server only
15+ public static void CreatePool ( string poolName , int spawnablePrefabIndex , uint size = 16 )
1816 {
19- if ( Pools . ContainsKey ( poolName ) )
17+ if ( ! NetworkingManager . singleton . isServer )
2018 {
21- Debug . LogWarning ( "MLAPI: A pool with the name " + poolName + " already exists ") ;
19+ Debug . LogWarning ( "MLAPI: Pools can only be created on the server " ) ;
2220 return ;
2321 }
24- else if ( poolPrefab == null )
25- {
26- Debug . LogWarning ( "MLAPI: A pool prefab is required" ) ;
27- }
28- PoolIndexToPoolName . Add ( PoolIndex , poolName ) ;
22+ NetworkPool pool = new NetworkPool ( spawnablePrefabIndex , size , PoolIndex ) ;
2923 PoolNamesToIndexes . Add ( poolName , PoolIndex ) ;
3024 PoolIndex ++ ;
31- Pools . Add ( poolName , new NetworkPool ( poolPrefab , size , poolName ) ) ;
3225 }
3326
34-
35- public static void CreatePool ( string poolName , GameObject [ ] poolPrefabs )
27+ public static void DestroyPool ( string poolName )
3628 {
37- if ( Pools . ContainsKey ( poolName ) )
29+ if ( ! NetworkingManager . singleton . isServer )
3830 {
39- Debug . LogWarning ( "MLAPI: A pool with the name " + poolName + " already exists ") ;
31+ Debug . LogWarning ( "MLAPI: Pools can only be destroyed on the server " ) ;
4032 return ;
4133 }
42- else if ( poolPrefabs == null )
34+ for ( int i = 0 ; i < Pools [ PoolNamesToIndexes [ poolName ] ] . objects . Length ; i ++ )
4335 {
44- Debug . LogWarning ( "MLAPI: A pool prefab array is required" ) ;
36+ MonoBehaviour . Destroy ( Pools [ PoolNamesToIndexes [ poolName ] ] . objects [ i ] ) ;
4537 }
46- PoolIndexToPoolName . Add ( PoolIndex , poolName ) ;
47- PoolNamesToIndexes . Add ( poolName , PoolIndex ) ;
48- PoolIndex ++ ;
49- Pools . Add ( poolName , new NetworkPool ( poolPrefabs , poolName ) ) ;
38+ Pools . Remove ( PoolNamesToIndexes [ poolName ] ) ;
5039 }
5140
5241 public static GameObject SpawnPoolObject ( string poolName , Vector3 position , Quaternion rotation )
5342 {
54- if ( NetworkingManager . singleton . isServer )
43+ if ( ! NetworkingManager . singleton . isServer )
44+ {
45+ Debug . LogWarning ( "MLAPI: Object spawning can only occur on server" ) ;
46+ return null ;
47+ }
48+ GameObject go = Pools [ PoolNamesToIndexes [ poolName ] ] . SpawnObject ( position , rotation ) ;
49+ using ( MemoryStream stream = new MemoryStream ( 28 ) )
5550 {
56- using ( MemoryStream stream = new MemoryStream ( 26 ) )
51+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
5752 {
58- using ( BinaryWriter writer = new BinaryWriter ( stream ) )
59- {
60- writer . Write ( PoolNamesToIndexes [ poolName ] ) ;
61- writer . Write ( position . x ) ;
62- writer . Write ( position . y ) ;
63- writer . Write ( position . z ) ;
64- writer . Write ( rotation . eulerAngles . x ) ;
65- writer . Write ( rotation . eulerAngles . y ) ;
66- writer . Write ( rotation . eulerAngles . z ) ;
67- }
68- NetworkingManager . singleton . Send ( "MLAPI_SPAWN_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
53+ writer . Write ( go . GetComponent < NetworkedObject > ( ) . NetworkId ) ;
54+ writer . Write ( position . x ) ;
55+ writer . Write ( position . y ) ;
56+ writer . Write ( position . z ) ;
57+ writer . Write ( rotation . eulerAngles . x ) ;
58+ writer . Write ( rotation . eulerAngles . y ) ;
59+ writer . Write ( rotation . eulerAngles . z ) ;
6960 }
61+ NetworkingManager . singleton . Send ( "MLAPI_SPAWN_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
7062 }
71- return Pools [ poolName ] . SpawnObject ( position , rotation ) ;
63+ return go ;
7264 }
7365
74- public static void DestroyPoolObject ( GameObject gameObject )
66+ public static void DestroyPoolObject ( NetworkedObject netObject )
7567 {
76- gameObject . SetActive ( false ) ;
68+ if ( ! NetworkingManager . singleton . isServer )
69+ {
70+ Debug . LogWarning ( "MLAPI: Objects can only be destroyed on the server" ) ;
71+ return ;
72+ }
73+ netObject . gameObject . SetActive ( false ) ;
74+ using ( MemoryStream stream = new MemoryStream ( 4 ) )
75+ {
76+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
77+ {
78+ writer . Write ( netObject . NetworkId ) ;
79+ }
80+ NetworkingManager . singleton . Send ( "MLAPI_DESTROY_POOL_OBJECT" , "MLAPI_RELIABLE_FRAGMENTED_SEQUENCED" , stream . GetBuffer ( ) ) ;
81+ }
7782 }
7883 }
7984}
0 commit comments