Exemplo n.º 1
0
 public void AddComponents(ref GhostTypeState ghostType, in NativeArray <ComponentType> components)
Exemplo n.º 2
0
        protected override void OnUpdate()
        {
            if (!HasSingleton <GhostPrefabCollectionComponent>())
            {
                return;
            }

            if (!Serializers.IsCreated)
            {
                CreateSerializersCollection();
            }

            if (GhostTypeCollection.IsCreated)
            {
                return;
            }

            GhostTypeCollection = new NativeList <GhostTypeState>(16, Allocator.Persistent);
            IndexCollection     = new NativeList <GhostComponentIndex>(16, Allocator.Persistent);
            GhostTypeCollection.Clear();
            IndexCollection.Clear();

            var ghostCollection = GetSingleton <GhostPrefabCollectionComponent>();
            var prefabList      = EntityManager.GetBuffer <GhostPrefabBuffer>(ghostCollection.ServerPrefabs);

            for (int prefab = 0; prefab < prefabList.Length; prefab++)
            {
                var prefabEntity           = prefabList[prefab].Value;
                var fallbackPredictionMode = GhostSpawnBuffer.Type.Interpolated;
                if (prefabList[prefab].PrefabType == GhostPrefabType.PredictedClient)
                {
                    fallbackPredictionMode = GhostSpawnBuffer.Type.Predicted;
                }
                bool isOwnerPredicted = prefabList[prefab].IsOwner;
                var  ghostType        = new GhostTypeState
                {
                    FirstComponent         = IndexCollection.Length,
                    NumComponents          = 0,
                    PredictionOwnerOffset  = -1,
                    SnapshotSize           = 0,
                    FallbackPredictionMode = fallbackPredictionMode
                };

                var components = EntityManager.GetComponentTypes(prefabEntity);
                AddComponents(ref ghostType, in components);
                components.Dispose();

                // 处理Owner的偏移
                if (!isOwnerPredicted)
                {
                    ghostType.PredictionOwnerOffset = -1;
                }
                else
                {
                    ghostType.PredictionOwnerOffset += GlobalConstants.TickSize;
                    // Debug.Log($"ghostType.PredictionOwnerOffset={ghostType.PredictionOwnerOffset}");
                }

                // 留出Tick空间.快照在Client对于每个Ghost来说,都要在每个快照的头上存放Tick。
                ghostType.SnapshotSize += GlobalConstants.TickSize;
                GhostTypeCollection.Add(ghostType);
            }
        }