Example #1
0
    public void Init()
    {
        string mapPath = "square";

        Debug.Log ("TileMapTest : Start");

        TextAsset dataAsset = (TextAsset)Resources.Load (mapPath, typeof(TextAsset));

        if (!dataAsset)
            Debug.Log ("TileMapTest: Couldn't load the map data from: " + mapPath);

        Dictionary<string, object> dict = dataAsset.text.dictionaryFromJson ();

        int count = dict.Count;
        Debug.Log ("Item count = " + dict.Count);

        if (count > 0) {

            // Map Metadata
            mapWidth = GetInteger (dict, "width");
            mapHeight = GetInteger (dict, "height");
            tileSize = GetInteger (dict, "tilewidth");

            // 1st tileset
            tilesets = GetList (dict, "tilesets");
            string elementPath = "";
            int tileIdStart = 0;
            int margin = 0;
            int spacing = 0;

            if (tilesets.Count > 0) {
                tileset = (Dictionary<string, object>)tilesets [0];
                elementPath = GetString (tileset, "image");
                tileIdStart = GetInteger (tileset, "firstgid");
                margin = GetInteger(tileset, "margin");
                spacing = GetInteger(tileset, "spacing");
                Debug.Log("1st tileset, margin = " + margin + ", spacing = " + spacing);
            }

            // Load Layers
            layers = GetList (dict, "layers");
            tdMapLayers = new TDMapLayer[layers.Count];
            for (int i=0; i < layers.Count; i++)
            {

                Dictionary<string, object> ld = (Dictionary<string, object>)layers [i];

                int layerWidth = GetInteger (ld, "width");
                int layerHeight = GetInteger (ld, "height");
                int xOffset = GetInteger (ld, "x");
                int yOffset = GetInteger (ld, "y");
                TDMapLayer newLayer = new TDMapLayer (layerWidth, layerHeight, xOffset, yOffset, tileSize);
                tdMapLayers [i] = newLayer;
                string layerName = GetString (ld, "name");

                if (layerName.Equals ("Object"))
                {
                    List<object> layerObjects = GetList (ld, "objects");
                    //				LoadMapObjects (objectList);
                }
                else
                {
                    List<object> layerData = GetList (ld, "data");
                    if (layerData != null && layerData.Count > 0)
                    {
                        string [] pathSplit = elementPath.Split (new System.Char [] {'/'});
                        string _tilesetElementName = pathSplit [pathSplit.Length - 1];
                        newLayer.LoadTiles (layerData.ToArray(), tileIdStart, _tilesetElementName, margin, spacing);
                    }
                }

                _allLayersByName.Add (layerName, newLayer);

                AddChild(newLayer);
            }

            Debug.Log ("layer count  = " + tdMapLayers.Length);

            Debug.Log ("mapWidth: " + mapWidth);
            Debug.Log ("mapHeight: " + mapHeight);
            Debug.Log ("tileSize: " + tileSize);
            Debug.Log ("tileIdStart: " + tileIdStart);
            Debug.Log ("elementPath: " + elementPath);

        }
    }
Example #2
0
    public TDMap(string mapPath)
    {
        TextAsset dataAsset = (TextAsset) Resources.Load (mapPath, typeof(TextAsset));

        if(!dataAsset)
            Debug.Log ("TDMap: Couldn't load the map data from: " + mapPath);

        Dictionary<string, object> dict = dataAsset.text.dictionaryFromJson();

        Hashtable hash = new Hashtable(dict);//dataAsset.text.hashtableFromJson();

        // Map Metadata
        _mapWidth = int.Parse(hash["width"].ToString());
        _mapHeight = int.Parse(hash["height"].ToString());
        _tileSize = int.Parse(hash["tilewidth"].ToString());

        Debug.Log(hash["tilesets"].GetType().ToString());
        ArrayList tilesetsList = (ArrayList)hash["tilesets"];
        Hashtable tileset = (Hashtable)tilesetsList[0];
        string elementPath = tileset["image"].ToString();
        string [] pathSplit = elementPath.Split(new Char [] {'/'});
        _tilesetElementName = pathSplit[pathSplit.Length-1];
        _tileIdStart = int.Parse (tileset["firstgid"].ToString());

        Debug.Log ("mapWidth: "+_mapWidth);
        Debug.Log ("mapHeight: "+_mapHeight);
        Debug.Log ("tileSize: "+_tileSize);
        Debug.Log ("tileIdStart: "+_tileIdStart);
        Debug.Log ("tilesetElementName: "+_tilesetElementName);

        // Load Layers
        ArrayList layersList = (ArrayList)hash["layers"];
        _layers = new TDMapLayer[layersList.Count];
        for (int i=0; i < layersList.Count; i++)
        {
            Hashtable layerHash = (Hashtable)layersList[i];
            int layerWidth = int.Parse (layerHash["width"].ToString());
            int layerHeight = int.Parse (layerHash["height"].ToString());
            int xOffset = int.Parse (layerHash["x"].ToString());
            int yOffset = int.Parse (layerHash["y"].ToString());
            TDMapLayer newLayer = new TDMapLayer (layerWidth, layerHeight, xOffset, yOffset, _tileSize);
            _layers[i] = newLayer;

            if (layerHash["name"].ToString().Equals("Objects"))
            {
                // Load object data if it exists...
                ArrayList objectList = (ArrayList)layerHash["objects"];
                LoadMapObjects (objectList);
            }
            else
            {
                // Load tile data if it exists...
                if (layerHash["data"] != null)
                {
                    ArrayList layerData = (ArrayList)layerHash["data"];
                    newLayer.LoadTiles (layerData.ToArray(), _tileIdStart, _tilesetElementName);
                }
                _allLayersByName.Add (layerHash["name"].ToString(), newLayer);
                AddChild(newLayer);
            }
        }
        _floorLayer = LayerWithName("Floor");
    }