public GameObject[] setRandomRegion(int difficulty) { currentRegion = (MapHelper.Region)UnityEngine.Random.Range(0, 4); float difficultyMultiplier = difficultyResourceModifier * difficulty; GameObject[] currentTileSet = null; if (currentRegion == MapHelper.Region.Forest) { currentTileSet = forestTiles; currentResourceChance = forestResourceChance - difficultyMultiplier; } else if (currentRegion == MapHelper.Region.Desert) { currentTileSet = desertTiles; currentResourceChance = desertResourceChance - difficultyMultiplier; } else if (currentRegion == MapHelper.Region.City) { currentTileSet = cityTiles; currentResourceChance = cityResourceChance - difficultyMultiplier; } else if (currentRegion == MapHelper.Region.Neon) { currentTileSet = neonTiles; currentResourceChance = neonResourceChance - difficultyMultiplier; } return(currentTileSet); }
public void GenerateMap(out Vector3 startPoint, out Vector3 endPoint) { //currentMap = GetComponent<Tilemap>(); currentMap.ClearAllTiles(); int totalTileNum = UnityEngine.Random.Range(totalTileMin, totalTileMax); /* * //create starting region * int startX = mapWidth + (UnityEngine.Random.Range(0, 10) * UnityEngine.Random.Range(0, 2) * 2 - 1); * if (startX > mapWidth) * { * startX = startX - mapWidth; * } * * int startY = mapLength + (UnityEngine.Random.Range(0, 10) * UnityEngine.Random.Range(0, 2) * 2 - 1); * if (startY > mapLength) * { * startY = startY - mapLength; * } * * startX = 0; * startY = 0; * Vector2 startpos = new Vector2(startX, startY); */ //todo - always start with start spawn piece currentRegion = MapHelper.Region.Forest;//(MapHelper.Region)Random.Range(0, 3); GameObject[] currentTileSet = setRandomRegion(0); GameObject currentTile = currentTileSet[UnityEngine.Random.Range(0, currentTileSet.Length)]; Tilemap prefabTilemap = currentTile.GetComponent <Tilemap>(); List <Locator> previousRegionLocators = new List <Locator>(); print("add first tile"); //print(prefabTilemap); CopyTiles(currentMap, prefabTilemap, 0, 0); previousRegionLocators.AddRange(prefabTilemap.GetComponent <TileStats>().locators); Locator startLocator = previousRegionLocators[UnityEngine.Random.Range(0, previousRegionLocators.Count)]; startPoint = currentMap.CellToWorld(new Vector3Int((int)startLocator.location.x, (int)startLocator.location.y, 0)); print(startPoint); for (int i = 0; i < previousRegionLocators.Count; i++) { Locator temp = previousRegionLocators[i]; temp.difficulty = 0; previousRegionLocators[i] = temp; } print("add first region"); GenerateRegion(previousRegionLocators, out previousRegionLocators); //print("locator list count: " + prefabTilemap.GetComponent<TileStats>().locators.Count); //print("locator count: " + openLocators.Count); while (tileNum < totalTileNum) { GenerateRegion(previousRegionLocators, out previousRegionLocators); } List <Locator> endPointLocators = new List <Locator>(); endPoint = new Vector3(0, 0, 0); //mark end point location foreach (Locator l in previousRegionLocators) { if (l.difficulty >= 4) { endPointLocators.Add(l); } } if (endPointLocators.Count <= 0) { Debug.LogError("no endpoint found"); return; } Locator endPointLocator = endPointLocators[UnityEngine.Random.Range(0, endPointLocators.Count)]; endPoint = currentMap.CellToWorld(new Vector3Int((int)endPointLocator.location.x, (int)endPointLocator.location.y, 0)); //CopyTiles(currentMap, prefabTilemap, 5, 0); //check if tile has resource //currentRegionSize++; //grid, instead of simplifying 2,1, use actual tile numbers - 21, 67, then i have exact offset of each til //can calculate fitting //e.g. tile at x:25-53 are filled with one prefab, next prefab can start at 54 //bool filled, bool locator if open locator //on generate region - copy tile over immediately, fill out helper grid values, don't keep reference to prefab? //only really need filled/open locator values //on fill tile, check if surrounding tile is locator, if so, remove open locator //set of open locator, closed locator //for (int x = 0; x < mapWidth; x++) //{ //for (int y = 0; y < mapLength; y++) //{ //check region type of tile to connect with //starting region always minimum size (length + width?) //on creating new region //create random number of region size //assign difficulty number based on connecting square //if region changes, difficulty + 1 //if region same, same difficulty //given random number, generate number of resources tiles //percent + noise //note adjust add tile frequency based on how initial maps turn out //what if each tile has approx size attached? //e.g. [2,1] or [5,2] //helper script object //holds region type //region tile number //resource number //array of tile size //holds prefabs of which tiles, and position start of tiles? //calculate based on locator connection, tag match, positional offset of locator //tile 0 //pick random connector //add matching tile //assign tile location //add to list //set tile 0 locator as filled, + index of filling tile //set tile 1 locator as filled, + index of filling tile //back to tile 0 //random locator not filled //roll if add tile //fill if roll passed //set as filled without prefab if roll failed //if all connectors filled, go to next index tile //repeat until region filled //if last index of tile filled without meeting region number requirements //rand index # of array range //pick a filled locator without a prefab //add tile //repeat until quota filled //roll for chance of resource tile //percent different based on difficulty //different pool of region tiles if resource tile //change region type //update difficulty //make random # of region tiles //pick random existing tile //pick random number of array //pick random direction //move in tile direction until i hit something that isn't populated //check to see there's at least a 3x3 grid of open space //start filling in from the first tile //for locator checks //when random locator chosen, check the corresponding grid tile //make sure no prefab already exists, if exist then assign to that locator //fill in large grid tracker as tiles are populated //for each region made, add region size to total size tracker //total size also random //when total size >= size set //make new tilemap, copy paste tiles based on grid location, and tile location inside prefab //overwrite tiles already existing //use helper function to ensure correct sprite //add props/exit //fake array grid with max side ranges, e.g. max 100 pieces, then 100x100 //pick random grid to start from from an area near the edge //grid numbers e.g. 50,3, with reference to the tile that makes up the area //exit - find all tiles >= a given distance from entrance //pick random tile //helper script for replacing tile types //check all tiles around it, check all tiles about to be around it from prefab? //change sprite depending on which side(s) are free //create random array (2d?) of which tile type //e.g. [[1,2,3],[2,4],[,8,1]] //choose random tile //characteristics of tile kept in public script? //e.g. [north, pond], [east] //along with which instance is attached to it //choose random direction of existing connectors //get direction connection requirements based on tags? //add random tile fulfilling requirements based on tag //add to total # of tiles in region //roll random number of if next connector gets a connection //the smaller the number of region type in group, the more likely the connecting tile is same region //starting region difficulty 1 // //} //} }