/// <summary> /// Checks if a generated point is a given distance to the road or any other tile than grass /// </summary> /// <param name="x"></param> /// <param name="z"></param> /// <param name="map"></param> /// <param name="dist"></param> /// <returns></returns> bool HousePossible(int x, int z, TDMapII map, int dist) { for (int i = 0; i < dist; i++) { if (!CheckIfPlacableTile(x + i, z + i, map)) return false; if (!CheckIfPlacableTile(x - i, z + i, map)) return false; if (!CheckIfPlacableTile(x + i, z - i, map)) return false; if (!CheckIfPlacableTile(x - i, z - i, map)) return false; if (!CheckIfPlacableTile(x, z + i, map)) return false; if (!CheckIfPlacableTile(x, z - i, map)) return false; if (!CheckIfPlacableTile(x + i, z, map)) return false; if (!CheckIfPlacableTile(x - i, z, map)) return false; } return true; }
/// <summary> /// Checks if a generated location is an land tile, so objects do not spawn on roads /// </summary> /// <param name="x"></param> /// <param name="z"></param> /// <param name="map"></param> /// <returns></returns> bool CheckIfPlacableTile(int x, int z, TDMapII map) { if (map.getTile(x, z) == 1 || map.getTile(x, z) == 2 || map.getTile(x, z) == 3) return true; else return false; }
/// <summary> /// Loads a TDMap into a tilemap used in this class /// </summary> /// <param name="Tiles"></param> public void LoadMapTiles(TDMapII Tiles) { TileMap = new int[300][]; for (int i = 0; i < 300; i++) { TileMap[i] = new int[300]; } for (int i = 0; i < 300; i++) { for (int j = 0; j < 300; j++) { TileMap[i][j] = Tiles.getTile(i, j); } } }
/// <summary> /// Places assets, such as houses and trees, in the world. /// It does not let objects collide with eachother during the build /// </summary> /// <param name="map"></param> public void ApplyAssets(TDMapII map) { int[] base_map = map.getBasePosition(); BasePos = new Vector3(150 - base_map[1], 0, 150 - base_map[0]); PlaceBase(); int[] Village = map.getVillages(); int[][] Forrests = map.getForrests(); int x_pos = Village[0]; int y_pos = Village[1]; for (int i = 0; i < 8; i++) { if (HousePos.Count == 60) break; for (int j = 0; j < 8; j++) { if (HousePos.Count == 60) break; int x = y_pos - 35 + 10 * i; int z = x_pos - 35 + 10 * j; Vector3 Pos = new Vector3(150 - x + offset, 5f, 150 - z + offset); if (HousePossible(z, x, map, 3) && ObjPossible(HousePos, Pos, 5)) { Vector3 Rot = new Vector3(-90, Random.Range(0, 2) * -90, 0); GameObject huisje = (GameObject)Instantiate(C_Building, Pos, Quaternion.identity); huisje.transform.Rotate(Rot); HousePos.Add(Pos); HouseInfo.Add(new Vector3[2] { Pos, Rot }); } } } nrHotels = HousePos.Count; float misses = 30; while (HousePos.Count <= nrHouses) { if (misses <= 0) break; int x = Random.Range(30, 270); int z = Random.Range(30, 270); Vector3 Pos = new Vector3(150 - x + offset, 0.8f, 150 - z + offset); if (HousePossible(z, x, map, 7) && ObjPossible(HousePos, Pos, 30)) { Vector3 Rot = new Vector3(90, Random.Range(0, 360), 0); GameObject huisje = (GameObject)Instantiate(House, Pos, Quaternion.identity); huisje.transform.Rotate(Rot); HousePos.Add(Pos); HouseInfo.Add(new Vector3[2] { Pos, Rot }); } else { misses--; } } //Debug.Log("misses left(house): " + misses + "\n nr houses: " + HousePos.Count); foreach (int[] forrest in Forrests) { int x_posF = forrest[0]; int y_posF = forrest[1]; int minF = forrest[2]; int maxF = forrest[3]; misses = 10; for (int i = 0; i < maxF*2; i++) { if (misses <= 0) break; int x = Random.Range(minF, maxF) + y_posF; int z = Random.Range(minF, maxF) + x_posF; Vector3 Pos = new Vector3(150 - x + offset, 0, 150 - z + offset); if (CheckIfPlacableTile(z, x, map) && ObjPossible(TreePos, Pos, 2) && ObjPossible(HousePos, Pos, 6)) { float nr = Random.Range(0f, 1f); if(nr >= .4) { Instantiate(Tree, Pos, Quaternion.identity); } else if (nr < .4) { Pos.y = 0.8f; Instantiate(PineTree, Pos, new Quaternion(-0.7071f, 0, 0, 0.7071f)); //PineTree } TreePos.Add(Pos); } else { misses--; } } } misses = 25; while (TreePos.Count <= nrTrees) { if (misses <= 0) break; int x = Random.Range(30, 270); int z = Random.Range(30, 270); Vector3 Pos = new Vector3(150 - x + offset, 0, 150 - z + offset); if (CheckIfPlacableTile(z, x, map) && ObjPossible(TreePos, Pos, 2) && ObjPossible(HousePos, Pos, 6)) { float nr = Random.Range(0f, 1f); if (nr >= .4) { Instantiate(Tree, Pos, Quaternion.identity); } else if (nr < .4) { Pos.y = 0.8f; Instantiate(PineTree, Pos, new Quaternion(-0.7071f, 0, 0, 0.7071f)); //PineTree } TreePos.Add(Pos); } else { misses--; } } for (int i = nrHotels; i < HousePos.Count; i++) { Vector3 Pos = HousePos[i]; int x = (int)(150 - Pos.z - offset); int z = (int)(150 - Pos.x - offset); if (HousePossible(z, x, map, 15) && ObjPossible(TreePos, Pos, 10)) { Pos.y = 0; placeWalls(Pos); WallsPos.Add(Pos); } } }
/// <summary> /// Creates a new map with textures and objects /// </summary> public void FirstLoad() { TDMapII map = new TDMapII(map_x, map_z); LoadMapTiles(map); BuildTexture(); //Debug.Log("texture build"); ApplyAssets(map); //Debug.Log("assets placed"); }