Ejemplo n.º 1
0
        // Primary constructor, inserts all necessary data from the JSON file.
        public Map(Rectangle mapSize, Layer[] layers, string orientation, string renderOrder, Rectangle tileSize, Tileset[] tilesets)
        {
            Height      = mapSize.Height;
            Layers      = layers;
            Orientation = orientation;
            RenderOrder = renderOrder;
            TileHeight  = tileSize.Height;
            Tilesets    = tilesets;
            TileWidth   = tileSize.Width;
            Width       = mapSize.Width;

            CentreOffset = new IsoVector(0, 0);
            ScaleFactor  = 1;
        }
Ejemplo n.º 2
0
        public static IsoVector ToCartesian(IsoVector v_in)
        {
            if (!v_in.IsCartesian)
            {
                int vx = (2 * v_in.Y + v_in.X) / 2;
                int vy = (2 * v_in.Y - v_in.X) / 2;

                return(new IsoVector(vx, vy, true));
            }
            else
            {
                return(v_in);
            }
        }
Ejemplo n.º 3
0
        // Default constructor, which calls the Layer and Tileset default constructors.
        public Map()
        {
            Height      = 1;
            Layers      = new Layer[] { new Layer() };
            Orientation = "isometric";
            RenderOrder = "right-down";
            TileHeight  = 1;
            Tilesets    = new Tileset[] { new Tileset() };
            TileWidth   = 1;
            Width       = 1;

            CentreOffset = new IsoVector(0, 0);
            ScaleFactor  = 1;
        }
Ejemplo n.º 4
0
        public static IsoVector ToIsometric(IsoVector v_in)
        {
            if (v_in.IsCartesian)
            {
                int vx = v_in.X - v_in.Y;
                int vy = (v_in.X + v_in.Y) / 2;

                return(new IsoVector(vx, vy, false));
            }
            else
            {
                return(v_in);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Get a particular tile's sprite and position from map data.
        /// </summary>
        /// <param name="layer">The map layer to lookup.</param>
        /// <param name="position">The x and y coordinates to lookup.</param>
        /// <returns>A tile object including the name of the tileset, a rectangle of the sprite within the tileset, and a rectangle of the tile's draw location on screen.</returns>
        public Tile GetTile(int layer, IsoVector position)
        {
            // Create local position vector, ensuring it is in cartesian coordinates.
            IsoVector pos = Coordinates.ToCartesian(position);

            // If the position vector is out of bounds, raise an error and output default value.
            if (pos.X < 0 || pos.X >= Width || pos.Y < 0 || pos.Y >= Height)
            {
                Console.Error.WriteLine("Map.GetTile() - Position vector out of bounds.");
                return(new Tile());
            }

            // If the layer is out of bounds, raise an error and output default value.
            if (layer < 0 || layer >= Layers.Length)
            {
                Console.Error.WriteLine("Map.GetTile() - Layer out of bounds.");
                return(new Tile());
            }

            // Get the GID at the specified layer and location.
            int gid = Layers[layer].Data[pos.X + pos.Y * Width];

            // If the GID is 0, the tile should be left empty. Return an empty tile.
            if (gid == 0)
            {
                return(new Tile());
            }

            // Find the tileset the GID corresponds to.
            int targetTileset = -1;

            for (int i = 0; i < Tilesets.Length; i++)
            {
                // If the GID is within the bounds of the tileset, it is part of the tileset.
                if (gid <= Tilesets[i].FirstGID + Tilesets[i].TileCount - 1)
                {
                    targetTileset = i;
                }
            }

            // If the GID wasn't in any tileset, there is a problem with the layer data. Raise an error and output default value.
            if (targetTileset == -1)
            {
                Console.Error.WriteLine("Map.GetTile() - Layer " + Layers[layer].Name + " data has invalid GID at " + pos.X + ":" + pos.Y + ", " + gid);
                return(new Tile());
            }

            Tileset ts = Tilesets[targetTileset];
            // Find the local tile id, by subtracting the first global id. For the first tileset, this is 1, while also aligns the local id back to 0!
            int localID = gid - ts.FirstGID;
            int areaX   = localID % ts.Columns;
            int areaY   = (localID - areaX) / ts.Columns;

            // Create a rectangle with the location and size of the tile within the tileset.
            Rectangle areaRect = new Rectangle(
                areaX * ts.TileWidth + areaX * ts.Spacing + ts.Margin,
                areaY * ts.TileHeight + areaY * ts.Spacing + ts.Margin,
                ts.TileWidth, ts.TileHeight);

            // Create a rectangle with the position of the tile for rendering on-screen.
            Rectangle posRect;

            // For orthogonal maps,
            if (Orientation == "orthogonal")
            {
                posRect = new Rectangle(
                    position.X * TileWidth * ScaleFactor + CentreOffset.X,
                    position.Y * TileHeight * ScaleFactor + CentreOffset.Y,
                    areaRect.Width * ScaleFactor, areaRect.Height * ScaleFactor);
            }
            // If the orientation is isometric, convert the position vector.
            else if (Orientation == "isometric")
            {
                posRect = new Rectangle(
                    (position.X - position.Y) * TileWidth / 2 * ScaleFactor + CentreOffset.X,
                    (position.X + position.Y) * TileHeight / 2 * ScaleFactor + CentreOffset.Y,
                    areaRect.Width * ScaleFactor, areaRect.Height * ScaleFactor);
            }
            // Otherwise, the orientation is not supported. Raise an error and output default value.
            else
            {
                Console.Error.WriteLine("Map.GetTile() - Map has unsupported orientation " + Orientation);
                return(new Tile());
            }



            // Return the tileset, location and area as a tile.
            return(new Tile(ts.Name, areaRect, posRect));
        }