TilemapPolylineObject.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. namespace MonoGame.Extended.Tilemaps
  5. {
  6. /// <summary>
  7. /// Represents an open polyline (unclosed polygon) object in a tilemap.
  8. /// </summary>
  9. /// <remarks>
  10. /// Polyline objects define a series of connected line segments and are
  11. /// typically used for paths or boundaries.
  12. /// </remarks>
  13. public class TilemapPolylineObject : TilemapObject
  14. {
  15. /// <summary>
  16. /// Gets or sets the vertices of the polyline relative to the object's position.
  17. /// </summary>
  18. public Vector2[] Points { get; set; }
  19. /// <summary>
  20. /// Gets the vertices of the polyline in world coordinates.
  21. /// </summary>
  22. public IEnumerable<Vector2> WorldPoints
  23. {
  24. get
  25. {
  26. foreach (var point in Points)
  27. {
  28. yield return Position + point;
  29. }
  30. }
  31. }
  32. /// <inheritdoc/>
  33. public override RectangleF Bounds
  34. {
  35. get
  36. {
  37. if (Points == null || Points.Length == 0)
  38. return new RectangleF(Position.X, Position.Y, 0, 0);
  39. // Calculate bounding box from all world points
  40. float minX = float.MaxValue;
  41. float minY = float.MaxValue;
  42. float maxX = float.MinValue;
  43. float maxY = float.MinValue;
  44. foreach (var worldPoint in WorldPoints)
  45. {
  46. minX = Math.Min(minX, worldPoint.X);
  47. minY = Math.Min(minY, worldPoint.Y);
  48. maxX = Math.Max(maxX, worldPoint.X);
  49. maxY = Math.Max(maxY, worldPoint.Y);
  50. }
  51. return new RectangleF(minX, minY, maxX - minX, maxY - minY);
  52. }
  53. }
  54. // NOTE: Will use BoundingPolygon2D (unclosed)
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="TilemapPolylineObject"/> class.
  57. /// </summary>
  58. /// <param name="id">The unique identifier for the object.</param>
  59. /// <param name="position">The position of the object (origin point).</param>
  60. /// <param name="points">The vertices of the polyline relative to the position.</param>
  61. public TilemapPolylineObject(int id, Vector2 position, Vector2[] points) : base(id, position)
  62. {
  63. Points = points;
  64. }
  65. }
  66. }