// Redo later while cycling through world chunk references rather then all worlds public void DestroyWorld(Entity worldEntity) { if (!World.EntityManager.Exists(worldEntity)) { return; } List <int> removeIDs = new List <int>(); World world = World.EntityManager.GetComponentData <World>(worldEntity); var worldID = World.EntityManager.GetComponentData <ZoxID>(worldEntity).id; //foreach (Entity e in chunks.Values) for (int i = 0; i < world.chunks.Length; i++) { var e = world.chunks[i]; var chunk = World.EntityManager.GetComponentData <Chunk>(e); var chunkID = World.EntityManager.GetComponentData <ZoxID>(e).id; //if (chunk.worldID == worldID) //{ var lookupTable = worldSpawnSystem.worldLookups[worldID]; lookupTable.chunks.Remove(chunk.Value.chunkPosition); worldSpawnSystem.worldLookups[worldID] = lookupTable; removeIDs.Add(chunkID); // chunk.renderIDs if (World.EntityManager.HasComponent <MonsterSpawnZone>(e)) { var monsterSpawner = World.EntityManager.GetComponentData <MonsterSpawnZone>(e); for (int a = 0; a < monsterSpawner.spawnedIDs.Length; a++) { characterDeathSystem.DestroyCharacter(monsterSpawner.spawnedIDs[a]); } } // for all chunkRenders (should be) for (int a = 0; a < chunk.chunkRenders.Length; a++) { var chunkRender = chunk.chunkRenders[a]; ChunkRenderSystem.DestroyChunkRender(World.EntityManager, chunkRender); } if (World.EntityManager.Exists(e)) { World.EntityManager.DestroyEntity(e); } //} } foreach (int removeID in removeIDs) { chunks.Remove(removeID); } }
public void RemoveRenderEntitiesFromChunk(ref Chunk chunk) { if (ChunkSpawnSystem.isDebugLog) { Debug.LogError("Removing chunk renders from chunk: " + chunk.Value.chunkPosition); } if (chunk.chunkRenders.Length != 0) { for (int i = 0; i < chunk.chunkRenders.Length; i++) { var chunkRenderEntity = chunk.chunkRenders[i]; ChunkRenderSystem.DestroyChunkRender(World.EntityManager, chunkRenderEntity); } chunk.chunkRenders = new BlitableArray <Entity>(0, Allocator.Persistent); } }
public void RemoveChunk(Entity chunkEntity, bool isRemoveFromWorld = true) { //if (chunks.ContainsKey(chunkID)) { //Entity chunkEntity = chunks[chunkID]; Chunk chunk = World.EntityManager.GetComponentData <Chunk>(chunkEntity); var chunkID = World.EntityManager.GetComponentData <ZoxID>(chunkEntity).id; var worldID = World.EntityManager.GetComponentData <ZoxID>(chunkEntity).creatorID; var lookupTable = worldSpawnSystem.worldLookups[worldID]; lookupTable.chunks.Remove(chunk.Value.chunkPosition); worldSpawnSystem.worldLookups[worldID] = lookupTable; if (isRemoveFromWorld) { Entity worldEntity = chunk.world; World world = World.EntityManager.GetComponentData <World>(worldEntity); int3[] positions = new int3[0]; var chunkPosition = chunk.Value.chunkPosition; RemovePositionIndex(ref world, chunkPosition, chunkID, positions, int3.Forward()); //new float3(0, 0, 1)); RemovePositionIndex(ref world, chunkPosition, chunkID, positions, int3.Back()); //new float3(0, 0, -1)); RemovePositionIndex(ref world, chunkPosition, chunkID, positions, int3.Right()); // new float3(1, 0, 0)); RemovePositionIndex(ref world, chunkPosition, chunkID, positions, int3.Left()); //new float3(-1, 0, 0)); World.EntityManager.SetComponentData(worldEntity, world); } for (int j = 0; j < chunk.chunkRenders.Length; j++) { Entity chunkRender = chunk.chunkRenders[j]; if (World.EntityManager.Exists(chunkRender)) { ChunkRenderSystem.DestroyChunkRender(World.EntityManager, chunkRender); } } if (World.EntityManager.HasComponent <MonsterSpawnZone>(chunkEntity)) { MonsterSpawnZone monsterSpawner = World.EntityManager.GetComponentData <MonsterSpawnZone>(chunkEntity); int[] spawnedIDs = monsterSpawner.spawnedIDs.ToArray(); for (int j = 0; j < spawnedIDs.Length; j++) { characterDeathSystem.DestroyCharacter(spawnedIDs[j]); } } chunks.Remove(chunkID); Chunk.Destroy(World.EntityManager, chunkEntity); } }
public void Initialize(Unity.Entities.World space) { // entity spawn worldSpawnSystem = space.GetOrCreateSystem <WorldSpawnSystem>(); AddSystemToUpdateList(worldSpawnSystem); chunkSpawnSystem = space.GetOrCreateSystem <ChunkSpawnSystem>(); AddSystemToUpdateList(chunkSpawnSystem); chunkRenderSystem = space.GetOrCreateSystem <ChunkRenderSystem>(); AddSystemToUpdateList(chunkRenderSystem); voxelSpawnSystem = space.GetOrCreateSystem <VoxelSpawnSystem>(); AddSystemToUpdateList(voxelSpawnSystem); // mesh gen chunkSideSystem = space.GetOrCreateSystem <ChunkSidesSystem>(); AddSystemToUpdateList(chunkSideSystem); chunkSideCullSystem = space.GetOrCreateSystem <ChunkSideCullingSystem>(); AddSystemToUpdateList(chunkSideCullSystem); chunkToRendererSystem = space.GetOrCreateSystem <ChunkToRendererSystem>(); AddSystemToUpdateList(chunkToRendererSystem); chunkMeshBuildSystem = space.GetOrCreateSystem <ChunkMeshBuilderSystem>(); AddSystemToUpdateList(chunkMeshBuildSystem); chunkMeshEndSystem = space.GetOrCreateSystem <ChunkMeshEndingSystem>(); AddSystemToUpdateList(chunkMeshEndSystem); chunkWeightBuilder = space.GetOrCreateSystem <ChunkWeightBuilder>(); AddSystemToUpdateList(chunkWeightBuilder); // maps chunkMapStarterSystem = space.GetOrCreateSystem <ChunkMapStarterSystem>(); AddSystemToUpdateList(chunkMapStarterSystem); chunkMapBuilderSystem = space.GetOrCreateSystem <ChunkMapBuilderSystem>(); AddSystemToUpdateList(chunkMapBuilderSystem); chunkMapCompleterSystem = space.GetOrCreateSystem <ChunkMapCompleterSystem>(); AddSystemToUpdateList(chunkMapCompleterSystem); // player streaming worldStreamSystem = space.GetOrCreateSystem <WorldStreamSystem>(); AddSystemToUpdateList(worldStreamSystem); chunkStreamSystem = space.GetOrCreateSystem <ChunkStreamSystem>(); AddSystemToUpdateList(chunkStreamSystem); chunkStreamEndSystem = space.GetOrCreateSystem <ChunkStreamEndSystem>(); AddSystemToUpdateList(chunkStreamEndSystem); // interact voxelRaycastSystem = space.GetOrCreateSystem <VoxelRaycastSystem>(); AddSystemToUpdateList(voxelRaycastSystem); voxelPreviewSystem = space.GetOrCreateSystem <VoxelPreviewSystem>(); AddSystemToUpdateList(voxelPreviewSystem); characterRaycastSystem = space.GetOrCreateSystem <CharacterRaycastSystem>(); AddSystemToUpdateList(characterRaycastSystem); renderSystem = space.GetOrCreateSystem <RenderSystem>(); AddSystemToUpdateList(renderSystem); if (Bootstrap.instance.isAnimateRenders) { chunkRendererAnimationSystem = space.GetOrCreateSystem <ChunkRendererAnimationSystem>(); AddSystemToUpdateList(chunkRendererAnimationSystem); } if (Bootstrap.DebugChunks) { debugChunkSystem = space.GetOrCreateSystem <DebugChunkSystem>(); AddSystemToUpdateList(debugChunkSystem); } if (!Bootstrap.isRenderChunks) { chunkSideSystem.Enabled = false; } SetLinks(); }
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(); }