Skip to content

Commit e90c550

Browse files
author
psilo
committed
Some fixes and additional functions.
1 parent 1d95287 commit e90c550

File tree

6 files changed

+221
-56
lines changed

6 files changed

+221
-56
lines changed

CollisionGrid/CollisionGrid.Point.cs

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// ***************************************************************************
1414

1515
using System.Collections.Generic;
16+
using System.Linq;
1617
using Microsoft.Xna.Framework;
1718

1819
namespace CollisionGrid
@@ -23,7 +24,32 @@ public T[] Get(Point cell)
2324
{
2425
lock (lockObject)
2526
{
26-
return grid[Clamp(cell)].ToArray();
27+
List<T> contents;
28+
Grid.TryGetValue(Clamp(cell), out contents);
29+
if (contents == null)
30+
{
31+
return new T[0];
32+
}
33+
return contents.ToArray();
34+
}
35+
}
36+
37+
/// <summary>
38+
/// Gets the first item encountered on the given cell.
39+
/// </summary>
40+
/// <param name="cell">The cell to search</param>
41+
/// <returns>The item or default(T)</returns>
42+
public T First(Point cell)
43+
{
44+
lock (lockObject)
45+
{
46+
List<T> contents;
47+
Grid.TryGetValue(Clamp(cell), out contents);
48+
if (contents != null && contents.Count > 0)
49+
{
50+
return contents[0];
51+
}
52+
return default(T);
2753
}
2854
}
2955

@@ -45,29 +71,46 @@ public void Add(T item, Point cell)
4571

4672
private void AddToGrid(T item, Point cell)
4773
{
48-
List<T> l = grid[cell];
49-
if (!l.Contains(item))
74+
List<T> l;
75+
Grid.TryGetValue(cell, out l);
76+
if (l == null)
5077
{
78+
if (ListOfItemQueue.Count > 0)
79+
{
80+
l = ListOfItemQueue.Dequeue();
81+
}
82+
else
83+
{
84+
l = new List<T>();
85+
}
5186
l.Add(item);
87+
Grid.Add(cell, l);
88+
}
89+
else
90+
{
91+
if (!l.Contains(item))
92+
{
93+
l.Add(item);
94+
}
5295
}
5396
}
5497

5598
private void AddToItems(T item, Point cell)
5699
{
57100
List<Point> pl;
58-
items.TryGetValue(item, out pl);
101+
Items.TryGetValue(item, out pl);
59102
if (pl == null)
60103
{
61-
if (listOfPointQueue.Count > 0)
104+
if (ListOfPointQueue.Count > 0)
62105
{
63-
pl = listOfPointQueue.Dequeue();
106+
pl = ListOfPointQueue.Dequeue();
64107
}
65108
else
66109
{
67110
pl = new List<Point>();
68111
}
69112
pl.Add(cell);
70-
items.Add(item, pl);
113+
Items.Add(item, pl);
71114
}
72115
else
73116
{
@@ -88,24 +131,29 @@ public void Remove(Point cell)
88131
lock (lockObject)
89132
{
90133
Point c = Clamp(cell);
91-
List<T> l = grid[c];
134+
List<T> l;
135+
Grid.TryGetValue(c, out l);
92136

93-
foreach (T i in l)
137+
if (l != null)
94138
{
95-
List<Point> pl;
96-
items.TryGetValue(i, out pl);
97-
if (pl != null)
139+
foreach (T i in l)
98140
{
99-
pl.Remove(c);
100-
if (pl.Count == 0)
141+
List<Point> pl;
142+
Items.TryGetValue(i, out pl);
143+
if (pl != null)
101144
{
102-
listOfPointQueue.Enqueue(pl);
103-
items.Remove(i);
145+
pl.Remove(c);
146+
if (pl.Count == 0)
147+
{
148+
ListOfPointQueue.Enqueue(pl);
149+
Items.Remove(i);
150+
}
104151
}
105152
}
153+
l.Clear();
154+
ListOfItemQueue.Enqueue(l);
155+
Grid.Remove(cell);
106156
}
107-
108-
l.Clear();
109157
}
110158
}
111159

@@ -123,5 +171,13 @@ public void Move(T item, Point cell)
123171
Add(item, cell);
124172
}
125173
}
174+
175+
public bool IsEmpty(Point cell)
176+
{
177+
lock (lockObject)
178+
{
179+
return Get(cell).Length == 0;
180+
}
181+
}
126182
}
127183
}

CollisionGrid/CollisionGrid.Rect.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ public T[] Get(Rect aabb)
2626
}
2727
}
2828

29+
/// <summary>
30+
/// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box.
31+
/// </summary>
32+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates</param>
33+
/// <returns>
34+
/// The item or default(T)
35+
/// </returns>
36+
public T First(Rect aabb)
37+
{
38+
lock (lockObject)
39+
{
40+
return First(Rectangle(aabb));
41+
}
42+
}
43+
2944
/// <summary>
3045
/// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box.
3146
/// If the cell already contains the item, it is not added a second time.
@@ -66,5 +81,13 @@ public void Move(T item, Rect aabb)
6681
Move(item, Rectangle(aabb));
6782
}
6883
}
84+
85+
public bool IsEmpty(Rect aabb)
86+
{
87+
lock (lockObject)
88+
{
89+
return IsEmpty(Rectangle(aabb));
90+
}
91+
}
6992
}
7093
}

CollisionGrid/CollisionGrid.Rectangle.cs

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,37 @@ public T[] Get(Rectangle aabb)
4949
}
5050
}
5151

52+
/// <summary>
53+
/// Gets the first item encountered in the cells that are hit by the given Axis-Aligned-Bounding-Box.
54+
/// </summary>
55+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates</param>
56+
/// <returns>
57+
/// The item or default(T)
58+
/// </returns>
59+
public T First(Rectangle aabb)
60+
{
61+
lock (lockObject)
62+
{
63+
FillList(aabb);
64+
result.Clear();
65+
foreach (Point p in lop)
66+
{
67+
T content = First(p);
68+
if (!content.Equals(default(T)))
69+
{
70+
return content;
71+
}
72+
}
73+
return default(T);
74+
}
75+
}
76+
5277
/// <summary>
5378
/// Adds a given item to the cells that are hit by the given Axis-Aligned-Bounding-Box.
5479
/// If the cell already contains the item, it is not added a second time.
5580
/// </summary>
5681
/// <param name="item">The item to add</param>
57-
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
82+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates</param>
5883
public void Add(T item, Rectangle aabb)
5984
{
6085
lock (lockObject)
@@ -71,7 +96,7 @@ public void Add(T item, Rectangle aabb)
7196
/// Removes all items from the cells that are hit by the given Axis-Aligned-Bounding-Box.
7297
/// If the items don't occupy another cell, they are removed as well.
7398
/// </summary>
74-
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
99+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates</param>
75100
public void Remove(Rectangle aabb)
76101
{
77102
lock (lockObject)
@@ -89,7 +114,7 @@ public void Remove(Rectangle aabb)
89114
/// If the item hasn't been in the grid before, this will just add it.
90115
/// </summary>
91116
/// <param name="item">The item to move</param>
92-
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates.</param>
117+
/// <param name="aabb">The Axis-Aligned-Bounding-Box given in int-cell-coordinates</param>
93118
public void Move(T item, Rectangle aabb)
94119
{
95120
lock (lockObject)
@@ -102,5 +127,21 @@ public void Move(T item, Rectangle aabb)
102127
}
103128
}
104129
}
130+
131+
public bool IsEmpty(Rectangle aabb)
132+
{
133+
lock (lockObject)
134+
{
135+
FillList(aabb);
136+
foreach (Point p in lop)
137+
{
138+
if (!IsEmpty(p))
139+
{
140+
return false;
141+
}
142+
}
143+
return true;
144+
}
145+
}
105146
}
106147
}

CollisionGrid/CollisionGrid.Vector2.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,23 @@ public T[] Get(Vector2 position)
2727
}
2828

2929
/// <summary>
30-
/// Adds a given item to a cell that contains the given position.
31-
/// If the cell already contains the item, it is not added a second time.
30+
/// Gets the first item encountered in the cell that contains the given position.
31+
/// </summary>
32+
/// <param name="position">The position</param>
33+
/// <returns>
34+
/// The item or default(T)
35+
/// </returns>
36+
public T First(Vector2 position)
37+
{
38+
lock (lockObject)
39+
{
40+
return First(Cell(position));
41+
}
42+
}
43+
44+
/// <summary>
45+
/// Adds a given item to a cell that contains the given position.
46+
/// If the cell already contains the item, it is not added a second time.
3247
/// </summary>
3348
/// <param name="item">The item to add</param>
3449
/// <param name="position">The position.</param>
@@ -41,8 +56,8 @@ public void Add(T item, Vector2 position)
4156
}
4257

4358
/// <summary>
44-
/// Removes all items from the cell that contains the given position.
45-
/// If the items don't occupy another cell, they are removed as well.
59+
/// Removes all items from the cell that contains the given position.
60+
/// If the items don't occupy another cell, they are removed as well.
4661
/// </summary>
4762
/// <param name="position">The position.</param>
4863
public void Remove(Vector2 position)
@@ -54,8 +69,8 @@ public void Remove(Vector2 position)
5469
}
5570

5671
/// <summary>
57-
/// Removes all occurrences of the given item and re-adds it at the new cell that contains the given position.
58-
/// If the item hasn't been in the grid before, this will just add it.
72+
/// Removes all occurrences of the given item and re-adds it at the new cell that contains the given position.
73+
/// If the item hasn't been in the grid before, this will just add it.
5974
/// </summary>
6075
/// <param name="item">The item to move</param>
6176
/// <param name="position">The position.</param>
@@ -66,5 +81,13 @@ public void Move(T item, Vector2 position)
6681
Move(item, Cell(position));
6782
}
6883
}
84+
85+
public bool IsEmpty(Vector2 position)
86+
{
87+
lock (lockObject)
88+
{
89+
return IsEmpty(Cell(position));
90+
}
91+
}
6992
}
7093
}

0 commit comments

Comments
 (0)