Ejemplo n.º 1
0
            public void Execute(int chunkIndex)
            {
                ArchetypeChunk chunk     = chunks[chunkIndex];
                int            dataCount = chunk.Count;

                NativeArray <Entity>              playerEntityArray        = chunk.GetNativeArray(entityTypeRO);
                NativeArray <PlayerInputData>     playerInputDataArray     = chunk.GetNativeArray(playerInputDataRO);
                NativeArray <PlayerMoveData>      playerMoveDataArray      = chunk.GetNativeArray(playerMoveDataRO);
                NativeArray <Position>            positionDataArray        = chunk.GetNativeArray(positionRO);
                NativeArray <PlayerSpawnBoltData> playerSpawnBoltDataArray = chunk.GetNativeArray(playerSpawnBoltDataRW);

                for (int dataIndex = 0; dataIndex < dataCount; dataIndex++)
                {
                    Entity              playerEntity        = playerEntityArray[dataIndex];
                    PlayerInputData     playerInputData     = playerInputDataArray[dataIndex];
                    PlayerMoveData      playerMoveData      = playerMoveDataArray[dataIndex];
                    Position            playerPosition      = positionDataArray[dataIndex];
                    PlayerSpawnBoltData playerSpawnBoltData = playerSpawnBoltDataArray[dataIndex];

                    if (playerInputData.fireButtonPressed == 1 && currentTime >= playerSpawnBoltData.nextFireTime)
                    {
                        playerSpawnBoltData.nextFireTime = currentTime + playerSpawnBoltData.fireRate;
                        spawnBoltEntityQueue.Enqueue(playerEntity);
                    }

                    playerSpawnBoltData.spawnPosition =
                        playerPosition.Value + (playerMoveData.forwardDirection * playerSpawnBoltData.offset);
                    playerSpawnBoltData.spawnDirection = playerMoveData.forwardDirection;

                    playerSpawnBoltDataArray[dataIndex] = playerSpawnBoltData;
                }
            }
            public void Execute(int index)
            {
                PlayerInputData     playerInputData     = playerInputDataArray[index];
                PlayerMoveData      playerMoveData      = playerMoveDataArray[index];
                PlayerSpawnBoltData playerSpawnBoltData = playerSpawnBoltDataArray[index];

                if (playerInputData.fireButtonPressed == 1 && currentTime >= playerSpawnBoltData.nextFireTime)
                {
                    playerSpawnBoltData.nextFireTime = currentTime + playerSpawnBoltData.fireRate;
                    spawnBoltEntityQueue.Enqueue(entityArray[index]);
                }

                playerSpawnBoltData.spawnPosition  = playerMoveData.position + (playerMoveData.forwardDirection * playerSpawnBoltData.offset);
                playerSpawnBoltData.spawnDirection = playerMoveData.forwardDirection;

                playerSpawnBoltDataArray[index] = playerSpawnBoltData;
            }
Ejemplo n.º 3
0
        JobHandle SpawnBoltFromEntityList(NativeList <Entity> entityList, Entity prefabEntity, bool isboltFromPlayerList, JobHandle jobDepency)
        {
            JobHandle jobDepencyToReturn = jobDepency;

            if (entityList.Length == 0)
            {
                return(jobDepencyToReturn);
            }

            UnityEngine.Profiling.Profiler.BeginSample("SpawnBoltFromEntityList");

            Entity boltCopy = EntityManager.Instantiate(prefabEntity);

            EntityManager.RemoveComponent <EntityPrefabData>(boltCopy);

            //Allocate the amount of entities we need in one shot
            NativeArray <Entity> newSpawnedBoltEntityArray = new NativeArray <Entity>(entityList.Length, Allocator.TempJob);

            EntityManager.Instantiate(boltCopy, newSpawnedBoltEntityArray);

            EntityManager.DestroyEntity(boltCopy);

            //If the bolts are from players we just set the BoltMoveData directly, they are not enough generated to warrant creating a job
            if (isboltFromPlayerList)
            {
                //For players bolt just set the new bolt data directly
                //(the cost of starting a job is not work the low amount of data to set)
                for (int i = 0; i < entityList.Length; i++)
                {
                    PlayerSpawnBoltData spawnBoltData = EntityManager.GetComponentData <PlayerSpawnBoltData>(entityList[i]);
                    BoltMoveData        moveData      = EntityManager.GetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i]);
                    moveData.position         = spawnBoltData.spawnPosition;
                    moveData.forwardDirection = spawnBoltData.spawnDirection;

                    EntityManager.SetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i], moveData);
                }

                newSpawnedBoltEntityArray.Dispose();
            }
            else
            {
                //For AI bolts, create a job to set the boltMoveData of the new entity
                //Use GetComponentDataFromEntity the get the components we need
                SetAIBoltMoveDataJob setAiBoldMoveDataJob = new SetAIBoltMoveDataJob
                {
                    spawningFromEntityList    = entityList,
                    spawnedBoltEntityArray    = newSpawnedBoltEntityArray,
                    aiSpawnBoltDataFromEntity = GetComponentDataFromEntity <AISpawnBoltData>(),
                    boldMoveDataFromEntity    = GetComponentDataFromEntity <BoltMoveData>(),
                };

                jobDepencyToReturn = setAiBoldMoveDataJob.Schedule(newSpawnedBoltEntityArray.Length,
                                                                   MonoBehaviourECSBridge.Instance.GetJobBatchCount(newSpawnedBoltEntityArray.Length),
                                                                   jobDepency);
            }


            UnityEngine.Profiling.Profiler.EndSample();

            return(jobDepencyToReturn);
        }
Ejemplo n.º 4
0
        JobHandle SpawnBoltFromEntityList(NativeList <Entity> entityList,
                                          NativeArray <Entity> newSpawnedBoltEntityArray,
                                          bool isboltFromPlayerList,
                                          JobHandle jobDepency)
        {
            JobHandle jobDepencyToReturn = jobDepency;

            if (entityList.Length == 0)
            {
                return(jobDepencyToReturn);
            }

            UnityEngine.Profiling.Profiler.BeginSample("SpawnBoltFromEntityList");

            //If the bolts are from players we just set the BoltMoveData directly, they are not enough generated to warrant creating a job
            if (isboltFromPlayerList)
            {
                //For players bolt just set the new bolt data directly
                //(the cost of starting a job is not work the low amount of data to set)
                for (int i = 0; i < entityList.Length; i++)
                {
                    PlayerSpawnBoltData spawnBoltData = EntityManager.GetComponentData <PlayerSpawnBoltData>(entityList[i]);

                    Position newPosition = new Position()
                    {
                        Value = spawnBoltData.spawnPosition,
                    };
                    EntityManager.SetComponentData <Position>(newSpawnedBoltEntityArray[i], newPosition);

                    Rotation newRotation = new Rotation()
                    {
                        Value = quaternion.LookRotation(new float3(0, -1, 0), new float3(0, 0, 1)),
                    };
                    EntityManager.SetComponentData <Rotation>(newSpawnedBoltEntityArray[i], newRotation);

                    BoltMoveData boltMoveData = EntityManager.GetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i]);
                    boltMoveData.forwardDirection = spawnBoltData.spawnDirection;
                    EntityManager.SetComponentData <BoltMoveData>(newSpawnedBoltEntityArray[i], boltMoveData);
                }

                newSpawnedBoltEntityArray.Dispose();
            }
            else
            {
                //For AI bolts, create a job to set the boltMoveData of the new entity
                //Use GetComponentDataFromEntity the get the components we need
                SetAIBoltMoveDataJob setAiBoldMoveDataJob = new SetAIBoltMoveDataJob
                {
                    spawningFromEntityList    = entityList,
                    spawnedBoltEntityArray    = newSpawnedBoltEntityArray,
                    aiSpawnBoltDataFromEntity = GetComponentDataFromEntity <AISpawnBoltData>(),
                    boltMoveDataFromEntity    = GetComponentDataFromEntity <BoltMoveData>(),
                    boltPositionFromEntity    = GetComponentDataFromEntity <Position>(),
                    boltRotationFromEntity    = GetComponentDataFromEntity <Rotation>(),
                };

                jobDepencyToReturn = setAiBoldMoveDataJob.Schedule(newSpawnedBoltEntityArray.Length,
                                                                   MonoBehaviourECSBridge.Instance.GetJobBatchCount(newSpawnedBoltEntityArray.Length),
                                                                   jobDepency);
            }


            UnityEngine.Profiling.Profiler.EndSample();

            return(jobDepencyToReturn);
        }