public int[] SpawnChunks(Entity world, int3[] positions, bool[] isRender) { SpawnChunkCommand newCommand = new SpawnChunkCommand(); newCommand.world = world; newCommand.SetChunkPositions(positions); newCommand.SetChunkRenders(isRender); // generate new ids var chunkIDs = new int[positions.Length]; for (int i = 0; i < chunkIDs.Length; i++) { chunkIDs[i] = Bootstrap.GenerateUniqueID(); } newCommand.SetChunkIDs(chunkIDs); Entity e = World.EntityManager.CreateEntity(); World.EntityManager.AddComponentData(e, newCommand); //SpawnChunks(newCommand); return(newCommand.chunkIDs); }
public void SpawnChunks(SpawnChunkCommand command) { var worldEntity = command.world; int worldID = World.EntityManager.GetComponentData <ZoxID>(worldEntity).id; var world = World.EntityManager.GetComponentData <World>(worldEntity); if (ChunkSpawnSystem.isDebugLog) { Debug.LogError("Spawning World's Chunks [" + command.chunkIDs.Length + "] with dimensions: " + world.voxelDimensions + ", id: " + worldID); } Translation worldTranslation = World.EntityManager.GetComponentData <Translation>(worldEntity); NativeArray <Entity> entities = new NativeArray <Entity>(command.chunkPositions.Length, Allocator.Temp); // materials int renderEntitiesCount = 0; for (int i = 0; i < command.isRender.Length; i++) { if (command.isRender[i] == 1) { renderEntitiesCount++; } } List <Material> materials = GetWorldMaterials(worldID); MapDatam map = null; VoxData model = new VoxData(); //NativeArray<Entity> renderEntities = new NativeArray<Entity>(renderEntitiesCount * materials.Count, Allocator.Temp); if (worldSpawnSystem.maps.ContainsKey(worldID)) { map = worldSpawnSystem.maps[worldID]; World.EntityManager.Instantiate(chunkPrefab, entities); //World.EntityManager.Instantiate(worldChunkRenderPrefab, renderEntities); } else if (worldSpawnSystem.models.ContainsKey(worldID)) { model = worldSpawnSystem.models[worldID]; World.EntityManager.Instantiate(modelChunkPrefab, entities); //World.EntityManager.Instantiate(modelChunkRenderPrefab, renderEntities); } // for all bullets, set custom data using indexes entity int renderEntityCount = 0; var chunkIDs = command.chunkIDs.ToArray(); var chunkPositions = command.chunkPositions.ToArray(); for (int i = 0; i < entities.Length; i++) { Entity chunkEntity = entities[i]; if (chunks.ContainsKey(chunkIDs[i])) { World.EntityManager.DestroyEntity(chunkEntity); continue; } chunks.Add(chunkIDs[i], entities[i]); var lookupTable = worldSpawnSystem.worldLookups[worldID]; if (lookupTable.chunks.ContainsKey(chunkPositions[i])) { lookupTable.chunks[chunkPositions[i]] = chunkEntity; } else { lookupTable.chunks.Add(chunkPositions[i], chunkEntity); } worldSpawnSystem.worldLookups[worldID] = lookupTable; World.EntityManager.SetComponentData(entities[i], new ZoxID { id = chunkIDs[i], creatorID = worldID }); Chunk chunk = World.EntityManager.GetComponentData <Chunk>(chunkEntity); chunk.world = worldEntity; chunk.Value.chunkPosition = chunkPositions[i]; chunk.Value.worldScale = world.scale; chunk.Value.voxelDimensions = world.voxelDimensions; chunk.Init(world.voxelDimensions); if (model.id != 0) { UpdateChunkWithModel(chunkEntity, ref chunk, model); } SetChunkSurroundingIndexes(chunkEntity, ref chunk); float3 spawnPosition = new float3( // worldOffset + chunk.Value.chunkPosition.x * chunk.Value.worldScale.x * chunk.Value.voxelDimensions.x, chunk.Value.chunkPosition.y * chunk.Value.worldScale.y * chunk.Value.voxelDimensions.y, chunk.Value.chunkPosition.z * chunk.Value.worldScale.z * chunk.Value.voxelDimensions.z); World.EntityManager.SetComponentData(chunkEntity, new Translation { Value = spawnPosition }); World.EntityManager.SetComponentData(chunkEntity, new NonUniformScale { Value = new float3(1, 1, 1) }); World.EntityManager.SetComponentData(chunkEntity, new Rotation { Value = quaternion.identity }); World.EntityManager.SetComponentData(chunkEntity, new Parent { Value = chunk.world }); // set depending on biome type - biomeDatam has CharacterDatam linked //World.EntityManager.SetComponentData(entities[i], chunk); World.EntityManager.SetComponentData(entities[i], chunk); } var entitiesArray = entities.ToArray();; world.chunks = new BlitableArray <Entity>(entitiesArray.Length, Allocator.Persistent); for (int i = 0; i < world.chunks.Length; i++) { world.chunks[i] = entitiesArray[i]; } World.EntityManager.SetComponentData(worldEntity, world); ChunkRenderSystem.SpawnChunkRenders(World.EntityManager, worldEntity, renderEntitiesCount, materials.Count, command.isRender.ToArray()); entities.Dispose(); }