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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 |
// *******************************************//
// *********Credits to Kyle Schouviller*******//
// *******for the original implementation*****//
// *******************************************//
using System;
using Microsoft.Xna.Framework;
namespace Brains.Framework.QuadTree
{
/// <summary>
/// A position item in a quadtree
/// </summary>
/// <typeparam name="T">The type of the QuadTree item's parent</typeparam>
public class QuadTreePositionItem<T>
{
public QuadTreeNode<T> node;
internal QuadTree<T> quadTree;
/// <summary>
/// Handles the move event
/// </summary>
internal void OnMove()
{
// Update rectangles
rect.TopLeft = position - (size * .5f);
rect.BottomRight = position + (size * .5f);
// Call event handler
// if (Move != null) Move(this);
hasMoved = true;
if (node != null) node.ItemMove(this);
}
internal bool hasMoved = false;
/// <summary>
/// Handles the destroy event
/// </summary>
protected void OnDestroy()
{
//if (Destroy != null) Destroy(this);
node.RemoveItem(this);
}
/// <summary>
/// The center position of this item
/// </summary>
private Vector2 position;
/// <summary>
/// Gets or sets the center position of this item
/// </summary>
public Vector2 Position
{
get { return position; }
set
{
position = value;
OnMove();
if (node != null)
{
if (node.ContainsRect(this.rect))
return;
// node.RemoveItem(this);
}
// if (quadTree!= null)
// quadTree.Insert(this);
}
}
/// <summary>
/// The size of this item
/// </summary>
private Vector2 size;
/// <summary>
/// Gets or sets the size of this item
/// </summary>
public Vector2 Size
{
get { return size; }
set
{
size = value;
rect.TopLeft = position - (size / 2f);
rect.BottomRight = position + (size / 2f);
OnMove();
}
}
/// <summary>
/// The rectangle containing this item
/// </summary>
private RectangleF rect;
/// <summary>
/// Gets a rectangle containing this item
/// </summary>
public RectangleF Rect
{
get { return rect; }
}
/// <summary>
/// The parent of this item
/// </summary>
/// <remarks>The Parent accessor is used to gain access to the item controlling this position item</remarks>
private T parent;
/// <summary>
/// Gets the parent of this item
/// </summary>
public T Parent
{
get { return parent; }
}
/// <summary>
/// Creates a position item in a QuadTree
/// </summary>
/// <param name="parent">The parent of this item</param>
/// <param name="position">The position of this item</param>
/// <param name="size">The size of this item</param>
public QuadTreePositionItem(T parent, Vector2 position, Vector2 size)
{
this.rect = new RectangleF(0f, 0f, 1f, 1f);
this.parent = parent;
this.position = position ;
this.size = size;
OnMove();
}
/// <summary>
/// Destroys this item and removes it from the QuadTree
/// </summary>
public void Delete()
{
OnDestroy();
}
}
} |