QuadTreeData.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. namespace MonoGame.Extended.Collisions.QuadTree;
  4. /// <summary>
  5. /// Data structure for the quad tree.
  6. /// Holds the entity and collision data for it.
  7. /// </summary>
  8. public class QuadtreeData
  9. {
  10. private readonly ICollisionActor _target;
  11. private readonly HashSet<QuadTree> _parents = new();
  12. /// <summary>
  13. /// Initialize a new instance of QuadTreeData.
  14. /// </summary>
  15. /// <param name="target"></param>
  16. public QuadtreeData(ICollisionActor target)
  17. {
  18. _target = target;
  19. Bounds = _target.Bounds.BoundingRectangle;
  20. }
  21. /// <summary>
  22. /// Remove a parent node.
  23. /// </summary>
  24. /// <param name="parent"></param>
  25. public void RemoveParent(QuadTree parent)
  26. {
  27. _parents.Remove(parent);
  28. }
  29. /// <summary>
  30. /// Add a parent node.
  31. /// </summary>
  32. /// <param name="parent"></param>
  33. public void AddParent(QuadTree parent)
  34. {
  35. _parents.Add(parent);
  36. Bounds = _target.Bounds.BoundingRectangle;
  37. }
  38. /// <summary>
  39. /// Remove all parent nodes from this node.
  40. /// </summary>
  41. public void RemoveFromAllParents()
  42. {
  43. foreach (var parent in _parents.ToList())
  44. {
  45. parent.Remove(this);
  46. }
  47. _parents.Clear();
  48. }
  49. /// <summary>
  50. /// Gets the bounding box for collision detection.
  51. /// </summary>
  52. public RectangleF Bounds { get; set; }
  53. /// <summary>
  54. /// Gets the collision actor target.
  55. /// </summary>
  56. public ICollisionActor Target => _target;
  57. /// <summary>
  58. /// Gets or sets whether Target has had its collision handled this
  59. /// iteration.
  60. /// </summary>
  61. public bool Dirty { get; private set; }
  62. /// <summary>
  63. /// Mark node as dirty.
  64. /// </summary>
  65. public void MarkDirty()
  66. {
  67. Dirty = true;
  68. }
  69. /// <summary>
  70. /// Mark node as clean, i.e. not dirty.
  71. /// </summary>
  72. public void MarkClean()
  73. {
  74. Dirty = false;
  75. }
  76. }