Exemplo n.º 1
0
            public override void ExecuteReplication(ComponentGroup replicationGroup, ComponentSystemBase system, global::Improbable.Worker.Core.Connection connection)
            {
                Profiler.BeginSample("NestedComponent");

                var chunkArray          = replicationGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var spatialOSEntityType = system.GetArchetypeChunkComponentType <SpatialEntityId>(true);
                var componentType       = system.GetArchetypeChunkComponentType <Improbable.Gdk.Tests.NestedComponent.Component>();

                foreach (var chunk in chunkArray)
                {
                    var entityIdArray  = chunk.GetNativeArray(spatialOSEntityType);
                    var componentArray = chunk.GetNativeArray(componentType);
                    for (var i = 0; i < componentArray.Length; i++)
                    {
                        var data        = componentArray[i];
                        var dirtyEvents = 0;

                        if (data.DirtyBit || dirtyEvents > 0)
                        {
                            var update = new global::Improbable.Worker.Core.SchemaComponentUpdate(20152);
                            Improbable.Gdk.Tests.NestedComponent.Serialization.SerializeUpdate(data, update);

                            // Send serialized update over the wire
                            connection.SendComponentUpdate(entityIdArray[i].EntityId, new global::Improbable.Worker.Core.ComponentUpdate(update));

                            data.DirtyBit     = false;
                            componentArray[i] = data;
                        }
                    }
                }

                chunkArray.Dispose();
                Profiler.EndSample();
            }
Exemplo n.º 2
0
        protected override void OnUpdate()
        {
            var chunkArray       = m_Group.CreateArchetypeChunkArray(Allocator.TempJob);
            var entityType       = GetArchetypeChunkEntityType();
            var knightType       = GetArchetypeChunkComponentType <Knight>(true);
            var orcWolfRiderType = GetArchetypeChunkComponentType <OrcWolfRider>(true);
            var skeletonType     = GetArchetypeChunkComponentType <Skeleton>(true);

            for (var chunkIndex = 0; chunkIndex < chunkArray.Length; chunkIndex++)
            {
                var chunk       = chunkArray[chunkIndex];
                var entityArray = chunk.GetNativeArray(entityType);

                if (chunk.Has(knightType))
                {
                    m_KnightList.AddRange(entityArray);
                }
                else if (chunk.Has(orcWolfRiderType))
                {
                    m_OrcWolfRiderList.AddRange(entityArray);
                }
                else if (chunk.Has(skeletonType))
                {
                    m_SkeletonList.AddRange(entityArray);
                }
            }

            chunkArray.Dispose();
            AddToPool(m_KnightPool, m_KnightList);
            AddToPool(m_OrcWolfRiderPool, m_OrcWolfRiderList);
            AddToPool(m_SkeletonPool, m_SkeletonList);
            m_KnightList.Clear();
            m_OrcWolfRiderList.Clear();
            m_SkeletonList.Clear();
        }
        protected override void OnUpdate()
        {
            var heartbeatType     = GetArchetypeChunkComponentType <HeartbeatData>();
            var entityDeleterType = GetArchetypeChunkComponentType <WorldCommands.DeleteEntity.CommandSender>();
            var responsesType     =
                GetArchetypeChunkComponentType <PlayerHeartbeatClient.CommandResponses.PlayerHeartbeat>(true);
            var spatialIdType = GetArchetypeChunkComponentType <SpatialEntityId>(true);

            var chunkArray = group.CreateArchetypeChunkArray(Allocator.TempJob);

            foreach (var chunk in chunkArray)
            {
                var heartbeats       = chunk.GetNativeArray(heartbeatType);
                var responses        = chunk.GetNativeArray(responsesType);
                var deleteRequesters = chunk.GetNativeArray(entityDeleterType);
                var spatialIds       = chunk.GetNativeArray(spatialIdType);

                for (var i = 0; i < responses.Length; i++)
                {
                    var heartbeatData = heartbeats[i];

                    var responded = false;
                    foreach (var response in responses[i].Responses)
                    {
                        if (response.StatusCode == StatusCode.Success)
                        {
                            responded = true;
                            break;
                        }
                    }

                    if (responded)
                    {
                        heartbeatData.NumFailedHeartbeats = 0;
                    }
                    else
                    {
                        heartbeatData.NumFailedHeartbeats += 1;

                        if (heartbeatData.NumFailedHeartbeats >= PlayerLifecycleConfig.MaxNumFailedPlayerHeartbeats)
                        {
                            var entityId = spatialIds[i].EntityId;
                            deleteRequesters[i].RequestsToSend.Add(WorldCommands.DeleteEntity.CreateRequest
                                                                   (
                                                                       entityId
                                                                   ));

                            worker.LogDispatcher.HandleLog(LogType.Log,
                                                           new LogEvent(
                                                               $"A client failed to respond to too many heartbeats. Deleting their player.")
                                                           .WithField("EntityID", entityId));
                        }
                    }

                    heartbeats[i] = heartbeatData;
                }
            }

            chunkArray.Dispose();
        }
Exemplo n.º 4
0
        void UpdateMissingVisibleLocalToWorld()
        {
            var localToWorldOrderVersion = EntityManager.GetComponentOrderVersion <LocalToWorld>();

            if (localToWorldOrderVersion == m_LastLocalToWorldOrderVersion)
            {
                return;
            }

            EntityCommandBuffer entityCommandBuffer = new EntityCommandBuffer(Allocator.Temp);
            var entityType = GetArchetypeChunkEntityType();
            var chunks     = m_MissingVisibleLocalToWorldQuery.CreateArchetypeChunkArray(Allocator.TempJob);

            for (int i = 0; i < chunks.Length; i++)
            {
                var chunk    = chunks[i];
                var entities = chunk.GetNativeArray(entityType);
                for (int j = 0; j < chunk.Count; j++)
                {
                    var entity = entities[j];
                    entityCommandBuffer.AddComponent(entity, default(VisibleLocalToWorld));
                }
            }

            entityCommandBuffer.Playback(EntityManager);
            entityCommandBuffer.Dispose();
            chunks.Dispose();

            m_LastLocalToWorldOrderVersion = localToWorldOrderVersion;
        }
            public override void AcknowledgeAuthorityLoss(ComponentGroup group, ComponentSystemBase system,
                                                          Improbable.Worker.Core.Connection connection)
            {
                var authorityLossType = system.GetArchetypeChunkComponentType <AuthorityLossImminent <Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFields.Component> >();
                var spatialEntityType = system.GetArchetypeChunkComponentType <SpatialEntityId>();

                var chunkArray = group.CreateArchetypeChunkArray(Allocator.TempJob);

                foreach (var chunk in chunkArray)
                {
                    var authorityArray       = chunk.GetNativeArray(authorityLossType);
                    var spatialEntityIdArray = chunk.GetNativeArray(spatialEntityType);

                    for (int i = 0; i < authorityArray.Length; ++i)
                    {
                        if (authorityArray[i].AcknowledgeAuthorityLoss)
                        {
                            connection.SendAuthorityLossImminentAcknowledgement(spatialEntityIdArray[i].EntityId,
                                                                                1003);
                        }
                    }
                }

                chunkArray.Dispose();
            }
Exemplo n.º 6
0
        protected override void OnUpdate()
        {
            var chunkArray      = m_Group.CreateArchetypeChunkArray(Allocator.TempJob);
            var targetType      = GetArchetypeChunkComponentType <Target>();
            var translationType = GetArchetypeChunkComponentType <Translation>();
            var destinationType = GetArchetypeChunkComponentType <Destination>();

            for (var chunkIndex = 0; chunkIndex < chunkArray.Length; chunkIndex++)
            {
                var chunk            = chunkArray[chunkIndex];
                var translationArray = chunk.GetNativeArray(translationType);
                var destinationArray = chunk.GetNativeArray(destinationType);

                if (chunk.Has(targetType))
                {
                    for (int entityIndex = 0; entityIndex < chunk.Count; entityIndex++)
                    {
                        Debug.DrawLine(translationArray[entityIndex].Value, destinationArray[entityIndex].Value, Color.red);
                    }
                }
                else
                {
                    for (int entityIndex = 0; entityIndex < chunk.Count; entityIndex++)
                    {
                        Debug.DrawLine(translationArray[entityIndex].Value, destinationArray[entityIndex].Value, Color.green);
                    }
                }
            }

            chunkArray.Dispose();
        }
    // this takes in the compGroup and entityType, and gives us out all entities from all chunks within the group.
    public NativeArray <Entity> GetEntities(ComponentGroup compGroup, ArchetypeChunkEntityType entityType)
    {
        NativeArray <ArchetypeChunk> dataChunks = compGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        NativeList <ArchetypeChunk> chunkList = new NativeList <ArchetypeChunk> (Allocator.Temp);

        NativeList <Entity> entityList = new NativeList <Entity> (Allocator.Temp);

        for (int d = 0; d < dataChunks.Length; d++)
        {
            chunkList.Add(dataChunks [d]);
        }
        dataChunks.Dispose();

        for (int c = 0; c < chunkList.Length; c++)
        {
            ArchetypeChunk dataChunk = chunkList [c];

            NativeArray <Entity> entityHolder = dataChunk.GetNativeArray(entityType);

            for (int i = 0; i < entityHolder.Length; i++)
            {
                entityList.Add(entityHolder [i]);
            }
        }
        chunkList.Dispose();

        NativeArray <Entity> chunkEntities = new NativeArray <Entity> (entityList.Length, Allocator.Persistent);

        chunkEntities.CopyFrom(entityList);

        entityList.Dispose();
        return(chunkEntities);
    }
Exemplo n.º 8
0
        private void UpdateFollowTargetEntity()
        {
            var followTargetOrderVersion = _entityManager.GetComponentOrderVersion <FollowTarget>();

            if (followTargetOrderVersion == _followTargetOrderVersion)
            {
                return;
            }

            var followTargetChunks = _componentGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            if (followTargetChunks.Length > 0)
            {
                Assert.IsTrue(
                    followTargetChunks.Length == 1 && followTargetChunks[0].Count == 1,
                    $"Only one followed target is supported. Make sure component {typeof(FollowTarget)} is only in one Entity."
                    );
                _followTargetEntity =
                    followTargetChunks[0].GetNativeArray(_entityManager.GetArchetypeChunkEntityType())[0];
            }
            else
            {
                _followTargetEntity = Entity.Null;
            }

            followTargetChunks.Dispose();

            _followTargetOrderVersion = followTargetOrderVersion;
        }
        void VerifyQueryCount(ComponentGroup group, int expectedCount)
        {
            var chunks = group.CreateArchetypeChunkArray(Allocator.TempJob);

            Assert.AreEqual(expectedCount, ArchetypeChunkArray.CalculateEntityCount(chunks));
            chunks.Dispose();
        }
        protected override void OnUpdate()
        {
            var chunkArray       = m_Group.CreateArchetypeChunkArray(Allocator.TempJob);
            var translationType  = GetArchetypeChunkComponentType <Translation>(true);
            var knightType       = GetArchetypeChunkComponentType <Knight>(true);
            var orcWolfRiderType = GetArchetypeChunkComponentType <OrcWolfRider>(true);
            var skeletonType     = GetArchetypeChunkComponentType <Skeleton>(true);

            for (var chunkIndex = 0; chunkIndex < chunkArray.Length; chunkIndex++)
            {
                var chunk            = chunkArray[chunkIndex];
                var translationArray = chunk.GetNativeArray(translationType);

                for (var entityIndex = 0; entityIndex < chunk.Count; entityIndex++)
                {
                    var translation = translationArray[entityIndex];

                    if (chunk.Has(knightType))
                    {
                        Debug.DrawRay(translation.Value, math.up(), Color.blue);
                    }
                    else if (chunk.Has(orcWolfRiderType))
                    {
                        Debug.DrawRay(translation.Value, math.up(), Color.magenta);
                    }
                    else if (chunk.Has(skeletonType))
                    {
                        Debug.DrawRay(translation.Value, math.up(), Color.black);
                    }
                }
            }

            chunkArray.Dispose();
        }
Exemplo n.º 11
0
            public override void ExecuteReplication(ComponentGroup replicationGroup, ComponentSystemBase system, global::Improbable.Worker.CInterop.Connection connection)
            {
                Profiler.BeginSample("ExhaustiveBlittableSingular");

                var chunkArray          = replicationGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var spatialOSEntityType = system.GetArchetypeChunkComponentType <SpatialEntityId>(true);
                var componentType       = system.GetArchetypeChunkComponentType <Improbable.Gdk.Tests.ExhaustiveBlittableSingular.Component>();

                foreach (var chunk in chunkArray)
                {
                    var entityIdArray  = chunk.GetNativeArray(spatialOSEntityType);
                    var componentArray = chunk.GetNativeArray(componentType);
                    for (var i = 0; i < componentArray.Length; i++)
                    {
                        var data         = componentArray[i];
                        var eventsToSend = 0;

                        if (data.IsDataDirty() || eventsToSend > 0)
                        {
                            var update = new global::Improbable.Worker.CInterop.SchemaComponentUpdate(197720);
                            Improbable.Gdk.Tests.ExhaustiveBlittableSingular.Serialization.SerializeUpdate(data, update);

                            // Send serialized update over the wire
                            connection.SendComponentUpdate(entityIdArray[i].EntityId.Id, new global::Improbable.Worker.CInterop.ComponentUpdate(update));

                            data.MarkDataClean();
                            componentArray[i] = data;
                        }
                    }
                }

                chunkArray.Dispose();
                Profiler.EndSample();
            }
        protected override void OnUpdate()
        {
            var entityType = GetArchetypeChunkEntityType();

            var createEntityType     = GetArchetypeChunkComponentType <WorldCommands.CreateEntity.CommandResponses>();
            var deleteEntityType     = GetArchetypeChunkComponentType <WorldCommands.DeleteEntity.CommandResponses>();
            var reserveEntityIdsType =
                GetArchetypeChunkComponentType <WorldCommands.ReserveEntityIds.CommandResponses>();
            var entityQueryType = GetArchetypeChunkComponentType <WorldCommands.EntityQuery.CommandResponses>();

            var chunkArray = worldCommandResponseGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            foreach (var chunk in chunkArray)
            {
                var entityArray = chunk.GetNativeArray(entityType);

                if (chunk.Has(createEntityType))
                {
                    var responseArray = chunk.GetNativeArray(createEntityType);
                    for (var i = 0; i < entityArray.Length; i++)
                    {
                        WorldCommands.CreateEntity.ResponsesProvider.Free(responseArray[i].Handle);
                        PostUpdateCommands.RemoveComponent <WorldCommands.CreateEntity.CommandResponses>(entityArray[i]);
                    }
                }

                if (chunk.Has(deleteEntityType))
                {
                    var responseArray = chunk.GetNativeArray(deleteEntityType);
                    for (var i = 0; i < entityArray.Length; i++)
                    {
                        WorldCommands.DeleteEntity.ResponsesProvider.Free(responseArray[i].Handle);
                        PostUpdateCommands.RemoveComponent <WorldCommands.DeleteEntity.CommandResponses>(entityArray[i]);
                    }
                }

                if (chunk.Has(reserveEntityIdsType))
                {
                    var responseArray = chunk.GetNativeArray(reserveEntityIdsType);
                    for (var i = 0; i < entityArray.Length; i++)
                    {
                        WorldCommands.ReserveEntityIds.ResponsesProvider.Free(responseArray[i].Handle);
                        PostUpdateCommands.RemoveComponent <WorldCommands.ReserveEntityIds.CommandResponses>(
                            entityArray[i]);
                    }
                }

                if (chunk.Has(entityQueryType))
                {
                    var responseArray = chunk.GetNativeArray(entityQueryType);
                    for (var i = 0; i < entityArray.Length; i++)
                    {
                        WorldCommands.EntityQuery.ResponsesProvider.Free(responseArray[i].Handle);
                        PostUpdateCommands.RemoveComponent <WorldCommands.EntityQuery.CommandResponses>(entityArray[i]);
                    }
                }
            }

            chunkArray.Dispose();
        }
Exemplo n.º 13
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var rvChunks = _rVelocityGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        var partition = new Partition()
        {
            Positions    = rvChunks,
            Nearby       = GetBufferFromEntity <NearbyEntity>(false),
            EntityType   = GetArchetypeChunkEntityType(),
            PositionType = GetArchetypeChunkComponentType <Position>(true),
            RegionSize   = RegionSize
        };

        var partDep = partition.Schedule(this, inputDeps);

        var adjust = new AdjustVelocity()
        {
            Nearby           = GetBufferFromEntity <NearbyEntity>(true),
            Positions        = GetComponentDataFromEntity <Position>(true),
            rVelocities      = GetComponentDataFromEntity <ReciprocalVelocityData>(true),
            PredictionWindow = PredictionWindow
        };

        return(adjust.Schedule(this, partDep));

        //return JobHandle.CombineDependencies(partDep, adjustDep);
    }
        protected override void OnUpdate()
        {
            if (Time.time < timeOfNextHeartbeat)
            {
                return;
            }

            timeOfNextHeartbeat = Time.time + PlayerLifecycleConfig.PlayerHeartbeatIntervalSeconds;

            var chunkArray = group.CreateArchetypeChunkArray(Allocator.TempJob);

            var senderType    = GetArchetypeChunkComponentType <PlayerHeartbeatClient.CommandSenders.PlayerHeartbeat>();
            var spatialIdType = GetArchetypeChunkComponentType <SpatialEntityId>(true);

            foreach (var chunk in chunkArray)
            {
                var requestSenders = chunk.GetNativeArray(senderType);
                var spatialIds     = chunk.GetNativeArray(spatialIdType);

                for (var i = 0; i < requestSenders.Length; i++)
                {
                    requestSenders[i].RequestsToSend
                    .Add(PlayerHeartbeatClient.PlayerHeartbeat.CreateRequest(spatialIds[i].EntityId, new Empty()));
                }
            }

            chunkArray.Dispose();
        }
Exemplo n.º 15
0
        void UpdateChangeParents()
        {
            var changeParentsChunks = m_ExistingParentsGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            if (changeParentsChunks.Length > 0)
            {
                var parentType         = GetArchetypeChunkComponentType <Parent>(true);
                var previousParentType = GetArchetypeChunkComponentType <PreviousParent>(true);
                var entityType         = GetArchetypeChunkEntityType();
                var changedParents     = new NativeList <ChangedParent>(Allocator.TempJob);

                var filterChangedParentsJob = new FilterChangedParents
                {
                    Chunks             = changeParentsChunks,
                    ChangedParents     = changedParents,
                    ParentType         = parentType,
                    PreviousParentType = previousParentType,
                    EntityType         = entityType
                };
                var filterChangedParentsJobHandle = filterChangedParentsJob.Schedule();
                filterChangedParentsJobHandle.Complete();

                for (int i = 0; i < changedParents.Length; i++)
                {
                    var childEntity          = changedParents[i].ChildEntity;
                    var previousParentEntity = changedParents[i].PreviousParentEntity;
                    var parentEntity         = changedParents[i].ParentEntity;

                    RemoveChildFromParent(childEntity, previousParentEntity);
                    AddChildToParent(childEntity, parentEntity);
                }
                changedParents.Dispose();
            }
            changeParentsChunks.Dispose();
        }
        protected override void OnUpdate()
        {
            var chunks                 = _group.CreateArchetypeChunkArray(Allocator.TempJob);
            var jobHandles             = new NativeArray <JobHandle>(SetUpData.ChunkCount, Allocator.Persistent);
            var archetypeDataComponent = GetArchetypeChunkBufferType <DataComponent>();
            var counter                = 0;

            // ReSharper disable once ForCanBeConvertedToForeach
            for (var i = 0; i < chunks.Length; i++)
            {
                var chunk          = chunks[i];
                var dataComponents = chunk.GetBufferAccessor(archetypeDataComponent);
                for (var j = 0; j < dataComponents.Length; j++)
                {
                    var dataComponent = dataComponents[j];
                    jobHandles[counter] = new Job
                    {
                        DataComponent = (DataComponent *)dataComponent.GetUnsafePtr(),
                    }.Schedule(dataComponent.Length, SetUpData.BatchCountMultipleJobs);
                    counter++;
                }
            }

            JobHandle.CompleteAll(jobHandles);
            jobHandles.Dispose();
            chunks.Dispose();
        }
Exemplo n.º 17
0
        protected override void OnUpdate()
        {
            var chunks = _group.CreateArchetypeChunkArray(Allocator.TempJob);

            new Job
            {
                Chunks        = chunks,
                DataComponent = GetArchetypeChunkBufferType <DataComponent>(),
            }.Schedule(chunks.Length, 1).Complete();
        }
Exemplo n.º 18
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            JobHandle bulletHandle;
            JobHandle asteroidHandle;
            JobHandle levelHandle;
            JobHandle settingsHandle;

            var asteroidJob = new DestroyAsteroidJob
            {
                commandBuffer = barrier.CreateCommandBuffer().ToConcurrent(),
                bulletChunks  = bulletGroup.CreateArchetypeChunkArray(Allocator.TempJob, out bulletHandle),
                bulletAgeType = GetArchetypeChunkComponentType <BulletAgeComponentData>(true),
                positionType  = GetArchetypeChunkComponentType <Translation>(true),
                sphereType    = GetArchetypeChunkComponentType <CollisionSphereComponentData>(true),
                entityType    = GetArchetypeChunkEntityType(),
                level         = m_LevelGroup.ToComponentDataArray <LevelComponent>(Allocator.TempJob, out levelHandle)
            };
            var shipJob = new DestroyShipJob
            {
                commandBuffer    = barrier.CreateCommandBuffer().ToConcurrent(),
                asteroidChunks   = asteroidGroup.CreateArchetypeChunkArray(Allocator.TempJob, out asteroidHandle),
                bulletChunks     = asteroidJob.bulletChunks,
                bulletAgeType    = asteroidJob.bulletAgeType,
                positionType     = asteroidJob.positionType,
                sphereType       = asteroidJob.sphereType,
                playerIdType     = GetArchetypeChunkComponentType <PlayerIdComponentData>(),
                entityType       = asteroidJob.entityType,
                serverSettings   = settingsGroup.ToComponentDataArray <ServerSettings>(Allocator.TempJob, out settingsHandle),
                playerClearQueue = playerClearQueue.ToConcurrent(),
                level            = asteroidJob.level
            };
            var asteroidDep = JobHandle.CombineDependencies(inputDeps, bulletHandle, levelHandle);
            var shipDep     = JobHandle.CombineDependencies(asteroidDep, asteroidHandle, settingsHandle);

            var h1 = asteroidJob.Schedule(asteroidGroup, asteroidDep);
            var h2 = shipJob.Schedule(shipGroup, shipDep);

            var handle = JobHandle.CombineDependencies(h1, h2);

            barrier.AddJobHandleForProducer(handle);

            var cleanupShipJob = new ClearShipPointerJob
            {
                playerClearQueue = playerClearQueue,
                playerState      = GetComponentDataFromEntity <PlayerStateComponentData>()
            };
            var cleanupChunkJob = new ChunkCleanupJob
            {
                bulletChunks   = shipJob.bulletChunks,
                asteroidChunks = shipJob.asteroidChunks,
                level          = shipJob.level
            };

            return(JobHandle.CombineDependencies(cleanupShipJob.Schedule(h2), cleanupChunkJob.Schedule(handle)));
        }
        void UpdateDynamicChunkCache()
        {
            var dynamicChunksOrderVersion = EntityManager.GetComponentOrderVersion <VisibleLocalToWorld>();

            if (dynamicChunksOrderVersion == m_LastDynamicChunksOrderVersion)
            {
                return;
            }

            // Dispose
            if (m_DynamicChunks.IsCreated)
            {
                m_DynamicChunks.Dispose();
            }

            var sharedComponentCount     = EntityManager.GetSharedComponentCount();
            var meshInstanceRendererType = GetArchetypeChunkSharedComponentType <MeshInstanceRenderer>();

            // Allocate temp data
            var chunkRendererMap = new NativeMultiHashMap <int, int>(100000, Allocator.TempJob);
            var foundArchetypes  = new NativeList <EntityArchetype>(Allocator.TempJob);

            Profiler.BeginSample("CreateArchetypeChunkArray");
            var chunks = m_DynamicChunksQuery.CreateArchetypeChunkArray(Allocator.TempJob);

            Profiler.EndSample();

            m_DynamicChunks = new NativeArray <ArchetypeChunk>(chunks.Length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);

            var mapChunkRenderersJob = new MapChunkRenderers
            {
                Chunks = chunks,
                MeshInstanceRendererType = meshInstanceRendererType,
                ChunkRendererMap         = chunkRendererMap.ToConcurrent()
            };
            var mapChunkRenderersJobHandle = mapChunkRenderersJob.Schedule(chunks.Length, 64);

            var gatherSortedChunksJob = new GatherSortedChunks
            {
                ChunkRendererMap     = chunkRendererMap,
                SharedComponentCount = sharedComponentCount,
                SortedChunks         = m_DynamicChunks,
                Chunks = chunks
            };
            var gatherSortedChunksJobHandle = gatherSortedChunksJob.Schedule(mapChunkRenderersJobHandle);

            gatherSortedChunksJobHandle.Complete();

            foundArchetypes.Dispose();
            chunkRendererMap.Dispose();
            chunks.Dispose();

            m_LastDynamicChunksOrderVersion = dynamicChunksOrderVersion;
        }
Exemplo n.º 20
0
 protected override void OnUpdate()
 {
     if (geneticTrainerGroup.GetEntityArray().Length != 0)
     {
         float maxPoint = 0;
         for (int i = 0, length = checkPointComponentGroup.GetEntityArray().Length; i < length; ++i)
         {
             maxPoint += checkPointComponentGroup.GetComponentDataArray <PointComponentData>()[i].PointValue;
         }
         var chunks = geneticTrainerGroup.CreateArchetypeChunkArray(Allocator.TempJob);
         for (int i = 0, length = chunks.Length; i < length; ++i)
         {
             var chunk    = chunks[i];
             var entities = chunk.GetNativeArray(GetArchetypeChunkEntityType());
             for (int j = 0, trainerCount = entities.Length; j < length; ++j)
             {
                 var entity         = entities[j];
                 var geneticTrainer = EntityManager.GetComponentObject <Transform>(entity).GetComponent <GeneticTrainerComponent>();
                 var timer          = EntityManager.GetComponentData <TimerData>(entity);
                 timer.Stopped = 1;
                 EntityManager.SetComponentData(entity, timer);
                 var data = geneticTrainer.Value;
                 data.maxPoint        = maxPoint;
                 geneticTrainer.Value = data;
                 if (geneticTrainer.preset == null)
                 {
                     geneticTrainer.preset = geneticTrainer.InstantiateGameObject(geneticTrainer.unInitialiazedPreset);
                     EntityManager.Instantiate(geneticTrainer.preset, geneticTrainer.pool);
                     EntityManager.AddComponent(geneticTrainer.pool, ComponentType.Create <CollisionStateComponent>());
                     EntityManager.AddComponent(geneticTrainer.pool, ComponentType.Create <CollisionResponseComponent>());
                     var geneticTrainerID = new GeneticTrainerIDComponentData {
                         ID = geneticTrainer.Value.TrainerID
                     };
                     for (int k = 0, poolLength = geneticTrainer.pool.Length; k < poolLength; ++k)
                     {
                         var poolEntity = geneticTrainer.pool[k];
                         EntityManager.AddComponentData(poolEntity, new FrozenComponentData());
                         EntityManager.AddComponentData(poolEntity, new NeuralNetworkIDComponentData {
                             ID = k
                         });
                         EntityManager.AddSharedComponentData(poolEntity, geneticTrainerID);
                         EntityManager.SetComponentObject(poolEntity, ComponentType.Create <CollisionStateComponent>(), new CollisionStateComponent());
                         EntityManager.SetComponentObject(poolEntity, ComponentType.Create <CollisionResponseComponent>(),
                                                          new CarCollisionResponse(poolEntity, EntityManager,
                                                                                   checkPointComponentGroup.GetEntityArray().Length, checkPointComponentGroup.GetComponentDataArray <Position>(),
                                                                                   checkPointComponentGroup.GetComponentDataArray <PointComponentData>(), checkPointComponentGroup.GetComponentDataArray <IDComponentData>()));
                     }
                     EntityManager.AddComponentData(entity, new InitializedComponentData());
                 }
             }
         }
         chunks.Dispose();
     }
 }
    void CheckNeighboursAreReady()
    {
        NativeArray <ArchetypeChunk> dataChunks = meshReadyGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        ArchetypeChunkEntityType entityType = GetArchetypeChunkEntityType();
        ArchetypeChunkComponentType <AdjacentSectors> adjacentType = GetArchetypeChunkComponentType <AdjacentSectors>(true);

        for (int c = 0; c < dataChunks.Length; c++)
        {
            ArchetypeChunk dataChunk = dataChunks[c];

            NativeArray <Entity>          entities  = dataChunk.GetNativeArray(entityType);
            NativeArray <AdjacentSectors> adjacents = dataChunk.GetNativeArray(adjacentType);

            for (int e = 0; e < entities.Length; e++)
            {
                Entity          entity          = entities[e];
                AdjacentSectors adjacentSquares = adjacents[e];

                NativeArray <Block> current        = new NativeArray <Block>(entityManager.GetBuffer <Block>(entity).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> northNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[0]).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> southNeighbour = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[1]).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> eastNeighbour  = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[2]).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> westNeighbour  = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[3]).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> upNeighbour    = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[4]).AsNativeArray(), Allocator.TempJob);
                NativeArray <Block> downNeighbour  = new NativeArray <Block>(entityManager.GetBuffer <Block>(adjacentSquares[5]).AsNativeArray(), Allocator.TempJob);

                if (northNeighbour.Length == 0 || southNeighbour.Length == 0 || eastNeighbour.Length == 0 ||
                    westNeighbour.Length == 0 || upNeighbour.Length == 0 || downNeighbour.Length == 0)
                {
                    current.Dispose();
                    northNeighbour.Dispose();
                    southNeighbour.Dispose();
                    eastNeighbour.Dispose();
                    westNeighbour.Dispose();
                    upNeighbour.Dispose();
                    downNeighbour.Dispose();
                    continue;
                }
                else
                {
                    PostUpdateCommands.AddComponent(entity, new NeighboursAreReady());
                }
                current.Dispose();
                northNeighbour.Dispose();
                southNeighbour.Dispose();
                eastNeighbour.Dispose();
                westNeighbour.Dispose();
                upNeighbour.Dispose();
                downNeighbour.Dispose();
            }
        }
        dataChunks.Dispose();
    }
Exemplo n.º 22
0
        protected override void OnUpdate()
        {
            ArchetypeChunkComponentType <PlayerInputData> playerInputDataRW = GetArchetypeChunkComponentType <PlayerInputData>(false);

            NativeArray <ArchetypeChunk> playerInputDataChunk = playerInputDataGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            if (playerInputDataChunk.Length == 0)
            {
                playerInputDataChunk.Dispose();
                return;
            }

            for (int chunkIndex = 0; chunkIndex < playerInputDataChunk.Length; chunkIndex++)
            {
                ArchetypeChunk chunk     = playerInputDataChunk[chunkIndex];
                int            dataCount = chunk.Count;

                NativeArray <PlayerInputData> playerInputDataArray = chunk.GetNativeArray(playerInputDataRW);

                for (int dataIndex = 0; dataIndex < dataCount; dataIndex++)
                {
                    PlayerInputData playerInputData = playerInputDataArray[dataIndex];

                    float moveHorizontal    = 0.0f;
                    float moveVertical      = 0.0f;
                    bool  fireButtonPressed = false;
                    switch (playerInputData.playerID)
                    {
                    case 0:
                    {
                        moveHorizontal    = Input.GetAxis("Horizontal");
                        moveVertical      = Input.GetAxis("Vertical");
                        fireButtonPressed = Input.GetButton("Fire1");
                    }
                    break;

                    default:
                    {
                        Debug.LogError("Addional players not supported for now");
                    }
                    break;
                    }

                    playerInputData.inputMovementDirection.x = moveHorizontal;
                    playerInputData.inputMovementDirection.z = moveVertical;
                    playerInputData.fireButtonPressed        = fireButtonPressed ? 1 : 0;

                    playerInputDataArray[dataIndex] = playerInputData;
                }
            }

            playerInputDataChunk.Dispose();
        }
Exemplo n.º 23
0
        protected override void OnUpdate()
        {
            var viewPoolSystem   = World.GetExistingManager <ViewPoolSystem>();
            var chunkArray       = m_Group.CreateArchetypeChunkArray(Allocator.TempJob);
            var entityType       = GetArchetypeChunkEntityType();
            var knightType       = GetArchetypeChunkComponentType <Knight>(true);
            var orcWolfRiderType = GetArchetypeChunkComponentType <OrcWolfRider>(true);
            var skeletonType     = GetArchetypeChunkComponentType <Skeleton>(true);

            for (int chunkIndex = 0; chunkIndex < chunkArray.Length; chunkIndex++)
            {
                var chunk           = chunkArray[chunkIndex];
                var hasOrcWolfRider = chunk.Has(orcWolfRiderType);
                var hasSkeleton     = chunk.Has(skeletonType);
                var entityArray     = chunk.GetNativeArray(entityType);
                {
                    if (chunk.Has(knightType))
                    {
                        m_KnightList.AddRange(entityArray);
                    }
                    else if (chunk.Has(orcWolfRiderType))
                    {
                        m_OrcWolfRiderList.AddRange(entityArray);
                    }
                    else if (chunk.Has(skeletonType))
                    {
                        m_SkeletonList.AddRange(entityArray);
                    }
                }
            }

            chunkArray.Dispose();

            for (var entityIndex = 0; entityIndex < m_KnightList.Length; entityIndex++)
            {
                Instantiate(viewPoolSystem.m_KnightPool, ViewType.Knight, m_KnightPrefab, m_KnightList[entityIndex]);
            }

            for (var entityIndex = 0; entityIndex < m_OrcWolfRiderList.Length; entityIndex++)
            {
                Instantiate(viewPoolSystem.m_OrcWolfRiderPool, ViewType.OrcWolfRider, m_OrcWolfRiderPrefab, m_OrcWolfRiderList[entityIndex]);
            }

            for (var entityIndex = 0; entityIndex < m_SkeletonList.Length; entityIndex++)
            {
                Instantiate(viewPoolSystem.m_SkeletonPool, ViewType.Skeleton, m_SkeletonPrefab, m_SkeletonList[entityIndex]);
            }

            m_KnightList.Clear();
            m_OrcWolfRiderList.Clear();
            m_SkeletonList.Clear();
        }
Exemplo n.º 24
0
            public override void ExecuteReplication(ComponentGroup replicationGroup, ComponentSystemBase system, global::Improbable.Worker.CInterop.Connection connection)
            {
                Profiler.BeginSample("Connection");

                var chunkArray          = replicationGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var spatialOSEntityType = system.GetArchetypeChunkComponentType <SpatialEntityId>(true);
                var componentType       = system.GetArchetypeChunkComponentType <Improbable.Gdk.Tests.AlternateSchemaSyntax.Connection.Component>();
                var eventMyEventType    = system.GetArchetypeChunkComponentType <EventSender.MyEvent>(true);

                foreach (var chunk in chunkArray)
                {
                    var entityIdArray     = chunk.GetNativeArray(spatialOSEntityType);
                    var componentArray    = chunk.GetNativeArray(componentType);
                    var eventMyEventArray = chunk.GetNativeArray(eventMyEventType);
                    for (var i = 0; i < componentArray.Length; i++)
                    {
                        var data          = componentArray[i];
                        var eventsToSend  = 0;
                        var eventsMyEvent = eventMyEventArray[i].Events;
                        eventsToSend += eventsMyEvent.Count;

                        if (data.IsDataDirty() || eventsToSend > 0)
                        {
                            var update = new global::Improbable.Worker.CInterop.SchemaComponentUpdate(1105);
                            Improbable.Gdk.Tests.AlternateSchemaSyntax.Connection.Serialization.SerializeUpdate(data, update);

                            // Serialize events
                            var eventsObject = update.GetEvents();
                            if (eventsMyEvent.Count > 0)
                            {
                                foreach (var e in eventsMyEvent)
                                {
                                    var obj = eventsObject.AddObject(1);
                                    global::Improbable.Gdk.Tests.AlternateSchemaSyntax.RandomDataType.Serialization.Serialize(e, obj);
                                }

                                eventsMyEvent.Clear();
                            }

                            // Send serialized update over the wire
                            connection.SendComponentUpdate(entityIdArray[i].EntityId.Id, new global::Improbable.Worker.CInterop.ComponentUpdate(update), UpdateParameters);

                            data.MarkDataClean();
                            componentArray[i] = data;
                        }
                    }
                }

                chunkArray.Dispose();
                Profiler.EndSample();
            }
Exemplo n.º 25
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityArray boltSpawnerEntityDataArray = boltSpawnerEntityDataGroup.GetEntityArray();

            if (boltSpawnerEntityDataArray.Length == 0)
            {
                return(inputDeps);
            }

            BoltSpawnerEntityData boltSpawnerEntityData = GetComponentDataFromEntity <BoltSpawnerEntityData>()[boltSpawnerEntityDataArray[0]];

            ArchetypeChunkEntityType entityTypeRO = GetArchetypeChunkEntityType();
            ArchetypeChunkComponentType <PlayerInputData>     playerInputDataRO     = GetArchetypeChunkComponentType <PlayerInputData>(true);
            ArchetypeChunkComponentType <PlayerMoveData>      playerMoveDataRO      = GetArchetypeChunkComponentType <PlayerMoveData>(true);
            ArchetypeChunkComponentType <Position>            positionRO            = GetArchetypeChunkComponentType <Position>(true);
            ArchetypeChunkComponentType <PlayerSpawnBoltData> playerSpawnBoltDataRW = GetArchetypeChunkComponentType <PlayerSpawnBoltData>(false);


            //CreateArchetypeChunkArray runs inside a job, we can use a job handle to make dependency on that job
            //A NativeArray<ArchetypeChunk> is allocated with the correct size on the main thread and that's what is returned, we are responsible for de-allocating it (In this case using [DeallocateOnJobCompletion] in the move job)
            //The job scheduled by CreateArchetypeChunkArray fill that array with correct chunk information
            JobHandle createChunckArrayJobHandle = new JobHandle();
            NativeArray <ArchetypeChunk> playerSpawnBoltDataChunks = playerSpawnBoltDataGroup.CreateArchetypeChunkArray(Allocator.TempJob, out createChunckArrayJobHandle);

            //Special case when our query return no chunk at all
            if (playerSpawnBoltDataChunks.Length == 0)
            {
                createChunckArrayJobHandle.Complete();
                playerSpawnBoltDataChunks.Dispose();
                return(inputDeps);
            }

            //Make sure our movejob is dependent on the job filling the array has completed
            JobHandle spawnJobDependency = JobHandle.CombineDependencies(inputDeps, createChunckArrayJobHandle);

            PlayerSpawnBoltJob playerSpawnBoltJob = new PlayerSpawnBoltJob
            {
                chunks                = playerSpawnBoltDataChunks,
                entityTypeRO          = entityTypeRO,
                playerInputDataRO     = playerInputDataRO,
                playerMoveDataRO      = playerMoveDataRO,
                positionRO            = positionRO,
                playerSpawnBoltDataRW = playerSpawnBoltDataRW,
                spawnBoltEntityQueue  = boltSpawnerEntityData.playerBoltSpawnQueueConcurrent,
                currentTime           = Time.time,
            };

            return(playerSpawnBoltJob.Schedule(playerSpawnBoltDataChunks.Length,
                                               MonoBehaviourECSBridge.Instance.GetJobBatchCount(playerSpawnBoltDataChunks.Length),
                                               spawnJobDependency));
        }
            public override void ExecuteReplication(ComponentGroup replicationGroup, ComponentSystemBase system, global::Improbable.Worker.Core.Connection connection)
            {
                Profiler.BeginSample("ComponentWithNoFieldsWithEvents");

                var chunkArray          = replicationGroup.CreateArchetypeChunkArray(Allocator.TempJob);
                var spatialOSEntityType = system.GetArchetypeChunkComponentType <SpatialEntityId>(true);
                var componentType       = system.GetArchetypeChunkComponentType <Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFieldsWithEvents.Component>();
                var eventEvtType        = system.GetArchetypeChunkComponentType <EventSender.Evt>(true);

                foreach (var chunk in chunkArray)
                {
                    var entityIdArray  = chunk.GetNativeArray(spatialOSEntityType);
                    var componentArray = chunk.GetNativeArray(componentType);
                    var eventEvtArray  = chunk.GetNativeArray(eventEvtType);
                    for (var i = 0; i < componentArray.Length; i++)
                    {
                        var data        = componentArray[i];
                        var dirtyEvents = 0;
                        var eventsEvt   = eventEvtArray[i].Events;
                        dirtyEvents += eventsEvt.Count;

                        if (data.DirtyBit || dirtyEvents > 0)
                        {
                            var update = new global::Improbable.Worker.Core.SchemaComponentUpdate(1004);
                            Improbable.Gdk.Tests.ComponentsWithNoFields.ComponentWithNoFieldsWithEvents.Serialization.SerializeUpdate(data, update);

                            // Serialize events
                            var eventsObject = update.GetEvents();
                            if (eventsEvt.Count > 0)
                            {
                                foreach (var e in eventsEvt)
                                {
                                    var obj = eventsObject.AddObject(1);
                                    global::Improbable.Gdk.Tests.ComponentsWithNoFields.Empty.Serialization.Serialize(e, obj);
                                }

                                eventsEvt.Clear();
                            }

                            // Send serialized update over the wire
                            connection.SendComponentUpdate(entityIdArray[i].EntityId, new global::Improbable.Worker.Core.ComponentUpdate(update));

                            data.DirtyBit     = false;
                            componentArray[i] = data;
                        }
                    }
                }

                chunkArray.Dispose();
                Profiler.EndSample();
            }
Exemplo n.º 27
0
        internal void CleanCloned()
        {
            var enType = GetArchetypeChunkEntityType();
            var aca    = clonedGroup.CreateArchetypeChunkArray(Allocator.TempJob);

            for (int i = 0; i < aca.Length; i++)
            {
                var ea = aca[i].GetNativeArray(enType);
                for (int j = 0; j < ea.Length; j++)
                {
                    EntityManager.RemoveComponent <Cloned>(ea[j]);
                }
            }
        }
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        NativeArray <ArchetypeChunk> dataChunks = meshDataGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        if (dataChunks.Length == 0)
        {
            dataChunks.Dispose();
            return(inputDeps);
        }

        EntityCommandBuffer eCBuffer = new EntityCommandBuffer(Allocator.TempJob);

        ArchetypeChunkEntityType             entityType = GetArchetypeChunkEntityType();
        ArchetypeChunkComponentType <Sector> sectorType = GetArchetypeChunkComponentType <Sector>(true);
        ArchetypeChunkComponentType <SectorVisFacesCount> faceCountsType = GetArchetypeChunkComponentType <SectorVisFacesCount>(true);
        ArchetypeChunkBufferType <Block>      blocksType = GetArchetypeChunkBufferType <Block>(true);
        ArchetypeChunkBufferType <BlockFaces> facesType  = GetArchetypeChunkBufferType <BlockFaces>(true);

        for (int c = 0; c < dataChunks.Length; c++)
        {
            ArchetypeChunk dataChunk = dataChunks[c];

            //	Get chunk data
            NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType);
            NativeArray <Sector> sectors  = dataChunk.GetNativeArray(sectorType);
            NativeArray <SectorVisFacesCount> faceCounts    = dataChunk.GetNativeArray(faceCountsType);
            BufferAccessor <Block>            blockAccessor = dataChunk.GetBufferAccessor(blocksType);
            BufferAccessor <BlockFaces>       facesAccessor = dataChunk.GetBufferAccessor(facesType);

            for (int e = 0; e < entities.Length; e++)
            {
                var meshDataJob = new MeshDataJob()
                {
                    ECBuffer = eCBuffer,
                    entity   = entities[e],
                    counts   = faceCounts[e],
                    sector   = sectors[e],

                    blocks     = new NativeArray <Block>(blockAccessor[e].AsNativeArray(), Allocator.TempJob),
                    blockFaces = new NativeArray <BlockFaces>(facesAccessor[e].AsNativeArray(), Allocator.TempJob),
                }.Schedule(inputDeps);
                meshDataJob.Complete();
            }
        }
        eCBuffer.Playback(entityManager);
        eCBuffer.Dispose();

        dataChunks.Dispose();
        return(inputDeps);
    }
Exemplo n.º 29
0
    void ScheduleMoreJobs()
    {
        NativeArray <ArchetypeChunk> dataChunks = meshDataGroup.CreateArchetypeChunkArray(Allocator.TempJob);

        EntityCommandBuffer eCBuffer       = new EntityCommandBuffer(Allocator.TempJob);
        JobHandle           allHandles     = new JobHandle();
        JobHandle           previousHandle = new JobHandle();

        ArchetypeChunkEntityType             entityType = GetArchetypeChunkEntityType();
        ArchetypeChunkComponentType <Sector> sectorType = GetArchetypeChunkComponentType <Sector>(true);
        ArchetypeChunkComponentType <SectorVisFacesCount> faceCountsType = GetArchetypeChunkComponentType <SectorVisFacesCount>(true);
        ArchetypeChunkBufferType <Block>      blocksType = GetArchetypeChunkBufferType <Block>(true);
        ArchetypeChunkBufferType <BlockFaces> facesType  = GetArchetypeChunkBufferType <BlockFaces>(true);

        for (int c = 0; c < dataChunks.Length; c++)
        {
            ArchetypeChunk dataChunk = dataChunks[c];

            //	Get chunk data
            NativeArray <Entity> entities = dataChunk.GetNativeArray(entityType);
            NativeArray <Sector> sectors  = dataChunk.GetNativeArray(sectorType);
            NativeArray <SectorVisFacesCount> faceCounts    = dataChunk.GetNativeArray(faceCountsType);
            BufferAccessor <Block>            blockAccessor = dataChunk.GetBufferAccessor(blocksType);
            BufferAccessor <BlockFaces>       facesAccessor = dataChunk.GetBufferAccessor(facesType);

            for (int e = 0; e < entities.Length; e++)
            {
                MeshDataJob meshDataJob = new MeshDataJob()
                {
                    ECBuffer = eCBuffer,
                    entity   = entities[e],
                    counts   = faceCounts[e],
                    sector   = sectors[e],

                    blocks     = new NativeArray <Block>(blockAccessor[e].AsNativeArray(), Allocator.TempJob),
                    blockFaces = new NativeArray <BlockFaces>(facesAccessor[e].AsNativeArray(), Allocator.TempJob),
                };

                JobHandle thisHandle = meshDataJob.Schedule(previousHandle);
                allHandles = JobHandle.CombineDependencies(thisHandle, allHandles);

                previousHandle = thisHandle;
            }
        }

        runningCommandBuffer = eCBuffer;
        runningJobHandle     = allHandles;

        dataChunks.Dispose();
    }
Exemplo n.º 30
0
        protected override void OnUpdate()
        {
            _positionType = GetArchetypeChunkComponentType <Position>();
            _rotationType = GetArchetypeChunkComponentType <Rotation>();
            _xrNodeType   = GetArchetypeChunkComponentType <VrNodeData>();

            var chunks = _groupControllers.CreateArchetypeChunkArray(Allocator.TempJob);

            for (int i = 0; i < chunks.Length; i++)
            {
                ProcessControllerChunks(chunks[i]);
            }
            chunks.Dispose();
        }