이렇게 장애물 두개 나와야하는데 밑에처럼 한개 나오는데 어떻게 바꾸냐?
유튜브 탑다운 변형해서 장애물 두개로 설정한개로 한건데 실행하면 한개만 나와
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapGenerator : MonoBehaviour
{
public Map[] maps;
public int mapIndex;
public Transform tilePrefab;
public Transform obstaclePrefab;
public Transform obstaclePrefab2;
public Transform navmeshFloor;
public Transform navmeshMaskPrefab;
public Vector2 maxMapSize;
[Range(0, 1)]
public float outlinePercent;
public float tileSize;
List allTileCoords;
Queue shuffledTileCoords;
Map currentMap;
void Start()
{
GenerateMap();
}
public void GenerateMap()
{
currentMap = maps[mapIndex];
System.Random prng = new System.Random(currentMap.seed);
GetComponent().size = new Vector3(currentMap.mapSize.x * tileSize, .05f, currentMap.mapSize.y * tileSize);
// Generating coords
allTileCoords = new List();
for (int x = 0; x {
for (int y = 0; y {
allTileCoords.Add(new Coord(x, y));
}
}
shuffledTileCoords = new Queue(Utility.ShuffleArray(allTileCoords.ToArray(), currentMap.seed));
// Create map holder object
string holderName = "Generated Map";
if (transform.Find(holderName))
{
DestroyImmediate(transform.Find(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
// Spawning tiles
for (int x = 0; x {
for (int y = 0; y {
Vector3 tilePosition = CoordToPositon(x, y);
Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent) * tileSize;
newTile.parent = mapHolder;
}
}
// Spawning obstacles
bool[,] obstacleMap = new bool[(int)currentMap.mapSize.x, (int)currentMap.mapSize.y];
int obstacleCount = (int)(currentMap.mapSize.x * currentMap.mapSize.y * currentMap.obstaclePercent);
int currentObstacleCount = 0;
for (int i = 0; i {
Coord randomCoord = GetRandomCoord();
obstacleMap[randomCoord.x, randomCoord.y] = true;
currentObstacleCount++;
if (randomCoord != currentMap.mapCentre && MapIsFullyAcessible(obstacleMap, currentObstacleCount))
{
float obstacleHeight = Mathf.Lerp(currentMap.minObstacleHeight, currentMap.maxObstacleHeight, (float)prng.NextDouble());
Vector3 obstaclePosition = CoordToPositon(randomCoord.x, randomCoord.y);
Vector3 obstaclePosition2 = CoordToPositon(randomCoord.x+1, randomCoord.y+1);
Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition + Vector3.up * .5f, Quaternion.identity) as Transform;
Transform newObstacle2 = Instantiate(obstaclePrefab2, obstaclePosition2 + Vector3.up * .5f, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
newObstacle2.parent = mapHolder;
newObstacle.localScale = new Vector3((1 - outlinePercent) * tileSize, obstacleHeight, (1 - outlinePercent) * tileSize);
newObstacle2.localScale = new Vector3((1 - outlinePercent) * tileSize, obstacleHeight, (1 - outlinePercent) * tileSize);
}
else
{
obstacleMap[randomCoord.x, randomCoord.y] = false;
currentObstacleCount--;
}
}
// Creating navmesh mask
Transform maskLeft = Instantiate(navmeshMaskPrefab, Vector3.left * (currentMap.mapSize.x + maxMapSize.x) / 4f * tileSize, Quaternion.identity) as Transform;
maskLeft.parent = mapHolder;
maskLeft.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2f, 1, currentMap.mapSize.y) * tileSize;
Transform maskRight = Instantiate(navmeshMaskPrefab, Vector3.right * (currentMap.mapSize.x + maxMapSize.x) / 4f * tileSize, Quaternion.identity) as Transform;
maskRight.parent = mapHolder;
maskRight.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2f, 1, currentMap.mapSize.y) * tileSize;
Transform maskTop = Instantiate(navmeshMaskPrefab, Vector3.forward * (currentMap.mapSize.y + maxMapSize.y) / 4f * tileSize, Quaternion.identity) as Transform;
maskTop.parent = mapHolder;
maskTop.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y) / 2f) * tileSize;
Transform maskBottom = Instantiate(navmeshMaskPrefab, Vector3.back * (currentMap.mapSize.y + maxMapSize.y) / 4f * tileSize, Quaternion.identity) as Transform;
maskBottom.parent = mapHolder;
maskBottom.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y) / 2f) * tileSize;
navmeshFloor.localScale = new Vector3(maxMapSize.x, maxMapSize.y) * tileSize;
}
bool MapIsFullyAcessible(bool[,] obstacleMap, int currentObstacleCount)
{
bool[,] mapFlags = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)];
Queue quene = new Queue();
quene.Enqueue(currentMap.mapCentre);
mapFlags[currentMap.mapCentre.x, currentMap.mapCentre.y] = true;
int accessibleTileCount = 1;
while (quene.Count > 0)
{
Coord tile = quene.Dequeue();
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
int neighbourX = tile.x + x;
int neighbourY = tile.y + y;
if (x==0 || y==0)
{
if(neighbourX >= 0 && neighbourX = 0 && neighbourY {
if (!mapFlags[neighbourX,neighbourY] && !obstacleMap[neighbourX,neighbourY]) {
mapFlags[neighbourX, neighbourY] = true;
quene.Enqueue(new Coord(neighbourX, neighbourY));
accessibleTileCount++;
}
}
}
}
}
}
int targetAccesibleTileCount = (int)currentMap.mapSize.x * (int)currentMap.mapSize.y - currentObstacleCount;
return targetAccesibleTileCount == accessibleTileCount;
}
Vector3 CoordToPositon(int x, int y)
{
return new Vector3(-currentMap.mapSize.x / 2 + 0.5f + x, 0, -currentMap.mapSize.y / 2 + 0.5f + y) * tileSize;
}
public Coord GetRandomCoord()
{
Coord randomCoord = shuffledTileCoords.Dequeue();
shuffledTileCoords.Enqueue(randomCoord);
return randomCoord;
}
[System.Serializable]
public struct Coord
{
public int x;
public int y;
public Coord(int _x, int _y)
{
x = _x;
y = _y;
}
public static bool operator ==(Coord c1, Coord c2)
{
return c1.x == c2.x && c1.y == c2.y;
}
public static bool operator !=(Coord c1, Coord c2)
{
return !(c1 == c2);
}
}
[System.Serializable]
public class Map
{
public Coord mapSize;
[Range(0, 1)]
public float obstaclePercent;
public int seed;
public float minObstacleHeight;
public float maxObstacleHeight;
public Color foregroundColour;
public Color backgroundColour;
public Coord mapCentre
{
get
{
return new Coord(mapSize.x / 2, mapSize.y / 2);
}
}
}
}
코드는 굳이 안읽어봤고 이런 건 처음껀 잘 나오는데 두 번째꺼로 해서 안나왔으면 당연히 알고리즘에서 문제가 있음. 본인이 찾아서 해결하면 됨... 천천히 종이에다가 적어 봐 코드는 거짓말 안한다
한 줄씩 로직을 생각해보면서 다시 봐 보셈
잘못된 질문의 예
디 버 깅 ㄱ