/// <summary>
 /// set the buffer elements of the given aBuffer
 /// </summary>
 /// <param name="ecb"></param>
 /// <param name="aBuffer"></param>
 /// <param name="bBuffer"></param>
 public static void SetBuffer(EntityCommandBuffer.ParallelWriter ecb, DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> aBuffer, DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> bBuffer)
 {
     aBuffer.Clear();
     aBuffer.CopyFrom(bBuffer);
 }
 /// <summary>
 /// set the buffer elements of the given aBuffer
 /// </summary>
 /// <param name="ecb"></param>
 /// <param name="aBuffer"></param>
 /// <param name="bBuffer"></param>
 public static void SetBuffer(EntityCommandBuffer.ParallelWriter ecb, DynamicBuffer <EntityPrefabBufferElement> aBuffer, DynamicBuffer <EntityPrefabBufferElement> bBuffer)
 {
     aBuffer.Clear();
     aBuffer.CopyFrom(bBuffer);
 }
 public DynamicBuffer <EntityPrefabBufferElement> AddBuffer(EntityCommandBuffer.ParallelWriter ecb, int jobIndex, Entity entity)
 {
     return(ecb.AddBuffer <EntityPrefabBufferElement>(jobIndex, entity));
 }
Пример #4
0
        public override void UpdateSystem()
        {
            EntityCommandBuffer.ParallelWriter ecb = m_endSimECBSystem.CreateCommandBuffer().AsParallelWriter();

            ComponentDataFromEntity <ResourceNode> resourceNodeLookup = GetComponentDataFromEntity <ResourceNode>();

            JobHandle movingToHarvestHandle = Entities
                                              .WithReadOnly(resourceNodeLookup)
                                              .WithAll <MovingToHarvestState>()
                                              .ForEach((Entity entity, int entityInQueryIndex, ref Harvester harvester, ref CurrentTarget currentTarget, ref DynamicBuffer <Command> commandBuffer, in Translation translation) =>
            {
                if (!resourceNodeLookup.TryGetComponentDataFromEntity(currentTarget.targetData.targetEntity, out ResourceNode resourceNode))
                {
                    Debug.Log($"Harvest node {currentTarget.targetData.targetEntity} destroyed when moving to it, finding nearby resource node of type {currentTarget.targetData.targetType} instead");

                    CommandProcessSystem.CompleteCommand(ref commandBuffer);

                    CommandProcessSystem.QueueCommand(CommandType.Harvest, commandBuffer, new TargetData {
                        targetType = currentTarget.targetData.targetType
                    }, true);
                    return;
                }

                //Get harvestable radius
                float dist  = math.distance(translation.Value, currentTarget.targetData.targetPos);
                float range = resourceNode.harvestableRadius + harvester.harvestRange;

                //Are we close enough to harvest yet?
                if (dist <= range)
                {
                    //Move the command onto the execution phase
                    CommandProcessSystem.ExecuteCommand(ref commandBuffer);

                    //Set type we are harvesting + empty inventory if type is different
                    ResourceNode resource = GetComponent <ResourceNode>(currentTarget.targetData.targetEntity);
                    if (harvester.currentlyCarryingType != resource.resourceType)
                    {
                        Debug.Log($"Harvesting type { resource.resourceType } setting carry amount to 0");

                        harvester.currentlyCarryingAmount = 0;
                        harvester.currentlyCarryingType   = resource.resourceType;
                    }
                }
            }).ScheduleParallel(Dependency);

            float dt = Time.DeltaTime;

            EntityCommandBuffer.ParallelWriter ecb2 = m_endSimECBSystem.CreateCommandBuffer().AsParallelWriter();

            Dependency = Entities
                         .WithReadOnly(resourceNodeLookup)
                         .WithAll <HarvestingState>()
                         .ForEach((Entity entity, int entityInQueryIndex, ref Harvester harvester, ref CurrentTarget currentTarget, ref DynamicBuffer <Command> commandBuffer) =>
            {
                if (!resourceNodeLookup.TryGetComponentDataFromEntity(currentTarget.targetData.targetEntity, out ResourceNode resourceNode))
                {
                    Debug.Log($"Harvest node {currentTarget.targetData.targetEntity} destroyed while harvesting it, finding nearby resource node of type {currentTarget.targetData.targetType} instead");

                    //Complete the harvest command.
                    CommandProcessSystem.CompleteCommand(ref commandBuffer);

                    CommandProcessSystem.QueueCommand(CommandType.Harvest, commandBuffer, new TargetData {
                        targetType = currentTarget.targetData.targetType
                    }, true);
                    return;
                }

                //If harvest is on cd
                if (harvester.harvestTickTimer > 0)
                {
                    //Cooling down
                    harvester.harvestTickTimer -= dt;
                    return;
                }
                //Put harvest on cd
                harvester.harvestTickTimer = harvester.harvestTickCooldown;

                //Harvest the smallest amount between amount of resource, amount harvestable and inventory space
                int inventorySpace = harvester.carryCapacity - harvester.currentlyCarryingAmount;
                int harvestAmount  = math.min(math.min(resourceNode.resourceAmount, harvester.harvestAmount), inventorySpace);

                //Transfer resource from resource node to harvester
                Debug.Log($"Harvested { harvestAmount } of {resourceNode.resourceType}");
                harvester.currentlyCarryingAmount += harvestAmount;
                resourceNode.resourceAmount       -= harvestAmount;

                //If the resource is empty destroy it, we must do this before deciding whether to continue harvesting or go deposit
                if (resourceNode.resourceAmount <= 0)
                {
                    Debug.Log("Fully harvested resource");
                    ecb2.DestroyEntity(entityInQueryIndex, currentTarget.targetData.targetEntity);
                }
                else                 //If the resource isn't being destroyed then update its values
                {
                    ecb2.SetComponent(entityInQueryIndex, currentTarget.targetData.targetEntity, resourceNode);
                }

                //If we are at capacity go back to deposit
                if (harvester.currentlyCarryingAmount >= harvester.carryCapacity)
                {
                    //Complete the harvest command.
                    CommandProcessSystem.CompleteCommand(ref commandBuffer);

                    CommandProcessSystem.QueueCommand(CommandType.Deposit, commandBuffer, new TargetData {
                        targetType = AITargetType.Store
                    }, true);
                    return;
                }

                //If the resource is empty find a new one
                if (resourceNode.resourceAmount <= 0)
                {
                    //Complete the harvest command.
                    CommandProcessSystem.CompleteCommand(ref commandBuffer);

                    CommandProcessSystem.QueueCommand(CommandType.Harvest, commandBuffer, new TargetData {
                        targetType = currentTarget.targetData.targetType
                    }, true);
                    return;
                }
            }).ScheduleParallel(movingToHarvestHandle);

            m_endSimECBSystem.AddJobHandleForProducer(Dependency);
        }
Пример #5
0
        public static void Execute(NativeArray <byte> inputData, ComponentType componentType, int typeSize, NativeArray <Entity> entityArray,
                                   NativeArray <PersistenceState> persistenceStateArray, EntityCommandBuffer.ParallelWriter ecb, int batchIndex)
        {
            int totalElementSize = typeSize + PersistenceMetaData.SizeOfStruct;

            for (int i = 0; i < entityArray.Length; i++)
            {
                PersistenceState persistenceState = persistenceStateArray[i];
                int inputMetaByteIndex            = persistenceState.ArrayIndex * totalElementSize;
                int inputDataByteIndex            = inputMetaByteIndex + PersistenceMetaData.SizeOfStruct;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (persistenceState.ArrayIndex * totalElementSize >= inputData.Length)
                {
                    throw new IndexOutOfRangeException("AddMissingComponent:: persistenceState.ArrayIndex seems to be out of range. Or the totalElementSize is wrong.");
                }
#endif

                var metaData = UnsafeUtility.ReadArrayElementWithStride <PersistenceMetaData>(inputData.GetUnsafeReadOnlyPtr(), persistenceState.ArrayIndex, totalElementSize);

                if (metaData.AmountFound == 1)
                {
                    ecb.AddComponent(batchIndex, entityArray[i], componentType);
                    if (typeSize != 0)
                    {
                        ecb.SetComponent(batchIndex, entityArray[i], componentType, typeSize, inputData.GetSubArray(inputDataByteIndex, typeSize));
                    }
                }
            }
        }
 public static void _SetFirstGeneration(ref SystemBase systemBase, ref JobHandle jobHandle, ref EntityCommandBuffer.ParallelWriter ecbp, in DynamicBuffer <NNPNewPopulationBuffer> a_newPopulation, Entity managerEntity)
Пример #7
0
        /// <summary>
        /// Gets the final position of a character attempting to move from a starting
        /// location with a given movement. The goal of this is to move the character
        /// but have the character 'bounce' off of objects at angles that are
        /// are along the plane perpendicular to the normal of the surface hit.
        /// This will cancel motion through the object and instead deflect it
        /// into another direction giving the effect of sliding along objects
        /// while the character's momentum is absorbed into the wall.
        /// </summary>
        /// <param name="commandBuffer">Command buffer for adding push events to objects</param>
        /// <param name="jobIndex">Index of this job for command buffer use</param>
        /// <param name="collisionWorld">World for checking collisions and other colliable objects</param>
        /// <param name="start">Starting location</param>
        /// <param name="movement">Intended direction of movement</param>
        /// <param name="collider">Collider controlling the character</param>
        /// <param name="entityIndex">Index of this entity</param>
        /// <param name="rotation">Current character rotation</param>
        /// <param name="physicsMassAccessor">Accessor to physics mass components</param>
        /// <param name="anglePower">Power to raise decay of movement due to
        /// changes in angle between intended movement and angle of surface.
        /// Will be angleFactor= 1 / (1 + normAngle) where normAngle is a normalized value
        /// between 0-1 where 1 is max angle (90 deg) and 0 is min angle (0 degrees).
        /// AnglePower is the power to which the angle factor is raised
        /// for a sharper decline. Value of zero negates this property.
        /// <param name="maxBounces">Maximum number of bounces when moving.
        /// After this has been exceeded the bouncing will stop. By default
        /// this is one assuming that each move is fairly small this should approximate
        /// normal movement.</param>
        /// <param name="pushPower">Multiplier for power when pushing on an object.
        /// Larger values mean more pushing.</param>
        /// <param name="pushDecay">How much does the current push decay the remaining
        /// movement by. Values between [0, 1]. A zero would mean all energy is lost
        /// when pushing, 1 means no momentum is lost by pushing. A value of 0.5
        /// would mean half of the energy is lost</param>
        /// <param name="epsilon">Epsilon parameter for pushing away from objects to avoid colliding
        /// with static objects. Should be some small float value that can be
        /// tuned for how much to push away while not allowing being shoved into objects.</param>
        /// <returns>The final location of the character.</returns>
        public static unsafe float3 ProjectValidMovement(
            EntityCommandBuffer.ParallelWriter commandBuffer,
            int jobIndex,
            CollisionWorld collisionWorld,
            float3 start,
            float3 movement,
            PhysicsCollider collider,
            int entityIndex,
            quaternion rotation,
            ComponentDataFromEntity <PhysicsMass> physicsMassGetter,
            float anglePower = 2,
            int maxBounces   = 1,
            float pushPower  = 25,
            float pushDecay  = 0,
            float epsilon    = 0.001f)
        {
            float3 from      = start;    // Starting location of movement
            float3 remaining = movement; // Remaining momentum
            int    bounces   = 0;        // current number of bounces

            // Continue computing while there is momentum and bounces remaining
            while (math.length(remaining) > epsilon && bounces <= maxBounces)
            {
                // Get the target location given the momentum
                float3 target = from + remaining;

                // Do a cast of the collider to see if an object is hit during this
                // movement action
                var input = new ColliderCastInput()
                {
                    Start       = from,
                    End         = target,
                    Collider    = collider.ColliderPtr,
                    Orientation = rotation
                };

                SelfFilteringClosestHitCollector <ColliderCastHit> hitCollector =
                    new SelfFilteringClosestHitCollector <ColliderCastHit>(entityIndex, 1.0f, collisionWorld);

                bool collisionOcurred = collisionWorld.CastCollider(input, ref hitCollector);

                if (!collisionOcurred && hitCollector.NumHits == 0)
                {
                    // If there is no hit, target can be returned as final position
                    return(target);
                }

                Unity.Physics.ColliderCastHit hit = hitCollector.ClosestHit;

                // Set the fraction of remaining movement (minus some small value)
                from = from + remaining * hit.Fraction;
                // Push slightly along normal to stop from getting caught in walls
                from = from + hit.SurfaceNormal * epsilon;

                // Apply some force to the object hit if it is moveable, Apply force on entity hit
                bool isKinematic = physicsMassGetter.HasComponent(hit.Entity) && IsKinematic(physicsMassGetter[hit.Entity]);
                if (hit.RigidBodyIndex < collisionWorld.NumDynamicBodies && !isKinematic)
                {
                    commandBuffer.AddBuffer <PushForce>(jobIndex, hit.Entity);
                    commandBuffer.AppendToBuffer(jobIndex, hit.Entity, new PushForce()
                    {
                        force = movement * pushPower, point = hit.Position
                    });
                    // If pushing something, reduce remaining force significantly
                    remaining *= pushDecay;
                }

                // Get angle between surface normal and remaining movement
                float angleBetween = math.length(math.dot(hit.SurfaceNormal, remaining)) / math.length(remaining);
                // Normalize angle between to be between 0 and 1
                angleBetween = math.min(KCCUtils.MaxAngleShoveRadians, math.abs(angleBetween));
                float normalizedAngle = angleBetween / KCCUtils.MaxAngleShoveRadians;
                // Create angle factor using 1 / (1 + normalizedAngle)
                float angleFactor = 1.0f / (1.0f + normalizedAngle);
                // If the character hit something
                // Reduce the momentum by the remaining movement that ocurred
                remaining *= (1 - hit.Fraction) * math.pow(angleFactor, anglePower);
                // Rotate the remaining remaining movement to be projected along the plane
                // of the surface hit (emulate pushing against the object)
                // A is our vector and B is normal of plane
                // A || B = B × (A×B / |B|) / |B|
                // From http://www.euclideanspace.com/maths/geometry/elements/plane/lineOnPlane/index.htm
                float3 planeNormal  = hit.SurfaceNormal;
                float  momentumLeft = math.length(remaining);
                remaining = math.cross(planeNormal, math.cross(remaining, planeNormal) / math.length(planeNormal)) / math.length(planeNormal);
                remaining = math.normalizesafe(remaining) * momentumLeft;
                // Track number of times the character has bounced
                bounces++;
            }
            return(from);
        }
Пример #8
0
 static void destroyPart_
     (EntityCommandBuffer.ParallelWriter cmd_, int uniqueIndex_, Entity part_)
 {
     cmd_.DestroyEntity(uniqueIndex_, part_);
 }
Пример #9
0
 public static EntityWrapper Wrap(Entity entity, EntityCommandBuffer.ParallelWriter entityCommandBuffer, int threadId)
 {
     return(new EntityWrapper(entity, EntityManagerWrapper.FromJobCommandBuffer(entityCommandBuffer, threadId)));
 }
        protected override void OnUpdate( )
        {
            if (group_MMMamager.CalculateChunkCount() == 0)
            {
                Debug.LogWarning("There is no active manager.");
                return;
            }

            EntityCommandBuffer ecb = becb.CreateCommandBuffer();

            EntityCommandBuffer.ParallelWriter ecbp = ecb.AsParallelWriter();


            l_managerSharedData.Clear();
            EntityManager.GetAllUniqueSharedComponentData(l_managerSharedData);

            ComponentDataFromEntity <NNManagerBestFitnessComponent> a_managerBestFitness = GetComponentDataFromEntity <NNManagerBestFitnessComponent> (false);
            ComponentDataFromEntity <NNManagerComponent>            a_manager            = GetComponentDataFromEntity <NNManagerComponent> (true);
            ComponentDataFromEntity <NNScoreComponent> a_managerScore = GetComponentDataFromEntity <NNScoreComponent> (true);

            ComponentDataFromEntity <NNBrainScoreComponent> a_brainScore = GetComponentDataFromEntity <NNBrainScoreComponent> (true);

            ComponentDataFromEntity <NNMangerIsSpawningNewGenerationTag> a_mangerIsSpawningNewGeneration = GetComponentDataFromEntity <NNMangerIsSpawningNewGenerationTag> (false);

            BufferFromEntity <NNInput2HiddenLayersWeightsBuffer>  NNInput2HiddenLayersWeightsBuffer  = GetBufferFromEntity <NNInput2HiddenLayersWeightsBuffer> (false);
            BufferFromEntity <NNHidden2OutputLayersWeightsBuffer> NNHidden2OutputLayersWeightsBuffer = GetBufferFromEntity <NNHidden2OutputLayersWeightsBuffer> (false);

            // BufferFromEntity <NNHiddenLayersNeuronsBiasBuffer> NNHiddenLayersNeuronsBiasBuffer           = GetBufferFromEntity <NNHiddenLayersNeuronsBiasBuffer> ( false ) ;


            // ComponentDataFromEntity <NNScoreComponent> a_managerScore                                           = GetComponentDataFromEntity <NNScoreComponent> ( true ) ;

            BufferFromEntity <NNINdexProbabilityBuffer> indexProbabilityBuffer = GetBufferFromEntity <NNINdexProbabilityBuffer> (false);

            // int i_validManagersCount                                                                     = 0 ;
            // bool canCalculateCrossovers                                                                  = false ;

            for (int i = 0; i < l_managerSharedData.Count; i++)
            {
                NNManagerSharedComponent mangerSharedComponent = l_managerSharedData [i];
                Entity nnManagerEntity = new Entity()
                {
                    Index = mangerSharedComponent.i_entityIndex, Version = mangerSharedComponent.i_entityVersion
                };

                if (a_mangerIsSpawningNewGeneration.HasComponent(nnManagerEntity))
                {
                    group_parentPopulation.SetSharedComponentFilter(mangerSharedComponent);
                    group_offspringPopulation.SetSharedComponentFilter(mangerSharedComponent);

                    NativeArray <Entity> na_parentPopulationEntities    = group_parentPopulation.ToEntityArray(Allocator.TempJob);
                    NativeArray <Entity> na_offspringPopulationEntities = group_offspringPopulation.ToEntityArray(Allocator.TempJob);

                    DynamicBuffer <NNINdexProbabilityBuffer> a_indexProbability = indexProbabilityBuffer [nnManagerEntity];


                    NNScoreComponent managerScore = a_managerScore [nnManagerEntity];
                    // int i_eliteScore                                            = managerScore.i ;



                    Debug.Log("Total score: " + managerScore.i + "; elite score: " + managerScore.i_elite);


                    if (managerScore.i_elite <= 1)
                    {
                        Dependency = new CopyLastBestGenerationDNAJob()
                        {
                            na_parentPopulationEntities    = na_parentPopulationEntities,
                            na_offspringPopulationEntities = na_offspringPopulationEntities,

                            // na_indexProbability              = na_indexProbability,

                            input2HiddenLayersWeightsBuffer  = NNInput2HiddenLayersWeightsBuffer,
                            hidden2OutputLayersWeightsBuffer = NNHidden2OutputLayersWeightsBuffer,

                            // hiddenLayersNeuronsBiasBuffer    = NNHiddenLayersNeuronsBiasBuffer
                        }.Schedule(na_parentPopulationEntities.Length, 256, Dependency);

                        Dependency.Complete();
                    }
                    else
                    {
                        // New score is fine.

                        // Calculate index probability, to get best parents.
                        // Each entity indicies will be in the array, as many times, as many score has
                        // e.g.
                        // 0th entity with 0 points won't be in the array
                        // 1st entity with 2 points will be 2 times
                        // nth entity with xth score will be xth times in the array

                        NNManagerComponent manager = a_manager [nnManagerEntity];

                        NativeMultiHashMap <int, EntityIndex> nmhm_parentEntitiesScore = new NativeMultiHashMap <int, EntityIndex> (na_parentPopulationEntities.Length, Allocator.TempJob);

// Debug.Log ( "crossover parent score" ) ;
                        Dependency = new CommonJobs.GetPopulationScoreJob( )
                        {
                            canGetEachScore       = false,
                            na_populationEntities = na_parentPopulationEntities,
                            a_brainScore          = a_brainScore,

                            nmhm_populationEntitiesScore = nmhm_parentEntitiesScore.AsParallelWriter()
                        }.Schedule(na_parentPopulationEntities.Length, 256, Dependency);

                        Dependency.Complete();

                        NativeArray <int> na_parentSortedKeysWithDuplicates = nmhm_parentEntitiesScore.GetKeyArray(Allocator.TempJob);
                        // This stores key keys in order. But keeps first unique keys at the front of an array.
                        // Total array size matches of total elements.
                        na_parentSortedKeysWithDuplicates.Sort();
                        // Sorted.
                        int i_uniqueKeyCount = na_parentSortedKeysWithDuplicates.Unique();

                        int i_eltieCountTemp = (int)(na_parentSortedKeysWithDuplicates.Length * manager.f_eliteSize);
                        // Minimum elite size mus be met.
                        int i_eltiesCount = i_eltieCountTemp > 0 ? i_eltieCountTemp : na_parentSortedKeysWithDuplicates.Length;

                        if (na_parentSortedKeysWithDuplicates.Length == 0)
                        {
                            Debug.LogError("Not enough elites for training. Please increase population, or elites %.");

                            na_offspringPopulationEntities.Dispose();
                            na_parentPopulationEntities.Dispose();
                            nmhm_parentEntitiesScore.Dispose();
                            na_parentSortedKeysWithDuplicates.Dispose();

                            continue;
                        }

                        NativeArray <EntityIndex> na_elities = new NativeArray <EntityIndex> (i_eltiesCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

                        DynamicBuffer <NNINdexProbabilityBuffer> a_eliteIndexProbability = indexProbabilityBuffer [nnManagerEntity];
                        int i_totalElitesScore = managerScore.i_elite;
                        a_eliteIndexProbability.ResizeUninitialized(i_totalElitesScore);

                        Dependency = new CommonJobs.GetElitesEntitiesJob()
                        {
                            i_eltiesCount = i_eltiesCount,

                            na_elities         = na_elities,
                            nmhm_entitiesScore = nmhm_parentEntitiesScore,
                            na_currentSortedKeysWithDuplicates = na_parentSortedKeysWithDuplicates
                        }.Schedule();



                        Dependency = new CalculateIndexProbabilityOfPopulationJob()
                        {
                            na_populationEntities = na_elities,

                            a_indexProbability = a_eliteIndexProbability,

                            a_brainScore = a_brainScore
                        }.Schedule(Dependency);


                        NativeArray <int> na_randomValues = new NativeArray <int> (na_parentPopulationEntities.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

                        random.NextInt2();
                        Dependency = new RandomIntsJob()
                        {
                            na_randomValues = na_randomValues,
                            random          = random
                        }.Schedule(Dependency);

                        Dependency.Complete();

// Debug.LogError ( "parent pop: " + na_parentPopulationEntities.Length + "; offspring pop: " + na_offspringPopulationEntities.Length ) ;
                        Dependency = new DNACrossOverJob()
                        {
                            na_parentPopulationEntities    = na_parentPopulationEntities,
                            na_offspringPopulationEntities = na_offspringPopulationEntities,

                            na_indexProbability = a_eliteIndexProbability.Reinterpret <int> ().AsNativeArray(),

                            input2HiddenLayersWeightsBuffer  = NNInput2HiddenLayersWeightsBuffer,
                            hidden2OutputLayersWeightsBuffer = NNHidden2OutputLayersWeightsBuffer,

                            na_randomValues = na_randomValues,
                            random          = random,

                            // i_eliteScore                     = i_eliteScore
                        }.Schedule(na_parentPopulationEntities.Length, 256, Dependency);

                        Dependency.Complete();

                        na_randomValues.Dispose();
                        na_elities.Dispose();
                        nmhm_parentEntitiesScore.Dispose();
                        na_parentSortedKeysWithDuplicates.Dispose();
                    }

                    ecb.RemoveComponent <NNMangerIsSpawningNewGenerationTag> (nnManagerEntity);
                    becb.AddJobHandleForProducer(Dependency);


                    na_offspringPopulationEntities.Dispose();
                    na_parentPopulationEntities.Dispose();
                }
            } // for


            Entities
            .WithName("GenerationSpawningIsCompleteJob")
            .WithAll <NNBrainTag, IsSpawningTag> ()
            .ForEach((Entity entity, int entityInQueryIndex) =>
            {
                ecbp.RemoveComponent <IsSpawningTag> (entityInQueryIndex, entity);
                ecbp.AddComponent <IsSpawningCompleteTag> (entityInQueryIndex, entity);
            }).ScheduleParallel();

            becb.AddJobHandleForProducer(Dependency);
        }
Пример #11
0
        static void HandleCompletePath(ComponentDataFromEntity <LocalToWorld> localToWorldFromEntity, Entity entity, Rotation rotation, ref NavAgent agent, ref DynamicBuffer <NavPathBufferElement> pathBuffer, Parent surface, Translation translation, PhysicsWorld physicsWorld, float elapsedSeconds, EntityCommandBuffer.ParallelWriter commandBuffer, int entityInQueryIndex, NavSettings settings)
        {
            var rayInput = new RaycastInput
            {
                Start  = localToWorldFromEntity[entity].Position + agent.Offset,
                End    = math.forward(rotation.Value) * settings.ObstacleRaycastDistanceMax,
                Filter = new CollisionFilter
                {
                    BelongsTo    = NavUtil.ToBitMask(settings.ColliderLayer),
                    CollidesWith = NavUtil.ToBitMask(settings.ObstacleLayer)
                }
            };

            if (
                !surface.Value.Equals(agent.DestinationSurface) &&
                !NavUtil.ApproxEquals(translation.Value, agent.LocalDestination, settings.StoppingDistance) &&
                !physicsWorld.CastRay(rayInput, out RaycastHit hit)
                )
            {
                agent.JumpSeconds = elapsedSeconds;

                commandBuffer.RemoveComponent <NavWalking>(entityInQueryIndex, entity);
                commandBuffer.AddComponent <NavJumping>(entityInQueryIndex, entity);
                commandBuffer.AddComponent <NavPlanning>(entityInQueryIndex, entity);

                return;
            }

            commandBuffer.RemoveComponent <NavWalking>(entityInQueryIndex, entity);
            commandBuffer.RemoveComponent <NavDestination>(entityInQueryIndex, entity);
        }
Пример #12
0
 protected static void triggerTimedSpawner(int jobIndex, ref TimedSpawner spawner, ref EntityCommandBuffer.ParallelWriter buffer, in Translation translation, in Rotation rotation)
        // Update is called once per frame
        protected override void OnUpdate( )
        {
            EntityCommandBuffer.ParallelWriter ecbp = becb.CreateCommandBuffer().AsParallelWriter();

            CollisionFilter collisionFilter = new CollisionFilter();

            collisionFilter.BelongsTo    = 1;
            collisionFilter.CollidesWith = 1;

            Entities
            .WithName("CreateBarrierCollidersJob")
            .WithAll <CreateColliderTag, BarrierTag> ()
            .ForEach((Entity entity, int entityInQueryIndex, in CompositeScale scale) =>
            {
                float3 f3_scale = new float3(scale.Value.c0.x, scale.Value.c1.y, 1);
                BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry {
                    Center = 0, Size = f3_scale, Orientation = quaternion.identity
                }, collisionFilter);

                PhysicsCollider physicsCollider = new PhysicsCollider()
                {
                    Value = collider
                };

                ecbp.AddComponent(entityInQueryIndex, entity, physicsCollider);
                ecbp.RemoveComponent <CreateColliderTag> (entityInQueryIndex, entity);
            }).ScheduleParallel();

            collisionFilter.BelongsTo    = 2;
            collisionFilter.CollidesWith = 1;

            Entities
            .WithName("CreateScoreCollidersJob")
            .WithAll <CreateColliderTag, ScoreTag> ()
            .ForEach((Entity entity, int entityInQueryIndex, in CompositeScale scale) =>
            {
                float3 f3_scale = new float3(scale.Value.c0.x, scale.Value.c1.y, 1);
                BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry {
                    Center = 0, Size = f3_scale, Orientation = quaternion.identity
                }, collisionFilter);

                PhysicsCollider physicsCollider = new PhysicsCollider()
                {
                    Value = collider
                };

                ecbp.AddComponent(entityInQueryIndex, entity, physicsCollider);
                ecbp.RemoveComponent <CreateColliderTag> (entityInQueryIndex, entity);
            }).ScheduleParallel();

            collisionFilter.BelongsTo    = 1;  // 4
            collisionFilter.CollidesWith = 1;

            Entities
            .WithName("CreateVehicleCollidersJob")
            .WithAll <CreateColliderTag, VehicleControllsComponent> ()
            .ForEach((Entity entity, int entityInQueryIndex, in CompositeScale scale) =>
            {
                float3 f3_scale = new float3(scale.Value.c0.x, scale.Value.c1.y, 1);
                BlobAssetReference <Unity.Physics.Collider> collider = Unity.Physics.BoxCollider.Create(new BoxGeometry {
                    Center = 0, Size = f3_scale, Orientation = quaternion.identity
                }, collisionFilter);

                PhysicsCollider physicsCollider = new PhysicsCollider()
                {
                    Value = collider
                };

                ecbp.AddComponent(entityInQueryIndex, entity, physicsCollider);
                ecbp.RemoveComponent <CreateColliderTag> (entityInQueryIndex, entity);
            }).ScheduleParallel();

            becb.AddJobHandleForProducer(Dependency);
        }
 /// <summary>
 /// Adds the buffer to the entity and returns the buffer
 /// </summary>
 /// <param name="ecb"></param>
 /// <param name="entity"></param>
 /// <returns></returns>
 public static DynamicBuffer <ProceduralGenerationSpawnDataBufferElement> AddBuffer(EntityCommandBuffer.ParallelWriter ecb, int indexJob, Entity entity)
 {
     return(ecb.AddBuffer <ProceduralGenerationSpawnDataBufferElement>(indexJob, entity));
 }
Пример #15
0
 public Entity Build(EntityCommandBuffer.ParallelWriter entityCommandBuffer, int threadId)
 {
     return(Build(EntityManagerWrapper.FromJobCommandBuffer(entityCommandBuffer, threadId)));
 }
Пример #16
0
        public static EntityWrapper CreateEntity(int threadId, EntityCommandBuffer.ParallelWriter entityCommandBuffer)
        {
            var entityManagerWrapper = EntityManagerWrapper.FromJobCommandBuffer(entityCommandBuffer, threadId);

            return(new EntityWrapper(entityManagerWrapper.CreateEntity(), entityManagerWrapper));
        }
        public override void UpdateSystem()
        {
            EntityCommandBuffer.ParallelWriter ecb = m_endSimulationECB.CreateCommandBuffer().AsParallelWriter();

            Entities.ForEach((Entity entity, int entityInQueryIndex, ref CurrentTarget target, ref PreviousTarget previousTarget, ref CurrentAIState currentAIState) =>
            {
                if (currentAIState.requestedAIState == AIState.None)
                {
                    return;
                }

                //Remove all states.
                ecb.RemoveComponent <IdleState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <MovingToPositionState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <MovingToHarvestState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <HarvestingState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <MovingToDepositState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <MovingToAttackState>(entityInQueryIndex, entity);
                ecb.RemoveComponent <AttackingState>(entityInQueryIndex, entity);

                //Add state that we're switching to.
                switch (currentAIState.requestedAIState)
                {
                case AIState.Idle:
                    {
                        ecb.AddComponent <IdleState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to Idle");
                        break;
                    }

                case AIState.MovingToPosition:
                    {
                        ecb.AddComponent <MovingToPositionState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to MovingToPosition");
                        break;
                    }

                case AIState.MovingToHarvest:
                    {
                        ecb.AddComponent <MovingToHarvestState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to MovingToHarvest");
                        break;
                    }

                case AIState.Harvesting:
                    {
                        ecb.AddComponent <HarvestingState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to Harvesting");
                        break;
                    }

                case AIState.MovingToDeposit:
                    {
                        ecb.AddComponent <MovingToDepositState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to MovingToDeposit");
                        break;
                    }

                case AIState.MovingToAttack:
                    {
                        ecb.AddComponent <MovingToAttackState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to MovingToAttack");
                        break;
                    }

                case AIState.Attacking:
                    {
                        ecb.AddComponent <AttackingState>(entityInQueryIndex, entity);
                        Debug.Log("State changed to Attacking");
                        break;
                    }

                default:
                    {
                        Debug.Assert(false, "Unrecognised State");
                        break;
                    }
                }

                //Set our target and previous target data.
                previousTarget.targetData = target.targetData;
                target.targetData         = currentAIState.requestedAIStateTargetData;

                currentAIState.CompleteStateChange();
            }).Schedule();

            m_endSimulationECB.AddJobHandleForProducer(Dependency);
        }
Пример #18
0
        public static EntityWrapper CreateEntity <T>(int threadId, EntityCommandBuffer.ParallelWriter entityCommandBuffer) where T : IArchetypeDescriptor
        {
            var entityManagerWrapper = EntityManagerWrapper.FromJobCommandBuffer(entityCommandBuffer, threadId);

            return(new EntityWrapper(entityManagerWrapper.CreateEntityFromArchetype <T>(), entityManagerWrapper));
        }
Пример #19
0
        public override void UpdateSystem()
        {
            bool leftClickPressed  = m_inputManagementSystem.InputData.mouseInput.leftClickPressed;
            bool leftClickReleased = m_inputManagementSystem.InputData.mouseInput.leftClickReleased;
            bool leftClickDown     = m_inputManagementSystem.InputData.mouseInput.leftClickDown;

            if (leftClickDown || leftClickReleased)
            {
                Dependency = JobHandle.CombineDependencies(Dependency, m_raycastSystem.RaycastSystemDependency);
                NativeArray <float3>        boxBounds              = m_boxBounds;
                NativeArray <float3>        boxBoundsSorted        = new NativeArray <float3>(2, Allocator.TempJob);
                NativeArray <RaycastResult> selectionRaycastResult = m_raycastSystem.SelectionRaycastResult;
                NativeArray <RaycastResult> raycastResult          = m_raycastSystem.RaycastResult;
                //Calculate box bounds
                Dependency = Job.WithReadOnly(selectionRaycastResult).WithCode(() =>
                {
                    if (leftClickPressed)
                    {
                        boxBounds[0] = selectionRaycastResult[0].hitPosition;
                    }
                    if (leftClickDown)
                    {
                        boxBounds[1] = selectionRaycastResult[0].hitPosition;
                    }

                    boxBoundsSorted[0] = math.min(boxBounds[0], boxBounds[1]);
                    boxBoundsSorted[1] = math.max(boxBounds[0], boxBounds[1]);
                }).Schedule(Dependency);

                EntityCommandBuffer.ParallelWriter ecbConcurrent = m_entityCommandBuffer.CreateCommandBuffer().AsParallelWriter();

                //Remove previous selections
                if (leftClickReleased)
                {
                    //Deselect things outside of the box
                    Dependency = Entities
                                 .WithReadOnly(boxBoundsSorted)
                                 .WithAll <SelectedTag>()
                                 .ForEach((Entity entity, int entityInQueryIndex, in Translation translation) =>
                    {
                        if (((translation.Value.x < boxBoundsSorted[0].x) || (translation.Value.x > boxBoundsSorted[1].x) &&
                             (translation.Value.z < boxBoundsSorted[0].z) || (translation.Value.z > boxBoundsSorted[1].z)) &&
                            (raycastResult[0].raycastTargetEntity != entity))
                        {
                            ecbConcurrent.RemoveComponent <SelectedTag>(entityInQueryIndex, entity);
                        }
                    }).ScheduleParallel(Dependency);

                    //Select units inside the box if they aren't already selected
                    Dependency = Entities
                                 .WithReadOnly(boxBoundsSorted)
                                 .WithAll <UnitTag>()
                                 .WithNone <SelectedTag>()
                                 .WithReadOnly(raycastResult)
                                 .ForEach((Entity entity, int entityInQueryIndex, in Translation translation) =>
                    {
                        if (((translation.Value.x > boxBoundsSorted[0].x) && (translation.Value.x < boxBoundsSorted[1].x) &&
                             (translation.Value.z > boxBoundsSorted[0].z) && (translation.Value.z < boxBoundsSorted[1].z)) ||
                            (raycastResult[0].raycastTargetEntity == entity))
                        {
                            ecbConcurrent.AddComponent(entityInQueryIndex, entity, new SelectedTag {
                            });
                        }
                    }).ScheduleParallel(Dependency);

                    m_entityCommandBuffer.AddJobHandleForProducer(Dependency);

                    redrawSelectedUnits = true;
                }
                boxBoundsSorted.Dispose(Dependency);
            }
        }
Пример #20
0
        public static EntityWrapper Instantiate(Entity prefab, int threadId, EntityCommandBuffer.ParallelWriter entityCommandBuffer)
        {
            var entityManagerWrapper = EntityManagerWrapper.FromJobCommandBuffer(entityCommandBuffer, threadId);

            return(new EntityWrapper(entityManagerWrapper.Instantiate(prefab), entityManagerWrapper));
        }
        protected override void OnUpdate( )
        {
            EntityCommandBuffer.ParallelWriter ecbp = eecb.CreateCommandBuffer().AsParallelWriter();

            this.random.NextInt2();
            Unity.Mathematics.Random random = this.random;


            ComponentDataFromEntity <NNManagerComponent> a_manager = GetComponentDataFromEntity <NNManagerComponent> (true);

            Entities
            .WithName("NNCreateFirstGenerationWeightsJob")
            .WithAll <NNBrainTag, IsSpawningCompleteTag, NNIsFirstGenerationTag> ()
            .WithNone <IsInitializedTag> ()
            .WithReadOnly(a_manager)
            .ForEach((Entity entity, ref DynamicBuffer <NNInput2HiddenLayersWeightsBuffer> a_input2hiddenLayerWeights, ref DynamicBuffer <NNHidden2OutputLayersWeightsBuffer> a_hidden2OutputLayerWeights, in NNAssignedToManagerComponent assignedManager) =>
            {
                random.InitState((uint)(random.NextInt() + entity.Index));
                random.NextInt2();

                NNManagerComponent managerComponent = a_manager [assignedManager.entity];

                float f_muatationRange = managerComponent.f_muatationRange;

                // Initialize random weights.
                for (int i = 0; i < a_input2hiddenLayerWeights.Length; i++)
                {
                    a_input2hiddenLayerWeights [i] = new NNInput2HiddenLayersWeightsBuffer()
                    {
                        f = random.NextFloat(-f_muatationRange, f_muatationRange)
                    };
                }

                // Initialize random weights.
                for (int i = 0; i < a_hidden2OutputLayerWeights.Length; i++)
                {
                    a_hidden2OutputLayerWeights [i] = new NNHidden2OutputLayersWeightsBuffer()
                    {
                        f = random.NextFloat(-f_muatationRange, f_muatationRange)
                    };
                }
            }).ScheduleParallel();


            this.random.NextInt2();
            random = this.random;

            // DNA mutation.
            Entities
            .WithName("NNDNAMutationJob")
            .WithAll <NNBrainTag, IsSpawningCompleteTag> ()
            .WithNone <IsInitializedTag, NNIsFirstGenerationTag> ()
            .WithReadOnly(a_manager)
            .ForEach((Entity entity, ref DynamicBuffer <NNInput2HiddenLayersWeightsBuffer> a_offspringInput2HiddenLayersWeights, ref DynamicBuffer <NNHidden2OutputLayersWeightsBuffer> a_offspringtHidden2OutputLayersWeights, in NNAssignedToManagerComponent assignedManager) =>
            {
                random.InitState((uint)(random.NextInt() + entity.Index));
                random.NextInt2();

                NNManagerComponent managerComponent = a_manager [assignedManager.entity];

                float f_range = managerComponent.f_muatationRange;

                float f_groupSelection = random.NextFloat();

                if (f_groupSelection <= managerComponent.f_firstGroupSizeInPercentage)
                {
                    float f_majorMutationChance     = managerComponent.f_majorMutationChance0;
                    float f_minorMutationChance     = managerComponent.f_minorMutationChance0;
                    float f_minorMutationRangeScale = managerComponent.f_minorMutationRangeScale0;

                    _MutationChances(ref a_offspringInput2HiddenLayersWeights, ref a_offspringtHidden2OutputLayersWeights, ref random, f_range, f_majorMutationChance, f_minorMutationChance, f_minorMutationRangeScale);
                }
                else if (f_groupSelection <= managerComponent.f_secondGroupSizeInPercentage)
                {
                    float f_majorMutationChance     = managerComponent.f_majorMutationChance1;
                    float f_minorMutationChance     = managerComponent.f_minorMutationChance1;
                    float f_minorMutationRangeScale = managerComponent.f_minorMutationRangeScale1;

                    _MutationChances(ref a_offspringInput2HiddenLayersWeights, ref a_offspringtHidden2OutputLayersWeights, ref random, f_range, f_majorMutationChance, f_minorMutationChance, f_minorMutationRangeScale);
                }
                else if (f_groupSelection <= managerComponent.f_thirdGroupSizeInPercentage)
                {
                    float f_majorMutationChance     = managerComponent.f_majorMutationChance2;
                    float f_minorMutationChance     = managerComponent.f_minorMutationChance2;
                    float f_minorMutationRangeScale = managerComponent.f_minorMutationRangeScale2;

                    _MutationChances(ref a_offspringInput2HiddenLayersWeights, ref a_offspringtHidden2OutputLayersWeights, ref random, f_range, f_majorMutationChance, f_minorMutationChance, f_minorMutationRangeScale);
                }
                else
                {
                    float f_majorMutationChance     = managerComponent.f_majorMutationChance2;
                    float f_minorMutationChance     = managerComponent.f_minorMutationChance2;
                    float f_minorMutationRangeScale = managerComponent.f_minorMutationRangeScale2;

                    _MutationChances(ref a_offspringInput2HiddenLayersWeights, ref a_offspringtHidden2OutputLayersWeights, ref random, f_range, f_majorMutationChance, f_minorMutationChance, f_minorMutationRangeScale);
                }
            }).ScheduleParallel();

            random.NextUInt2();

            // DNA mutation.
            Entities
            .WithName("NNDNAMutationOfFirstGenerationJob")
            .WithAll <NNBrainTag, IsSpawningCompleteTag, NNIsFirstGenerationTag> ()
            .WithNone <IsInitializedTag> ()
            .WithReadOnly(a_manager)
            .ForEach((Entity entity, ref DynamicBuffer <NNInput2HiddenLayersWeightsBuffer> a_offspringInput2HiddenLayersWeights, ref DynamicBuffer <NNHidden2OutputLayersWeightsBuffer> a_offspringtHidden2OutputLayersWeights, in NNAssignedToManagerComponent assignedManager) =>
            {
                random.InitState((uint)(random.NextInt() + entity.Index));
                random.NextInt2();

                NNManagerComponent managerComponent = a_manager [assignedManager.entity];

                float f_range = managerComponent.f_muatationRange;

                for (int i = 0; i < a_offspringInput2HiddenLayersWeights.Length; i++)
                {
                    float f = random.NextFloat(-f_range, f_range);
                    a_offspringInput2HiddenLayersWeights [i] = new NNInput2HiddenLayersWeightsBuffer()
                    {
                        f = f
                    };
                }

                for (int i = 0; i < a_offspringtHidden2OutputLayersWeights.Length; i++)
                {
                    float f = random.NextFloat(-f_range, f_range);
                    a_offspringtHidden2OutputLayersWeights [i] = new NNHidden2OutputLayersWeightsBuffer()
                    {
                        f = f
                    };
                }
            }).ScheduleParallel();

            Entities
            .WithName("NNActivateNewPopulationJob")
            .WithAll <NNBrainTag, IsSpawningCompleteTag> ()
            .WithNone <IsInitializedTag> ()
            .ForEach((Entity entity, int entityInQueryIndex) =>
            {
                ecbp.AddComponent <IsInitializedTag> (entityInQueryIndex, entity);
                ecbp.AddComponent <IsAliveTag> (entityInQueryIndex, entity);
            }).ScheduleParallel();

            eecb.AddJobHandleForProducer(Dependency);
        }
Пример #22
0
 private static void SetupViewPart(int entityInQueryIndex, Entity entity, Entity parent, float4x4 localToParent, EntityCommandBuffer.ParallelWriter commandBuffer)
 {
     commandBuffer.AddComponent(entityInQueryIndex, entity, new Parent {
         Value = parent
     });
     commandBuffer.AddComponent(entityInQueryIndex, entity, new LocalToParent {
         Value = localToParent
     });;
     commandBuffer.AddComponent(entityInQueryIndex, entity, new ViewPart());
 }
Пример #23
0
        public static void Execute(NativeArray <byte> inputData, ComponentType typeToRemove, int typeSize, NativeArray <Entity> entityArray,
                                   NativeArray <PersistenceState> persistenceStateArray, EntityCommandBuffer.ParallelWriter ecb, int batchIndex)
        {
            for (int i = 0; i < entityArray.Length; i++)
            {
                var persistenceState = persistenceStateArray[i];
                int stride           = typeSize + PersistenceMetaData.SizeOfStruct;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
                if (persistenceState.ArrayIndex * stride >= inputData.Length)
                {
                    throw new IndexOutOfRangeException("RemoveComponent:: persistenceState.ArrayIndex seems to be out of range. Or the stride is wrong.");
                }
#endif

                var metaData = UnsafeUtility.ReadArrayElementWithStride <PersistenceMetaData>(inputData.GetUnsafeReadOnlyPtr(), persistenceState.ArrayIndex, stride);
                if (metaData.AmountFound == 0)
                {
                    ecb.RemoveComponent(batchIndex, entityArray[i], typeToRemove);
                }
            }
        }
Пример #24
0
 public static void PlayAnimJobs(Entity entity, int index, EntityCommandBuffer.ParallelWriter entityCommandBuffer, Skeleton_Data skeletonData, ECS_UnitAnimType.TypeEnum ecsUnitAnimTypeEnum, Vector3 dir, Skeleton_Anim_OnComplete onComplete)
 {
     PlayAnimJobs(entity, index, entityCommandBuffer, skeletonData, ecsUnitAnimTypeEnum, GetAnimDir(dir), onComplete);
 }
Пример #25
0
 //Returns true if entity is already on location, false if it is not
 private static bool GoToLocation(Entity location, Entity movedEntity, ComponentDataFromEntity <CurrentLocationComponent> currentLocationData,
                                  ComponentDataFromEntity <ExchangeTaskLocationComponent> exchangeTaskLocationData, EntityCommandBuffer.ParallelWriter ecb, int entityInQueryIndex)
 {
     if (location == movedEntity)
     {
         return(true);
     }
     if (!currentLocationData.HasComponent(movedEntity) || !(currentLocationData[movedEntity].entity == location))
     {
         var exchangeTaskLocation = new ExchangeTaskLocationComponent {
             Entity = location
         };
         if (exchangeTaskLocationData.HasComponent(movedEntity))
         {
             exchangeTaskLocationData[movedEntity] = exchangeTaskLocation;
         }
         else
         {
             ecb.AddComponent(entityInQueryIndex, movedEntity, exchangeTaskLocation);
         }
         return(false);
     }
     else
     {
         return(true);
     }
 }
Пример #26
0
 public EntityManagerWrapper(EntityCommandBuffer.ParallelWriter entityCommandBufferConcurrent, int jobIndex)
 {
     EntityCommandBufferConcurrent = entityCommandBufferConcurrent;
     EntityCommandBufferJobIndex   = jobIndex;
     Type = EntityManagerType.ENTITY_COMMAND_BUFFER_CONCURRENT;
 }
 public DynamicBufferEntityPrefab(EntityCommandBuffer.ParallelWriter ecb, int jobIndex)
 {
     dataEntity = ecb.CreateEntity(jobIndex);
     buffer     = ecb.AddBuffer <EntityPrefabBufferElement>(jobIndex, dataEntity);
 }
Пример #28
0
 public static EntityManagerWrapper FromJobCommandBuffer(EntityCommandBuffer.ParallelWriter commandBuffer, int jobIndex)
 {
     return(new EntityManagerWrapper(commandBuffer, jobIndex));
 }
        protected override void OnUpdate()
        {
            EntityCommandBuffer.ParallelWriter parallelWriterECB = ecbSource.CreateCommandBuffer().AsParallelWriter();

            // Entities with GeneralPurposeComponentA but not StateComponentB
            Entities
            .WithNone <StateComponentB>()
            .ForEach(
                (Entity entity, int entityInQueryIndex, in GeneralPurposeComponentA gpA) =>
            {
                // Add an ISystemStateComponentData instance
                parallelWriterECB.AddComponent <StateComponentB>
                (
                    entityInQueryIndex,
                    entity,
                    new StateComponentB()
                {
                    State = 1
                }
                );
            })
            .ScheduleParallel();
            ecbSource.AddJobHandleForProducer(this.Dependency);

            // Create new command buffer
            parallelWriterECB = ecbSource.CreateCommandBuffer().AsParallelWriter();

            // Entities with both GeneralPurposeComponentA and StateComponentB
            Entities
            .WithAll <StateComponentB>()
            .ForEach(
                (Entity entity,
                 int entityInQueryIndex,
                 ref GeneralPurposeComponentA gpA) =>
            {
                // Process entity, in this case by decrementing the Lifetime count
                gpA.Lifetime--;

                // If out of time, destroy the entity
                if (gpA.Lifetime <= 0)
                {
                    parallelWriterECB.DestroyEntity(entityInQueryIndex, entity);
                }
            })
            .ScheduleParallel();
            ecbSource.AddJobHandleForProducer(this.Dependency);

            // Create new command buffer
            parallelWriterECB = ecbSource.CreateCommandBuffer().AsParallelWriter();

            // Entities with StateComponentB but not GeneralPurposeComponentA
            Entities
            .WithAll <StateComponentB>()
            .WithNone <GeneralPurposeComponentA>()
            .ForEach(
                (Entity entity, int entityInQueryIndex) =>
            {
                // This system is responsible for removing any ISystemStateComponentData instances it adds
                // Otherwise, the entity is never truly destroyed.
                parallelWriterECB.RemoveComponent <StateComponentB>(entityInQueryIndex, entity);
            })
            .ScheduleParallel();
            ecbSource.AddJobHandleForProducer(this.Dependency);
        }
Пример #30
0
 public EntityCommandJob(EntityCommandBuffer.ParallelWriter ecb, Entity entity, int jobIndex)
 {
     _ecb      = ecb;
     _entity   = entity;
     _jobIndex = jobIndex;
 }