Exemplo n.º 1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var entityType = GetArchetypeChunkEntityType();

        JobHandle playerHandle;
        var       playerEntity = playerGroup.ToEntityArray(Allocator.TempJob, out playerHandle);

        var updateJob = new UpdateJob
        {
            snapshotFromEntity = GetBufferFromEntity <ShipSnapshotData>(),
            targetTick         = NetworkTimeSystem.interpolateTargetTick
        };

        inputDeps = updateJob.Schedule(this, inputDeps);
        var destroyJob = new DestroyJob
        {
            commandBuffer         = m_Barrier.CreateCommandBuffer().ToConcurrent(),
            entityType            = entityType,
            playerEntity          = playerEntity,
            playerStateFromEntity = GetComponentDataFromEntity <PlayerStateComponentData>()
        };

        inputDeps = destroyJob.Schedule(destroyGroup, JobHandle.CombineDependencies(inputDeps, playerHandle));
        m_Barrier.AddJobHandleForProducer(inputDeps);
        return(inputDeps);
    }
Exemplo n.º 2
0
        NativeArray <SceneTag> ExternalRefToSceneTag(NativeArray <ExternalEntityRefInfo> externalEntityRefInfos, Allocator allocator)
        {
            var sceneTags = new NativeArray <SceneTag>(externalEntityRefInfos.Length, allocator);

            using (var sectionDataEntities = m_SectionData.ToEntityArray(Allocator.TempJob))
            {
                using (var sectionData = m_SectionData.ToComponentDataArray <SceneData>(Allocator.TempJob))
                {
                    for (int i = 0; i < sectionData.Length; ++i)
                    {
                        for (int j = 0; j < externalEntityRefInfos.Length; ++j)
                        {
                            if (
                                externalEntityRefInfos[j].SceneGUID == sectionData[i].SceneGUID
                                &&
                                externalEntityRefInfos[j].SubSectionIndex == sectionData[i].SubSectionIndex
                                )
                            {
                                sceneTags[j] = new SceneTag {
                                    SceneEntity = sectionDataEntities[i]
                                };
                                break;
                            }
                        }
                    }
                }
            }

            return(sceneTags);
        }
        protected override void OnUpdate()
        {
            using (NativeArray <Entity> playerSpawns = playerSpawnGroup.ToEntityArray(Allocator.TempJob))
            {
                int playerCount = 0;
                foreach (Entity playerSpawn in playerSpawns)
                {
                    GameObject playerPrefab   = EntityManager.GetSharedComponentData <PlayerSpawn>(playerSpawn).prefab;
                    Entity     playerEntity   = EntityManager.Instantiate(playerPrefab);
                    Player     player         = EntityManager.GetComponentData <Player>(playerEntity);
                    Position   playerPosition = EntityManager.GetComponentData <Position>(playerSpawn);
                    Rotation   playerRotation = EntityManager.GetComponentData <Rotation>(playerSpawn);

                    player.Id = playerCount;

                    EntityManager.SetComponentData(playerEntity, player);
                    EntityManager.SetComponentData(playerEntity, playerPosition);
                    EntityManager.SetComponentData(playerEntity, playerRotation);

                    EntityManager.DestroyEntity(playerSpawn);

                    playerCount++;
                }
            }
        }
Exemplo n.º 4
0
 protected override void OnUpdate()
 {
     using (var rows = _rows.ToEntityArray(Allocator.TempJob)) {
         foreach (var row in rows)
         {
             var rowData   = EntityManager.GetSharedComponentData <CompositionRow>(row);
             var positions = rowData.Positions;
             positions.Sort(CompareByDistance);
             var queue = new NativeQueue <float2>(Allocator.Persistent);
             foreach (var pos in positions)
             {
                 queue.Enqueue(pos);
             }
             var maxPos     = positions[positions.Count - 1];
             var maxLength  = 10 + math.max(math.abs(maxPos.x), math.abs(maxPos.y));
             var runtimeRow = new RuntimeRow {
                 Prefab    = rowData.Prefab,
                 Delay     = rowData.Delay,
                 Interval  = rowData.Interval,
                 Positions = queue,
                 Distance  = maxLength
             };
             EntityManager.AddSharedComponentData(row, runtimeRow);
             EntityManager.RemoveComponent <CompositionRow>(row);
         }
     }
 }
Exemplo n.º 5
0
    protected override void OnUpdate()
    {
        //Get all selected units
        using (var selectedUnits = m_selectedUnits.ToEntityArray(Allocator.TempJob))
            using (var highlights = m_highlights.ToEntityArray(Allocator.TempJob))
            {
                foreach (var selectedUnit in selectedUnits)
                {
                    //Bit of a hack, but we know there is only one highlight spawner
                    var highlight = highlights[0];
                    var prefab    = EntityManager.GetSharedComponentData <HighlightSpawner>(highlight).prefab;
                    var entity    = EntityManager.Instantiate(prefab);

                    //Trying out the Attach component with the TransformSystem
                    var attach = EntityManager.CreateEntity(typeof(Attach));

                    EntityManager.SetComponentData(entity, new Position {
                        Value = new float3(0, -0.5f, 0)
                    });
                    EntityManager.SetComponentData(entity, new Scale {
                        Value = new float3(2, 0.1f, 2)
                    });
                    EntityManager.SetComponentData(attach, new Attach {
                        Parent = selectedUnit, Child = entity
                    });

                    // Remove the "tagging" component so we only create & attach
                    // the highlight once
                    EntityManager.RemoveComponent(selectedUnit, typeof(Selecting));
                }
            }
    }
        private void DestroyExceedingCharacters(int count, int maxCount, int entityCount)
        {
            if (entityCount > 0)
            {
                return;
            }

            if (maxCount == 0 && count > 0)
            {
                World.GetExistingManager <BeginSimulationEntityCommandBufferSystem>().CreateCommandBuffer().CreateEntity(m_DestroyAllCharactersArchetype);
            }
            else if (entityCount < 0)
            {
                entityCount = math.abs(entityCount);
                var entityArray     = m_Group.ToEntityArray(Allocator.TempJob);
                var maxDestroyCount = math.select(entityCount, m_MaxDestroyCount, entityCount > m_MaxDestroyCount);

                for (var entityIndex = 0; entityIndex < maxDestroyCount; entityIndex++)
                {
                    PostUpdateCommands.AddComponent(entityArray[entityIndex], new Destroy());
                    PostUpdateCommands.AddComponent(entityArray[entityIndex], new Disabled());
                }

                entityArray.Dispose();
            }
        }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var transforms = m_InitialTransformGroup.GetTransformAccessArray();
            var entities   = m_InitialTransformGroup.ToEntityArray(Allocator.TempJob);

            var transformStashes   = new NativeArray <TransformStash>(transforms.length, Allocator.TempJob);
            var stashTransformsJob = new StashTransforms
            {
                transformStashes = transformStashes
            };

            var stashTransformsJobHandle = stashTransformsJob.Schedule(transforms, inputDeps);

            var copyTransformsJob = new CopyTransforms
            {
                transformStashes = transformStashes,
            };

            var copyTransformsJobHandle = copyTransformsJob.ScheduleGroup(m_InitialTransformGroup, stashTransformsJobHandle);

            var removeComponentsJob = new RemoveCopyInitialTransformFromGameObjectComponent
            {
                entities            = entities,
                entityCommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer()
            };
            var removeComponentsJobHandle = removeComponentsJob.Schedule(copyTransformsJobHandle);

            m_EntityCommandBufferSystem.AddJobHandleForProducer(removeComponentsJobHandle);
            return(removeComponentsJobHandle);
        }
Exemplo n.º 8
0
    protected override void OnUpdate()
    {
        int cameraGroupLength = cameraGroup.CalculateLength();
        int mapGroupLength    = mapGroup.CalculateLength();

        if ((cameraGroupLength == 0) ||
            (cameraGroupLength != mapGroupLength))
        {
            return;
        }

        var cameraEntities = cameraGroup.ToEntityArray(Allocator.TempJob);
        var mapEntities    = mapGroup.ToEntityArray(Allocator.TempJob);

        Position position;

        for (int i = 0; i < cameraEntities.Length; i++)
        {
            Map map = EntityManager.GetSharedComponentData <Map>(mapEntities[i]);
            position.Value = new float3(map.cols / 2, map.rows / 2, -10f);
            EntityManager.SetComponentData <Position>(cameraEntities[i], position);
            EntityManager.RemoveComponent <Initializer>(cameraEntities[i]);
        }

        cameraEntities.Dispose();
        mapEntities.Dispose();
    }
Exemplo n.º 9
0
        public void DetachFromAllVehicles()
        {
            using (var trafficEntities = m_AliveVehiclesGroup.ToEntityArray(Allocator.TempJob))
            {
                for (int i = 0; i < trafficEntities.Length; ++i)
                {
                    EntityManager.RemoveComponent <VehicleEmitter>(trafficEntities[i]);
                    EntityManager.RemoveComponent <PositionalFlyByData>(trafficEntities[i]);
                }
            }

            using (var flyBys = m_FlyByGroup.ToEntityArray(Allocator.TempJob))
            {
                for (int i = 0; i < flyBys.Length; ++i)
                {
                    EntityManager.DestroyEntity(flyBys[i]);
                }
            }
        }
Exemplo n.º 10
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            inputDeps = new ProcessJob
            {
                DamagedEntityArray = m_Group.ToEntityArray(Allocator.TempJob),
                DamagedFromEntity  = GetComponentDataFromEntity <Damaged>(true),
                HealthFromEntity   = GetComponentDataFromEntity <Health>()
            }.Schedule(m_Group.CalculateLength(), 64, inputDeps);

            return(inputDeps);
        }
Exemplo n.º 11
0
        void DestroyAllButOneEntityInGroup(int version, ComponentGroup group)
        {
            var entities = group.ToEntityArray(Allocator.TempJob);

            for (int i = 0; i < 49; i++)
            {
                var e = entities[i];
                m_Manager.DestroyEntity(e);
            }

            entities.Dispose();
        }
    protected override void OnUpdate()
    {
        int spawnerLength = spawnerGroup.CalculateLength();
        int mapLength     = mapGroup.CalculateLength();

        if (spawnerLength <= 0 || mapLength <= 0 || spawnerLength != mapLength)
        {
            return;
        }

        var spawnerEntities = spawnerGroup.ToEntityArray(Allocator.TempJob);
        var mapEntities     = mapGroup.ToEntityArray(Allocator.TempJob);

        PhysicsObject physicsObject;

        for (int k = 0; k < spawnerEntities.Length; k++)
        {
            Map     map     = EntityManager.GetSharedComponentData <Map>(mapEntities[k]);
            Spawner spawner = EntityManager.GetSharedComponentData <Spawner>(spawnerEntities[k]);

            for (int i = 1; i < map.rows - 1; i++)
            {
                for (int j = 1; j < map.cols - 1; j++)
                {
                    if ((i == map.rows / 2) && (j == map.cols / 2))
                    {
                        var entity = EntityManager.Instantiate(spawner.playerPrefab);
                        physicsObject    = spawner.playerPrefab.GetComponent <PhysicsObjectProxy>().Value;
                        physicsObject.cx = j;
                        physicsObject.cy = i;
                        EntityManager.SetComponentData(entity, physicsObject);
                    }
                    else if (map.mapArray[i * map.cols + j] == 0)
                    {
                        if (rnd.NextFloat(0f, 1f) > 0.3f)
                        {
                            var entity = EntityManager.Instantiate(spawner.obstaclePrefab);
                            physicsObject    = spawner.playerPrefab.GetComponent <PhysicsObjectProxy>().Value;
                            physicsObject.cx = j;
                            physicsObject.cy = i;
                            EntityManager.SetComponentData(entity, physicsObject);
                        }
                    }
                }
            }

            EntityManager.RemoveComponent <Initializer>(spawnerEntities[k]);
        }

        spawnerEntities.Dispose();
        mapEntities.Dispose();
    }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var transforms = m_TransformGroup.GetTransformAccessArray();
            var entities   = m_TransformGroup.ToEntityArray(Allocator.TempJob);

            var copyTransformsJob = new CopyTransforms
            {
                LocalToWorlds = GetComponentDataFromEntity <LocalToWorld>(true),
                entities      = entities
            };

            return(copyTransformsJob.Schedule(transforms, inputDeps));
        }
Exemplo n.º 14
0
 protected override void OnUpdate()
 {
     using (var rows = _rows.ToEntityArray(Allocator.TempJob)) {
         foreach (var row in rows)
         {
             var rowData = EntityManager.GetSharedComponentData <RuntimeRow>(row);
             if (rowData.Delay > 0)
             {
                 rowData.Delay -= Time.deltaTime;
                 EntityManager.SetSharedComponentData(row, rowData);
                 continue;
             }
             if (rowData.Positions.Count == 0)
             {
                 rowData.Positions.Dispose();
                 EntityManager.DestroyEntity(row);
                 continue;
             }
             if (rowData.Timer < rowData.Interval)
             {
                 rowData.Timer += Time.deltaTime;
             }
             else
             {
                 rowData.Timer = 0;
                 var pos2D = rowData.Positions.Dequeue();
                 var pos   = EntityManager.GetComponentData <Position>(row).Value;
                 pos.x += pos2D.x;
                 pos.z += pos2D.y;
                 var instance     = EntityManager.Instantiate(rowData.Prefab);
                 var startPos     = GetStartPos(pos, rowData.Distance);
                 var speedPerUnit = EntityManager.GetComponentData <MovementSpeed>(instance).Value;
                 var actualSpeed  = speedPerUnit / math.distance(startPos, pos);
                 EntityManager.SetComponentData(instance, new Position()
                 {
                     Value = startPos
                 });
                 EntityManager.SetComponentData(instance, new MovementSpeed {
                     Value = actualSpeed
                 });
                 EntityManager.AddComponentData(instance, new MovementTarget()
                 {
                     Start = startPos, End = pos
                 });
             }
             EntityManager.SetSharedComponentData(row, rowData);
         }
     }
 }
    protected override void OnUpdate()
    {
        var groupEntities = m_MainGroup.ToEntityArray(Allocator.TempJob);

        foreach (var entity in groupEntities)
        {
            var creator = EntityManager.GetComponentData <CreatePyramids>(entity);

            float3 boxSize  = creator.BoxSize;
            int    boxCount = creator.Count * (creator.Height * (creator.Height + 1) / 2);

            var positions = new NativeArray <float3>(boxCount, Allocator.Temp);
            int boxIdx    = 0;
            for (int p = 0; p < creator.Count; p++)
            {
                for (int i = 0; i < creator.Height; i++)
                {
                    int    rowSize = creator.Height - i;
                    float3 start   = new float3(-rowSize * boxSize.x * 0.5f + boxSize.x * 0.5f, i * boxSize.y, 0);
                    for (int j = 0; j < rowSize; j++)
                    {
                        float3 shift = new float3(j * boxSize.x, 0f, p * boxSize.z * creator.Space);
                        positions[boxIdx]  = creator.StartPosition;
                        positions[boxIdx] += start + shift;
                        boxIdx++;
                    }
                }
            }

            var entities = new NativeArray <Entity>(boxCount, Allocator.Temp);
            EntityManager.Instantiate(creator.BoxEntity, entities);

            var pyramidComponent = new PhysicsPyramid();
            for (boxIdx = 0; boxIdx < entities.Length; boxIdx++)
            {
                EntityManager.AddComponentData <PhysicsPyramid>(entities[boxIdx], pyramidComponent);
                EntityManager.SetComponentData <Translation>(entities[boxIdx], new Translation()
                {
                    Value = positions[boxIdx]
                });
            }

            entities.Dispose();
            positions.Dispose();

            PostUpdateCommands.DestroyEntity(entity);
        }
        groupEntities.Dispose();
    }
Exemplo n.º 16
0
 protected override void OnUpdate()
 {
     using (var attachedHighlights = m_highlights.ToEntityArray(Allocator.TempJob))
     {
         foreach (var highlight in attachedHighlights)
         {
             var parent = EntityManager.GetComponentData <Parent>(highlight).Value;
             if (EntityManager.HasComponent <Deselecting>(parent))
             {
                 EntityManager.RemoveComponent <Deselecting>(parent);
                 EntityManager.DestroyEntity(highlight);
             }
         }
     }
 }
 protected override void OnUpdate()
 {
     using (var mapGeneratorEntities = mapGeneratorGroup.ToEntityArray(Allocator.TempJob))
     {
         foreach (var generator in mapGeneratorEntities)
         {
             var map = EntityManager.GetSharedComponentData <Map>(generator);
             //map = WhiteRndInitMap(map);
             map = WorleyInitMap(map);
             //LogMap(map);
             EntityManager.SetSharedComponentData <Map>(generator, map);
             EntityManager.RemoveComponent <Initializer>(generator);
             SpawnMap(map);
         }
     }
 }
Exemplo n.º 18
0
        void ChangeGroupOrder(int version, ComponentGroup group)
        {
            var entities = group.ToEntityArray(Allocator.TempJob);

            for (int i = 0; i < 50; i++)
            {
                var e = entities[i];
                if ((i & 0x01) == 0)
                {
                    var testData2 = new EcsTestData2(i);
                    m_Manager.AddComponentData(e, testData2);
                }
            }

            entities.Dispose();
        }
    protected override void OnUpdate()
    {
        if (controlGroup.CalculateLength() != 1)
        {
            throw new System.Exception("Number of Control Entity is not 1!");
        }

        using (var controlEntities = controlGroup.ToEntityArray(Allocator.TempJob))
        {
            var control = EntityManager.GetComponentData <Control>(controlEntities[0]);

            control.move = new float2(Input.GetAxis(Constants.Contorl.Horizontal), Input.GetAxis(Constants.Contorl.Vertial));

            EntityManager.SetComponentData <Control>(controlEntities[0], control);
        }
    }
    protected override void OnUpdate()
    {
        if (playerGroup.CalculateLength() != 1)
        {
            throw new System.Exception("Number of Player is not 1!");
        }
        if (controlGroup.CalculateLength() != 1)
        {
            throw new System.Exception("Number of Control is not 1!");
        }

        float dT = Time.deltaTime;

        var playerEntities  = playerGroup.ToEntityArray(Allocator.TempJob);
        var controlEntities = controlGroup.ToEntityArray(Allocator.TempJob);

        var physicsObject = EntityManager.GetComponentData <PhysicsObject>(playerEntities[0]);
        var control       = EntityManager.GetComponentData <Control>(controlEntities[0]);

        physicsObject.dx += physicsObject.moveSpeed * dT * control.move.x;
        physicsObject.dy += physicsObject.moveSpeed * dT * control.move.y;

        if (physicsObject.dx > physicsObject.maxSpeed)
        {
            physicsObject.dx = physicsObject.maxSpeed;
        }
        if (physicsObject.dx < -physicsObject.maxSpeed)
        {
            physicsObject.dx = -physicsObject.maxSpeed;
        }
        if (physicsObject.dy > physicsObject.maxSpeed)
        {
            physicsObject.dy = physicsObject.maxSpeed;
        }
        if (physicsObject.dy < -physicsObject.maxSpeed)
        {
            physicsObject.dy = -physicsObject.maxSpeed;
        }

        EntityManager.SetComponentData <PhysicsObject>(playerEntities[0], physicsObject);



        playerEntities.Dispose();
        controlEntities.Dispose();
    }
Exemplo n.º 21
0
        void UpdateRemoveParents()
        {
            var childEntities   = m_RemovedParentsGroup.ToEntityArray(Allocator.TempJob);
            var previousParents = m_RemovedParentsGroup.ToComponentDataArray <PreviousParent>(Allocator.TempJob);

            for (int i = 0; i < childEntities.Length; i++)
            {
                var childEntity          = childEntities[i];
                var previousParentEntity = previousParents[i].Value;

                RemoveChildFromParent(childEntity, previousParentEntity);
            }

            EntityManager.RemoveComponent(m_RemovedParentsGroup, typeof(PreviousParent));
            childEntities.Dispose();
            previousParents.Dispose();
        }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
//            var reader = new OctreeReader(octree.octree);
//            var handle = new AssignVolumesAndHashJob
//            {
//                octree = reader,
//                ebc = barrier.CreateCommandBuffer().ToConcurrent(),
//                simulationBufferElement = GetBufferFromEntity<ChunkVolumes>(false)
//            }.Schedule(this, inputDeps);
//
//            handle.Complete();
//            reader.Dispose();


            var handle = new VolumeBufferToHashJob
            {
                ebc = barrier.CreateCommandBuffer().ToConcurrent(),
                simulationBufferElement = GetBufferFromEntity <ChunkVolumes>(false),
                volumeBounds            = GetComponentDataFromEntity <VolumeBounds>(true)
            }.Schedule(this, inputDeps);

            handle.Complete();


            var disableChunksEntities = enabledChunks.ToEntityArray(Allocator.TempJob);
            var enableChunksEntities  = disabledChunks.ToEntityArray(Allocator.TempJob);

            var disableHandle = new DisableEmptyChunksJob
            {
                chunks = disableChunksEntities,
                ecb    = barrier.CreateCommandBuffer().ToConcurrent(),
                hashes = GetComponentDataFromEntity <ChunkVolumesHash>()
            }.Schedule(disableChunksEntities.Length, 64, handle);
            var enableHandle = new EnableFilledChunksJob
            {
                chunks = enableChunksEntities,
                ecb    = barrier.CreateCommandBuffer().ToConcurrent(),
                hashes = GetComponentDataFromEntity <ChunkVolumesHash>()
            }.Schedule(enableChunksEntities.Length, 64, handle);
            var stateHandle = JobHandle.CombineDependencies(disableHandle, enableHandle);

            barrier.AddJobHandleForProducer(stateHandle);

            return(stateHandle);
        }
Exemplo n.º 23
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var commandBuffer = m_Barrier.CreateCommandBuffer();

        if (playerGroup.IsEmptyIgnoreFilter)
        {
            m_DelayedDespawnQueue.Clear();
            var clearMapJob = new ClearMapJob
            {
                ghostMap = m_ghostEntityMap
            };
            var clearHandle = clearMapJob.Schedule(inputDeps);
            var clearJob    = new ClearGhostsJob
            {
                commandBuffer = commandBuffer.ToConcurrent()
            };
            inputDeps = clearJob.Schedule(this, inputDeps);
            m_Barrier.AddJobHandleForProducer(inputDeps);
            return(JobHandle.CombineDependencies(inputDeps, clearHandle));
        }

        serializers.BeginDeserialize(this);
        JobHandle playerHandle;
        var       readJob = new ReadStreamJob
        {
            commandBuffer         = commandBuffer,
            players               = playerGroup.ToEntityArray(Allocator.TempJob, out playerHandle),
            snapshotFromEntity    = GetBufferFromEntity <IncomingSnapshotDataStreamBufferComponent>(),
            snapshotAckFromEntity = GetComponentDataFromEntity <NetworkSnapshotAck>(),
            ghostEntityMap        = m_ghostEntityMap,
            compressionModel      = m_CompressionModel,
            serializers           = serializers,
            #if ENABLE_UNITY_COLLECTIONS_CHECKS
            netStats = m_NetStats,
            #endif
            replicatedEntityType = ComponentType.ReadWrite <ReplicatedEntity>(),
            delayedDespawnQueue  = m_DelayedDespawnQueue,
            targetTick           = NetworkTimeSystem.interpolateTargetTick
        };

        inputDeps = readJob.Schedule(JobHandle.CombineDependencies(inputDeps, playerHandle));

        m_Barrier.AddJobHandleForProducer(inputDeps);
        return(inputDeps);
    }
Exemplo n.º 24
0
        protected override void OnUpdate()
        {
            var hasCameraTarget = HasSingleton <CameraTarget>();

            if (hasCameraTarget && !EntityManager.Exists(GetSingleton <CameraTarget>().Value))
            {
                EntityManager.RemoveComponent(m_RemoveGroup, ComponentType.ReadWrite <CameraTarget>());
            }

            if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(1) || !hasCameraTarget)
            {
                Entities.With(m_AddGroup).ForEach((Entity entity, ref SpectatorCamera spectatorCamera, ref Translation translation, ref Rotation rotation) =>
                {
                    if (m_TargetGroup.CalculateLength() == 0)
                    {
                        return;
                    }

                    var targetArray = m_TargetGroup.ToEntityArray(Unity.Collections.Allocator.TempJob);
                    var target      = targetArray[m_Random.NextInt(0, targetArray.Length)];
                    if (!hasCameraTarget)
                    {
                        PostUpdateCommands.AddComponent(entity, new CameraTarget {
                            Value = target
                        });
                    }
                    else
                    {
                        PostUpdateCommands.SetComponent(entity, new CameraTarget {
                            Value = target
                        });
                    }
                    targetArray.Dispose();
                });
            }

            if (hasCameraTarget)
            {
                Entities.With(m_Group).ForEach((ref SpectatorCamera spectatorCamera, ref Translation translation, ref Rotation rotation, ref CameraTarget cameraTarget) =>
                {
                    translation.Value = math.lerp(translation.Value, EntityManager.GetComponentData <Translation>(cameraTarget.Value).Value, Time.deltaTime);
                    rotation.Value    = math.mul(math.normalize(rotation.Value), quaternion.AxisAngle(math.up(), spectatorCamera.RotationSpeed * Time.deltaTime));
                });
            }
        }
Exemplo n.º 25
0
        void UpdateNewParents()
        {
            var childEntities = m_NewParentsGroup.ToEntityArray(Allocator.TempJob);
            var parents       = m_NewParentsGroup.ToComponentDataArray <Parent>(Allocator.TempJob);

            EntityManager.AddComponent(m_NewParentsGroup, typeof(PreviousParent));

            for (int i = 0; i < childEntities.Length; i++)
            {
                var childEntity  = childEntities[i];
                var parentEntity = parents[i].Value;

                AddChildToParent(childEntity, parentEntity);
            }

            childEntities.Dispose();
            parents.Dispose();
        }
Exemplo n.º 26
0
            public void ExecuteEach(EntityManager entityManager, ComponentGroup phaseEnablerGroup)
            {
                NativeArray <Entity> entities = phaseEnablerGroup.ToEntityArray(Allocator.TempJob);

                for (int entityIndex = 0, numEntities = entities.Length; entityIndex < numEntities; ++entityIndex)
                {
                    Entity            entity            = entities[entityIndex];
                    PhaseEnabler      phaseEnabler      = entityManager.GetComponentData <PhaseEnabler>(entity);
                    ActivatableObject activatableObject = entityManager.GetSharedComponentData <ActivatableObject>(entity);
                    if (!TryActivateObjectByPhase(ref phaseEnabler, ref activatableObject))
                    {
                        continue;
                    }

                    entityManager.SetSharedComponentData <ActivatableObject>(entity, activatableObject);
                }
                entities.Dispose();
            }
Exemplo n.º 27
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var mapEntityArray = mapGroup.ToEntityArray(Allocator.TempJob);
        Map map            = EntityManager.GetSharedComponentData <Map>(mapEntityArray[0]);
        var mapArray       = new NativeArray <int>(map.mapArray, Allocator.TempJob);

        var objArray = objGroup.ToComponentDataArray <PhysicsObject>(Allocator.TempJob);

        var job = new CollisionJob
        {
            rows     = map.rows,
            cols     = map.cols,
            mapArray = mapArray,
            objArray = objArray
        };

        mapEntityArray.Dispose();
        return(job.Schedule(this, inputDeps));
    }
    protected override void OnUpdate()
    {
        // Get all the spawners in the scene.
        using (var spawners = m_Spawners.ToEntityArray(Allocator.TempJob))
        {
            foreach (var spawner in spawners)
            {
                var spawnerData   = EntityManager.GetSharedComponentData <ObjectSpawner>(spawner);
                var lastSpawnTime = spawnerData.lastSpawnTime;
                if (lastSpawnTime > 1.0f)
                {
                    var count = spawnerData.spawnNumber;
                    if (count > 0)
                    {
                        // Create an entity from the prefab set on the spawner component.
                        var prefab = spawnerData.prefab;
                        var entity = EntityManager.Instantiate(prefab);

                        // Copy the position of the spawner to the new entity.
                        var position = EntityManager.GetComponentData <Position>(spawner);
                        var index    = spawnerData.curIndex;
                        position.Value += new float3(1.1f * index, 0, 0);
                        EntityManager.SetComponentData(entity, position);
                        spawnerData.curIndex = ++index;
                    }
                    count--;
                    if (count <= 0)
                    {
                        // Destroy the spawner
                        EntityManager.DestroyEntity(spawner);
                        return;
                    }
                    // Write back
                    spawnerData.spawnNumber = count;
                    lastSpawnTime           = 0f;
                }
                lastSpawnTime            += UnityEngine.Time.deltaTime;
                spawnerData.lastSpawnTime = lastSpawnTime;
                EntityManager.SetSharedComponentData(spawner, spawnerData);
            }
        }
    }
Exemplo n.º 29
0
        void UpdateDeletedParents()
        {
            var previousParents = m_DeletedParentsGroup.ToEntityArray(Allocator.TempJob);

            for (int i = 0; i < previousParents.Length; i++)
            {
                var parentEntity        = previousParents[i];
                var childEntitiesSource = EntityManager.GetBuffer <Child>(parentEntity).AsNativeArray();
                var childEntities       = new NativeArray <Entity>(childEntitiesSource.Length, Allocator.Temp);
                for (int j = 0; j < childEntitiesSource.Length; j++)
                {
                    childEntities[j] = childEntitiesSource[j].Value;
                }

                for (int j = 0; j < childEntities.Length; j++)
                {
                    var childEntity = childEntities[j];

                    if (!EntityManager.Exists(childEntity))
                    {
                        continue;
                    }

                    if (EntityManager.HasComponent(childEntity, typeof(Parent)))
                    {
                        EntityManager.RemoveComponent(childEntity, typeof(Parent));
                    }
                    if (EntityManager.HasComponent(childEntity, typeof(PreviousParent)))
                    {
                        EntityManager.RemoveComponent(childEntity, typeof(PreviousParent));
                    }
                    if (EntityManager.HasComponent(childEntity, typeof(LocalToParent)))
                    {
                        EntityManager.RemoveComponent(childEntity, typeof(LocalToParent));
                    }
                }

                childEntities.Dispose();
            }
            EntityManager.RemoveComponent(m_DeletedParentsGroup, typeof(Child));
            previousParents.Dispose();
        }
    protected override void OnUpdate()
    {
        // Get all the spawners in the scene.
        using (var spawners = m_Spawners.ToEntityArray(Allocator.TempJob))
        {
            foreach (var spawner in spawners)
            {
                // Create an entity from the prefab set on the spawner component.
                var prefab = EntityManager.GetSharedComponentData <HelloSpawner>(spawner).prefab;
                var entity = EntityManager.Instantiate(prefab);

                // Copy the position of the spawner to the new entity.
                var position = EntityManager.GetComponentData <Position>(spawner);
                EntityManager.SetComponentData(entity, position);

                // Destroy the spawner so this system only runs once.
                EntityManager.DestroyEntity(spawner);
            }
        }
    }