//private void Awake() //{ // //sets a random Seed ~ this should be done in the game manager later // if (RandomSeed) // seed = Random.Range(-10000, 10000); // Generate(); //} public void Generate() { //Creates the village gameobject village = new GameObject(); village.name = "Village"; village.transform.position = Vector3.zero; village.isStatic = true; //Sets up for the shuffle for (int i = 0; i < VillageSize.x; i++) { for (int j = 0; j < VillageSize.y; j++) { Tiles.Add(new Coord(i, j)); } } //queues shuffle shuffledCoords = new Queue <Coord>(Shuffle.ShuffleArray(Tiles.ToArray())); //sets up the center of the map to be empty MapCenter = new Coord((int)(VillageSize.x * .5f), (int)(VillageSize.y * .5f)); //sets up the village map buildingMap = new bool[(int)VillageSize.x, (int)VillageSize.y]; //max number of houses allowed int houseCount = (int)(VillageSize.x * VillageSize.y * percentage); int currentHouseCount = 0; //while under the max number of houses for (int i = 0; i < houseCount; i++) { //get the next coord Coord rnd = GetRandomCoord(); buildingMap[rnd.x, rnd.y] = true; currentHouseCount++; //if its the map center of the building isnt accessible anymore and checks its within the radius if (rnd == MapCenter || !IsAccessible(buildingMap, currentHouseCount) || (new Vector2(rnd.x, rnd.y) - new Vector2(MapCenter.x, MapCenter.y)).magnitude > radius) { buildingMap[rnd.x, rnd.y] = false; currentHouseCount--; } } //smooth out the houses into blocks for (int i = 0; i < numOfIterations; i++) { SmoothOutSides(); } //build the connected cells together into blocks for (int x = 0; x < VillageSize.x; x++) { for (int y = 0; y < VillageSize.y; y++) { //local function ~ if its within the list then return true bool inList(Coord c) { for (int i = 0; i < buildings.Count; i++) { if (buildings[i].tiles.Contains(c)) { return(true); } } return(false); } if (buildingMap[x, y] && !inList(new Coord(x, y))) { //create a new object GameObject buildingGroup = new GameObject(); buildingGroup.name = "Building Group"; buildingGroup.transform.parent = village.transform; buildingGroup.isStatic = true; //add the building component to it Building b = buildingGroup.AddComponent <Building>(); //add in the current coord to that list b.AddCoord(new Coord(x, y)); //create a temporary search list List <Coord> toSearch = new List <Coord>(); //add in the close coords if (x + 1 < VillageSize.x) { toSearch.Add(new Coord(x + 1, y)); } if (x - 1 >= 0) { toSearch.Add(new Coord(x - 1, y)); } if (y + 1 < VillageSize.y) { toSearch.Add(new Coord(x, y + 1)); } if (y - 1 >= 0) { toSearch.Add(new Coord(x, y - 1)); } //while there are still coords to search through while (toSearch.Count > 0) { //take the first element in the array Coord searchingCoord = toSearch[0]; //if this is a taken space on the map if (buildingMap[searchingCoord.x, searchingCoord.y]) { //add the coord to the buildings list b.AddCoord(searchingCoord); //searches through all the next coords if (searchingCoord.x + 1 < VillageSize.x) { if (!b.tiles.Contains(new Coord(searchingCoord.x + 1, searchingCoord.y)) && !toSearch.Contains(new Coord(searchingCoord.x + 1, searchingCoord.y))) { toSearch.Add(new Coord(searchingCoord.x + 1, searchingCoord.y)); } } if (searchingCoord.x - 1 >= 0) { if (!b.tiles.Contains(new Coord(searchingCoord.x - 1, searchingCoord.y)) && !toSearch.Contains(new Coord(searchingCoord.x - 1, searchingCoord.y))) { toSearch.Add(new Coord(searchingCoord.x - 1, searchingCoord.y)); } } if (searchingCoord.y + 1 < VillageSize.y) { if (!b.tiles.Contains(new Coord(searchingCoord.x, searchingCoord.y + 1)) && !toSearch.Contains(new Coord(searchingCoord.x, searchingCoord.y + 1))) { toSearch.Add(new Coord(searchingCoord.x, searchingCoord.y + 1)); } } if (searchingCoord.y - 1 >= 0) { if (!b.tiles.Contains(new Coord(searchingCoord.x, searchingCoord.y - 1)) && !toSearch.Contains(new Coord(searchingCoord.x, searchingCoord.y - 1))) { toSearch.Add(new Coord(searchingCoord.x, searchingCoord.y - 1)); } } } //remove it from the search list toSearch.Remove(searchingCoord); } //building validating if (b.Size <= MinBuildingSize || b.Size > MaxBuildingSize) { foreach (Coord c in b.tiles) { buildingMap[c.x, c.y] = false; } DestroyImmediate(buildingGroup); } else { //if its a valid building, make it more blocky for (int j = 0; j < 20; j++) { List <Coord> changed = b.Smooth(); foreach (Coord c in changed) { buildingMap[c.x, c.y] = true; } } //adds the building component to a list buildings.Add(b); } } } } //create paths around buildings. CreatePath(); //sort buildings buildings = BuildingSorter(buildings, true); //assign buildings AssingFunction(); //create buildings CreateBuildings(); SpawnNPC(); }
public void CreateBuildings() { int i = 0; for (int j = 0; j < buildings.Count; j++) { buildings[j].identifier = districtColors[i % districtColors.Length]; buildings[j].Build(BuildingList, animate); buildings[j].gameObject.name = (j.ToString()); i++; } //create unique buildings first bool castle = false, barracks = false, square = false; int startingIndex = 0; //quest generation first if (data != null) { if (!data.requiresCastle) { startingIndex = 1; castle = true; } } for (int index = startingIndex; index < buildings.Count; index++) { if (buildings[index].containsUniqueBuilding) { continue; } int neededBuilding = (!castle ? 1 : (!barracks ? 2 : 3)); int j = buildings[index].CreateUnique(neededBuilding); if (j == 1) { buildings[index].containsUniqueBuilding = true; castle = true; index = 0; } if (j == 2) { buildings[index].containsUniqueBuilding = true; barracks = true; index = 0; } if (j == 3) { buildings[index].containsUniqueBuilding = true; square = true; break; } if (index == buildings.Count - 1) { if (!castle) { StartCoroutine(ReStartCreation()); return; castle = true; index = 0; continue; } if (!barracks) { barracks = true; index = 0; continue; } if (!square) { square = true; index = 0; break; } } } foreach (Building b in buildings) { b.Create(); //b.Visualize(cube); } //moves the village to have its ceneter at 0,0 //village.transform.position = new Vector3(-VillageSize.x * 4f, 0, -VillageSize.y * 4f); //create the terrain StartCoroutine(TerrainGenerator.instance.Generate(buildingMap, lowresPathMap, radius)); List <Coord> terrainMapReturn = TerrainGenerator.instance.GetMap(); //create a new object GameObject buildingGroup = new GameObject(); buildingGroup.name = "Envioment Group"; buildingGroup.transform.parent = village.transform; buildingGroup.isStatic = true; //add the building component to it Building enviomentGroup = buildingGroup.AddComponent <Building>(); foreach (Coord c in terrainMapReturn) { enviomentGroup.AddCoord(c); buildingMap[c.x, c.y] = true; } enviomentGroup.functionType = Building.Function.Envioment; buildings.Add(enviomentGroup); enviomentGroup.identifier = districtColors[0]; enviomentGroup.Build(BuildingList, animate); enviomentGroup.Create(); //enviomentGroup.Visualize(cube); CreateNature(); }