Example #1
0
    //////////////////
    //Parser Methods//
    //////////////////

    void ParseMapData(JSONObject container)
    {
        //Handle Map Size
        //print("Handle Map Size");
        JSONObject obj = container.list[1];

        //Debug.Log(obj);

        mapHandler.SetMapSize((int)obj.list[0].n, (int)obj.list[1].n);
        mazeSize.text = "Maze size: " + (int)obj.list[0].n + "x" + (int)obj.list[1].n;

        //Handle Map Data
        //print("Handle Map Data");
        obj = container.list[0];

        for (int i = 0; i < obj.list.Count; i++)
        {
            JSONObject tile = obj.list[i];
            int        inX = 0, inY = 0;
            bool[]     inWalls = new bool[4];

            for (int j = 0; j < tile.list.Count; j++)
            {
                JSONObject value = tile.list[j];
                switch (tile.keys[j])                                   //Parse each tile based on the keys in the JSON object
                {
                case "x":
                    inX = (int)value.n;                                 //JSONObect defaults to float so must cast to int.
                    break;

                case "y":
                    inY = (int)value.n;
                    break;

                case "walls":
                    for (int k = 0; k < value.list.Count; k++)
                    {
                        inWalls[k] = value.list[k].b;
                    }
                    break;

                default:
                    Debug.LogWarning("Unknown key");
                    break;
                }
            }
            //Debug.Log("Adding tile: " + inX + ", " + inY);
            mapHandler.AddNewTile(inX, inY, inWalls);
        }

        //Handle Start tile
        //print("Handle Start Tile");
        obj = container.list[2];
        //Debug.Log(obj);

        mapHandler.SetStartTile((int)obj.list[0].n, (int)obj.list[1].n);

        //Handle End Tile
        //print("Handle End Tile");
        obj = container.list[3];
        //Debug.Log(obj);

        mapHandler.SetEndTile((int)obj.list[0].n, (int)obj.list[1].n);
    }