Exemplo n.º 1
0
    void Start()
    {
        MapCreateType.InitMapCreateType(); // 맵 생성 타입 모델 데이터 로드
        CreateWorldData(1, 1);
        CreateMapTiles();

        LoadMapData(0, 0);

        if (_textDesc)
        {
            _textDesc.text = string.Format("MaxWorldSize : X : {0}, Y :{1}", MAX_WORLD_WIDTH, MAX_WORLD_HEIGHT);
        }

        Debug.Log("World create complete");
    }
Exemplo n.º 2
0
    MapData CreateMapData(Offset mapOffset, int mapWidth, int mapHeight, bool isRandomized)
    {
        TileData[,] tileArrData = new TileData[mapWidth, mapHeight];

        if (isRandomized)
        {
            InitMapSettingRandom();
        }

        // TODO (용훈): 맵 타입 얻는 코드, 주석 풀고 디버깅 돌려서 MapCreateType 클래스와 MAP_CREATE.json 내용 비교하며 감 잡을 것.
        //MapCreateType mapCreateType = MapCreateType.GetMapCreateTypeById(Random.Range(1,13));
        //Debug.Log(string.Format("TypeName : {0} TerrainSort : {1} LocateTerrain : {2}", mapCreateType.TypeName, mapCreateType.TerrainSort, mapCreateType.LocateTerrain));



        for (int i = 0; i < mapWidth; ++i)
        {
            for (int j = 0; j < mapHeight; ++j)
            {
                // Not setting terrain type at the instaitiation (might need to change constructor)
                tileArrData[i, j] = new TileData(new Offset(i, j), TileData.TERRAIN_TYPE.DEFAULT);
            }
        }

        // Now we have map data with random noise

        // For test only
        MapCreateType temp = new MapCreateType();

        temp.Id            = 1;
        temp.Divide        = _terrainNumbersMax;
        temp.LocateTerrain = _terrainNumbers;
        temp.TypeName      = "sand_01";
        temp.TerrainSort   = "sand";

        // Modify map for corresponding map type
        SetMapType(tileArrData, mapWidth, mapHeight, temp);
        SetTileType(tileArrData, mapWidth, mapHeight);

        MapData mapData = new MapData(mapOffset, mapWidth, mapHeight, tileArrData);


        return(mapData);
    }
Exemplo n.º 3
0
    public static void InitMapCreateType()
    {
        Dictionary <string, object> json = Util.LoadJSON("WorldScene/MAP_CREATE");

        Dictionary <string, object>[] MAP_CREATE = json["MAP_CREATE"] as Dictionary <string, object>[];
        int nLength = MAP_CREATE.Length;

        _arrMapCreateType = new MapCreateType[nLength];

        int nCount = 0;

        foreach (var typeData in MAP_CREATE)
        {
            _arrMapCreateType[nCount] = new MapCreateType();

            _arrMapCreateType[nCount].Id          = Convert.ToInt32(typeData["id"]);
            _arrMapCreateType[nCount].TypeName    = typeData["TYPE_NAME"].ToString();
            _arrMapCreateType[nCount].TerrainSort = typeData["TERRAIN_SORT"].ToString();
            _arrMapCreateType[nCount].Divide      = Convert.ToInt32(typeData["DIVIDE"]);

            // 이녀석은 입력 값이 "1,2,4,5" 와 같은 string으로 들어오기 때문에 ',' 단위로 잘라서 int 배열에 밀어넣는 고달픈 작업이 필요.
            string strLocateTerrain = typeData["LOCATE_TERRAIN"].ToString();
            string strTemp          = strLocateTerrain;

            //먼저 이게 총 몇 개가 필요한지 구해보자
            int i = 0;
            while (strTemp.Length >= 0)
            {
                int index = strTemp.IndexOf(",");
                if (index != -1)
                {
                    strTemp = strTemp.Substring(index + 1, strTemp.Length - index - 1);
                }
                else
                {
                    i++;
                    break;
                }

                if (i > 100) // 무한 루프 방지 예외처리
                {
                    break;
                }

                i++;
            }

            int[] arrData = new int[i];

            // 길이 구했으니까 해당 길이에 맞게 데이터를 밀어넣는다.
            i = 0;
            while (strLocateTerrain.Length >= 0)
            {
                int index = strLocateTerrain.IndexOf(",");
                if (index != -1)
                {
                    int.TryParse(strLocateTerrain.Substring(0, index), out arrData[i]);
                    strLocateTerrain = strLocateTerrain.Substring(index + 1, strLocateTerrain.Length - index - 1);
                }
                else
                {
                    // 마지막, 완전히 비어있는 경우는 뻑날것임.
                    int.TryParse(strLocateTerrain, out arrData[i]);
                    break;
                }

                if (i > 100) // 무한 루프 방지 예외처리
                {
                    break;
                }

                i++;
            }

            _arrMapCreateType[nCount].LocateTerrain = arrData;

            nCount++;
        }
    }
Exemplo n.º 4
0
    void SetMapType(TileData[,] tileArrData, float mapWidth, float mapHeight, MapCreateType mapCreateType)
    {
        int sideLength    = (int)Mathf.Sqrt(mapCreateType.Divide);
        int sectionWidth  = (int)(mapWidth / sideLength);
        int sectionHeight = (int)(mapHeight / sideLength);

        // sets the default noise (could be placed somewhere else..
        for (int index = 0; index < mapWidth; ++index)
        {
            for (int j = 0; j < mapHeight; ++j)
            {
                // Not setting terrain type at the instaitiation (might need to change constructor)
                tileArrData[index, j].Elevation = Mathf.PerlinNoise(_section + index * _noiseness, _section + j * _noiseness) - 0.3f;
            }
        }

        // set elevation for selected division
        for (int index = 0; index < mapCreateType.LocateTerrain.Length; index++)
        {
            // change to 0 based indexing
            int division = mapCreateType.LocateTerrain[index] - 1;

            int centerX, centerY;
            // 110% of width and height / 2 on each side
            int horizontalRange = (int)(sectionWidth * 1.5f);
            int verticalRange   = (int)(sectionHeight * 1.5f);
            // handle vertices first
            // left top
            if (division == 0)
            {
                centerX = 0;
                centerY = 0;
            }
            // right top
            else if (division == sideLength - 1)
            {
                centerX = (int)mapWidth;
                centerY = 0;
            }
            // left bottom
            else if (division == mapCreateType.Divide - sideLength)
            {
                centerX = 0;
                centerY = (int)mapHeight;
            }
            // right bottom
            else if (division == mapCreateType.Divide - 1)
            {
                centerX = (int)mapWidth;
                centerY = (int)mapHeight;
            }

            // edges
            // left edges
            else if (division % sideLength == 0)
            {
                centerX = 0;
                centerY = sectionHeight * (int)(division / sideLength) + sectionHeight / 2;
            }
            // top edges
            else if (division / sideLength == 0)
            {
                centerX = sectionWidth * (division % sideLength) + sectionWidth / 2;
                centerY = 0;
            }
            // right edges
            else if (division % sideLength == 2)
            {
                centerX = (int)mapWidth;
                centerY = sectionHeight * (int)(division / sideLength) + sectionHeight / 2;
            }
            // bottom edges
            else if (division / sideLength == sideLength - 1)
            {
                centerX = sectionWidth * (division % sideLength) + sectionWidth / 2;
                centerY = (int)mapHeight;
            }
            // others are centered at the center
            else
            {
                centerX = (int)mapWidth / 2;
                centerY = (int)mapHeight / 2;
            }

            // could be useful but can be taken away
            centerX += Random.Range(-sectionWidth / 4, sectionWidth / 4);
            centerY += Random.Range(-sectionHeight / 4, sectionHeight / 4);

            // increament elevation from centerX and centerY
            //float maxDist = Mathf.Sqrt(horizontalRange/2 * horizontalRange/2 + verticalRange/2 * verticalRange/2);
            float maxDist = horizontalRange > verticalRange ? horizontalRange : verticalRange;
            maxDist *= 0.7f;

            for (int i = centerX - horizontalRange; i < centerX + horizontalRange; i++)
            {
                if (i < 0 || i >= mapWidth)
                {
                    continue;
                }

                for (int j = centerY - verticalRange; j < centerY + verticalRange; j++)
                {
                    if (j < 0 || j >= mapHeight)
                    {
                        continue;
                    }

                    float dist = Mathf.Sqrt((centerX - i) * (centerX - i) + (centerY - j) * (centerY - j));
                    if (dist >= maxDist)
                    {
                        continue;
                    }
                    float elevationToAdd = Mathf.Log((1 - dist / maxDist) + 1) * 7;

                    tileArrData[i, j].Elevation += elevationToAdd;
                }
            }
        }
    }