77using NitroxModel . DataStructures . GameLogic . Entities . Metadata ;
88using NitroxModel . DataStructures . Util ;
99using NitroxModel . Helper ;
10- using NitroxModel . Logger ;
1110using NitroxModel . Packets ;
1211using NitroxModel_Subnautica . DataStructures ;
1312using UnityEngine ;
@@ -36,9 +35,7 @@ public void BroadcastTransforms(Dictionary<NitroxId, GameObject> gameObjectsById
3635
3736 foreach ( KeyValuePair < NitroxId , GameObject > gameObjectWithId in gameObjectsById )
3837 {
39- GameObject go = gameObjectWithId . Value ;
40-
41- if ( go )
38+ if ( gameObjectWithId . Value )
4239 {
4340 update . AddUpdate ( gameObjectWithId . Key , gameObjectWithId . Value . transform . position . ToDto ( ) , gameObjectWithId . Value . transform . rotation . ToDto ( ) ) ;
4441 }
@@ -49,21 +46,20 @@ public void BroadcastTransforms(Dictionary<NitroxId, GameObject> gameObjectsById
4946
5047 public void BroadcastMetadataUpdate ( NitroxId id , EntityMetadata metadata )
5148 {
52- EntityMetadataUpdate update = new EntityMetadataUpdate ( id , metadata ) ;
53- packetSender . Send ( update ) ;
49+ packetSender . Send ( new EntityMetadataUpdate ( id , metadata ) ) ;
5450 }
5551
5652 public void Spawn ( List < Entity > entities )
5753 {
5854 foreach ( Entity entity in entities )
5955 {
60- LargeWorldStreamer . main . cellManager . UnloadBatchCells ( ToInt3 ( entity . AbsoluteEntityCell . CellId ) ) ; // Just in case
56+ LargeWorldStreamer . main . cellManager . UnloadBatchCells ( entity . AbsoluteEntityCell . CellId . ToUnity ( ) ) ; // Just in case
6157
62- if ( alreadySpawnedIds . Contains ( entity . Id ) )
58+ if ( WasSpawnedByServer ( entity . Id ) )
6359 {
6460 UpdatePosition ( entity ) ;
6561 }
66- else if ( entity . ParentId != null && ! alreadySpawnedIds . Contains ( entity . ParentId ) )
62+ else if ( entity . ParentId != null && ! WasSpawnedByServer ( entity . ParentId ) )
6763 {
6864 AddPendingParentEntity ( entity ) ;
6965 }
@@ -79,12 +75,11 @@ public void Spawn(List<Entity> entities)
7975 private EntityCell EnsureCell ( Entity entity )
8076 {
8177 EntityCell entityCell ;
82- BatchCells batchCells ;
8378
84- Int3 batchId = ToInt3 ( entity . AbsoluteEntityCell . BatchId ) ;
85- Int3 cellId = ToInt3 ( entity . AbsoluteEntityCell . CellId ) ;
79+ Int3 batchId = entity . AbsoluteEntityCell . BatchId . ToUnity ( ) ;
80+ Int3 cellId = entity . AbsoluteEntityCell . CellId . ToUnity ( ) ;
8681
87- if ( ! batchCellsById . TryGetValue ( batchId , out batchCells ) )
82+ if ( ! batchCellsById . TryGetValue ( batchId , out BatchCells batchCells ) )
8883 {
8984 batchCells = LargeWorldStreamer . main . cellManager . InitializeBatchCells ( batchId ) ;
9085 }
@@ -102,11 +97,6 @@ private EntityCell EnsureCell(Entity entity)
10297 return entityCell ;
10398 }
10499
105- private Int3 ToInt3 ( NitroxModel . DataStructures . Int3 int3 )
106- {
107- return new Int3 ( int3 . X , int3 . Y , int3 . Z ) ;
108- }
109-
110100 private void Spawn ( Entity entity , Optional < GameObject > parent )
111101 {
112102 alreadySpawnedIds . Add ( entity . Id ) ;
@@ -116,28 +106,27 @@ private void Spawn(Entity entity, Optional<GameObject> parent)
116106 IEntitySpawner entitySpawner = entitySpawnerResolver . ResolveEntitySpawner ( entity ) ;
117107 Optional < GameObject > gameObject = entitySpawner . Spawn ( entity , parent , cellRoot ) ;
118108
119- foreach ( Entity childEntity in entity . ChildEntities )
109+ if ( ! entitySpawner . SpawnsOwnChildren ( ) )
120110 {
121- if ( ! alreadySpawnedIds . Contains ( childEntity . Id ) && ! entitySpawner . SpawnsOwnChildren ( ) )
111+ foreach ( Entity childEntity in entity . ChildEntities )
122112 {
123- Spawn ( childEntity , gameObject ) ;
113+ if ( ! WasSpawnedByServer ( childEntity . Id ) )
114+ {
115+ Spawn ( childEntity , gameObject ) ;
116+ }
124117 }
125-
126- alreadySpawnedIds . Add ( childEntity . Id ) ;
127118 }
128119 }
129120
130121 private void SpawnAnyPendingChildren ( Entity entity )
131122 {
132- List < Entity > pendingEntities ;
133-
134- if ( pendingParentEntitiesByParentId . TryGetValue ( entity . Id , out pendingEntities ) )
123+ if ( pendingParentEntitiesByParentId . TryGetValue ( entity . Id , out List < Entity > pendingEntities ) )
135124 {
136125 Optional < GameObject > parent = NitroxEntity . GetObjectFrom ( entity . Id ) ;
137126
138127 foreach ( Entity child in pendingEntities )
139128 {
140- Spawn ( entity , parent ) ;
129+ Spawn ( child , parent ) ;
141130 }
142131
143132 pendingParentEntitiesByParentId . Remove ( entity . Id ) ;
@@ -163,9 +152,7 @@ private void UpdatePosition(Entity entity)
163152
164153 private void AddPendingParentEntity ( Entity entity )
165154 {
166- List < Entity > pendingEntities ;
167-
168- if ( ! pendingParentEntitiesByParentId . TryGetValue ( entity . ParentId , out pendingEntities ) )
155+ if ( ! pendingParentEntitiesByParentId . TryGetValue ( entity . ParentId , out List < Entity > pendingEntities ) )
169156 {
170157 pendingEntities = new List < Entity > ( ) ;
171158 pendingParentEntitiesByParentId [ entity . ParentId ] = pendingEntities ;
@@ -174,15 +161,8 @@ private void AddPendingParentEntity(Entity entity)
174161 pendingEntities . Add ( entity ) ;
175162 }
176163
177- public bool WasSpawnedByServer ( NitroxId id )
178- {
179- return alreadySpawnedIds . Contains ( id ) ;
180- }
181-
182- public bool RemoveEntity ( NitroxId id )
183- {
184- return alreadySpawnedIds . Remove ( id ) ;
185- }
164+ public bool WasSpawnedByServer ( NitroxId id ) => alreadySpawnedIds . Contains ( id ) ;
186165
166+ public bool RemoveEntity ( NitroxId id ) => alreadySpawnedIds . Remove ( id ) ;
187167 }
188168}
0 commit comments