Question by
unity_rLX1qcn8S_vjhA · Dec 10, 2018 at 10:37 PM ·
tilemaptileprocedural-generationmap-generationhex
How evenely distribute ownership of tiles?
Hi everyone,
Can somebody give a direction where I can find out how evenly distribute generated tile map to several "owners" (players)? I randomly spawn capitals on the map. Knowing the size of the map and number of players we set a limit on number of tiles each player should not exceed. I just don't get how can I randomly assign tiles around capitals so that the total amount would be even for all players, while not making country shape to stretchy or looking too unnatural. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GameManagerScript : MonoBehaviour {
//______________________________
//Instances
MapGenerator mapGenerator;
GameObject[] tiles;
//_______________________________
public string[] countryNames = { "France", "Germany", "USA", "Poland", "UK", "Spain", "Russia", "China", "Brazil", "Japan", "Iraq", "Iran" };
int CountryTileLimit;
public int numberOfPlayers;
Vector3[] capitalsCoords;
// Use this for initialization
void Start () {
tiles = GameObject.FindGameObjectsWithTag("Tile");
mapGenerator = GameObject.Find("Map").GetComponent<MapGenerator>();
CountryTileLimit = tiles.Length / numberOfPlayers;
//______________________________________________________________________
// Spanws capitals
for(int k=0;k<numberOfPlayers;k++) {
int x = Mathf.RoundToInt( Random.Range(0, mapGenerator.mapSize.x));
// print(x);
int y = Mathf.RoundToInt(Random.Range(0, mapGenerator.mapSize.y));
// print(y);
Vector3 capital = new Vector3(x, 0, y);
capitalsCoords[k] = capital;
for (int tileNumber = 0; tileNumber < tiles.Length; tileNumber++)
{
TileProperties t = tiles[tileNumber].GetComponent<TileProperties>();
if (t.coord == capital) {
t.owner = countryNames[k];
Material m = t.tempMaterial;
m.color = Random.ColorHSV();
t.setMaterial(m);
}
}
//______________________________________________________________________
}
}
// Update is called once per frame
void Update () {
}
}
Comment