protected override void OnCreateManager(int capacity) { base.OnCreateManager(capacity); //Create our queues to hold entities to spawn bolt from aiBoltSpawnQueue = new NativeQueue <Entity>(Allocator.Persistent); playerBoltSpawnQueue = new NativeQueue <Entity>(Allocator.Persistent); aiBoltSpawnList = new NativeList <Entity>(100000, Allocator.Persistent); playerBoltSpawnList = new NativeList <Entity>(100000, Allocator.Persistent); //Create the entitie that holds our queue, one way of making them accessible to other systems BoltSpawnerEntityData data = new BoltSpawnerEntityData(); data.aiBoltSpawnQueueConcurrent = aiBoltSpawnQueue; data.playerBoltSpawnQueueConcurrent = playerBoltSpawnQueue; dataEntity = EntityManager.CreateEntity(); EntityManager.AddComponentData(dataEntity, data); //Create entities that we will use as "prefab" for our bolts //Add the EntityPrefabData IComponentData to make sure those entities are not picked up by systems prefabEnemyBolt = EntityManager.Instantiate(MonoBehaviourECSBridge.Instance.enemyBolt); EntityManager.AddComponentData <EntityPrefabData>(prefabEnemyBolt, new EntityPrefabData()); prefabAllyBolt = EntityManager.Instantiate(MonoBehaviourECSBridge.Instance.allyBolt); EntityManager.AddComponentData <EntityPrefabData>(prefabAllyBolt, new EntityPrefabData()); prefabPlayerBolt = EntityManager.Instantiate(MonoBehaviourECSBridge.Instance.playerBolt); EntityManager.AddComponentData <EntityPrefabData>(prefabPlayerBolt, new EntityPrefabData()); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityArray boltSpawnerEntityDataArray = boltSpawnerEntityDataGroup.GetEntityArray(); if (boltSpawnerEntityDataArray.Length == 0) { return(inputDeps); } BoltSpawnerEntityData boltSpawnerEntityData = GetComponentDataFromEntity <BoltSpawnerEntityData>()[boltSpawnerEntityDataArray[0]]; uniqueEntityTypes.Clear(); EntityManager.GetAllUniqueSharedComponentData(uniqueEntityTypes); JobHandle spawnJobHandle = new JobHandle(); JobHandle spawnJobDependency = inputDeps; for (int i = 0; i != uniqueEntityTypes.Count; i++) { EntityTypeData entityTypeData = uniqueEntityTypes[i]; if (entityTypeData.entityType == EntityTypeData.EntityType.EnemyShip || entityTypeData.entityType == EntityTypeData.EntityType.AllyShip) { aiSpawnBoltDataGroup.SetFilter(uniqueEntityTypes[i]); NativeQueue <Entity> .Concurrent spawnBoltEntityQueueToUse = boltSpawnerEntityData.enemyBoltSpawnQueueConcurrent; if (entityTypeData.entityType == EntityTypeData.EntityType.AllyShip) { spawnBoltEntityQueueToUse = boltSpawnerEntityData.allyBoltSpawnQueueConcurrent; } AISpawnBoltJob aiSpawnBoltJob = new AISpawnBoltJob { entityArray = aiSpawnBoltDataGroup.GetEntityArray(), aiPositionArray = aiSpawnBoltDataGroup.GetComponentDataArray <Position>(), aiRotationArray = aiSpawnBoltDataGroup.GetComponentDataArray <Rotation>(), aiSpawnBoltDataArray = aiSpawnBoltDataGroup.GetComponentDataArray <AISpawnBoltData>(), spawnBoltEntityQueue = spawnBoltEntityQueueToUse, deltaTime = Time.deltaTime }; JobHandle tmpJobHandle = aiSpawnBoltJob.Schedule(aiSpawnBoltJob.aiSpawnBoltDataArray.Length, MonoBehaviourECSBridge.Instance.GetJobBatchCount(aiSpawnBoltJob.aiSpawnBoltDataArray.Length), spawnJobDependency); spawnJobHandle = JobHandle.CombineDependencies(spawnJobHandle, tmpJobHandle); spawnJobDependency = JobHandle.CombineDependencies(spawnJobDependency, tmpJobHandle); } } aiSpawnBoltDataGroup.ResetFilter(); return(spawnJobHandle); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { EntityArray boltSpawnerEntityDataArray = boltSpawnerEntityDataGroup.GetEntityArray(); if (boltSpawnerEntityDataArray.Length == 0) { return(inputDeps); } BoltSpawnerEntityData boltSpawnerEntityData = GetComponentDataFromEntity <BoltSpawnerEntityData>()[boltSpawnerEntityDataArray[0]]; ArchetypeChunkEntityType entityTypeRO = GetArchetypeChunkEntityType(); ArchetypeChunkComponentType <PlayerInputData> playerInputDataRO = GetArchetypeChunkComponentType <PlayerInputData>(true); ArchetypeChunkComponentType <PlayerMoveData> playerMoveDataRO = GetArchetypeChunkComponentType <PlayerMoveData>(true); ArchetypeChunkComponentType <Position> positionRO = GetArchetypeChunkComponentType <Position>(true); ArchetypeChunkComponentType <PlayerSpawnBoltData> playerSpawnBoltDataRW = GetArchetypeChunkComponentType <PlayerSpawnBoltData>(false); //CreateArchetypeChunkArray runs inside a job, we can use a job handle to make dependency on that job //A NativeArray<ArchetypeChunk> is allocated with the correct size on the main thread and that's what is returned, we are responsible for de-allocating it (In this case using [DeallocateOnJobCompletion] in the move job) //The job scheduled by CreateArchetypeChunkArray fill that array with correct chunk information JobHandle createChunckArrayJobHandle = new JobHandle(); NativeArray <ArchetypeChunk> playerSpawnBoltDataChunks = playerSpawnBoltDataGroup.CreateArchetypeChunkArray(Allocator.TempJob, out createChunckArrayJobHandle); //Special case when our query return no chunk at all if (playerSpawnBoltDataChunks.Length == 0) { createChunckArrayJobHandle.Complete(); playerSpawnBoltDataChunks.Dispose(); return(inputDeps); } //Make sure our movejob is dependent on the job filling the array has completed JobHandle spawnJobDependency = JobHandle.CombineDependencies(inputDeps, createChunckArrayJobHandle); PlayerSpawnBoltJob playerSpawnBoltJob = new PlayerSpawnBoltJob { chunks = playerSpawnBoltDataChunks, entityTypeRO = entityTypeRO, playerInputDataRO = playerInputDataRO, playerMoveDataRO = playerMoveDataRO, positionRO = positionRO, playerSpawnBoltDataRW = playerSpawnBoltDataRW, spawnBoltEntityQueue = boltSpawnerEntityData.playerBoltSpawnQueueConcurrent, currentTime = Time.time, }; return(playerSpawnBoltJob.Schedule(playerSpawnBoltDataChunks.Length, MonoBehaviourECSBridge.Instance.GetJobBatchCount(playerSpawnBoltDataChunks.Length), spawnJobDependency)); }