/// <summary>
 /// Default constructor requires a name, the bounds of the object, the orientation of projection, and a collection of properties assigned to the object
 /// </summary>
 /// <param name="name"></param>
 /// <param name="bounds"></param>
 /// <param name="orientation"></param>
 /// <param name="properties"></param>
 public MapObject(string name, Rectangle bounds, Orientation orientation, PropertyCollection properties)
 {
     Name = name;
     Bounds = bounds;
     Orientation = orientation;
     Properties = properties;
 }
        public ObjectContent(XmlNode node)
        {
            if (node.Attributes[AttributeNames.MapObjectAttributes.Name] != null)
                Name = node.Attributes[AttributeNames.MapObjectAttributes.Name].Value;

            if (node.Attributes[AttributeNames.MapObjectAttributes.Type] != null)
                Type = node.Attributes[AttributeNames.MapObjectAttributes.Type].Value;

            if (node[AttributeNames.MapObjectAttributes.Properties] != null)
                properties = new PropertyCollection(node[AttributeNames.MapObjectAttributes.Properties]);

            int x = 0, y = 0, width = 0, height = 0;

            if (node.Attributes[AttributeNames.MapObjectAttributes.X] != null)
                x = Utilities.TryToParseInt(node.Attributes[AttributeNames.MapObjectAttributes.X].Value);

            if (node.Attributes[AttributeNames.MapObjectAttributes.Y] != null)
                y = Utilities.TryToParseInt(node.Attributes[AttributeNames.MapObjectAttributes.Y].Value);

            if (node.Attributes[AttributeNames.MapObjectAttributes.Width] != null)
                width = Utilities.TryToParseInt(node.Attributes[AttributeNames.MapObjectAttributes.Width].Value);

            if (node.Attributes[AttributeNames.MapObjectAttributes.Height] != null)
                height = Utilities.TryToParseInt(node.Attributes[AttributeNames.MapObjectAttributes.Height].Value);

            Bounds = new Rectangle(x, y, width, height);

            string objectPoints = String.Empty;

            if (node.Attributes[AttributeNames.MapObjectAttributes.GID] != null)
            {
                objectType = MapObjectType.Tile;
                GID = Utilities.TryToParseInt(node.Attributes[AttributeNames.MapObjectAttributes.GID].Value);
            }
            else if (node.Attributes[AttributeNames.MapObjectAttributes.Polygon] != null)
            {
                objectType = MapObjectType.Polygon;
                objectPoints = node.Attributes[AttributeNames.MapObjectAttributes.Polygon].Value;
            }
            else if (node.Attributes[AttributeNames.MapObjectAttributes.Polyline] != null)
            {
                objectType = MapObjectType.Polyline;
                objectPoints = node.Attributes[AttributeNames.MapObjectAttributes.Polyline].Value;
            }

            if(!String.IsNullOrEmpty(objectPoints))
            {
                string[] splitPoints = objectPoints.Split(' ');
                foreach(string splitPoint in splitPoints)
                {
                    string[] coordinates = splitPoint.Split(',');

                    float coordinateX = 0f, coordinateY = 0f;
                    float.TryParse(coordinates[0], NumberStyles.None, CultureInfo.InvariantCulture, out coordinateX);
                    float.TryParse(coordinates[1], NumberStyles.None, CultureInfo.InvariantCulture, out coordinateY);

                    points.Add(new Point((int)x, (int)y));
                }
            }
        }
Exemple #3
0
 /// <summary>
 /// Main constructor used to instantiate a tile when data is known at import
 /// </summary>
 /// <param name="texture"></param>
 /// <param name="source"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 public Tile(Texture texture, Rectangle source, int width, int height)
 {
     ID = Guid.NewGuid();
     IsEmpty = false;
     Texture = texture;
     SourceTextureBounds = source;
     Width = width;
     Height = height;
 }
 public TileContent(Rectangle source, PropertyCollection properties)
 {
     SourceTextureBounds = source;
     this.properties = properties;
 }
        internal void RenderTexture(IntPtr textureHandle, float positionX, float positionY, Rectangle source)
        {
            Debug.Assert(textureHandle != IntPtr.Zero, Errors.E_TEXTURE_NULL);

            int width = source.Width;
            int height = source.Height;

            // SDL only accepts integer positions (x,y) in the rendering Rect
            SDL.SDL_Rect destinationRectangle = new SDL.SDL_Rect() { x = (int)positionX, y = (int)positionY, w = width, h = height };
            SDL.SDL_Rect sourceRectangle = new SDL.SDL_Rect() { x = source.X, y = source.Y, w = width, h = height };

            int result = SDL.SDL_RenderCopy(Handle, textureHandle, ref sourceRectangle, ref destinationRectangle);
            if (Utilities.IsError(result))
            {
                throw new Exception(Utilities.GetErrorMessage("SDL_RenderCopy"));
            }
        }
 internal void RenderTexture(IntPtr textureHandle, float positionX, float positionY, int sourceWidth, int sourceHeight)
 {
     Rectangle source = new Rectangle(0, 0, sourceWidth, sourceHeight);
     RenderTexture(textureHandle, positionX, positionY, source);
 }
        /// <summary>
        /// Based on a passed tile index, create a Tile by looking up which TileSet it belongs to, assign the proper TilSet texture,
        /// and find the bounds of the rectangle that encompasses the correct tile texture within the total tileset texture.
        /// </summary>
        /// <param name="tileIndex">Index of the tile (GID) within the map file</param>
        /// <param name="tileSets">Enumerable list of tilesets used to find out which tileset a tile belongs to</param>
        /// <param name="tileLayerType"></param>
        /// <returns></returns>
        private Tile CreateTile(int tileIndex, IEnumerable<TileSetContent> tileSets)
        {
            if (tileSets == null) throw new ArgumentNullException("tileSets");

            Tile tile = new Tile();

            // we don't want to look up tiles with ID 0 in tile sets because Tiled Map Editor treats ID 0 as an empty tile
            if (tileIndex > Tile.EmptyTileID)
            {
                Texture tileSetTexture = null;
                Rectangle source = new Rectangle();

                foreach (TileSetContent tileSet in tileSets)
                {
                    if (tileIndex - tileSet.FirstGID < tileSet.Tiles.Count)
                    {
                        tileSetTexture = tileSet.Texture;
                        source = tileSet.Tiles[tileIndex - tileSet.FirstGID].SourceTextureBounds;
                        break;
                    }
                }

                tile = new Tile(tileSetTexture, source, TileWidth, TileHeight);
            }

            return tile;
        }
Exemple #8
0
 public void Draw(float x, float y, Rectangle sourceBounds)
 {
     Draw((int)x, (int)y, sourceBounds);
 }
Exemple #9
0
        public void Draw(int x, int y, Rectangle sourceBounds)
        {
            Assert.IsNotNull(Handle, Errors.E_TEXTURE_NULL);
            Assert.IsNotNull(renderer, Errors.E_RENDERER_NULL);

            renderer.RenderTexture(Handle, x, y, sourceBounds);
        }
 /// <summary>
 /// Determines if two rectangles intersect.
 /// </summary>
 /// <param name="rectangle">Rectangle.</param>
 public bool Intersects(Rectangle rectangle)
 {
     return rectangle.Left <= Right && Left <= rectangle.Right && rectangle.Top <= Bottom && Top <= rectangle.Bottom;
 }
        public Vector GetIntersectionDepth(Rectangle rectangle)
        {
            // Calculate half sizes.
            float halfWidthA = this.Width / 2.0f;
            float halfHeightA = this.Height / 2.0f;
            float halfWidthB = rectangle.Width / 2.0f;
            float halfHeightB = rectangle.Height / 2.0f;

            // Calculate centers.
            Vector centerA = new Vector(this.Left + halfWidthA, this.Top + halfHeightA);
            Vector centerB = new Vector(rectangle.Left + halfWidthB, rectangle.Top + halfHeightB);

            // Calculate current and minimum-non-intersecting distances between centers.
            float distanceX = centerA.X - centerB.X;
            float distanceY = centerA.Y - centerB.Y;
            float minDistanceX = halfWidthA + halfWidthB;
            float minDistanceY = halfHeightA + halfHeightB;

            // If we are not intersecting at all, return (0, 0).
            if (Math.Abs(distanceX) >= minDistanceX || Math.Abs(distanceY) >= minDistanceY)
                return Vector.Zero;

            // Calculate and return intersection depths.
            float depthX = distanceX > 0 ? minDistanceX - distanceX : -minDistanceX - distanceX;
            float depthY = distanceY > 0 ? minDistanceY - distanceY : -minDistanceY - distanceY;
            return new Vector(depthX, depthY);
        }
 public bool Contains(Rectangle rectangle)
 {
     if (Left <= rectangle.Left && Right >= rectangle.Right && Top <= rectangle.Top && Bottom >= rectangle.Bottom)
         return true;
     else
         return false;
 }
        private void GenerateTileSourceRectangles()
        {
            // process the tilesets, calculate tiles to fit in each set, calculate source rectangles
            foreach (TileSetContent tileSet in tileSets)
            {
                int imageWidth = tileSet.Texture.Width;
                int imageHeight = tileSet.Texture.Height;

                imageWidth -= tileSet.Margin * 2;
                imageHeight -= tileSet.Margin * 2;

                int tileCountX = 0;
                while ((tileCountX + 1) * tileSet.TileWidth <= imageWidth)
                {
                    tileCountX++;
                    imageWidth -= tileSet.Spacing;
                }

                int tileCountY = 0;
                while ((tileCountY + 1) * tileSet.TileHeight <= imageHeight)
                {
                    tileCountY++;
                    imageHeight -= tileSet.Spacing;
                }

                for (int y = 0; y < tileCountY; y++)
                {
                    for (int x = 0; x < tileCountX; x++)
                    {
                        int rx = tileSet.Margin + x * (tileSet.TileWidth + tileSet.Spacing);
                        int ry = tileSet.Margin + y * (tileSet.TileHeight + tileSet.Spacing);
                        Rectangle source = new Rectangle(rx, ry, tileSet.TileWidth, tileSet.TileHeight);

                        int index = tileSet.FirstGID + (y * tileCountX + x);
                        PropertyCollection tileProperties = new PropertyCollection();
                        if (tileSet.TileProperties.ContainsKey(index))
                            tileProperties = tileSet.TileProperties[index];

                        TileContent tile = new TileContent(source, tileProperties);
                        tileSet.Tiles.Add(tile);
                    }
                }
            }
        }