コード例 #1
0
        public Layer(XmlNode node)
        {
            name = node.Attributes["name"].Value;
            width = int.Parse (node.Attributes ["width"].Value);
            height = int.Parse (node.Attributes ["height"].Value);

            XmlNode data = node.SelectSingleNode("data");
            XmlNodeList tiles = data.SelectNodes("tile");

            layerTiles = new List<List<LayerTile>>();

            int tileIndex = 0;
            for (int y = 0; y < height; y++) {

                layerTiles.Add(new List<LayerTile>());

                for (int x = 0; x < width; x++) {
                    XmlNode tile = tiles[tileIndex];
                    tileIndex++;

                    uint gid = uint.Parse (tile.Attributes["gid"].Value);

                    bool flippedH = (gid & Horizontal) == Horizontal;
                    bool flippedV = (gid & Vertical) == Vertical;
                    bool flippedD = (gid & Diagonal) == Diagonal;

                    uint gidNoFlags = gid;
                    gidNoFlags &= ~(Horizontal | Vertical | Diagonal);

                    LayerTile layerTile = new LayerTile((int)gidNoFlags, flippedD, flippedH, flippedV);

                    layerTiles[y].Add(layerTile);
                }
            }

            layerTiles.Reverse ();
        }
コード例 #2
0
        // Draws the tiles inside the Map object we set earlier
        void drawLayers()
        {
            int tileWidth  = map.tileWidth;
            int tileHeight = map.tileHeight;

            List <Layer> layers = map.layers;

            int layerHeight = 0;

            foreach (Layer layer in layers)
            {
                GameObject layerObject = new GameObject(layer.name);

                layerObject.transform.position = new Vector3(0, 0, layerHeight);
                layerObject.transform.parent   = mapObject.transform;

                for (int y = 0; y < layer.layerTiles.Count; y++)
                {
                    List <LayerTile> xTiles = layer.layerTiles[y];
                    for (int x = 0; x < xTiles.Count; x++)
                    {
                        LayerTile layerTile = xTiles[x];
                        int       gid       = layerTile.gid;

                        if (gid == 0)
                        {
                            continue;
                        }

                        // Gets the right tileset and texture for this gid
                        Tileset   tileset = map.GetTileset(gid);
                        Texture2D texture = tileset.texture;
                        if (tileset == null || texture == null)
                        {
                            continue;
                        }

                        Tile tile = tileset.tiles[gid];
                        Rect rect = tile.area;

                        Sprite     sprite     = Sprite.Create(texture, rect, new Vector2(0.5f, 0.5f), scale);
                        GameObject tileObject = new GameObject("tile-" + x + "-" + y);
                        tileObject.transform.parent = layerObject.transform;

                        float xCoordinate = (float)((x * tileWidth) / scale);
                        float yCoordinate = (float)((y * tileHeight) / scale);

                        int xScale = 1;
                        if (layerTile.flippedH)
                        {
                            xScale       = -1;
                            xCoordinate += (float)(tileWidth / scale);
                        }

                        int yScale = 1;
                        if (layerTile.flippedV)
                        {
                            yScale       = -1;
                            yCoordinate += (float)(tileHeight / scale);
                        }

                        if (layerTile.flippedD)
                        {
                            tileObject.transform.Rotate(new Vector3(0, 0, -90));
                            yCoordinate -= (float)(tileHeight / scale);
                        }

                        tileObject.transform.localScale = new Vector3(xScale, yScale, 0);
                        tileObject.transform.position   = new Vector3(xCoordinate, yCoordinate, layerHeight);

                        SpriteRenderer renderer = tileObject.AddComponent <SpriteRenderer>();
                        renderer.sprite           = sprite;
                        renderer.sortingLayerName = layer.name;
                        renderer.material         = pixelSnapMaterial;

                        if (tile.collides)
                        {
                            tileObject.AddComponent <BoxCollider2D>().size = new Vector2(1, 1);
                            //tileObject.GetComponent<BoxCollider2D>().offset = new Vector2(0, 0);
                        }
                    }
                }
                layerHeight -= 5;
            }
        }