예제 #1
0
        public static void SaveMapToJson(this MapGrid mapGrid, string fileName)
        {
            Deployable[] walkablTtiles    = mapGrid.GetAllChildren(Deployable.DeployLayer._WalkableLayer);
            Deployable[] buildableTtiles  = mapGrid.GetAllChildren(Deployable.DeployLayer._BuildableLayer);
            Deployable[] gameObjectTtiles = mapGrid.GetAllChildren(Deployable.DeployLayer._GameObjectLayer);

            List <Deployable> monsterStartList = new List <Deployable> ();
            List <Deployable> playerStartList  = new List <Deployable> ();

            foreach (Deployable tile in gameObjectTtiles)
            {
                if (tile.GetDeployableType() == Deployable.DeployableType._Monster)
                {
                    monsterStartList.Add(tile);
                }
                else if (tile.GetDeployableType() == Deployable.DeployableType._Player)
                {
                    playerStartList.Add(tile);
                }
            }

            string json = "{";

            // pathfinding data
            json += "\"map\":[";
            for (int i = 0; i < mapGrid.Rows; i++)
            {
                if (i != 0)
                {
                    json += ",";
                }
                json += "[";
                for (int j = 0; j < mapGrid.Columns; j++)
                {
                    if (j != 0)
                    {
                        json += ",";
                    }
                    string result = mapGrid.WalkableCells [mapGrid.CalculateIndex(i, j)].IsEmpty ? "0" : "1";
                    json += result;
                }
                json += "]";
            }
            json += "]";

            // walkable point
            json += ",\"walkable\":[";
            for (int i = 0; i < walkablTtiles.Length; i++)
            {
                if (i != 0)
                {
                    json += ",";
                }
                json += "[";
                json += walkablTtiles [i].GridIndex.X;
                json += ",";
                json += walkablTtiles [i].GridIndex.Y;
                json += "]";
            }
            json += "]";

            // buildable point
            json += ",\"buildable\":[";
            for (int i = 0; i < buildableTtiles.Length; i++)
            {
                if (i != 0)
                {
                    json += ",";
                }
                json += "[";
                json += buildableTtiles [i].GridIndex.X;
                json += ",";
                json += buildableTtiles [i].GridIndex.Y;
                json += "]";
            }
            json += "]";

            // monster start point
            json += ",\"monster\":[";
            for (int i = 0; i < monsterStartList.Count; i++)
            {
                if (i != 0)
                {
                    json += ",";
                }
                json += "[";
                json += monsterStartList [i].GridIndex.X;
                json += ",";
                json += monsterStartList [i].GridIndex.Y;
                json += "]";
            }
            json += "]";

            // player start point
            json += ",\"player\":[";
            for (int i = 0; i < playerStartList.Count; i++)
            {
                if (i != 0)
                {
                    json += ",";
                }
                json += "[";
                json += playerStartList [i].GridIndex.X;
                json += ",";
                json += playerStartList [i].GridIndex.Y;
                json += "]";
            }
            json += "]";

            json += "}";

            String path = MapEditorMain.Instance.FileDirectory + fileName + ".json";

            using (StreamWriter sw = new StreamWriter(path, false))
            {
                sw.Write(json);
            }
            Debug.Log("Map Saved:" + path);
        }