Exemplo n.º 1
0
    private void PointSourceGenerator(Transform mapHolder, bool[,] obstacleMap)
    {
        //shuffled accessibleTileCoords
        accessibleTileCoords = new Queue <Coord> (FisherYatesShuffle.ShuffleArray(accessibleTileCoords.ToArray(), seed));
        for (int i = 0; i < pointSourceNumber; i++)
        {
            Coord randomAccessibleTile = accessibleTileCoords.Dequeue();
            if (obstacleMap[randomAccessibleTile.x, randomAccessibleTile.y])
            {
                i--;
                continue;
            }
            Vector3 pointSourcePosition = CoordToPosition(randomAccessibleTile.x, randomAccessibleTile.y);


            Transform newPoint = Instantiate(pointSourcePrefab, pointSourcePosition + new Vector3(0, .5f, 0), Quaternion.identity);
            newPoint.parent = mapHolder;
        }
    }
Exemplo n.º 2
0
    public void GenerateMap()
    {
        allTileCoords = new List <Coord> ();
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                allTileCoords.Add(new Coord(x, y));
            }
        }
        shuffledTileCoords = new Queue <Coord> (FisherYatesShuffle.ShuffleArray(allTileCoords.ToArray(), seed));
        mapCentre          = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);

        string holderName = "Generated Map";

        if (transform.Find(holderName))
        {
            DestroyImmediate(transform.Find(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;

        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3   tilePosition = CoordToPosition(x, y);
                Transform newTile      = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90));
                newTile.localScale = Vector3.one * (1 - outlinePercent);
                newTile.parent     = mapHolder;
            }
        }

        bool[,] obstacleMap = new bool[(int)mapSize.x, (int)mapSize.y];

        int obstacleCount        = (int)(mapSize.x * mapSize.y * obstaclePercent);
        int currentObstacleCount = 0;

        for (int i = 0; i < obstacleCount; i++)
        {
            Coord randomCoord = GetRandomCoord();
            obstacleMap[randomCoord.x, randomCoord.y] = true;
            currentObstacleCount++;

            if (randomCoord != mapCentre && MapIsFullyAccessible(obstacleMap, currentObstacleCount))
            {
                Vector3 obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);

                Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition
                                                    + new Vector3(0, obstaclePrefab.localScale.x * .5f, 0), Quaternion.identity);
                newObstacle.localScale += new Vector3(-0.15f, Random.Range(3, 8), -0.15f);
                newObstacle.parent      = mapHolder;
            }
            else
            {
                obstacleMap[randomCoord.x, randomCoord.y] = false;
                currentObstacleCount--;
            }
        }

        PointSourceGenerator(mapHolder, obstacleMap);
    }
Exemplo n.º 3
0
    public void GenerateMap()
    {
        if (isRandomSeed)
        {
            seed = Random.Range(1, 100);
        }

        // 맵 크기에 해당하는 모든 타일 좌표를 저장.
        allTileCoords = new List <Coord>();
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                allTileCoords.Add(new Coord(x, y));
            }
        }

        // FisherYatesShuffle 알고리즘으로 모든 타일 좌표를 섞는다.
        shuffledTileCoords = new Queue <Coord>(FisherYatesShuffle.ShuffleArray(allTileCoords.ToArray(), seed));

        // 맵의 가운데를 찾는다.
        mapCentre = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);

        // 자동생성된 맵이 가지게될 부모 Object 체크
        string holderName = "Generated Map";

        if (transform.Find(holderName))
        {
            DestroyImmediate(transform.Find(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;
        mapHolder.transform.localPosition = Vector3.zero;

        if (AllNodeList == null)
        {
            AllNodeList = new List <Node>();
        }
        else
        {
            AllNodeList.Clear();
        }

        // 맵 사이즈 만큼 반복하면서 타일을 만든다.
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3    tilePosition = CoordToPosition(x, y);
                GameObject newTile      = Instantiate(tilePrefab, mapHolder);
                newTile.name = string.Format("Tile_{0}_{1}", x, y);
                newTile.transform.localPosition = tilePosition;
                newTile.transform.localScale    = Vector3.one * (1 - outlinePercent);
                newTile.transform.parent        = mapHolder;
                Node newNode = newTile.GetComponent <Node>();
                newNode.X        = x;
                newNode.Y        = y;
                newNode.TileType = NodeType.None;

                AllNodeList.Add(newNode);
            }
        }

        // 장애물 여부를 저장할 2차원 배열을 맵크기만 큼 생성
        bool[,] obstacleMap = new bool[(int)mapSize.x, (int)mapSize.y];

        // 장애물 개수를 계산
        int obstacleCount = (int)(mapSize.x * mapSize.y * obstaclePercent);
        // 현재까지 만들어진 장애물의 개수
        int currentObstacleCount = 0;

        for (int i = 0; i < obstacleCount; i++)
        {
            // 셔플된 좌표중에서 하나를 가져온다.
            Coord randomCoord = GetRandomCoord();

            // 장애물 여부를 true 처리함.
            obstacleMap[randomCoord.x, randomCoord.y] = true;
            currentObstacleCount++;

            // 장애물의 좌표가 맵의 중앙이 아니고, 접근가능한 위치에 놓여져 있다면
            if (randomCoord != mapCentre && MapIsFullyAccessible(obstacleMap, currentObstacleCount))
            {
                Vector3 obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);
                // 장애물을 생성한다.
                GameObject newObstacle = Instantiate(obstaclePrefab, mapHolder);
                newObstacle.transform.localPosition = obstaclePosition;
                newObstacle.transform.parent        = mapHolder;

                AllNodeList[((int)mapSize.x * randomCoord.x) + randomCoord.y].TileType = NodeType.Obstacle;
            }
            else
            {
                // 장애물 여부를 false 처리함.
                obstacleMap[randomCoord.x, randomCoord.y] = false;
                currentObstacleCount--;
            }
        }
    }
Exemplo n.º 4
0
    public void GenerateMap()
    {
        if (seed == 10)
        {
            isRandomSeed = true;
        }

        if (isRandomSeed)
        {
            seed = Random.Range(1, 100);
        }

        allTileCoords = new List <Coord>();
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                allTileCoords.Add(new Coord(x, y));
            }
        }
        shuffledTileCoords = new Queue <Coord>(FisherYatesShuffle.ShuffleArray(allTileCoords.ToArray(), seed));
        mapCentre          = new Coord((int)mapSize.x / 2, (int)mapSize.y / 2);


        string holderName = "Generated Map";

        if (transform.Find(holderName))
        {
            DestroyImmediate(transform.Find(holderName).gameObject);
        }

        Transform mapHolder = new GameObject(holderName).transform;

        mapHolder.parent = transform;
        mapHolder.transform.localPosition = new Vector3(-4.2f, 0, 0);

        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3    tilePosition = CoordToPosition(x, y);
                GameObject newTile      = Instantiate(tilePrefab, mapHolder);
                newTile.name = string.Format("Tile_{0}_{1}", x, y);
                newTile.transform.localPosition = tilePosition;
                newTile.transform.localScale    = Vector3.one * (1 - outlinePercent);
                newTile.transform.parent        = mapHolder;
            }
        }

        bool[,] obstacleMap = new bool[(int)mapSize.x, (int)mapSize.y];

        int obstacleCount        = (int)(mapSize.x * mapSize.y * obstaclePercent);
        int currentObstacleCount = 0;

        for (int i = 0; i < obstacleCount; i++)
        {
            Coord randomCoord = GetRandomCoord();
            obstacleMap[randomCoord.x, randomCoord.y] = true;
            currentObstacleCount++;
            if (randomCoord != mapCentre && MapIsFullyAccessible(obstacleMap, currentObstacleCount))
            {
                Vector3 obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);

                GameObject newObstacle = Instantiate(obstaclePrefab, mapHolder);
                newObstacle.transform.localPosition = obstaclePosition;
                newObstacle.transform.parent        = mapHolder;
            }
            else
            {
                obstacleMap[randomCoord.x, randomCoord.y] = false;
                currentObstacleCount--;
            }
        }
    }