/*public int GetFirstWorldID() * { * foreach (KeyValuePair<int, Entity> KVP in worlds) * { * return KVP.Key; * } * return 0; * }*/ public int SpawnMap(MapDatam map, Entity game) { int newID = Bootstrap.GenerateUniqueID(); SpawnWorld(newID, map.worldPosition, map, game); return(newID); }
public int QueueMap(float3 spawnPosition, MapDatam map, Entity game) { int newID = Bootstrap.GenerateUniqueID(); Entity e = World.EntityManager.CreateEntity(); World.EntityManager.AddComponentData(e, new SpawnMapCommand { spawnID = newID, mapID = map.id, spawnPosition = spawnPosition, game = game }); return(newID); }
private void UpdateMap(Entity worldEntity, MapDatam map, int id) { if (maps.ContainsKey(id)) { maps[id] = map; } else { maps.Add(id, map); } // initiate the biomes with tilemap values for (int i = 0; i < map.biomes.Count; i++) { map.biomes[i].InitializeIDs(map.tilemap); } World.EntityManager.SetComponentData(worldEntity, new Rotation { Value = Quaternion.Euler(map.worldRotation) }); World.EntityManager.SetComponentData(worldEntity, new NonUniformScale { Value = map.worldScale }); World world = new World { chunkIDs = new BlitableArray <int>(0, Unity.Collections.Allocator.Persistent), chunkPositions = new BlitableArray <int3>(0, Unity.Collections.Allocator.Persistent), scale = map.worldScale, voxelDimensions = map.voxelDimensions, bounds = 32 }; World.EntityManager.SetComponentData(worldEntity, world); World.EntityManager.SetComponentData(worldEntity, new ZoxID { id = id }); if (Application.isPlaying == false) { WorldStreamSystem.StreamChunksIn(World.EntityManager, chunkSpawnSystem, world.modelID != 0, worldEntity, ref world, int3.Zero(), Bootstrap.GetRenderDistance(), Bootstrap.GetLoadDistance()); } }
private void SpawnWorld(int id, float3 spawnPosition, MapDatam map, Entity gameEntity) { if (map != null) { Entity world = World.EntityManager.CreateEntity(worldArchtype); worlds.Add(id, world); worldLookups.Add(id, new WorldChunkMap { chunks = new Dictionary <int3, Entity>() }); World.EntityManager.SetComponentData(world, new Translation { Value = spawnPosition }); UpdateMap(world, map, id); Game game = World.EntityManager.GetComponentData <Game>(gameEntity); game.map = world; World.EntityManager.SetComponentData(gameEntity, game); } else { Debug.LogError("Cannot spawn world at: " + spawnPosition.ToString() + " as map is null."); } }
protected override void OnUpdate() { Entities.WithAll <WorldGenerationChunk>().ForEach((Entity e, ref Chunk chunk, ref WorldGenerationChunk baby) => { if (baby.state == 0) { baby.state = 1; chunk.isGenerating = 1; } else { return; } int worldID = World.EntityManager.GetComponentData <ZoxID>(e).creatorID; MapDatam map = worldSpawnSystem.maps[worldID]; Biome chunkBiome = new Biome { }; // World.EntityManager.GetComponentData<ChunkTerrain>(e); chunkBiome.biomes = new BlitableArray <byte>((int)(chunk.Value.voxelDimensions.x * chunk.Value.voxelDimensions.z), Allocator.Persistent); chunkBiome.blends = new BlitableArray <float>((int)(chunk.Value.voxelDimensions.x * chunk.Value.voxelDimensions.z), Allocator.Persistent); World.EntityManager.AddComponentData(e, chunkBiome); ChunkTerrain chunkTerrain = new ChunkTerrain { }; // World.EntityManager.GetComponentData<ChunkTerrain>(e); chunkTerrain.biomes = new BlitableArray <BiomeData>(map.biomes.Count, Allocator.Persistent); for (int i = 0; i < map.biomes.Count; i++) { chunkTerrain.biomes[i] = map.biomes[i].Value; } chunkTerrain.chunkPosition = chunk.Value.chunkPosition; chunkTerrain.heights = new BlitableArray <int>((int)(chunk.Value.voxelDimensions.x * chunk.Value.voxelDimensions.z), Allocator.Persistent); World.EntityManager.AddComponentData(e, chunkTerrain); var chunkPosition = chunk.GetVoxelPosition(); var dimensions = chunk.Value.voxelDimensions; float leftSideChunk = chunkPosition.x; float rightSideChunk = chunkPosition.x + dimensions.x; float leftSideChunkZ = chunkPosition.z; float rightSideChunkZ = chunkPosition.z + dimensions.z; // Use something like WorldTowns, and pass down relevant chunk information into the chunks List <Town> towns = new List <Town>(); List <Building> buildings = new List <Building>(); for (int i = 0; i < map.towns.Count; i++) { float leftSideBuilding = map.towns[i].position.x - map.towns[i].dimensions.x; float rightSideBuilding = map.towns[i].position.x + map.towns[i].dimensions.x; float leftSideBuildingZ = map.towns[i].position.z - map.towns[i].dimensions.z; float rightSideBuildingZ = map.towns[i].position.z + map.towns[i].dimensions.z; if (!(leftSideBuilding > rightSideChunk || rightSideBuilding < leftSideChunk || leftSideBuildingZ > rightSideChunkZ || rightSideBuildingZ < leftSideChunkZ)) { towns.Add(map.towns[i]); //for (int j = 0; j < map.buildings.Count; j++) { ///buildings.Add(map.buildings[j]); } } } // if building intersects chunk for (int i = 0; i < map.buildings.Count; i++) { float leftSideBuilding = map.buildings[i].position.x - map.buildings[i].dimensions.x; float rightSideBuilding = map.buildings[i].position.x + map.buildings[i].dimensions.x; float leftSideBuildingZ = map.buildings[i].position.z - map.buildings[i].dimensions.z; float rightSideBuildingZ = map.buildings[i].position.z + map.buildings[i].dimensions.z; //Debug.DrawLine(new float3(leftSideBuilding)) if (!(leftSideBuilding > rightSideChunk || rightSideBuilding < leftSideChunk || leftSideBuildingZ > rightSideChunkZ || rightSideBuildingZ < leftSideChunkZ)) { buildings.Add(map.buildings[i]); } } // for towns ChunkTown chunkTown = new ChunkTown { }; // World.EntityManager.GetComponentData<ChunkTown>(e); if (towns.Count > 0) { chunkTown.towns = new BlitableArray <Town>(towns.Count, Allocator.Persistent); for (int i = 0; i < towns.Count; i++) { chunkTown.towns[i] = towns[i]; //Debug.DrawLine(chunk.GetVoxelPosition() + new float3(8, 0, 8), chunk.GetVoxelPosition() + new float3(8, 64, 8), // Color.green, 30); //Debug.LogError("Town added to chunk: " + chunk.Value.chunkPosition + " : " + towns[i].position); } } if (buildings.Count > 0) { chunkTown.buildings = new BlitableArray <Building>(buildings.Count, Allocator.Persistent); for (int i = 0; i < buildings.Count; i++) { chunkTown.buildings[i] = buildings[i]; //Debug.DrawLine(chunk.GetVoxelPosition() + new float3(8, 0, 8), chunk.GetVoxelPosition() + new float3(8, 64, 8), // Color.red, 30); //Debug.LogError("BUildings added to chunk: " + chunk.Value.chunkPosition + " : " + buildings[i].position); } } World.EntityManager.AddComponentData(e, chunkTown); /* chunkTown.centrePosition = float3.zero; * chunkTown.wallSize = new float3(60, 8, 60); * chunkTown.wallThickness = 4f;*/ /*chunkTown.buildings[0] = new Building * { * position = new float3(-8, 0, -8), * dimensions = new float3(4, 4, 4) * }; * chunkTown.buildings[1] = new Building * { * position = new float3(8, 0, -8), * dimensions = new float3(4, 4, 4) * };*/ //if (map.towns.buildings) // monsters //if (biome.monsters.Count > 0) { MonsterSpawnZone monsterSpawner = new MonsterSpawnZone { }; // World.EntityManager.GetComponentData<MonsterSpawnZone>(e); monsterSpawner.lastTimeSpawned = UnityEngine.Time.time - 9; monsterSpawner.spawnDatas = new BlitableArray <MonsterBiome>(map.biomes.Count, Allocator.Persistent); for (int i = 0; i < monsterSpawner.spawnDatas.Length; i++) { int monsterMetaID = 0; if (map.biomes[i].monsters.Count != 0) { monsterMetaID = map.biomes[i].monsters[UnityEngine.Random.Range(0, map.biomes[i].monsters.Count - 1)].Value.id; } monsterSpawner.spawnDatas[i] = new MonsterBiome { spawnAmount = map.biomes[i].monsterSpawnAmount, spawnCooldown = map.biomes[i].monsterSpawnCooldown, monsterMetaID = monsterMetaID }; } //monsterSpawner.monsterMetaID = biome.monsters[UnityEngine.Random.Range(0, biome.monsters.Count)].Value.id; //monsterSpawner.spawnAmount = biome.monsterSpawnAmount; //monsterSpawner.spawnCooldown = biome.monsterSpawnCooldown; monsterSpawner.CalculateValues(); World.EntityManager.AddComponentData(e, monsterSpawner); } }); }
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(); }