public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        RemoveComponent rmc = (RemoveComponent)target;

        if (GUILayout.Button("Remove Component"))
        {
            rmc.RemoveComponents();
        }
    }
Example #2
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        if (m_array.IsCreated)
        {
            m_array.Dispose();
        }
        if (pathOutput.IsCreated)
        {
            pathOutput.Dispose();
        }
        if (currentSelectedEntity.IsCreated)
        {
            currentSelectedEntity.Dispose();
        }

        EntityQuery m_GroupCalculateMove = GetEntityQuery(ComponentType.ReadOnly <CalculateMove>());

        if (m_GroupCalculateMove.CalculateEntityCount() > 0 && m_GroupCalculateMove.CalculateEntityCount() < 2)
        {
            //GetCalculate Move Entity
            m_array = m_GroupCalculateMove.ToComponentDataArray <CalculateMove>(Allocator.TempJob);

            EntityQuery          e_GroupMap = GetEntityQuery(typeof(MapEntityBuffer));
            NativeArray <Entity> e_array    = e_GroupMap.ToEntityArray(Allocator.TempJob);

            DynamicBuffer <Entity> mapEntityBuffers = EntityManager.GetBuffer <MapEntityBuffer>(e_array[0]).Reinterpret <Entity>();
            NativeArray <PathNode> pathNodeArray    = new NativeArray <PathNode>(mapEntityBuffers.Length, Allocator.TempJob);

            // send the end position and startposition with the calculatemove tag
            PopulatePathNodeArray populatePathNodeArrayJob = new PopulatePathNodeArray
            {
                endPosition = m_array[0].Destination,
                lookup      = GetComponentDataFromEntity <Tile>(true),
                mapArray    = mapEntityBuffers.ToNativeArray(Allocator.TempJob),
                nodeArray   = pathNodeArray
            };

            JobHandle populatePathArrayHandle = populatePathNodeArrayJob.Schedule(mapEntityBuffers.Length, 32);
            //inputDeps = populatePathArrayHandle;
            // calculate the path and add the MovePath component the the entity


            populatePathArrayHandle.Complete();

            pathOutput = new NativeList <int2>(Allocator.TempJob);

            FindPathJob findPathJob = new FindPathJob
            {
                endPosition   = m_array[0].Destination,
                startPosition = m_array[0].StartPosition,
                gridSize      = new int2(TileHandler.instance.width, TileHandler.instance.height),
                pathNodeArray = pathNodeArray,
                path          = pathOutput
            };

            JobHandle findPathJobHandle = findPathJob.Schedule(populatePathArrayHandle);
            //inputDeps = JobHandle.CombineDependencies(inputDeps, populatePathArrayHandle);
            findPathJobHandle.Complete();

            if (pathOutput.Length >= 1)
            {
                //remove the CalculateMove component from the entity - create a foreach job with the unitselected and calculate move components
                currentSelectedEntity = new NativeArray <Entity>(1, Allocator.TempJob);
                QueueMoveEntity queueMoveEntity = new QueueMoveEntity
                {
                    currentEntity     = currentSelectedEntity,
                    calculateMoveType = typeof(CalculateMove),
                    commandBuffer     = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
                    pathLength        = pathOutput.Length
                };

                JobHandle queueMoveEntityHandle = queueMoveEntity.Schedule(this, JobHandle.CombineDependencies(findPathJobHandle, inputDeps));
                inputDeps = JobHandle.CombineDependencies(inputDeps, queueMoveEntityHandle);

                queueMoveEntityHandle.Complete();

                if (!currentSelectedEntity[0].Equals(Entity.Null))
                {
                    UpdateBuffer updateBuffer = new UpdateBuffer
                    {
                        path   = pathOutput.AsArray(),
                        entity = currentSelectedEntity[0],
                        lookup = GetBufferFromEntity <PathBuffer>(false)
                    };
                    inputDeps = updateBuffer.Schedule(inputDeps);
                }
            }
            else
            {
                RemoveComponent removeComponent = new RemoveComponent
                {
                    commandBuffer       = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
                    typeOfCalculateMove = typeof(CalculateMove)
                };
                inputDeps = removeComponent.Schedule(this, inputDeps);
            }
            e_array.Dispose();
        }

        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(inputDeps);



        return(inputDeps);
    }