using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace MonoGame.Extended.Tilemaps
{
///
/// Represents a closed polygonal object in a tilemap.
///
public class TilemapPolygonObject : TilemapObject
{
///
/// Gets or sets the vertices of the polygon relative to the object's position.
///
public Vector2[] Points { get; set; }
///
/// Gets the vertices of the polygon in world coordinates.
///
public IEnumerable WorldPoints
{
get
{
foreach (var point in Points)
{
yield return Position + point;
}
}
}
///
public override RectangleF Bounds
{
get
{
if (Points == null || Points.Length == 0)
return new RectangleF(Position.X, Position.Y, 0, 0);
// Calculate bounding box from all world points
float minX = float.MaxValue;
float minY = float.MaxValue;
float maxX = float.MinValue;
float maxY = float.MinValue;
foreach (var worldPoint in WorldPoints)
{
minX = Math.Min(minX, worldPoint.X);
minY = Math.Min(minY, worldPoint.Y);
maxX = Math.Max(maxX, worldPoint.X);
maxY = Math.Max(maxY, worldPoint.Y);
}
return new RectangleF(minX, minY, maxX - minX, maxY - minY);
}
}
// NOTE: Will use BoundingPolygon2D
///
/// Initializes a new instance of the class.
///
/// The unique identifier for the object.
/// The position of the object (origin point).
/// The vertices of the polygon relative to the position.
public TilemapPolygonObject(int id, Vector2 position, Vector2[] points) : base(id, position)
{
Points = points;
}
}
}