summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Collisions/QuadTree/QuadTreeData.cs
blob: db6da3eb0dd857c5050229645af739a6fe01e8cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Collections.Generic;
using System.Linq;

namespace MonoGame.Extended.Collisions.QuadTree;

/// <summary>
/// Data structure for the quad tree.
/// Holds the entity and collision data for it.
/// </summary>
public class QuadtreeData
{
    private readonly ICollisionActor _target;
    private readonly HashSet<QuadTree> _parents = new();

    /// <summary>
    /// Initialize a new instance of QuadTreeData.
    /// </summary>
    /// <param name="target"></param>
    public QuadtreeData(ICollisionActor target)
    {
        _target = target;
        Bounds = _target.Bounds.BoundingRectangle;
    }

    /// <summary>
    /// Remove a parent node.
    /// </summary>
    /// <param name="parent"></param>
    public void RemoveParent(QuadTree parent)
    {
        _parents.Remove(parent);
    }

    /// <summary>
    /// Add a parent node.
    /// </summary>
    /// <param name="parent"></param>
    public void AddParent(QuadTree parent)
    {
        _parents.Add(parent);
        Bounds = _target.Bounds.BoundingRectangle;
    }

    /// <summary>
    /// Remove all parent nodes from this node.
    /// </summary>
    public void RemoveFromAllParents()
    {
        foreach (var parent in _parents.ToList())
        {
            parent.Remove(this);
        }

        _parents.Clear();
    }

    /// <summary>
    /// Gets the bounding box for collision detection.
    /// </summary>
    public RectangleF Bounds { get; set; }

    /// <summary>
    /// Gets the collision actor target.
    /// </summary>
    public ICollisionActor Target => _target;

    /// <summary>
    /// Gets or sets whether Target has had its collision handled this
    /// iteration.
    /// </summary>
    public bool Dirty { get; private set; }

    /// <summary>
    /// Mark node as dirty.
    /// </summary>
    public void MarkDirty()
    {
        Dirty = true;
    }

    /// <summary>
    /// Mark node as clean, i.e. not dirty.
    /// </summary>
    public void MarkClean()
    {
        Dirty = false;
    }
}